AIPurchaseWindowController.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. //
  2. // AIPurchaseWindowController.swift
  3. // PDF Reader Pro Edition
  4. //
  5. // Created by Niehaoyu on 2024/1/18.
  6. //
  7. import Cocoa
  8. class AIPurchaseWindowController: NSWindowController, NSWindowDelegate {
  9. @IBOutlet weak var contendBox: NSBox!
  10. @IBOutlet weak var tipImage: NSImageView!
  11. @IBOutlet weak var titleLabel: NSTextField!
  12. @IBOutlet weak var subTitleLabel: NSTextField!
  13. @IBOutlet weak var purchaseBox: NSBox!
  14. @IBOutlet weak var purchaseButton: KMButton!
  15. @IBOutlet weak var priceLabel: NSTextField!
  16. @IBOutlet weak var priceDesLabel: NSTextField!
  17. @IBOutlet weak var restoreButton: HyperLinkButton!
  18. @IBOutlet weak var privacyButton: HyperLinkButton!
  19. @IBOutlet weak var termOfButton: HyperLinkButton!
  20. static var currentWindowController: AIPurchaseWindowController!
  21. public var isAIPurchase = false
  22. @objc static func currentWC() -> AIPurchaseWindowController {
  23. if currentWindowController != nil {
  24. return currentWindowController
  25. } else {
  26. let guideInfoWC: AIPurchaseWindowController = AIPurchaseWindowController.init(windowNibName: "AIPurchaseWindowController")
  27. currentWindowController = guideInfoWC;
  28. return guideInfoWC
  29. }
  30. }
  31. override func showWindow(_ sender: Any?) {
  32. super.showWindow(sender)
  33. isAIPurchase = false
  34. if let window = self.window {
  35. window.center()
  36. self.reloadData()
  37. }
  38. }
  39. deinit {
  40. NotificationCenter.default.removeObserver(self)
  41. }
  42. override func windowDidLoad() {
  43. super.windowDidLoad()
  44. // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
  45. self.titleLabel.font = NSFont.UbuntuBoldFontWithSize(20)
  46. self.subTitleLabel.font = NSFont.SFProTextRegularFont(14)
  47. self.priceLabel.font = NSFont.UbuntuBoldFontWithSize(20)
  48. self.priceDesLabel.font = NSFont.SFProTextSemiboldFont(13)
  49. self.restoreButton.font = NSFont.SFProTextRegularFont(14)
  50. self.privacyButton.font = NSFont.SFProTextRegularFont(14)
  51. self.termOfButton.font = NSFont.SFProTextRegularFont(14)
  52. self.titleLabel.stringValue = NSLocalizedString("Purchase AI Tools", comment: "")
  53. self.subTitleLabel.stringValue = NSLocalizedString("Level up your PDF workflow with AI assistant. Unlock advanced features like AI Summarize, AI Translate, AI Rewrite, AI Proofread. ", comment: "")
  54. self.priceDesLabel.stringValue = NSLocalizedString("Auto-renewal | 50 credits/month | Cancel anytime", comment: "")
  55. self.restoreButton.title = NSLocalizedString("Restore", comment: "")
  56. self.privacyButton.title = NSLocalizedString("Privacy Policy", comment: "")
  57. self.termOfButton.title = NSLocalizedString("Terms of Service", comment: "")
  58. self.purchaseBox.wantsLayer = true
  59. self.purchaseBox.layer?.cornerRadius = NSHeight(self.purchaseBox.frame)/2
  60. self.purchaseBox.layer?.masksToBounds = true
  61. self.purchaseButton.wantsLayer = true
  62. self.purchaseButton.mouseMoveCallback = {[unowned self] mouseEntered in
  63. if self.purchaseButton.isEnabled == false {
  64. return
  65. }
  66. if mouseEntered {
  67. self.purchaseButton.layer?.backgroundColor = NSColor.white.withAlphaComponent(0.2).cgColor
  68. } else {
  69. self.purchaseButton.layer?.backgroundColor = NSColor.clear.cgColor
  70. }
  71. }
  72. self.restoreButton.mouseMoveCallback = {[weak self] mouseEntered in
  73. if mouseEntered {
  74. if KMAppearance.isDarkMode() {
  75. self?.restoreButton.setTitleColor(NSColor(red: 1, green: 1, blue: 1, alpha: 0.7))
  76. } else {
  77. self?.restoreButton.setTitleColor(NSColor(red: 0, green: 0, blue: 0, alpha: 0.7))
  78. }
  79. } else {
  80. self?.restoreButton.setTitleColor(KMAppearance.KMColor_Layout_H2())
  81. }
  82. }
  83. self.privacyButton.mouseMoveCallback = {[weak self] mouseEntered in
  84. if mouseEntered {
  85. if KMAppearance.isDarkMode() {
  86. self?.privacyButton.setTitleColor(NSColor(red: 1, green: 1, blue: 1, alpha: 0.7))
  87. } else {
  88. self?.privacyButton.setTitleColor(NSColor(red: 0, green: 0, blue: 0, alpha: 0.7))
  89. }
  90. } else {
  91. self?.privacyButton.setTitleColor(KMAppearance.KMColor_Layout_H2())
  92. }
  93. }
  94. self.termOfButton.mouseMoveCallback = {[weak self] mouseEntered in
  95. if mouseEntered {
  96. if KMAppearance.isDarkMode() {
  97. self?.termOfButton.setTitleColor(NSColor(red: 1, green: 1, blue: 1, alpha: 0.7))
  98. } else {
  99. self?.termOfButton.setTitleColor(NSColor(red: 0, green: 0, blue: 0, alpha: 0.7))
  100. }
  101. } else {
  102. self?.termOfButton.setTitleColor(KMAppearance.KMColor_Layout_H2())
  103. }
  104. }
  105. self.refreshUI()
  106. self.addNotiObserver()
  107. }
  108. func addNotiObserver() -> Void {
  109. NotificationCenter.default.addObserver(self, selector: #selector(IAPProductLoadedNotification), name: NSNotification.Name(rawValue: NSNotification.Name.KMIAPProductLoaded.rawValue), object: nil)
  110. NotificationCenter.default.addObserver(self, selector: #selector(IAPSubscriptionLoadedNotification), name: NSNotification.Name(rawValue: NSNotification.Name.KMIAPSubscriptionLoaded.rawValue), object: nil)
  111. NotificationCenter.default.addObserver(self, selector: #selector(IAPProductPurchasedNotification), name: NSNotification.Name(rawValue: NSNotification.Name.KMIAPProductPurchased.rawValue), object: nil)
  112. NotificationCenter.default.addObserver(self, selector: #selector(IAPProductFailedNotification), name: NSNotification.Name(rawValue: NSNotification.Name.KMIAPProductFailed.rawValue), object: nil)
  113. NotificationCenter.default.addObserver(self, selector: #selector(IAPProductRestoreFinishedNotification), name: NSNotification.Name(rawValue: NSNotification.Name.KMIAPProductRestoreFinished.rawValue), object: nil)
  114. NotificationCenter.default.addObserver(self, selector: #selector(IAPProductRestoreFailedNotification), name: NSNotification.Name(rawValue: NSNotification.Name.KMIAPProductRestoreFailed.rawValue), object: nil)
  115. NotificationCenter.default.addObserver(self, selector: #selector(AIDeviceStatusChangeNotification), name: NSNotification.Name(rawValue: "kDeviceAIStatusChangeNotification"), object: nil)
  116. }
  117. func refreshUI() -> Void {
  118. if KMAppearance.isDarkMode() {
  119. self.contendBox.fillColor = NSColor(red: 24/255, green: 22/255, blue: 31/255, alpha: 1)
  120. } else {
  121. self.contendBox.fillColor = NSColor(red: 247/255, green: 245/255, blue: 1, alpha: 1)
  122. }
  123. }
  124. func reloadData() -> Void {
  125. self.purchaseButton.isEnabled = true
  126. var priceLabel = ""
  127. #if VERSION_FREE
  128. #if VERSION_DMG
  129. priceLabel = IAPProductsManager.default().dmgpdfReaderProAI_Monthly.price()
  130. #else
  131. priceLabel = IAPProductsManager.default().aiAllAccessPack1month_lite.price()
  132. #endif
  133. #else
  134. priceLabel = IAPProductsManager.default().aiAllAccessPack1month_pro.price()
  135. #endif
  136. let allowPurchaseAI = KMMemberInfo.shared.allowPurchaseAI
  137. if allowPurchaseAI {
  138. self.purchaseButton.isEnabled = true
  139. self.purchaseButton.layer?.backgroundColor = NSColor.clear.cgColor
  140. } else {
  141. self.purchaseButton.isEnabled = false
  142. self.purchaseButton.layer?.backgroundColor = NSColor.white.withAlphaComponent(0.2).cgColor
  143. }
  144. self.priceLabel.stringValue = priceLabel
  145. }
  146. func appStoreRestoreFinish() -> Void {
  147. var didPurchased: Bool = false
  148. #if VERSION_FREE
  149. didPurchased = IAPProductsManager.default().aiAllAccessPack1month_lite.isSubscribed
  150. #else
  151. didPurchased = IAPProductsManager.default().aiAllAccessPack1month_pro.isSubscribed
  152. #endif
  153. if didPurchased {
  154. AIInfoManager.default().restoreAI(withInfo: ["receipt_str":IAPProductsManager.default().temptransactioReceipt as Any]) { dict, error in
  155. if AIInfoManager.default().aiInfo.infoDict.keys.count > 0 {
  156. let alert = NSAlert.init()
  157. alert.messageText = NSLocalizedString("Restore successfully", comment: "")
  158. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  159. alert.runModal()
  160. }
  161. self.removeWaitingView((self.window?.contentView)!)
  162. }
  163. }
  164. }
  165. func addWaingView(_ view: NSView) -> Void {
  166. self.removeWaitingView(view)
  167. let wView = WaitingView.init(frame: view.bounds)
  168. wView.autoresizingMask = [.width, .height]
  169. view.addSubview(wView)
  170. wView.startAnimation()
  171. }
  172. func removeWaitingView(_ view: NSView) -> Void {
  173. for view in view.subviews {
  174. if view.className == WaitingView.className() {
  175. view.removeFromSuperview()
  176. break
  177. }
  178. }
  179. }
  180. //MARK: - IBAction
  181. @IBAction func purchaseAction(_ sender: KMButton) {
  182. if KMMemberInfo.shared.isLogin && KMMemberInfo.shared.validFlag == "5" {
  183. KMMemberPromptWC.shared.showWindow(nil)
  184. KMMemberPromptWC.shared.tipType = .signouting
  185. } else {
  186. #if VERSION_FREE
  187. IAPProductsManager.default().make(IAPProductsManager.default().aiAllAccessPack1month_lite)
  188. #else
  189. IAPProductsManager.default().make(IAPProductsManager.default().aiAllAccessPack1month_pro)
  190. #endif
  191. self.addWaingView((self.window?.contentView)!)
  192. FMTrackEventManager.defaultManager.trackOnceEvent(event: "PUW", withProperties: ["PUW_Btn":"PUW_Btn_BuyAITools"])
  193. }
  194. }
  195. @IBAction func restoreAction(_ sender: Any) {
  196. IAPProductsManager.default().restoreSubscriptions()
  197. self.addWaingView((self.window?.contentView)!)
  198. }
  199. @IBAction func privacyAction(_ sender: Any) {
  200. var url = URL(string:"https://www.pdfreaderpro.com/privacy-policy")!
  201. NSWorkspace.shared.open(url)
  202. }
  203. @IBAction func termOfAction(_ sender: Any) {
  204. var url = URL(string:"https://www.pdfreaderpro.com/terms_of_service")!
  205. NSWorkspace.shared.open(url)
  206. }
  207. //MARK: - Notification
  208. func windowWillClose(_ notification: Notification) {
  209. FMTrackEventManager.defaultManager.trackOnceEvent(event: "PUW", withProperties: ["PUW_Btn":"PUW_Btn_BuyAITools_Cancel"])
  210. AIPurchaseWindowController.currentWindowController = nil
  211. }
  212. @objc func IAPProductLoadedNotification() {
  213. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
  214. self.reloadData()
  215. }
  216. }
  217. @objc func IAPSubscriptionLoadedNotification() {
  218. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
  219. self.removeWaitingView((self.window?.contentView)!)
  220. self.reloadData()
  221. }
  222. }
  223. @objc func IAPProductPurchasedNotification() {
  224. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
  225. self.removeWaitingView((self.window?.contentView)!)
  226. self.reloadData()
  227. if (self.isAIPurchase == true) {
  228. self.trackEvent(eventName: self.kEventName, params: self.kEventParams, platform: .firebase)
  229. }
  230. //Appstore购买结束后绑定票据信息
  231. }
  232. }
  233. @objc func IAPProductFailedNotification() {
  234. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
  235. self.removeWaitingView((self.window?.contentView)!)
  236. }
  237. }
  238. @objc func IAPProductRestoreFinishedNotification() {
  239. self.reloadData()
  240. self.appStoreRestoreFinish()
  241. }
  242. @objc func IAPProductRestoreFailedNotification() {
  243. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
  244. self.removeWaitingView((self.window?.contentView)!)
  245. }
  246. }
  247. @objc func AIDeviceStatusChangeNotification() {
  248. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
  249. self.reloadData()
  250. if KMMemberInfo.shared.isPermitAI {
  251. self.close()
  252. }
  253. }
  254. }
  255. }