KMNewUserGiftWindowController.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. //
  2. // KMNewUserGiftWindowController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by User-Tangchao on 2024/12/25.
  6. //
  7. import Cocoa
  8. @objcMembers class KMNewUserGiftWindowController: KMBaseWindowController {
  9. @IBOutlet weak var contentBox: NSBox!
  10. @IBOutlet weak var backgroundIv: NSImageView!
  11. @IBOutlet weak var titleLabel: NSTextField!
  12. @IBOutlet weak var subTitleLabel: NSTextField!
  13. @IBOutlet weak var listBox: NSBox!
  14. @IBOutlet weak var listBos2: NSBox!
  15. @IBOutlet weak var buttonBox: NSBox!
  16. private lazy var listBackgroundIv: NSImageView = {
  17. let view = NSImageView()
  18. return view
  19. }()
  20. private lazy var listBackgroundIv2: NSImageView = {
  21. let view = NSImageView()
  22. return view
  23. }()
  24. private lazy var giftItemView_: KMNewUserGiftItemView = {
  25. let view = KMNewUserGiftItemView()
  26. return view
  27. }()
  28. private lazy var giftItemView2_: KMNewUserGiftItemView = {
  29. let view = KMNewUserGiftItemView()
  30. return view
  31. }()
  32. private lazy var submitButton_: NSButton = {
  33. let view = NSButton()
  34. view.isBordered = false
  35. return view
  36. }()
  37. private lazy var buttonIv_: NSImageView = {
  38. let view = NSImageView()
  39. return view
  40. }()
  41. static let shared = KMNewUserGiftWindowController(windowNibName: "KMNewUserGiftWindowController")
  42. private let hasVaildShowKey_ = "hasVaildShow"
  43. override func windowDidLoad() {
  44. super.windowDidLoad()
  45. let closeButton = window?.standardWindowButton(.closeButton)
  46. closeButton?.target = self
  47. closeButton?.action = #selector(closeAction)
  48. window?.standardWindowButton(.zoomButton)?.isHidden = true
  49. window?.standardWindowButton(.miniaturizeButton)?.isHidden = true
  50. window?.delegate = self
  51. backgroundIv.image = NSImage(named: "KMImageNewUserGiftBg")
  52. backgroundIv.image?.size = window?.frame.size ?? NSMakeSize(660, 490)
  53. titleLabel.stringValue = NSLocalizedString("Welcome Gift For New Users!", comment: "")
  54. subTitleLabel.stringValue = NSLocalizedString("Hi! We’re delighted to meet you. Dive in and enjoy your PDF journey with us!", comment: "")
  55. titleLabel.font = .UbuntuMediumFontWithSize(30)
  56. titleLabel.alignment = .center
  57. subTitleLabel.font = .UbuntuMediumFontWithSize(16)
  58. contentBox.contentView?.addSubview(listBackgroundIv, positioned: .below, relativeTo: listBox)
  59. listBackgroundIv.mas_makeConstraints { make in
  60. make?.centerX.equalTo()(listBox)?.offset()(-3)
  61. make?.centerY.equalTo()(listBox)?.offset()(3)
  62. }
  63. listBackgroundIv.image = NSImage(named: "KMImageNewUserGiftItemBg")
  64. contentBox.contentView?.addSubview(listBackgroundIv2, positioned: .below, relativeTo: listBos2)
  65. listBackgroundIv2.mas_makeConstraints { make in
  66. make?.centerX.equalTo()(listBos2)?.offset()(-3)
  67. make?.centerY.equalTo()(listBos2)?.offset()(3)
  68. }
  69. listBackgroundIv2.image = NSImage(named: "KMImageNewUserGiftItemBg2")
  70. listBox.borderWidth = 0
  71. listBos2.borderWidth = 0
  72. listBox.contentView = giftItemView_
  73. listBos2.contentView = giftItemView2_
  74. giftItemView_.titleLabel.stringValue = String(format: NSLocalizedString("%d-day VIP Free Trial", comment: ""), 7)
  75. giftItemView_.titleLabel.font = .SFProTextRegularFont(20)
  76. giftItemView_.subTitleLabel.stringValue = NSLocalizedString("Enjoy the full functions of PDF Reader Pro.\nEdit, convert, annotate, and combine PDFs.", comment: "")
  77. giftItemView_.subTitleLabel.font = .SFProTextRegularFont(14)
  78. giftItemView2_.titleLabel.font = .SFProTextRegularFont(20)
  79. if KMIsDMGVersion() {
  80. if NSLocalizedString("USD", comment: "") == "USD" {
  81. giftItemView2_.titleLabel.stringValue = String(format: NSLocalizedString("%@ Coupons in Total", comment: ""), "$60")
  82. giftItemView2_.subTitleLabel.stringValue = String(format: NSLocalizedString("New users can get %@ Coupons to upgrade the plan.", comment: ""), "$60")
  83. } else {
  84. giftItemView2_.titleLabel.stringValue = String(format: NSLocalizedString("%@ Coupons in Total", comment: ""), "360元")
  85. giftItemView2_.subTitleLabel.stringValue = String(format: NSLocalizedString("New users can get %@ Coupons to upgrade the plan.", comment: ""), "360元")
  86. }
  87. } else {
  88. var showCNY = false
  89. if let localeId = IAPProductsManager.default()?.allAccessPackPermanent_lite?.product?.priceLocale.identifier {
  90. showCNY = localeId.contains("zh_CN") || localeId.contains("zh_TW")
  91. }
  92. if showCNY == false {
  93. // if NSLocalizedString("USD", comment: "") == "USD" {
  94. giftItemView2_.titleLabel.stringValue = String(format: NSLocalizedString("%@ Coupons in Total", comment: ""), "USD 30")
  95. giftItemView2_.subTitleLabel.stringValue = String(format: NSLocalizedString("New users can get %@ Coupons to upgrade the plan.", comment: ""), "$30")
  96. } else {
  97. giftItemView2_.titleLabel.stringValue = String(format: NSLocalizedString("%@ Coupons in Total", comment: ""), "180元")
  98. giftItemView2_.subTitleLabel.stringValue = String(format: NSLocalizedString("New users can get %@ Coupons to upgrade the plan.", comment: ""), "180元")
  99. }
  100. }
  101. giftItemView2_.subTitleLabel.font = .SFProTextRegularFont(14)
  102. buttonBox.borderWidth = 0
  103. contentBox.contentView?.addSubview(buttonIv_, positioned: .below, relativeTo: buttonBox)
  104. buttonIv_.image = NSImage(named: "KMImageNameCancelSubscribeSuccessButton")
  105. buttonIv_.mas_makeConstraints { make in
  106. make?.centerX.mas_equalTo()
  107. make?.centerY.equalTo()(buttonBox)?.offset()(6)
  108. }
  109. buttonBox.contentView?.addSubview(submitButton_)
  110. submitButton_.frame = buttonBox.contentView?.frame ?? .zero
  111. submitButton_.autoresizingMask = [.width, .height]
  112. submitButton_.title = NSLocalizedString("Sign in to Get Gift", comment: "")
  113. submitButton_.font = .UbuntuMediumFontWithSize(16)
  114. submitButton_.target = self
  115. submitButton_.action = #selector(submitAction)
  116. interfaceThemeDidChanged(self.window?.appearance?.name ?? .aqua)
  117. }
  118. override func interfaceThemeDidChanged(_ appearance: NSAppearance.Name) {
  119. super.interfaceThemeDidChanged(appearance)
  120. KMMainThreadExecute {
  121. if KMAppearance.isDarkMode() {
  122. self.titleLabel.textColor = .white
  123. self.subTitleLabel.textColor = .white
  124. self.submitButton_.setTitleColor(NSColor(hex: "#050022"))
  125. } else {
  126. self.titleLabel.textColor = NSColor(hex: "#000150")
  127. self.subTitleLabel.textColor = NSColor(hex: "#000150")
  128. self.submitButton_.setTitleColor(.white)
  129. }
  130. self.giftItemView_.titleLabel.textColor = .white
  131. self.giftItemView_.subTitleLabel.textColor = .white
  132. self.giftItemView2_.titleLabel.textColor = .white
  133. self.giftItemView2_.subTitleLabel.textColor = .white
  134. }
  135. }
  136. // MARK: - Actions
  137. @objc func submitAction() {
  138. trackEvent(eventName: "PUW", params: ["PUW_Btn" : "Welcom_Signin"], platform: .AppCenter)
  139. saveShowRecord()
  140. closeWindow()
  141. _showLoginWindow()
  142. }
  143. @objc func closeAction() {
  144. trackEvent(eventName: "PUW", params: ["PUW_Btn" : "Welcom_Cancel"], platform: .AppCenter)
  145. saveShowRecord()
  146. closeWindow()
  147. _showLoginWindow()
  148. }
  149. // MARK: - Private Methods
  150. private func _showNewGuideWindow() {
  151. if KMMemberInfo.shared.isMemberAllFunction { // 有权益(本地 或 账号)
  152. return
  153. }
  154. KMNewUserGuideWindowController.shared.openWindow()
  155. }
  156. private func _showLoginWindow() {
  157. if KMMemberInfo.shared.isLogin {
  158. _showNewGuideWindow()
  159. return
  160. }
  161. KMLoginWindowsController.shared.openWindow { [unowned self] success in
  162. if success == false {
  163. return
  164. }
  165. _showNewGuideWindow()
  166. }
  167. }
  168. private func _showCenter(animate: Bool){
  169. guard let screenFrame = NSScreen.main?.frame else {
  170. return
  171. }
  172. guard let win = self.window else {
  173. return
  174. }
  175. var frame = win.frame
  176. frame.origin.y = (screenFrame.size.height-frame.size.height)*0.5
  177. frame.origin.x = (screenFrame.size.width-frame.size.width)*0.5
  178. win.setFrame(frame, display: true, animate: animate)
  179. }
  180. // MARK: - Public Methods
  181. public func needShowForlocalCheck() -> Bool {
  182. if KMDataManager.ud_bool(forKey: hasVaildShowKey_) { // 不重复显示
  183. #if DEBUG
  184. #else
  185. return false
  186. #endif
  187. }
  188. let appFirstLaunch = KMDataManager.default.appFirstLaunch
  189. let appFirstLaunchForUpdate = KMDataManager.default.appFirstLaunchForUpdate
  190. if appFirstLaunch == false && appFirstLaunchForUpdate == false {
  191. #if DEBUG
  192. #else
  193. // return false
  194. #endif
  195. }
  196. return true
  197. }
  198. public func needShow() -> Bool {
  199. if needShowForlocalCheck() == false {
  200. return false
  201. }
  202. if KMNewUserGiftManager.default.needShow() == false {
  203. return false
  204. }
  205. if KMMemberInfo.shared.isLogin { // 登陆
  206. return false
  207. }
  208. return true
  209. }
  210. public func saveShowRecord() {
  211. KMDataManager.ud_set(true, forKey: hasVaildShowKey_)
  212. }
  213. public func openWindow() {
  214. showWindow(nil)
  215. _showCenter(animate: false)
  216. window?.makeKeyAndOrderFront(nil)
  217. trackEvent(eventName: "PUW", params: ["PUW_Exposure" : "PUW_WelcomGift"], platform: .AppCenter)
  218. }
  219. public func closeWindow() {
  220. window?.close()
  221. }
  222. }
  223. // MARK: - NSWindowDelegate
  224. extension KMNewUserGiftWindowController: NSWindowDelegate {
  225. func windowDidResize(_ notification: Notification) {
  226. guard let data = window?.isEqual(to: notification.object), data == true else {
  227. return
  228. }
  229. backgroundIv.image?.size = window?.frame.size ?? NSMakeSize(660, 490)
  230. }
  231. }