123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- //
- // KMAdsInfo.swift
- // PDF Reader Pro Edition
- //
- // Created by Niehaoyu on 2024/2/28.
- //
- import Cocoa
- @objcMembers class KMAdsInfoManager: NSObject {
-
- public static let shareInstance = KMAdsInfoManager.init()
-
- public var adsInfoArrM: NSMutableArray = NSMutableArray()
-
- 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" {
- return "zh_CN"
- } else if language == "zh_TW" {
- return "zh_TW"
- }
- return "en"
- }
-
- var versionKey: String {
- return self.infoDict["version"] as! String
- }
-
-
- }
|