// // KMAdsInfo.swift // PDF Reader Pro Edition // // Created by Niehaoyu on 2024/2/28. // import Cocoa enum KMDFPJumpType: Int { case Web = 0 // 网页 case ComparisonSheet // 比较表 } @objcMembers class KMAdsInfoManager: NSObject { public static let shareInstance = KMAdsInfoManager.init() public var adsInfoArrM: NSMutableArray = NSMutableArray() var couponInfo: KMCouponInfo? override init() { super.init() } func getRandomAdsInfo() -> KMAdsInfo? { let randomValue = Int(arc4random()%100) for idObject in self.adsInfoArrM { let adsInfo = idObject as! KMAdsInfo let percentValue = adsInfo.percentValue let minValue = Int(percentValue.components(separatedBy: "-").first!) let maxValue = Int(percentValue.components(separatedBy: "-").last!) if minValue! <= randomValue && randomValue <= maxValue! { return adsInfo } } return nil } } @objcMembers class KMAdsInfo: NSObject { var infoDict: NSMutableDictionary! var percentValue: String { get { return self.infoDict["percent"] as! String } } var show: Bool { get { let showString = self.infoDict.object(forKey: "show") as! String if let showValue = Bool(showString) { if showValue == false { return false } } var startTime = Double(self.infoDict["startTime"] as! String)! var endTime = Double(self.infoDict["endTime"] as! String)! if startTime > 1000000000 { startTime = startTime / 1000 } if endTime > 1000000000 { endTime = endTime / 1000 } let startDate = NSDate(timeIntervalSince1970: startTime) let endDate = NSDate(timeIntervalSince1970: endTime) if startDate.compare(Date()) == .orderedAscending && endDate.compare(Date()) == .orderedDescending{ return true } return false } } var adsImage: NSImage { get { let imageDict: [String: Any] = self.infoDict["image"] as! [String : Any] let imageURLString = imageDict[self.languageKey] as! NSString var imageName = self.versionKey imageName.append(String(format: "_%@.%@", self.languageKey, imageURLString.lastPathComponent)) let kImageCacheFilePath: String = NSTemporaryDirectory() + "Advertisement/ImageCache" let imgSavePath = kImageCacheFilePath + "/" + imageName if FileManager.default.fileExists(atPath: imgSavePath) { return NSImage(contentsOfFile: imgSavePath)! } DispatchQueue.global().async { if let url = URL(string: imageURLString as String), let imageData = try? Data(contentsOf: url) { if imageData.isEmpty == false { DispatchQueue.main.async { try?imageData.write(to: URL(fileURLWithPath: imgSavePath)) NotificationCenter.default.post(name: Notification.Name(rawValue: "KMRecommondInfoUpdateNoti"), object: ["unique":self.versionKey]) } } } } return NSImage(named: "ComPDFKit_EN")! } } var adsURLLink: String { get { let linkDict: [String: Any] = self.infoDict["linkURL"] as! [String : Any] return linkDict[self.languageKey] as! String } } var languageKey: String { let language = Bundle.main.preferredLocalizations[0] if language == "zh_CN" || language == "zh-Hans" { return "zh_CN" } else if language == "zh_TW" || language == "zh-Hant" { return "zh_TW" } return "en" } var versionKey: String { return self.infoDict["version"] as! String } var jumpType: KMDFPJumpType { get { guard let typeString = self.infoDict.object(forKey: "jumpType") else { return .Web } if typeString is String { let jumpTypeString = typeString as! String if jumpTypeString == "Comparison Sheet" { return .ComparisonSheet } else if jumpTypeString == "Web" { return .Web } } return .Web } } } @objcMembers class KMCouponInfo: NSObject { var infoDict: NSMutableDictionary! var show: Bool { get { let showString = self.infoDict.object(forKey: "show") as! String if let showValue = Bool(showString) { if showValue == false { return false } } var startTime = Double(self.infoDict["startTime"] as! String)! var endTime = Double(self.infoDict["endTime"] as! String)! if startTime > 1000000000 { startTime = startTime / 1000 } if endTime > 1000000000 { endTime = endTime / 1000 } let startDate = NSDate(timeIntervalSince1970: startTime) let endDate = NSDate(timeIntervalSince1970: endTime) if startDate.compare(Date()) == .orderedAscending && endDate.compare(Date()) == .orderedDescending{ return true } return false } } var adsImage: NSImage { get { let imageDict: [String: Any] = self.infoDict["image"] as! [String : Any] var languageKey = KMAdsInfo().languageKey if KMAppearance.isDarkMode() { languageKey = "dark_" + KMAdsInfo().languageKey } guard let imageURLString = imageDict[languageKey] as? String else { return NSImage(named: "cnLight")! } var imageName = self.versionKey imageName.append(String(format: "_%@.%@", languageKey, imageURLString.lastPathComponent)) let kImageCacheFilePath: String = NSTemporaryDirectory() + "Advertisement/ImageCache" let imgSavePath = kImageCacheFilePath + "/" + imageName if FileManager.default.fileExists(atPath: imgSavePath) { return NSImage(contentsOfFile: imgSavePath)! } DispatchQueue.global().async { if let url = URL(string: imageURLString as String), let imageData = try? Data(contentsOf: url) { if imageData.isEmpty == false { DispatchQueue.main.async { try?imageData.write(to: URL(fileURLWithPath: imgSavePath)) NotificationCenter.default.post(name: Notification.Name(rawValue: "KMRecommondInfoUpdateNoti"), object: ["unique":self.versionKey]) } } } } return NSImage(named: "cnLight")! } } var adsDetailImage: NSImage { get { let imageDict: [String: Any] = self.infoDict["detailImage"] as! [String : Any] var languageKey = KMAdsInfo().languageKey if KMAppearance.isDarkMode() { languageKey = "dark_" + KMAdsInfo().languageKey } guard let imageURLString = imageDict[languageKey] as? NSString else { return NSImage(named: "detailCnDark")! } var imageName = self.versionKey imageName.append(String(format: "_%@.%@", languageKey, imageURLString.lastPathComponent)) let kImageCacheFilePath: String = NSTemporaryDirectory() + "Advertisement/ImageCache" let imgSavePath = kImageCacheFilePath + "/" + imageName if FileManager.default.fileExists(atPath: imgSavePath) { return NSImage(contentsOfFile: imgSavePath)! } DispatchQueue.global().async { if let url = URL(string: imageURLString as String), let imageData = try? Data(contentsOf: url) { if imageData.isEmpty == false { DispatchQueue.main.async { try?imageData.write(to: URL(fileURLWithPath: imgSavePath)) NotificationCenter.default.post(name: Notification.Name(rawValue: "KMRecommondInfoUpdateNoti"), object: ["unique":self.versionKey]) } } } } return NSImage(named: "detailCnDark")! } } var adsURLLink: String { get { let linkDict: [String: Any] = self.infoDict["linkURL"] as! [String : Any] return linkDict[KMAdsInfo().languageKey] as! String } } var versionKey: String { return self.infoDict["version"] as! String } var title: String { let titleDict: [String: Any] = self.infoDict["title"] as! [String : Any] return titleDict[KMAdsInfo().languageKey] as! String } var subTitle: String { let titleDict: [String: Any] = self.infoDict["subTitle"] as! [String : Any] return titleDict[KMAdsInfo().languageKey] as! String } var buyBtnTitle: String { let titleDict: [String: Any] = self.infoDict["buyBtnTitle"] as! [String : Any] return titleDict[KMAdsInfo().languageKey] as! String } var laterInfoString: String { let titleDict: [String: Any] = self.infoDict["laterInfoString"] as! [String : Any] return titleDict[KMAdsInfo().languageKey] as! String } }