KMComparativeTableViewController.swift 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. //
  2. // KMComparativeTableViewController.swift
  3. // PDF Master
  4. //
  5. // Created by lizhe on 2023/2/24.
  6. //
  7. import Cocoa
  8. typealias KMComparativeTableViewControllerLoginAction = (_ controller: KMComparativeTableViewController) -> Void
  9. typealias KMComparativeTableViewControllerSignUpAction = (_ controller: KMComparativeTableViewController) -> Void
  10. typealias KMComparativeTableViewControllerSubscriptionAction = (_ controller: KMComparativeTableViewController) -> Void
  11. typealias KMComparativeTableViewControllerRestoreAction = (_ controller: KMComparativeTableViewController) -> Void
  12. var comparativeController: KMComparativeTableViewController?
  13. var comparativeMainWindow: NSWindow?
  14. enum KMComparativeInputType: Int {
  15. case unknown = 0
  16. case login = 1 //登录界面
  17. case register = 2 //注册界面
  18. case resetPassword = 3 //重置密码
  19. case verificationCode = 4 //验证码
  20. case registerSuccess = 5 //注册成功
  21. case loginInputPassword = 6 //登录密码输入
  22. case accountInfo = 7 //用户信息
  23. case compress = 8 // 压缩
  24. case convert = 9 // 转档
  25. case secure = 10 // 安全
  26. case pageEdit = 11 // 页面编辑
  27. case merge = 12 // 合并
  28. case crop = 13 // 裁切
  29. case thumb = 14 // 缩略图列表
  30. //后续添加
  31. }
  32. class KMComparativeTableViewController: NSWindowController {
  33. @IBOutlet weak var comparativeTableView: KMComparativeTableView!
  34. @IBOutlet weak var comparativeView: KMComparativeView!
  35. var inputType: KMComparativeInputType?
  36. var loginAction: KMComparativeTableViewControllerLoginAction?
  37. var signUpAction: KMComparativeTableViewControllerSignUpAction?
  38. var subscriptionAction: KMComparativeTableViewControllerSubscriptionAction?
  39. var restoreAction: KMComparativeTableViewControllerRestoreAction?
  40. deinit {
  41. print("KMComparativeTableViewController 释放")
  42. }
  43. //MARK: 打开文件
  44. /**
  45. @abstract 弹出比较表
  46. @param window 弹出window
  47. @param type 从什么页面跳转到比较表 默认batch
  48. */
  49. static func show(window: NSWindow, _ type: KMComparativeInputType = .unknown) -> KMComparativeTableViewController? {
  50. #if VERSION_DMG
  51. if KMLightMemberManager.manager.isLogin() {
  52. KMPurchaseManager.manager.purchaseProduct(productIdentifier: "") { isSuccess, error in
  53. }
  54. } else {
  55. var email: String = UserDefaults.standard.value(forKey: "kLoginEmail") as? String ?? ""
  56. if email.count == 0 {
  57. KMLoginWindowController.show(window: NSApp.mainWindow!, .Batch, .register)
  58. } else {
  59. KMLoginWindowController.show(window: NSApp.mainWindow!)
  60. }
  61. }
  62. #endif
  63. #if VERSION_FREE
  64. let controller: KMComparativeTableViewController = KMComparativeTableViewController.init(windowNibName: "KMComparativeTableViewController")
  65. controller.inputType = type
  66. controller.loginAction = { controller in
  67. comparativeController = nil
  68. comparativeMainWindow?.endSheet(controller.window!)
  69. comparativeMainWindow = nil
  70. controller.close()
  71. if NSApp.mainWindow != nil {
  72. KMLoginWindowController.show(window: NSApp.mainWindow!, .Batch, .login)
  73. }
  74. }
  75. controller.signUpAction = { controller in
  76. comparativeController = nil
  77. comparativeMainWindow?.endSheet(controller.window!)
  78. comparativeMainWindow = nil
  79. controller.close()
  80. if NSApp.mainWindow != nil {
  81. KMLoginWindowController.show(window: NSApp.mainWindow!, .Batch, .register)
  82. }
  83. }
  84. controller.subscriptionAction = { controller in
  85. controller.subscribeAction(controller: controller)
  86. }
  87. controller.restoreAction = { controller in
  88. controller.subscribeAction(controller: controller, isRestore:true)
  89. }
  90. window.beginSheet(controller.window!)
  91. comparativeController = controller
  92. comparativeMainWindow = window
  93. return controller
  94. #endif
  95. return nil
  96. }
  97. override func windowDidLoad() {
  98. super.windowDidLoad()
  99. // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
  100. self.setup()
  101. print("订阅状态: \(KMInAppPurchaseManager.manager.state)")
  102. }
  103. func setup() {
  104. self.window?.contentView?.backgroundColor(NSColor(hex: "FFFFFF"))
  105. self.comparativeView.closeAction = { view in
  106. print("关闭")
  107. comparativeMainWindow?.endSheet(view.window!)
  108. view.window?.close()
  109. comparativeController = nil
  110. comparativeMainWindow = nil
  111. }
  112. self.comparativeView.signUpAction = { [weak self] view in
  113. print("注册")
  114. guard let callBack = self?.signUpAction else { return }
  115. callBack(self!)
  116. }
  117. // self.comparativeTableView.loginAction = { [weak self] view in
  118. // print("登录")
  119. // guard let callBack = self?.loginAction else { return }
  120. //
  121. // callBack(self!)
  122. // }
  123. //
  124. self.comparativeView.subscriptionAction = { [weak self] view in
  125. print("订阅")
  126. guard let callBack = self?.subscriptionAction else { return }
  127. callBack(self!)
  128. }
  129. self.comparativeView.restoreAction = { [weak self] view in
  130. print("restore订阅")
  131. guard let callBack = self?.restoreAction else { return }
  132. callBack(self!)
  133. }
  134. }
  135. static func isSampleController() -> KMComparativeTableViewController {
  136. for window in NSApp.windows {
  137. let controller = window.windowController
  138. if controller is KMComparativeTableViewController {
  139. return controller as! KMComparativeTableViewController
  140. }
  141. }
  142. return KMComparativeTableViewController()
  143. }
  144. func subscribeAction(controller: KMComparativeTableViewController, isRestore: Bool = false) {
  145. //loading
  146. controller.comparativeView.beginLoading(backgroundColor: NSColor.black.withAlphaComponent(0.2))
  147. if KMLightMemberManager.manager.isLogin() {
  148. if isRestore {
  149. KMPurchaseManager.manager.restorePurchases { isSuccess in
  150. if isSuccess {
  151. comparativeController = nil
  152. comparativeMainWindow?.endSheet(controller.window!)
  153. comparativeMainWindow = nil
  154. controller.close()
  155. } else {
  156. controller.purchasefailed()
  157. }
  158. controller.comparativeView.endLoading()
  159. }
  160. } else {
  161. KMPurchaseManager.manager.purchaseProduct(productIdentifier: PRODUCT_1) { isSuccess, error in
  162. if isSuccess {
  163. print("订阅成功")
  164. comparativeController = nil
  165. comparativeMainWindow?.endSheet(controller.window!)
  166. comparativeMainWindow = nil
  167. controller.close()
  168. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1) {
  169. if NSApp.mainWindow != nil {
  170. _ = KMSubscribeSuccessWindowController.show(window: NSApp.mainWindow!)
  171. }
  172. }
  173. } else {
  174. controller.purchasefailed()
  175. }
  176. //loading
  177. controller.comparativeView.endLoading()
  178. }
  179. }
  180. } else {
  181. comparativeController = nil
  182. comparativeMainWindow?.endSheet(controller.window!)
  183. comparativeMainWindow = nil
  184. controller.close()
  185. var email: String = UserDefaults.standard.value(forKey: "kLoginEmail") as? String ?? ""
  186. if email.count == 0 {
  187. let tempController = KMLoginWindowController.show(window: NSApp.mainWindow!, .ComparativeTable, .register)
  188. } else {
  189. let tempController = KMLoginWindowController.show(window: NSApp.mainWindow!, .ComparativeTable)
  190. }
  191. }
  192. }
  193. func purchasefailed() {
  194. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1, execute: {
  195. let alert = NSAlert()
  196. alert.messageText = NSLocalizedString("购买失败", comment: "")
  197. alert.informativeText = NSLocalizedString("", comment: "")
  198. // alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  199. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  200. var window = NSApp.mainWindow
  201. if NSApp.mainWindow?.sheets.first != nil {
  202. window = NSApp.mainWindow?.sheets.first
  203. }
  204. if window != nil {
  205. alert.beginSheetModal(for: window!) { result in
  206. if (result == .alertFirstButtonReturn) {
  207. } else if result == .alertSecondButtonReturn {
  208. return
  209. }
  210. }
  211. }
  212. })
  213. }
  214. }