KMToolCompareWindowController.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. NotificationCenter.default.removeObserver(self)
  90. }
  91. convenience init(toolType: KMCompareWithToolType, selectNum: Int) {
  92. self.init(windowNibName: "KMToolCompareWindowController")
  93. self.type = toolType
  94. self.selectIndex = selectNum
  95. }
  96. class func toolCompare(toolType:KMCompareWithToolType, selectNum:Int) -> KMToolCompareWindowController {
  97. if (currentWindowController != nil) {
  98. return currentWindowController!
  99. }
  100. let vc = KMToolCompareWindowController(toolType: toolType, selectNum: selectNum)
  101. currentWindowController = vc
  102. return vc
  103. }
  104. override func windowDidLoad() {
  105. super.windowDidLoad()
  106. #if VERSION_DMG
  107. IAPProductsManager.default().loadProducts()
  108. #endif
  109. NotificationCenter.default.addObserver(self, selector: #selector(IAPProductLoadedNotification(notification:)), name: NSNotification.Name(NSNotification.Name.KMIAPProductLoaded.rawValue), object: nil)
  110. NotificationCenter.default.addObserver(self, selector: #selector(IAPSubscriptionLoadedNotification(notification:)), name: NSNotification.Name(NSNotification.Name.KMIAPSubscriptionLoaded.rawValue), object: nil)
  111. NotificationCenter.default.addObserver(self, selector: #selector(IAPProductPurchasedNotification(notification:)), name: NSNotification.Name(NSNotification.Name.KMIAPProductPurchased.rawValue), object: nil)
  112. NotificationCenter.default.addObserver(self, selector: #selector(IAPProductFailedNotification(notification:)), name: NSNotification.Name(NSNotification.Name.KMIAPProductFailed.rawValue), object: nil)
  113. NotificationCenter.default.addObserver(self, selector: #selector(IAPProductRestoreFinishedNotification(notification:)), name: NSNotification.Name(NSNotification.Name.KMIAPProductRestoreFinished.rawValue), object: nil)
  114. NotificationCenter.default.addObserver(self, selector: #selector(IAPProductRestoreFailedNotification(notification:)), name: NSNotification.Name(NSNotification.Name.KMIAPProductRestoreFailed.rawValue), object: nil)
  115. self.window?.backgroundColor = NSColor(red: 36.0/255.0, green:37.0/255.0, blue:39.0/255.0, alpha:1.0)
  116. self.window?.isMovableByWindowBackground = true
  117. if type == .Convert {
  118. self.window?.title = NSLocalizedString("PDF to Office", comment: "")
  119. if IAPProductsManager.default().isAvailableAllFunction() {
  120. self.setContentView(self.payConvertCompareViewController?.view ?? NSView())
  121. } else {
  122. self.setContentView(self.convertCompareViewController?.view ?? NSView())
  123. }
  124. self.window?.backgroundColor = KMAppearance.Upgrade.vipBGColor()
  125. } else if type == .OCR {
  126. self.window?.title = NSLocalizedString("OCR", comment: "")
  127. self.setContentView(self.convertCompareViewController?.view ?? NSView())
  128. } else if type == .PageEdit {
  129. self.window?.title = NSLocalizedString("Page Edit", comment: "")
  130. self.setContentView(self.convertCompareViewController?.view ?? NSView())
  131. } else if type == .PDFEdit {
  132. self.window?.title = NSLocalizedString("PDF Editor", comment: "")
  133. self.setContentView(self.convertCompareViewController?.view ?? NSView())
  134. } else if type == .FromSign || type == .Sign {
  135. self.window?.title = NSLocalizedString("Form & Sign", comment: "")
  136. self.setContentView(self.convertCompareViewController?.view ?? NSView())
  137. } else if type == .EditPDF {
  138. self.window?.title = NSLocalizedString("Edit PDF", comment: "")
  139. self.setContentView(self.convertCompareViewController?.view ?? NSView())
  140. }
  141. }
  142. override func showWindow(_ sender: Any?) {
  143. if currentWindowController == nil {
  144. currentWindowController = self
  145. }
  146. super.showWindow(sender)
  147. }
  148. func windowWillClose(_ notification: Notification) {
  149. if currentWindowController != nil {
  150. currentWindowController = nil
  151. payConvertCompareViewController?.removeObserver()
  152. payConvertCompareViewController = nil
  153. convertCompareViewController?.removeObserver()
  154. convertCompareViewController = nil
  155. }
  156. }
  157. func windowShouldClose(_ sender: NSWindow) -> Bool {
  158. endModal(sender)
  159. return true
  160. }
  161. override func close() {
  162. super.close()
  163. endModal(nil)
  164. }
  165. @IBAction func endModal(_ sender: Any?) {
  166. if let modalSession = self.modalSession {
  167. NSApp.stopModal()
  168. NSApp.endModalSession(modalSession)
  169. self.window?.orderOut(self)
  170. self.modalSession = nil
  171. }
  172. }
  173. @IBAction func startModal(_ sender: Any) {
  174. NSApp.stopModal()
  175. var modalCode: Int
  176. self.modalSession = NSApp.beginModalSession(for: self.window!)
  177. repeat {
  178. modalCode = (NSApp.runModalSession(modalSession!)).rawValue
  179. } while modalCode == NSApplication.ModalResponse.continue.rawValue
  180. }
  181. func removeWaitingView(view: NSView) {
  182. for subview in view.subviews {
  183. if subview is WaitingView {
  184. subview.removeFromSuperview()
  185. break
  186. }
  187. }
  188. }
  189. func setContentView(_ view: NSView) {
  190. var frame: NSRect = self.window?.frame ?? .zero
  191. frame.origin.y -= view.frame.size.height - frame.size.height;
  192. frame.origin.x -= (view.frame.size.width - frame.size.width) / 2.0;
  193. frame.size.width = view.frame.size.width;
  194. frame.size.height = view.frame.size.height;
  195. self.box.contentView = view
  196. self.window?.setFrame(frame, display: true, animate: true)
  197. }
  198. func reloadData() {
  199. if IAPProductsManager.default().isAvailableAllFunction() {
  200. self.payConvertCompareViewController?.reloadData()
  201. } else {
  202. self.convertCompareViewController?.reloadData()
  203. }
  204. }
  205. func verificationBuy() {
  206. #if VERSION_DMG
  207. var url = URL(string: "https://www.pdfreaderpro.com/store?utm_source=MacAppDmg&utm_campaign=StoreLink&utm_medium=PdfStore")
  208. if SKInspectPublicTool.currentisLanguageHans() {
  209. url = URL(string: "https://www.pdfreaderpro.com/zh-cn/store?utm_source=MacAppDmg&utm_campaign=StoreLink&utm_medium=PdfStore")
  210. }
  211. NSWorkspace.shared.open(url!)
  212. #endif
  213. }
  214. func officeVerificationBuy() {
  215. #if VERSION_DMG
  216. var url = URL(string: "https://www.pdfreaderpro.com/store?product_code=product_2&utm_source=MacAppDmg&utm_campaign=OfficeStoreLink&utm_medium=PdfOfficeStore")
  217. if SKInspectPublicTool.currentisLanguageHans() {
  218. url = URL(string: "https://www.pdfreaderpro.com/zh-cn/store?product_code=product_2&utm_source=MacAppDmg&utm_campaign=OfficeStoreLink&utm_medium=PdfOfficeStore")
  219. }
  220. NSWorkspace.shared.open(url!)
  221. #endif
  222. }
  223. func showWindowRestore(sender: Any) {
  224. self.showWindow(sender)
  225. IAPProductsManager.default().restoreSubscriptions()
  226. self.addWaingView(view: self.window?.contentView ?? NSView())
  227. }
  228. func addWaingView(view: NSView) {
  229. self.removeWaitingView(view: view)
  230. let wView = WaitingView(frame: view.bounds)
  231. wView.autoresizingMask = [.width, .height]
  232. view.addSubview(wView)
  233. wView.startAnimation()
  234. }
  235. @objc func IAPProductFailedNotification(notification: NSNotification) {
  236. self.removeWaitingView(view: (self.window?.contentView)!)
  237. }
  238. @objc func IAPProductPurchasedNotification(notification: NSNotification) {
  239. self.removeWaitingView(view: (self.window?.contentView)!)
  240. self.reloadData()
  241. }
  242. @objc func IAPProductLoadedNotification(notification: NSNotification) {
  243. self.reloadData()
  244. }
  245. @objc func IAPProductRestoreFinishedNotification(notification: NSNotification) {
  246. self.removeWaitingView(view: (self.window?.contentView)!)
  247. self.reloadData()
  248. }
  249. @objc func IAPProductRestoreFailedNotification(notification: NSNotification) {
  250. self.removeWaitingView(view: (self.window?.contentView)!)
  251. }
  252. @objc func IAPSubscriptionLoadedNotification(notification: NSNotification) {
  253. self.removeWaitingView(view: (self.window?.contentView)!)
  254. self.reloadData()
  255. }
  256. }