KMAdsInfo.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //
  2. // KMAdsInfo.swift
  3. // PDF Reader Pro Edition
  4. //
  5. // Created by Niehaoyu on 2024/2/28.
  6. //
  7. import Cocoa
  8. @objcMembers class KMAdsInfoManager: NSObject {
  9. public static let shareInstance = KMAdsInfoManager.init()
  10. public var adsInfoArrM: NSMutableArray = NSMutableArray()
  11. override init() {
  12. super.init()
  13. }
  14. func getRandomAdsInfo() -> KMAdsInfo? {
  15. let randomValue = Int(arc4random()%100)
  16. for idObject in self.adsInfoArrM {
  17. let adsInfo = idObject as! KMAdsInfo
  18. let percentValue = adsInfo.percentValue
  19. let minValue = Int(percentValue.components(separatedBy: "-").first!)
  20. let maxValue = Int(percentValue.components(separatedBy: "-").last!)
  21. if minValue! <= randomValue && randomValue <= maxValue! {
  22. return adsInfo
  23. }
  24. }
  25. return nil
  26. }
  27. }
  28. @objcMembers class KMAdsInfo: NSObject {
  29. var infoDict: NSMutableDictionary!
  30. var percentValue: String {
  31. get {
  32. return self.infoDict["percent"] as! String
  33. }
  34. }
  35. var show: Bool {
  36. get {
  37. let showString = self.infoDict.object(forKey: "show") as! String
  38. if let showValue = Bool(showString) {
  39. if showValue == false {
  40. return false
  41. }
  42. }
  43. var startTime = Double(self.infoDict["startTime"] as! String)!
  44. var endTime = Double(self.infoDict["endTime"] as! String)!
  45. if startTime > 1000000000 {
  46. startTime = startTime / 1000
  47. }
  48. if endTime > 1000000000 {
  49. endTime = endTime / 1000
  50. }
  51. let startDate = NSDate(timeIntervalSince1970: startTime)
  52. let endDate = NSDate(timeIntervalSince1970: endTime)
  53. if startDate.compare(Date()) == .orderedAscending &&
  54. endDate.compare(Date()) == .orderedDescending{
  55. return true
  56. }
  57. return false
  58. }
  59. }
  60. var adsImage: NSImage {
  61. get {
  62. let imageDict: [String: Any] = self.infoDict["image"] as! [String : Any]
  63. let imageURLString = imageDict[self.languageKey] as! NSString
  64. var imageName = self.versionKey
  65. imageName.append(String(format: "_%@.%@", self.languageKey, imageURLString.lastPathComponent))
  66. let kImageCacheFilePath: String = NSTemporaryDirectory() + "Advertisement/ImageCache"
  67. let imgSavePath = kImageCacheFilePath + "/" + imageName
  68. if FileManager.default.fileExists(atPath: imgSavePath) {
  69. return NSImage(contentsOfFile: imgSavePath)!
  70. }
  71. DispatchQueue.global().async {
  72. if let url = URL(string: imageURLString as String),
  73. let imageData = try? Data(contentsOf: url) {
  74. if imageData.isEmpty == false {
  75. DispatchQueue.main.async {
  76. try?imageData.write(to: URL(fileURLWithPath: imgSavePath))
  77. NotificationCenter.default.post(name: Notification.Name(rawValue: "KMRecommondInfoUpdateNoti"), object: ["unique":self.versionKey])
  78. }
  79. }
  80. }
  81. }
  82. return NSImage(named: "ComPDFKit_EN")!
  83. }
  84. }
  85. var adsURLLink: String {
  86. get {
  87. let linkDict: [String: Any] = self.infoDict["linkURL"] as! [String : Any]
  88. return linkDict[self.languageKey] as! String
  89. }
  90. }
  91. var languageKey: String {
  92. let language = Bundle.main.preferredLocalizations[0]
  93. if language == "zh_CN" {
  94. return "zh_CN"
  95. } else if language == "zh_TW" {
  96. return "zh_TW"
  97. }
  98. return "en"
  99. }
  100. var versionKey: String {
  101. return self.infoDict["version"] as! String
  102. }
  103. }