AIPurchaseWindowController.swift 13 KB

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