// // KMCouponDueWindowController.swift // PDF Reader Pro // // Created by Niehaoyu on 2024/5/22. // import Cocoa class KMCouponDueWindowController: NSWindowController { @IBOutlet var contendBox: NSBox! @IBOutlet var contendImage: NSImageView! @IBOutlet var titleLabel: NSTextField! @IBOutlet var subTitleLabel: NSTextField! @IBOutlet var tipInfoView: NSView! @IBOutlet var tipInfoImage: NSImageView! @IBOutlet var buyBox: NSBox! @IBOutlet var buyButton: KMButton! @IBOutlet var laterInfoLabel: NSTextField! var disCount: Int = 20 var couponCode: String = "" var endUpAt: String = "" deinit { NotificationCenter.default.removeObserver(self) } override func windowDidLoad() { super.windowDidLoad() // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file. self.window?.contentView?.wantsLayer = true self.titleLabel.font = NSFont.SFProTextBoldFont(21) self.subTitleLabel.font = NSFont.SFProTextRegularFont(14) self.buyButton.font = NSFont.SFProTextSemiboldFont(16) self.laterInfoLabel.font = NSFont.SFProTextRegularFont(12) self.buyBox.cornerRadius = NSHeight(self.buyBox.frame)/2 self.buyBox.fillColor = NSColor(red: 255/255, green: 158/255, blue: 42/255, alpha: 1) self.buyButton.mouseMoveCallback = {[weak self] mouseEntered in if mouseEntered { self?.buyBox.fillColor = NSColor(red: 255/255, green: 139/255, blue: 0, alpha: 1) } else { self?.buyBox.fillColor = NSColor(red: 255/255, green: 158/255, blue: 42/255, alpha: 1) } } self.buyButton.setTitleColor(NSColor.white) NotificationCenter.default.addObserver(self, selector: #selector(recommondInfoUpdateNoti), name: NSNotification.Name("KMRecommondInfoUpdateNoti"), object: nil) } override func showWindow(_ sender: Any?) { super.showWindow(sender) self.window?.isRestorable = false // self.window?.center() } func refreshUI() { if KMAppearance.isDarkMode() { self.window?.contentView?.layer?.backgroundColor = NSColor(red: 24/255, green: 22/255, blue: 31/255, alpha: 1).cgColor self.titleLabel.textColor = NSColor(red: 252/255, green: 253/255, blue: 255/255, alpha: 1) self.subTitleLabel.textColor = NSColor(red: 252/255, green: 253/255, blue: 255/255, alpha: 1) self.laterInfoLabel.textColor = NSColor(red: 252/255, green: 253/255, blue: 255/255, alpha: 1) } else { self.window?.contentView?.layer?.backgroundColor = NSColor(red: 245/255, green: 249/255, blue: 255/255, alpha: 1).cgColor self.titleLabel.textColor = NSColor(red: 0, green: 33/255, blue: 67/255, alpha: 1) self.subTitleLabel.textColor = NSColor(red: 14/255, green: 17/255, blue: 20/255, alpha: 1) self.laterInfoLabel.textColor = NSColor(red: 0, green: 33/255, blue: 67/255, alpha: 1) } self.buyButton.setTitleColor(NSColor.white) } func reloadData() { self.titleLabel.stringValue = KMAdsInfoManager.shareInstance.couponInfo?.title ?? "" if KMAdsInfo().languageKey == "zh_CN" { self.subTitleLabel.stringValue = String(format: KMAdsInfoManager.shareInstance.couponInfo?.subTitle ?? "", self.endUpAt) self.buyButton.title = String(format: KMAdsInfoManager.shareInstance.couponInfo?.buyBtnTitle ?? "", String(format: "%ld", 10-self.disCount/10)) } else { self.subTitleLabel.stringValue = String(format: KMAdsInfoManager.shareInstance.couponInfo?.subTitle ?? "", self.endUpAt, String(format: "%ld%@", self.disCount,"%")) self.buyButton.title = String(format: KMAdsInfoManager.shareInstance.couponInfo?.buyBtnTitle ?? "", String(format: "%ld", self.disCount)+"%") } self.laterInfoLabel.stringValue = String(format: KMAdsInfoManager.shareInstance.couponInfo?.laterInfoString ?? "", self.couponCode) self.contendImage.image = KMAdsInfoManager.shareInstance.couponInfo?.adsImage self.tipInfoImage.image = KMAdsInfoManager.shareInstance.couponInfo?.adsDetailImage self.refreshUI() } //MARK: IBAction @IBAction func buyBtnClicked(_ sender: KMButton) { guard let string = KMAdsInfoManager.shareInstance.couponInfo?.adsURLLink else { return } #if VERSION_DMG let embeddedWC = KMPurchaseEmbeddedWindowController.currentFirstTrialWC("com.brother.pdfreaderpro.mac.product_3") embeddedWC.couponCode = self.couponCode embeddedWC.showWindow(nil) embeddedWC.window?.center() #else let newURL = NSURL(string: string+"&coupon_code="+self.couponCode) NSWorkspace.shared.open(newURL! as URL) #endif FMTrackEventManager.defaultManager.trackEvent(event: "PUW", withProperties: ["PUW_Btn" : "Btn_PUW_TrialExpireOffer_Buy"]) } @objc func recommondInfoUpdateNoti(_ notification: Notification) { DispatchQueue.main.async { guard let dict = notification.object as? NSDictionary else { return } guard let unique = dict["unique"] as? String else { return } if unique == KMAdsInfoManager.shareInstance.couponInfo?.versionKey { self.reloadData() } } } }