KMAdsInfo.swift 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. var couponInfo: KMCouponInfo?
  12. override init() {
  13. super.init()
  14. }
  15. func getRandomAdsInfo() -> KMAdsInfo? {
  16. let randomValue = Int(arc4random()%100)
  17. for idObject in self.adsInfoArrM {
  18. let adsInfo = idObject as! KMAdsInfo
  19. let percentValue = adsInfo.percentValue
  20. let minValue = Int(percentValue.components(separatedBy: "-").first!)
  21. let maxValue = Int(percentValue.components(separatedBy: "-").last!)
  22. if minValue! <= randomValue && randomValue <= maxValue! {
  23. return adsInfo
  24. }
  25. }
  26. return nil
  27. }
  28. }
  29. @objcMembers class KMAdsInfo: NSObject {
  30. var infoDict: NSMutableDictionary!
  31. var percentValue: String {
  32. get {
  33. return self.infoDict["percent"] as! String
  34. }
  35. }
  36. var show: Bool {
  37. get {
  38. let showString = self.infoDict.object(forKey: "show") as! String
  39. if let showValue = Bool(showString) {
  40. if showValue == false {
  41. return false
  42. }
  43. }
  44. var startTime = Double(self.infoDict["startTime"] as! String)!
  45. var endTime = Double(self.infoDict["endTime"] as! String)!
  46. if startTime > 1000000000 {
  47. startTime = startTime / 1000
  48. }
  49. if endTime > 1000000000 {
  50. endTime = endTime / 1000
  51. }
  52. let startDate = NSDate(timeIntervalSince1970: startTime)
  53. let endDate = NSDate(timeIntervalSince1970: endTime)
  54. if startDate.compare(Date()) == .orderedAscending &&
  55. endDate.compare(Date()) == .orderedDescending{
  56. return true
  57. }
  58. return false
  59. }
  60. }
  61. var adsImage: NSImage {
  62. get {
  63. let imageDict: [String: Any] = self.infoDict["image"] as! [String : Any]
  64. let imageURLString = imageDict[self.languageKey] as! NSString
  65. var imageName = self.versionKey
  66. imageName.append(String(format: "_%@.%@", self.languageKey, imageURLString.lastPathComponent))
  67. let kImageCacheFilePath: String = NSTemporaryDirectory() + "Advertisement/ImageCache"
  68. let imgSavePath = kImageCacheFilePath + "/" + imageName
  69. if FileManager.default.fileExists(atPath: imgSavePath) {
  70. return NSImage(contentsOfFile: imgSavePath)!
  71. }
  72. DispatchQueue.global().async {
  73. if let url = URL(string: imageURLString as String),
  74. let imageData = try? Data(contentsOf: url) {
  75. if imageData.isEmpty == false {
  76. DispatchQueue.main.async {
  77. try?imageData.write(to: URL(fileURLWithPath: imgSavePath))
  78. NotificationCenter.default.post(name: Notification.Name(rawValue: "KMRecommondInfoUpdateNoti"), object: ["unique":self.versionKey])
  79. }
  80. }
  81. }
  82. }
  83. return NSImage(named: "ComPDFKit_EN")!
  84. }
  85. }
  86. var adsURLLink: String {
  87. get {
  88. let linkDict: [String: Any] = self.infoDict["linkURL"] as! [String : Any]
  89. return linkDict[self.languageKey] as! String
  90. }
  91. }
  92. var languageKey: String {
  93. let language = Bundle.main.preferredLocalizations[0]
  94. if language == "zh_CN" ||
  95. language == "zh-Hans" {
  96. return "zh_CN"
  97. } else if language == "zh_TW" ||
  98. language == "zh-Hant" {
  99. return "zh_TW"
  100. }
  101. return "en"
  102. }
  103. var versionKey: String {
  104. return self.infoDict["version"] as! String
  105. }
  106. }
  107. @objcMembers class KMCouponInfo: NSObject {
  108. var infoDict: NSMutableDictionary!
  109. var show: Bool {
  110. get {
  111. let showString = self.infoDict.object(forKey: "show") as! String
  112. if let showValue = Bool(showString) {
  113. if showValue == false {
  114. return false
  115. }
  116. }
  117. var startTime = Double(self.infoDict["startTime"] as! String)!
  118. var endTime = Double(self.infoDict["endTime"] as! String)!
  119. if startTime > 1000000000 {
  120. startTime = startTime / 1000
  121. }
  122. if endTime > 1000000000 {
  123. endTime = endTime / 1000
  124. }
  125. let startDate = NSDate(timeIntervalSince1970: startTime)
  126. let endDate = NSDate(timeIntervalSince1970: endTime)
  127. if startDate.compare(Date()) == .orderedAscending &&
  128. endDate.compare(Date()) == .orderedDescending{
  129. return true
  130. }
  131. return false
  132. }
  133. }
  134. var adsImage: NSImage {
  135. get {
  136. let imageDict: [String: Any] = self.infoDict["image"] as! [String : Any]
  137. var languageKey = KMAdsInfo().languageKey
  138. if KMAppearance.isDarkMode() {
  139. languageKey = "dark_" + KMAdsInfo().languageKey
  140. }
  141. guard let imageURLString = imageDict[languageKey] as? String else {
  142. return NSImage(named: "cnLight")!
  143. }
  144. var imageName = self.versionKey
  145. imageName.append(String(format: "_%@.%@", languageKey, imageURLString.lastPathComponent))
  146. let kImageCacheFilePath: String = NSTemporaryDirectory() + "Advertisement/ImageCache"
  147. let imgSavePath = kImageCacheFilePath + "/" + imageName
  148. if FileManager.default.fileExists(atPath: imgSavePath) {
  149. return NSImage(contentsOfFile: imgSavePath)!
  150. }
  151. DispatchQueue.global().async {
  152. if let url = URL(string: imageURLString as String),
  153. let imageData = try? Data(contentsOf: url) {
  154. if imageData.isEmpty == false {
  155. DispatchQueue.main.async {
  156. try?imageData.write(to: URL(fileURLWithPath: imgSavePath))
  157. NotificationCenter.default.post(name: Notification.Name(rawValue: "KMRecommondInfoUpdateNoti"), object: ["unique":self.versionKey])
  158. }
  159. }
  160. }
  161. }
  162. return NSImage(named: "cnLight")!
  163. }
  164. }
  165. var adsDetailImage: NSImage {
  166. get {
  167. let imageDict: [String: Any] = self.infoDict["detailImage"] as! [String : Any]
  168. var languageKey = KMAdsInfo().languageKey
  169. if KMAppearance.isDarkMode() {
  170. languageKey = "dark_" + KMAdsInfo().languageKey
  171. }
  172. guard let imageURLString = imageDict[languageKey] as? NSString else {
  173. return NSImage(named: "detailCnDark")!
  174. }
  175. var imageName = self.versionKey
  176. imageName.append(String(format: "_%@.%@", languageKey, imageURLString.lastPathComponent))
  177. let kImageCacheFilePath: String = NSTemporaryDirectory() + "Advertisement/ImageCache"
  178. let imgSavePath = kImageCacheFilePath + "/" + imageName
  179. if FileManager.default.fileExists(atPath: imgSavePath) {
  180. return NSImage(contentsOfFile: imgSavePath)!
  181. }
  182. DispatchQueue.global().async {
  183. if let url = URL(string: imageURLString as String),
  184. let imageData = try? Data(contentsOf: url) {
  185. if imageData.isEmpty == false {
  186. DispatchQueue.main.async {
  187. try?imageData.write(to: URL(fileURLWithPath: imgSavePath))
  188. NotificationCenter.default.post(name: Notification.Name(rawValue: "KMRecommondInfoUpdateNoti"), object: ["unique":self.versionKey])
  189. }
  190. }
  191. }
  192. }
  193. return NSImage(named: "detailCnDark")!
  194. }
  195. }
  196. var adsURLLink: String {
  197. get {
  198. let linkDict: [String: Any] = self.infoDict["linkURL"] as! [String : Any]
  199. return linkDict[KMAdsInfo().languageKey] as! String
  200. }
  201. }
  202. var versionKey: String {
  203. return self.infoDict["version"] as! String
  204. }
  205. var title: String {
  206. let titleDict: [String: Any] = self.infoDict["title"] as! [String : Any]
  207. return titleDict[KMAdsInfo().languageKey] as! String
  208. }
  209. var subTitle: String {
  210. let titleDict: [String: Any] = self.infoDict["subTitle"] as! [String : Any]
  211. return titleDict[KMAdsInfo().languageKey] as! String
  212. }
  213. var buyBtnTitle: String {
  214. let titleDict: [String: Any] = self.infoDict["buyBtnTitle"] as! [String : Any]
  215. return titleDict[KMAdsInfo().languageKey] as! String
  216. }
  217. var laterInfoString: String {
  218. let titleDict: [String: Any] = self.infoDict["laterInfoString"] as! [String : Any]
  219. return titleDict[KMAdsInfo().languageKey] as! String
  220. }
  221. }