KMToolCompareWindowController.swift 13 KB

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