KMToolCompareWindowController.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. //
  2. // KMToolCompareWindowController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by liujiajie on 2023/11/9.
  6. //
  7. import Cocoa
  8. var currentWindowController: KMToolCompareWindowController? = nil
  9. @objcMembers class KMToolCompareWindowController: NSWindowController, NSWindowDelegate{
  10. var type: KMCompareWithToolType?
  11. lazy var payConvertCompareViewController: KMConvertComparePayViewController? = {
  12. let Com = KMConvertComparePayViewController(select: self.selectIndex)
  13. Com?.block = { type in
  14. if type == .restore {
  15. IAPProductsManager.default().restoreSubscriptions()
  16. self.addWaingView(view: self.window?.contentView ?? NSView())
  17. } else if type == .refresh {
  18. VerificationManager().verification(complention: { (status, info, error) in
  19. if let error = error {
  20. let alert = NSAlert()
  21. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  22. alert.alertStyle = .warning
  23. alert.messageText = NSLocalizedString("Failed to Refresh", comment: "")
  24. alert.runModal()
  25. }
  26. self.reloadData()
  27. self.removeWaitingView(view: self.window?.contentView ?? NSView())
  28. })
  29. self.addWaingView(view: self.window?.contentView ?? NSView())
  30. } else if type == .STORE {
  31. IAPProductsManager.default().make(IAPProductsManager.default().pdfToOfficeProduct)
  32. self.addWaingView(view: self.window?.contentView ?? NSView())
  33. } else if type == .DMG {
  34. self.officeVerificationBuy()
  35. }
  36. }
  37. return Com
  38. }()
  39. lazy var convertCompareViewController: KMConvertCompareViewController? = {
  40. let Com = KMConvertCompareViewController(compareType: self.type?.rawValue ?? 0, select: self.selectIndex)
  41. Com?.block = { type in
  42. if type == .compare {
  43. if #available(macOS 10.13, *) {
  44. self.close()
  45. }
  46. // KMPurchaseCompareWindowController.sharedInstance().startModal("")
  47. } else if type == .license {
  48. self.close()
  49. if VerificationManager.default().status == .none {
  50. let vc = KMVerificationWindowController.verification(with: .trial)
  51. vc?.showWindow(nil)
  52. } else {
  53. let vc = KMVerificationWindowController.verification(with: .activate)
  54. vc?.callback = { [weak self] in
  55. #if VERSION_DMG
  56. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  57. #else
  58. if IAPProductsManager.default().isAvailableAllFunction() {
  59. let vc = KMToolCompareWindowController.toolCompare(toolType: self?.type ?? .Convert, selectNum: self?.selectIndex ?? 0)
  60. vc.showWindow(nil)
  61. } else {
  62. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  63. }
  64. #endif
  65. }
  66. vc?.showWindow(nil)
  67. }
  68. } else if type == .restore {
  69. IAPProductsManager.default().restoreSubscriptions()
  70. self.addWaingView(view: self.window?.contentView ?? NSView())
  71. } else if type == .month {
  72. IAPProductsManager.default().make(IAPProductsManager.default().newlyMonthProduct)
  73. self.addWaingView(view: self.window?.contentView ?? NSView())
  74. } else if type == .oneTime {
  75. #if VERSION_DMG
  76. self.verificationBuy()
  77. #else
  78. IAPProductsManager.default().make(IAPProductsManager.default().allAccessProduct)
  79. self.addWaingView(view: self.window?.contentView ?? NSView())
  80. #endif
  81. }
  82. }
  83. return Com
  84. }()
  85. var selectIndex: Int = 0
  86. @IBOutlet var box: NSBox!
  87. var modalSession: NSApplication.ModalSession?
  88. deinit {
  89. #if VERSION_DMG
  90. #else
  91. NotificationCenter.default.removeObserver(self)
  92. #endif
  93. }
  94. convenience init(toolType: KMCompareWithToolType, selectNum: Int) {
  95. self.init(windowNibName: "KMToolCompareWindowController")
  96. self.type = toolType
  97. self.selectIndex = selectNum
  98. }
  99. class func toolCompare(toolType:KMCompareWithToolType, selectNum:Int) -> KMToolCompareWindowController {
  100. if (currentWindowController != nil) {
  101. return currentWindowController!
  102. }
  103. let vc = KMToolCompareWindowController(toolType: toolType, selectNum: selectNum)
  104. currentWindowController = vc
  105. return vc
  106. }
  107. override func windowDidLoad() {
  108. super.windowDidLoad()
  109. #if VERSION_DMG
  110. IAPProductsManager.default().loadProducts()
  111. #endif
  112. NotificationCenter.default.addObserver(self, selector: #selector(IAPProductLoadedNotification(notification:)), name: NSNotification.Name(NSNotification.Name.KMIAPProductLoaded.rawValue), object: nil)
  113. NotificationCenter.default.addObserver(self, selector: #selector(IAPSubscriptionLoadedNotification(notification:)), name: NSNotification.Name(NSNotification.Name.KMIAPSubscriptionLoaded.rawValue), object: nil)
  114. NotificationCenter.default.addObserver(self, selector: #selector(IAPProductPurchasedNotification(notification:)), name: NSNotification.Name(NSNotification.Name.KMIAPProductPurchased.rawValue), object: nil)
  115. NotificationCenter.default.addObserver(self, selector: #selector(IAPProductFailedNotification(notification:)), name: NSNotification.Name(NSNotification.Name.KMIAPProductFailed.rawValue), object: nil)
  116. NotificationCenter.default.addObserver(self, selector: #selector(IAPProductRestoreFinishedNotification(notification:)), name: NSNotification.Name(NSNotification.Name.KMIAPProductRestoreFinished.rawValue), object: nil)
  117. NotificationCenter.default.addObserver(self, selector: #selector(IAPProductRestoreFailedNotification(notification:)), name: NSNotification.Name(NSNotification.Name.KMIAPProductRestoreFailed.rawValue), object: nil)
  118. self.window?.backgroundColor = NSColor(red: 36.0/255.0, green:37.0/255.0, blue:39.0/255.0, alpha:1.0)
  119. self.window?.isMovableByWindowBackground = true
  120. if type == .Convert {
  121. self.window?.title = NSLocalizedString("PDF to Office", comment: "")
  122. if IAPProductsManager.default().isAvailableAllFunction() {
  123. self.setContentView(self.payConvertCompareViewController?.view ?? NSView())
  124. } else {
  125. self.setContentView(self.convertCompareViewController?.view ?? NSView())
  126. }
  127. self.window?.backgroundColor = KMAppearance.Upgrade.vipBGColor()
  128. } else if type == .OCR {
  129. self.window?.title = NSLocalizedString("OCR", comment: "")
  130. self.setContentView(self.convertCompareViewController?.view ?? NSView())
  131. } else if type == .PageEdit {
  132. self.window?.title = NSLocalizedString("Page Edit", comment: "")
  133. self.setContentView(self.convertCompareViewController?.view ?? NSView())
  134. } else if type == .PDFEdit {
  135. self.window?.title = NSLocalizedString("PDF Editor", comment: "")
  136. self.setContentView(self.convertCompareViewController?.view ?? NSView())
  137. } else if type == .FromSign || type == .Sign {
  138. self.window?.title = NSLocalizedString("Form & Sign", comment: "")
  139. self.setContentView(self.convertCompareViewController?.view ?? NSView())
  140. } else if type == .EditPDF {
  141. self.window?.title = NSLocalizedString("Edit PDF", comment: "")
  142. self.setContentView(self.convertCompareViewController?.view ?? NSView())
  143. }
  144. }
  145. override func showWindow(_ sender: Any?) {
  146. if currentWindowController == nil {
  147. currentWindowController = self
  148. }
  149. super.showWindow(sender)
  150. }
  151. func windowWillClose(_ notification: Notification) {
  152. if currentWindowController != nil {
  153. currentWindowController = nil
  154. payConvertCompareViewController?.removeObserver()
  155. payConvertCompareViewController = nil
  156. convertCompareViewController?.removeObserver()
  157. convertCompareViewController = nil
  158. }
  159. }
  160. func windowShouldClose(_ sender: NSWindow) -> Bool {
  161. endModal(sender)
  162. return true
  163. }
  164. override func close() {
  165. super.close()
  166. endModal(nil)
  167. }
  168. @IBAction func endModal(_ sender: Any?) {
  169. if let modalSession = self.modalSession {
  170. NSApp.stopModal()
  171. NSApp.endModalSession(modalSession)
  172. self.window?.orderOut(self)
  173. self.modalSession = nil
  174. }
  175. }
  176. @IBAction func startModal(_ sender: Any) {
  177. NSApp.stopModal()
  178. var modalCode: Int
  179. self.modalSession = NSApp.beginModalSession(for: self.window!)
  180. repeat {
  181. modalCode = (NSApp.runModalSession(modalSession!)).rawValue
  182. } while modalCode == NSApplication.ModalResponse.continue.rawValue
  183. }
  184. func removeWaitingView(view: NSView) {
  185. for subview in view.subviews {
  186. if subview is WaitingView {
  187. subview.removeFromSuperview()
  188. break
  189. }
  190. }
  191. }
  192. func setContentView(_ view: NSView) {
  193. var frame: NSRect = self.window?.frame ?? .zero
  194. frame.origin.y -= view.frame.size.height - frame.size.height;
  195. frame.origin.x -= (view.frame.size.width - frame.size.width) / 2.0;
  196. frame.size.width = view.frame.size.width;
  197. frame.size.height = view.frame.size.height;
  198. self.box.contentView = view
  199. self.window?.setFrame(frame, display: true, animate: true)
  200. }
  201. func reloadData() {
  202. if IAPProductsManager.default().isAvailableAllFunction() {
  203. self.payConvertCompareViewController?.reloadData()
  204. } else {
  205. self.convertCompareViewController?.reloadData()
  206. }
  207. }
  208. func verificationBuy() {
  209. #if VERSION_DMG
  210. var url = URL(string: "https://www.pdfreaderpro.com/store?utm_source=MacAppDmg&utm_campaign=StoreLink&utm_medium=PdfStore")
  211. if SKInspectPublicTool.currentisLanguageHans() {
  212. url = URL(string: "https://www.pdfreaderpro.com/zh-cn/store?utm_source=MacAppDmg&utm_campaign=StoreLink&utm_medium=PdfStore")
  213. }
  214. NSWorkspace.shared.open(url!)
  215. #endif
  216. }
  217. func officeVerificationBuy() {
  218. #if VERSION_DMG
  219. var url = URL(string: "https://www.pdfreaderpro.com/store?product_code=product_2&utm_source=MacAppDmg&utm_campaign=OfficeStoreLink&utm_medium=PdfOfficeStore")
  220. if SKInspectPublicTool.currentisLanguageHans() {
  221. url = URL(string: "https://www.pdfreaderpro.com/zh-cn/store?product_code=product_2&utm_source=MacAppDmg&utm_campaign=OfficeStoreLink&utm_medium=PdfOfficeStore")
  222. }
  223. NSWorkspace.shared.open(url!)
  224. FMTrackEventManager.defaultManager.trackOnceEvent(event: "PUW", withProperties: ["PUW_Btn":"Btn_PUW_PDFtoOffice_Buy"])
  225. #endif
  226. }
  227. func showWindowRestore(sender: Any) {
  228. self.showWindow(sender)
  229. IAPProductsManager.default().restoreSubscriptions()
  230. self.addWaingView(view: self.window?.contentView ?? NSView())
  231. }
  232. func addWaingView(view: NSView) {
  233. self.removeWaitingView(view: view)
  234. let wView = WaitingView(frame: view.bounds)
  235. wView.autoresizingMask = [.width, .height]
  236. view.addSubview(wView)
  237. wView.startAnimation()
  238. }
  239. @objc func IAPProductFailedNotification(notification: NSNotification) {
  240. self.removeWaitingView(view: (self.window?.contentView)!)
  241. }
  242. @objc func IAPProductPurchasedNotification(notification: NSNotification) {
  243. self.removeWaitingView(view: (self.window?.contentView)!)
  244. self.reloadData()
  245. }
  246. @objc func IAPProductLoadedNotification(notification: NSNotification) {
  247. self.reloadData()
  248. }
  249. @objc func IAPProductRestoreFinishedNotification(notification: NSNotification) {
  250. self.removeWaitingView(view: (self.window?.contentView)!)
  251. self.reloadData()
  252. }
  253. @objc func IAPProductRestoreFailedNotification(notification: NSNotification) {
  254. self.removeWaitingView(view: (self.window?.contentView)!)
  255. }
  256. @objc func IAPSubscriptionLoadedNotification(notification: NSNotification) {
  257. self.removeWaitingView(view: (self.window?.contentView)!)
  258. self.reloadData()
  259. }
  260. }