KMCouponDueWindowController.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. func refreshUI() {
  45. if KMAppearance.isDarkMode() {
  46. self.window?.contentView?.layer?.backgroundColor = NSColor(red: 24/255, green: 22/255, blue: 31/255, alpha: 1).cgColor
  47. self.titleLabel.textColor = NSColor(red: 252/255, green: 253/255, blue: 255/255, alpha: 1)
  48. self.subTitleLabel.textColor = NSColor(red: 252/255, green: 253/255, blue: 255/255, alpha: 1)
  49. self.laterInfoLabel.textColor = NSColor(red: 252/255, green: 253/255, blue: 255/255, alpha: 1)
  50. } else {
  51. self.window?.contentView?.layer?.backgroundColor = NSColor(red: 245/255, green: 249/255, blue: 255/255, alpha: 1).cgColor
  52. self.titleLabel.textColor = NSColor(red: 0, green: 33/255, blue: 67/255, alpha: 1)
  53. self.subTitleLabel.textColor = NSColor(red: 14/255, green: 17/255, blue: 20/255, alpha: 1)
  54. self.laterInfoLabel.textColor = NSColor(red: 0, green: 33/255, blue: 67/255, alpha: 1)
  55. }
  56. self.buyButton.setTitleColor(NSColor.white)
  57. }
  58. func reloadData() {
  59. self.titleLabel.stringValue = KMAdsInfoManager.shareInstance.couponInfo?.title ?? ""
  60. if KMAdsInfo().languageKey == "zh_CN" {
  61. self.subTitleLabel.stringValue = String(format: KMAdsInfoManager.shareInstance.couponInfo?.subTitle ?? "", self.endUpAt)
  62. self.buyButton.title = String(format: KMAdsInfoManager.shareInstance.couponInfo?.buyBtnTitle ?? "", String(format: "%ld", 10-self.disCount/10))
  63. } else {
  64. self.subTitleLabel.stringValue = String(format: KMAdsInfoManager.shareInstance.couponInfo?.subTitle ?? "", self.endUpAt, String(format: "%ld%@", self.disCount,"%"))
  65. self.buyButton.title = String(format: KMAdsInfoManager.shareInstance.couponInfo?.buyBtnTitle ?? "", String(format: "%ld", self.disCount)+"%")
  66. }
  67. self.laterInfoLabel.stringValue = String(format: KMAdsInfoManager.shareInstance.couponInfo?.laterInfoString ?? "", self.couponCode)
  68. self.contendImage.image = KMAdsInfoManager.shareInstance.couponInfo?.adsImage
  69. self.tipInfoImage.image = KMAdsInfoManager.shareInstance.couponInfo?.adsDetailImage
  70. self.refreshUI()
  71. }
  72. //MARK: IBAction
  73. @IBAction func buyBtnClicked(_ sender: KMButton) {
  74. guard let string = KMAdsInfoManager.shareInstance.couponInfo?.adsURLLink else {
  75. return
  76. }
  77. let newURL = NSURL(string: string+"&coupon_code="+self.couponCode)
  78. NSWorkspace.shared.open(newURL! as URL)
  79. FMTrackEventManager.defaultManager.trackEvent(event: "PUW", withProperties: ["PUW_Btn" : "Btn_PUW_TrialExpireOffer_Buy"])
  80. }
  81. @objc func recommondInfoUpdateNoti(_ notification: Notification) {
  82. DispatchQueue.main.async {
  83. guard let dict = notification.object as? NSDictionary else {
  84. return
  85. }
  86. guard let unique = dict["unique"] as? String else {
  87. return
  88. }
  89. if unique == KMAdsInfoManager.shareInstance.couponInfo?.versionKey {
  90. self.reloadData()
  91. }
  92. }
  93. }
  94. }