KMAdsInfo.swift 10 KB

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