123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- //
- // 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 {
- if self.infoDict.value(forKey: "dfpImage") != nil {
- let imageDict: [String: Any] = self.infoDict["dfpImage"] 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: "dfpJumpType") 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
- }
- }
|