KMAdsInfo.swift 10 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. if self.infoDict.value(forKey: "dfpImage") != nil {
  68. let imageDict: [String: Any] = self.infoDict["dfpImage"] as! [String : Any]
  69. let imageURLString = imageDict[self.languageKey] as! NSString
  70. var imageName = self.versionKey
  71. imageName.append(String(format: "_%@.%@", self.languageKey, imageURLString.lastPathComponent))
  72. let kImageCacheFilePath: String = NSTemporaryDirectory() + "Advertisement/ImageCache"
  73. let imgSavePath = kImageCacheFilePath + "/" + imageName
  74. if FileManager.default.fileExists(atPath: imgSavePath) {
  75. return NSImage(contentsOfFile: imgSavePath)!
  76. }
  77. DispatchQueue.global().async {
  78. if let url = URL(string: imageURLString as String),
  79. let imageData = try? Data(contentsOf: url) {
  80. if imageData.isEmpty == false {
  81. DispatchQueue.main.async {
  82. try?imageData.write(to: URL(fileURLWithPath: imgSavePath))
  83. NotificationCenter.default.post(name: Notification.Name(rawValue: "KMRecommondInfoUpdateNoti"), object: ["unique":self.versionKey])
  84. }
  85. }
  86. }
  87. }
  88. }
  89. return NSImage(named: "ComPDFKit_EN")!
  90. }
  91. }
  92. var adsURLLink: String {
  93. get {
  94. let linkDict: [String: Any] = self.infoDict["linkURL"] as! [String : Any]
  95. return linkDict[self.languageKey] as! String
  96. }
  97. }
  98. var languageKey: String {
  99. let language = Bundle.main.preferredLocalizations[0]
  100. if language == "zh_CN" ||
  101. language == "zh-Hans" {
  102. return "zh_CN"
  103. } else if language == "zh_TW" ||
  104. language == "zh-Hant" {
  105. return "zh_TW"
  106. }
  107. return "en"
  108. }
  109. var versionKey: String {
  110. return self.infoDict["version"] as! String
  111. }
  112. var jumpType: KMDFPJumpType {
  113. get {
  114. guard let typeString = self.infoDict.object(forKey: "dfpJumpType") else {
  115. return .Web
  116. }
  117. if typeString is String {
  118. let jumpTypeString = typeString as! String
  119. if jumpTypeString == "Comparison Sheet" {
  120. return .ComparisonSheet
  121. } else if jumpTypeString == "Web" {
  122. return .Web
  123. }
  124. }
  125. return .Web
  126. }
  127. }
  128. }
  129. @objcMembers class KMCouponInfo: NSObject {
  130. var infoDict: NSMutableDictionary!
  131. var show: Bool {
  132. get {
  133. let showString = self.infoDict.object(forKey: "show") as! String
  134. if let showValue = Bool(showString) {
  135. if showValue == false {
  136. return false
  137. }
  138. }
  139. var startTime = Double(self.infoDict["startTime"] as! String)!
  140. var endTime = Double(self.infoDict["endTime"] as! String)!
  141. if startTime > 1000000000 {
  142. startTime = startTime / 1000
  143. }
  144. if endTime > 1000000000 {
  145. endTime = endTime / 1000
  146. }
  147. let startDate = NSDate(timeIntervalSince1970: startTime)
  148. let endDate = NSDate(timeIntervalSince1970: endTime)
  149. if startDate.compare(Date()) == .orderedAscending &&
  150. endDate.compare(Date()) == .orderedDescending{
  151. return true
  152. }
  153. return false
  154. }
  155. }
  156. var adsImage: NSImage {
  157. get {
  158. let imageDict: [String: Any] = self.infoDict["image"] as! [String : Any]
  159. var languageKey = KMAdsInfo().languageKey
  160. if KMAppearance.isDarkMode() {
  161. languageKey = "dark_" + KMAdsInfo().languageKey
  162. }
  163. guard let imageURLString = imageDict[languageKey] as? String else {
  164. return NSImage(named: "cnLight")!
  165. }
  166. var imageName = self.versionKey
  167. imageName.append(String(format: "_%@.%@", languageKey, imageURLString.lastPathComponent))
  168. let kImageCacheFilePath: String = NSTemporaryDirectory() + "Advertisement/ImageCache"
  169. let imgSavePath = kImageCacheFilePath + "/" + imageName
  170. if FileManager.default.fileExists(atPath: imgSavePath) {
  171. return NSImage(contentsOfFile: imgSavePath)!
  172. }
  173. DispatchQueue.global().async {
  174. if let url = URL(string: imageURLString as String),
  175. let imageData = try? Data(contentsOf: url) {
  176. if imageData.isEmpty == false {
  177. DispatchQueue.main.async {
  178. try?imageData.write(to: URL(fileURLWithPath: imgSavePath))
  179. NotificationCenter.default.post(name: Notification.Name(rawValue: "KMRecommondInfoUpdateNoti"), object: ["unique":self.versionKey])
  180. }
  181. }
  182. }
  183. }
  184. return NSImage(named: "cnLight")!
  185. }
  186. }
  187. var adsDetailImage: NSImage {
  188. get {
  189. let imageDict: [String: Any] = self.infoDict["detailImage"] as! [String : Any]
  190. var languageKey = KMAdsInfo().languageKey
  191. if KMAppearance.isDarkMode() {
  192. languageKey = "dark_" + KMAdsInfo().languageKey
  193. }
  194. guard let imageURLString = imageDict[languageKey] as? NSString else {
  195. return NSImage(named: "detailCnDark")!
  196. }
  197. var imageName = self.versionKey
  198. imageName.append(String(format: "_%@.%@", languageKey, imageURLString.lastPathComponent))
  199. let kImageCacheFilePath: String = NSTemporaryDirectory() + "Advertisement/ImageCache"
  200. let imgSavePath = kImageCacheFilePath + "/" + imageName
  201. if FileManager.default.fileExists(atPath: imgSavePath) {
  202. return NSImage(contentsOfFile: imgSavePath)!
  203. }
  204. DispatchQueue.global().async {
  205. if let url = URL(string: imageURLString as String),
  206. let imageData = try? Data(contentsOf: url) {
  207. if imageData.isEmpty == false {
  208. DispatchQueue.main.async {
  209. try?imageData.write(to: URL(fileURLWithPath: imgSavePath))
  210. NotificationCenter.default.post(name: Notification.Name(rawValue: "KMRecommondInfoUpdateNoti"), object: ["unique":self.versionKey])
  211. }
  212. }
  213. }
  214. }
  215. return NSImage(named: "detailCnDark")!
  216. }
  217. }
  218. var adsURLLink: String {
  219. get {
  220. let linkDict: [String: Any] = self.infoDict["linkURL"] as! [String : Any]
  221. return linkDict[KMAdsInfo().languageKey] as! String
  222. }
  223. }
  224. var versionKey: String {
  225. return self.infoDict["version"] as! String
  226. }
  227. var title: String {
  228. let titleDict: [String: Any] = self.infoDict["title"] as! [String : Any]
  229. return titleDict[KMAdsInfo().languageKey] as! String
  230. }
  231. var subTitle: String {
  232. let titleDict: [String: Any] = self.infoDict["subTitle"] as! [String : Any]
  233. return titleDict[KMAdsInfo().languageKey] as! String
  234. }
  235. var buyBtnTitle: String {
  236. let titleDict: [String: Any] = self.infoDict["buyBtnTitle"] as! [String : Any]
  237. return titleDict[KMAdsInfo().languageKey] as! String
  238. }
  239. var laterInfoString: String {
  240. let titleDict: [String: Any] = self.infoDict["laterInfoString"] as! [String : Any]
  241. return titleDict[KMAdsInfo().languageKey] as! String
  242. }
  243. }