KMCouponDueWindowController.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //
  2. // KMCouponDueWindowController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/5/22.
  6. //
  7. import Cocoa
  8. class KMCouponDueWindowController: NSWindowController {
  9. @IBOutlet var contendBox: NSBox!
  10. @IBOutlet var contendImage: NSImageView!
  11. @IBOutlet var titleLabel: NSTextField!
  12. @IBOutlet var subTitleLabel: NSTextField!
  13. @IBOutlet var tipInfoView: NSView!
  14. @IBOutlet var tipInfoImage: NSImageView!
  15. @IBOutlet var buyBox: NSBox!
  16. @IBOutlet var buyButton: KMButton!
  17. @IBOutlet var laterInfoLabel: NSTextField!
  18. var disCount: Int = 20
  19. var couponCode: String = ""
  20. var endUpAt: String = ""
  21. deinit {
  22. NotificationCenter.default.removeObserver(self)
  23. }
  24. override func windowDidLoad() {
  25. super.windowDidLoad()
  26. // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
  27. self.window?.contentView?.wantsLayer = true
  28. self.titleLabel.font = NSFont.SFProTextBoldFont(21)
  29. self.subTitleLabel.font = NSFont.SFProTextRegularFont(14)
  30. self.buyButton.font = NSFont.SFProTextSemiboldFont(16)
  31. self.laterInfoLabel.font = NSFont.SFProTextRegularFont(12)
  32. self.buyBox.cornerRadius = NSHeight(self.buyBox.frame)/2
  33. self.buyBox.fillColor = NSColor(red: 255/255, green: 158/255, blue: 42/255, alpha: 1)
  34. self.buyButton.mouseMoveCallback = {[weak self] mouseEntered in
  35. if mouseEntered {
  36. self?.buyBox.fillColor = NSColor(red: 255/255, green: 139/255, blue: 0, alpha: 1)
  37. } else {
  38. self?.buyBox.fillColor = NSColor(red: 255/255, green: 158/255, blue: 42/255, alpha: 1)
  39. }
  40. }
  41. self.buyButton.setTitleColor(NSColor.white)
  42. NotificationCenter.default.addObserver(self, selector: #selector(recommondInfoUpdateNoti), name: NSNotification.Name("KMRecommondInfoUpdateNoti"), object: nil)
  43. }
  44. override func showWindow(_ sender: Any?) {
  45. super.showWindow(sender)
  46. self.window?.isRestorable = false
  47. // self.window?.center()
  48. }
  49. func refreshUI() {
  50. if KMAppearance.isDarkMode() {
  51. self.window?.contentView?.layer?.backgroundColor = NSColor(red: 24/255, green: 22/255, blue: 31/255, alpha: 1).cgColor
  52. self.titleLabel.textColor = NSColor(red: 252/255, green: 253/255, blue: 255/255, alpha: 1)
  53. self.subTitleLabel.textColor = NSColor(red: 252/255, green: 253/255, blue: 255/255, alpha: 1)
  54. self.laterInfoLabel.textColor = NSColor(red: 252/255, green: 253/255, blue: 255/255, alpha: 1)
  55. } else {
  56. self.window?.contentView?.layer?.backgroundColor = NSColor(red: 245/255, green: 249/255, blue: 255/255, alpha: 1).cgColor
  57. self.titleLabel.textColor = NSColor(red: 0, green: 33/255, blue: 67/255, alpha: 1)
  58. self.subTitleLabel.textColor = NSColor(red: 14/255, green: 17/255, blue: 20/255, alpha: 1)
  59. self.laterInfoLabel.textColor = NSColor(red: 0, green: 33/255, blue: 67/255, alpha: 1)
  60. }
  61. self.buyButton.setTitleColor(NSColor.white)
  62. }
  63. func reloadData() {
  64. self.titleLabel.stringValue = KMAdsInfoManager.shareInstance.couponInfo?.title ?? ""
  65. if KMAdsInfo().languageKey == "zh_CN" {
  66. self.subTitleLabel.stringValue = String(format: KMAdsInfoManager.shareInstance.couponInfo?.subTitle ?? "", self.endUpAt)
  67. self.buyButton.title = String(format: KMAdsInfoManager.shareInstance.couponInfo?.buyBtnTitle ?? "", String(format: "%ld", 10-self.disCount/10))
  68. } else {
  69. self.subTitleLabel.stringValue = String(format: KMAdsInfoManager.shareInstance.couponInfo?.subTitle ?? "", self.endUpAt, String(format: "%ld%@", self.disCount,"%"))
  70. self.buyButton.title = String(format: KMAdsInfoManager.shareInstance.couponInfo?.buyBtnTitle ?? "", String(format: "%ld", self.disCount)+"%")
  71. }
  72. self.laterInfoLabel.stringValue = String(format: KMAdsInfoManager.shareInstance.couponInfo?.laterInfoString ?? "", self.couponCode)
  73. self.contendImage.image = KMAdsInfoManager.shareInstance.couponInfo?.adsImage
  74. self.tipInfoImage.image = KMAdsInfoManager.shareInstance.couponInfo?.adsDetailImage
  75. self.refreshUI()
  76. }
  77. //MARK: IBAction
  78. @IBAction func buyBtnClicked(_ sender: KMButton) {
  79. guard let string = KMAdsInfoManager.shareInstance.couponInfo?.adsURLLink else {
  80. return
  81. }
  82. #if VERSION_DMG
  83. let embeddedWC = KMPurchaseEmbeddedWindowController.currentCode(KMAdvancedAnnualSubscriptionCodeKey_DMG)
  84. embeddedWC.showWindow(nil)
  85. embeddedWC.window?.center()
  86. #else
  87. let newURL = NSURL(string: string+"&coupon_code="+self.couponCode)
  88. NSWorkspace.shared.open(newURL! as URL)
  89. #endif
  90. FMTrackEventManager.defaultManager.trackEvent(event: "PUW", withProperties: ["PUW_Btn" : "Btn_PUW_TrialExpireOffer_Buy"])
  91. }
  92. @objc func recommondInfoUpdateNoti(_ notification: Notification) {
  93. DispatchQueue.main.async {
  94. guard let dict = notification.object as? NSDictionary else {
  95. return
  96. }
  97. guard let unique = dict["unique"] as? String else {
  98. return
  99. }
  100. if unique == KMAdsInfoManager.shareInstance.couponInfo?.versionKey {
  101. self.reloadData()
  102. }
  103. }
  104. }
  105. }