KMAdsInfo.swift 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. //
  2. // KMAdsInfo.swift
  3. // PDF Reader Pro Edition
  4. //
  5. // Created by Niehaoyu on 2024/2/28.
  6. //
  7. import Cocoa
  8. enum KMDFPJumpType: Int {
  9. case Web = 0 // 网页
  10. case ComparisonSheet // 比较表
  11. }
  12. @objcMembers class KMAdsInfoManager: NSObject {
  13. public static let shareInstance = KMAdsInfoManager.init()
  14. public var adsInfoArrM: NSMutableArray = NSMutableArray()
  15. var couponInfo: KMCouponInfo?
  16. override init() {
  17. super.init()
  18. }
  19. func getRandomAdsInfo() -> KMAdsInfo? {
  20. let randomValue = Int(arc4random()%100)
  21. for idObject in self.adsInfoArrM {
  22. let adsInfo = idObject as! KMAdsInfo
  23. let percentValue = adsInfo.percentValue
  24. let minValue = Int(percentValue.components(separatedBy: "-").first!)
  25. let maxValue = Int(percentValue.components(separatedBy: "-").last!)
  26. if minValue! <= randomValue && randomValue <= maxValue! {
  27. return adsInfo
  28. }
  29. }
  30. return nil
  31. }
  32. }
  33. @objcMembers class KMAdsInfo: NSObject {
  34. var infoDict: NSMutableDictionary!
  35. var percentValue: String {
  36. get {
  37. return self.infoDict["percent"] as! String
  38. }
  39. }
  40. var show: Bool {
  41. get {
  42. let showString = self.infoDict.object(forKey: "show") as! String
  43. if let showValue = Bool(showString) {
  44. if showValue == false {
  45. return false
  46. }
  47. }
  48. var startTime = Double(self.infoDict["startTime"] as! String)!
  49. var endTime = Double(self.infoDict["endTime"] as! String)!
  50. if startTime > 1000000000 {
  51. startTime = startTime / 1000
  52. }
  53. if endTime > 1000000000 {
  54. endTime = endTime / 1000
  55. }
  56. let startDate = NSDate(timeIntervalSince1970: startTime)
  57. let endDate = NSDate(timeIntervalSince1970: endTime)
  58. if startDate.compare(Date()) == .orderedAscending &&
  59. endDate.compare(Date()) == .orderedDescending{
  60. return true
  61. }
  62. return false
  63. }
  64. }
  65. var adsImage: NSImage {
  66. get {
  67. let imageDict: [String: Any] = self.infoDict["image"] as! [String : Any]
  68. let imageURLString = imageDict[self.languageKey] as! NSString
  69. var imageName = self.versionKey
  70. imageName.append(String(format: "_%@.%@", self.languageKey, imageURLString.lastPathComponent))
  71. let kImageCacheFilePath: String = NSTemporaryDirectory() + "Advertisement/ImageCache"
  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. var jumpType: KMDFPJumpType {
  111. get {
  112. guard let typeString = self.infoDict.object(forKey: "jumpType") else {
  113. return .Web
  114. }
  115. if typeString is String {
  116. let jumpTypeString = typeString as! String
  117. if jumpTypeString == "Comparison Sheet" {
  118. return .ComparisonSheet
  119. } else if jumpTypeString == "Web" {
  120. return .Web
  121. }
  122. }
  123. return .Web
  124. }
  125. }
  126. }
  127. @objcMembers class KMCouponInfo: NSObject {
  128. var infoDict: NSMutableDictionary!
  129. var show: Bool {
  130. get {
  131. let showString = self.infoDict.object(forKey: "show") as! String
  132. if let showValue = Bool(showString) {
  133. if showValue == false {
  134. return false
  135. }
  136. }
  137. var startTime = Double(self.infoDict["startTime"] as! String)!
  138. var endTime = Double(self.infoDict["endTime"] as! String)!
  139. if startTime > 1000000000 {
  140. startTime = startTime / 1000
  141. }
  142. if endTime > 1000000000 {
  143. endTime = endTime / 1000
  144. }
  145. let startDate = NSDate(timeIntervalSince1970: startTime)
  146. let endDate = NSDate(timeIntervalSince1970: endTime)
  147. if startDate.compare(Date()) == .orderedAscending &&
  148. endDate.compare(Date()) == .orderedDescending{
  149. return true
  150. }
  151. return false
  152. }
  153. }
  154. var adsImage: NSImage {
  155. get {
  156. let imageDict: [String: Any] = self.infoDict["image"] as! [String : Any]
  157. var languageKey = KMAdsInfo().languageKey
  158. if KMAppearance.isDarkMode() {
  159. languageKey = "dark_" + KMAdsInfo().languageKey
  160. }
  161. guard let imageURLString = imageDict[languageKey] as? String else {
  162. return NSImage(named: "cnLight")!
  163. }
  164. var imageName = self.versionKey
  165. imageName.append(String(format: "_%@.%@", languageKey, imageURLString.lastPathComponent))
  166. let kImageCacheFilePath: String = NSTemporaryDirectory() + "Advertisement/ImageCache"
  167. let imgSavePath = kImageCacheFilePath + "/" + imageName
  168. if FileManager.default.fileExists(atPath: imgSavePath) {
  169. return NSImage(contentsOfFile: imgSavePath)!
  170. }
  171. DispatchQueue.global().async {
  172. if let url = URL(string: imageURLString as String),
  173. let imageData = try? Data(contentsOf: url) {
  174. if imageData.isEmpty == false {
  175. DispatchQueue.main.async {
  176. try?imageData.write(to: URL(fileURLWithPath: imgSavePath))
  177. NotificationCenter.default.post(name: Notification.Name(rawValue: "KMRecommondInfoUpdateNoti"), object: ["unique":self.versionKey])
  178. }
  179. }
  180. }
  181. }
  182. return NSImage(named: "cnLight")!
  183. }
  184. }
  185. var adsDetailImage: NSImage {
  186. get {
  187. let imageDict: [String: Any] = self.infoDict["detailImage"] as! [String : Any]
  188. var languageKey = KMAdsInfo().languageKey
  189. if KMAppearance.isDarkMode() {
  190. languageKey = "dark_" + KMAdsInfo().languageKey
  191. }
  192. guard let imageURLString = imageDict[languageKey] as? NSString else {
  193. return NSImage(named: "detailCnDark")!
  194. }
  195. var imageName = self.versionKey
  196. imageName.append(String(format: "_%@.%@", languageKey, imageURLString.lastPathComponent))
  197. let kImageCacheFilePath: String = NSTemporaryDirectory() + "Advertisement/ImageCache"
  198. let imgSavePath = kImageCacheFilePath + "/" + imageName
  199. if FileManager.default.fileExists(atPath: imgSavePath) {
  200. return NSImage(contentsOfFile: imgSavePath)!
  201. }
  202. DispatchQueue.global().async {
  203. if let url = URL(string: imageURLString as String),
  204. let imageData = try? Data(contentsOf: url) {
  205. if imageData.isEmpty == false {
  206. DispatchQueue.main.async {
  207. try?imageData.write(to: URL(fileURLWithPath: imgSavePath))
  208. NotificationCenter.default.post(name: Notification.Name(rawValue: "KMRecommondInfoUpdateNoti"), object: ["unique":self.versionKey])
  209. }
  210. }
  211. }
  212. }
  213. return NSImage(named: "detailCnDark")!
  214. }
  215. }
  216. var adsURLLink: String {
  217. get {
  218. let linkDict: [String: Any] = self.infoDict["linkURL"] as! [String : Any]
  219. return linkDict[KMAdsInfo().languageKey] as! String
  220. }
  221. }
  222. var versionKey: String {
  223. return self.infoDict["version"] as! String
  224. }
  225. var title: String {
  226. let titleDict: [String: Any] = self.infoDict["title"] as! [String : Any]
  227. return titleDict[KMAdsInfo().languageKey] as! String
  228. }
  229. var subTitle: String {
  230. let titleDict: [String: Any] = self.infoDict["subTitle"] as! [String : Any]
  231. return titleDict[KMAdsInfo().languageKey] as! String
  232. }
  233. var buyBtnTitle: String {
  234. let titleDict: [String: Any] = self.infoDict["buyBtnTitle"] as! [String : Any]
  235. return titleDict[KMAdsInfo().languageKey] as! String
  236. }
  237. var laterInfoString: String {
  238. let titleDict: [String: Any] = self.infoDict["laterInfoString"] as! [String : Any]
  239. return titleDict[KMAdsInfo().languageKey] as! String
  240. }
  241. }