KMNewUserGiftWindowController.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 VERSION_DMG
  90. #else
  91. if let localeId = IAPProductsManager.default()?.allAccessPackPermanent_lite?.product?.priceLocale.identifier {
  92. showCNY = localeId.contains("zh_CN") || localeId.contains("zh_TW")
  93. }
  94. #endif
  95. if showCNY == false {
  96. // if NSLocalizedString("USD", comment: "") == "USD" {
  97. giftItemView2_.titleLabel.stringValue = String(format: NSLocalizedString("%@ Coupons in Total", comment: ""), "USD 30")
  98. giftItemView2_.subTitleLabel.stringValue = String(format: NSLocalizedString("New users can get %@ Coupons to upgrade the plan.", comment: ""), "USD 30")
  99. } else {
  100. giftItemView2_.titleLabel.stringValue = String(format: NSLocalizedString("%@ Coupons in Total", comment: ""), "180元")
  101. giftItemView2_.subTitleLabel.stringValue = String(format: NSLocalizedString("New users can get %@ Coupons to upgrade the plan.", comment: ""), "180元")
  102. }
  103. }
  104. giftItemView2_.subTitleLabel.font = .SFProTextRegularFont(14)
  105. buttonBox.borderWidth = 0
  106. contentBox.contentView?.addSubview(buttonIv_, positioned: .below, relativeTo: buttonBox)
  107. buttonIv_.image = NSImage(named: "KMImageNameCancelSubscribeSuccessButton")
  108. buttonIv_.mas_makeConstraints { make in
  109. make?.centerX.mas_equalTo()
  110. make?.centerY.equalTo()(buttonBox)?.offset()(6)
  111. }
  112. buttonBox.contentView?.addSubview(submitButton_)
  113. submitButton_.frame = buttonBox.contentView?.frame ?? .zero
  114. submitButton_.autoresizingMask = [.width, .height]
  115. submitButton_.title = NSLocalizedString("Sign in to Get Gift", comment: "")
  116. submitButton_.font = .UbuntuMediumFontWithSize(16)
  117. submitButton_.target = self
  118. submitButton_.action = #selector(submitAction)
  119. interfaceThemeDidChanged(self.window?.appearance?.name ?? .aqua)
  120. }
  121. override func interfaceThemeDidChanged(_ appearance: NSAppearance.Name) {
  122. super.interfaceThemeDidChanged(appearance)
  123. KMMainThreadExecute {
  124. if KMAppearance.isDarkMode() {
  125. self.titleLabel.textColor = .white
  126. self.subTitleLabel.textColor = .white
  127. self.submitButton_.setTitleColor(NSColor(hex: "#050022"))
  128. } else {
  129. self.titleLabel.textColor = NSColor(hex: "#000150")
  130. self.subTitleLabel.textColor = NSColor(hex: "#000150")
  131. self.submitButton_.setTitleColor(.white)
  132. }
  133. self.giftItemView_.titleLabel.textColor = .white
  134. self.giftItemView_.subTitleLabel.textColor = .white
  135. self.giftItemView2_.titleLabel.textColor = .white
  136. self.giftItemView2_.subTitleLabel.textColor = .white
  137. }
  138. }
  139. // MARK: - Actions
  140. @objc func submitAction() {
  141. trackEvent(eventName: "PUW", params: ["PUW_Btn" : "Welcom_Signin"], platform: .AppCenter)
  142. saveShowRecord()
  143. closeWindow()
  144. _showLoginWindow()
  145. }
  146. @objc func closeAction() {
  147. trackEvent(eventName: "PUW", params: ["PUW_Btn" : "Welcom_Cancel"], platform: .AppCenter)
  148. saveShowRecord()
  149. closeWindow()
  150. _showLoginWindow()
  151. }
  152. // MARK: - Private Methods
  153. private func _showNewGuideWindow() {
  154. if KMMemberInfo.shared.isMemberAllFunction { // 有权益(本地 或 账号)
  155. return
  156. }
  157. KMNewUserGuideWindowController.shared.openWindow()
  158. }
  159. private func _showLoginWindow() {
  160. if KMMemberInfo.shared.isLogin {
  161. _showNewGuideWindow()
  162. return
  163. }
  164. KMLoginWindowsController.shared.openWindow { [unowned self] success in
  165. if success == false {
  166. return
  167. }
  168. _showNewGuideWindow()
  169. }
  170. }
  171. private func _showCenter(animate: Bool){
  172. guard let screenFrame = NSScreen.main?.frame else {
  173. return
  174. }
  175. guard let win = self.window else {
  176. return
  177. }
  178. var frame = win.frame
  179. frame.origin.y = (screenFrame.size.height-frame.size.height)*0.5
  180. frame.origin.x = (screenFrame.size.width-frame.size.width)*0.5
  181. win.setFrame(frame, display: true, animate: animate)
  182. }
  183. // MARK: - Public Methods
  184. public func needShowForlocalCheck() -> Bool {
  185. if KMDataManager.ud_bool(forKey: hasVaildShowKey_) { // 不重复显示
  186. #if DEBUG
  187. #else
  188. return false
  189. #endif
  190. }
  191. let appFirstLaunch = KMDataManager.default.appFirstLaunch
  192. let appFirstLaunchForUpdate = KMDataManager.default.appFirstLaunchForUpdate
  193. if appFirstLaunch == false && appFirstLaunchForUpdate == false {
  194. #if DEBUG
  195. #else
  196. // return false
  197. #endif
  198. }
  199. return true
  200. }
  201. public func needShow() -> Bool {
  202. if needShowForlocalCheck() == false {
  203. return false
  204. }
  205. if KMNewUserGiftManager.default.needShow() == false {
  206. return false
  207. }
  208. if KMMemberInfo.shared.isLogin { // 登陆
  209. return false
  210. }
  211. return true
  212. }
  213. public func saveShowRecord() {
  214. KMDataManager.ud_set(true, forKey: hasVaildShowKey_)
  215. }
  216. public func openWindow() {
  217. showWindow(nil)
  218. _showCenter(animate: false)
  219. window?.makeKeyAndOrderFront(nil)
  220. trackEvent(eventName: "PUW", params: ["PUW_Exposure" : "PUW_WelcomGift"], platform: .AppCenter)
  221. }
  222. public func closeWindow() {
  223. window?.close()
  224. }
  225. }
  226. // MARK: - NSWindowDelegate
  227. extension KMNewUserGiftWindowController: NSWindowDelegate {
  228. func windowDidResize(_ notification: Notification) {
  229. guard let data = window?.isEqual(to: notification.object), data == true else {
  230. return
  231. }
  232. backgroundIv.image?.size = window?.frame.size ?? NSMakeSize(660, 490)
  233. }
  234. }