KMToolCompareWindowController.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. } else if type == .license {
  47. self.close()
  48. if VerificationManager.default().status == .none {
  49. let vc = KMVerificationWindowController.verification(with: .trial)
  50. vc?.showWindow(nil)
  51. } else {
  52. let vc = KMVerificationWindowController.verification(with: .activate)
  53. vc?.callback = { [weak self] in
  54. #if VERSION_DMG
  55. KMProductCompareWC.shared.orientation = false
  56. KMProductCompareWC.shared.showWindow(nil)
  57. #else
  58. if KMMemberInfo.shared.isMemberAllFunction == false {
  59. let vc = KMToolCompareWindowController.toolCompare(toolType: self?.type ?? .Convert, selectNum: self?.selectIndex ?? 0)
  60. vc.showWindow(nil)
  61. } else {
  62. KMMemberInfo.shared.advancedFunctionUsage()
  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().allAccessPackNew6months_lite)
  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().allAccessPackPermanent_lite)
  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 KMMemberInfo.shared.isMemberAllFunction {
  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 KMMemberInfo.shared.isMemberAllFunction {
  203. self.payConvertCompareViewController?.reloadData()
  204. } else {
  205. self.convertCompareViewController?.reloadData()
  206. }
  207. }
  208. func verificationBuy() {
  209. #if VERSION_DMG
  210. let embeddedWC = KMPurchaseEmbeddedWindowController.currentFirstTrialWC("com.brother.pdfreaderpro.mac.product_1")
  211. embeddedWC.showWindow(nil)
  212. embeddedWC.window?.center()
  213. #endif
  214. }
  215. func officeVerificationBuy() {
  216. #if VERSION_DMG
  217. // var url = URL(string: Store_Link + "?product_code=product_2&utm_source=MacAppDmg&utm_campaign=OfficeStoreLink&utm_medium=PdfOfficeStore")
  218. // NSWorkspace.shared.open(url!)
  219. // FMTrackEventManager.defaultManager.trackOnceEvent(event: "PUW", withProperties: ["PUW_Btn":"Btn_PUW_PDFtoOffice_Buy"])
  220. let embeddedWC = KMPurchaseEmbeddedWindowController.currentFirstTrialWC("com.brother.pdfreaderpro.mac.product_2")
  221. embeddedWC.showWindow(nil)
  222. embeddedWC.window?.center()
  223. #endif
  224. }
  225. func showWindowRestore(sender: Any) {
  226. self.showWindow(sender)
  227. IAPProductsManager.default().restoreSubscriptions()
  228. self.addWaingView(view: self.window?.contentView ?? NSView())
  229. }
  230. func addWaingView(view: NSView) {
  231. self.removeWaitingView(view: view)
  232. let wView = WaitingView(frame: view.bounds)
  233. wView.autoresizingMask = [.width, .height]
  234. view.addSubview(wView)
  235. wView.startAnimation()
  236. }
  237. @objc func IAPProductFailedNotification(notification: NSNotification) {
  238. self.removeWaitingView(view: (self.window?.contentView)!)
  239. }
  240. @objc func IAPProductPurchasedNotification(notification: NSNotification) {
  241. self.removeWaitingView(view: (self.window?.contentView)!)
  242. self.reloadData()
  243. }
  244. @objc func IAPProductLoadedNotification(notification: NSNotification) {
  245. self.reloadData()
  246. }
  247. @objc func IAPProductRestoreFinishedNotification(notification: NSNotification) {
  248. self.removeWaitingView(view: (self.window?.contentView)!)
  249. self.reloadData()
  250. }
  251. @objc func IAPProductRestoreFailedNotification(notification: NSNotification) {
  252. self.removeWaitingView(view: (self.window?.contentView)!)
  253. }
  254. @objc func IAPSubscriptionLoadedNotification(notification: NSNotification) {
  255. self.removeWaitingView(view: (self.window?.contentView)!)
  256. self.reloadData()
  257. }
  258. }