KMToolCompareWindowController.swift 13 KB

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