KMComparativeTableViewController.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. @objc 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 loginSuccess = 8 //用户登录成功
  24. case loginCancel = 9 //用户登录成功
  25. case compress = 10 // 压缩
  26. case convert = 11 // 转档
  27. case secure = 12 // 安全
  28. case pageEdit = 13 // 页面编辑
  29. case merge = 14 // 合并
  30. case crop = 15 // 裁切
  31. case thumb = 16 // 缩略图列表
  32. case shareFlatten = 17 // 分享副本
  33. //后续添加
  34. case upgrade = 18 //升级按钮
  35. case equity = 19 //权益弹窗
  36. }
  37. @objcMembers class KMComparativeTableViewController: NSWindowController {
  38. @IBOutlet weak var comparativeTableView: KMComparativeTableView!
  39. @IBOutlet weak var comparativeView: KMComparativeView!
  40. var inputType: KMComparativeInputType?
  41. var loginAction: KMComparativeTableViewControllerLoginAction?
  42. var signUpAction: KMComparativeTableViewControllerSignUpAction?
  43. var subscriptionAction: KMComparativeTableViewControllerSubscriptionAction?
  44. var restoreAction: KMComparativeTableViewControllerRestoreAction?
  45. deinit {
  46. KMPrint("KMComparativeTableViewController 释放")
  47. }
  48. //MARK: 打开文件
  49. /**
  50. @abstract 弹出比较表
  51. @param window 弹出window
  52. @param type 从什么页面跳转到比较表 默认batch
  53. */
  54. static func show(window: NSWindow, _ type: KMComparativeInputType = .unknown, inputType: KMSubscribeWaterMarkType = .none) -> KMComparativeTableViewController? {
  55. #if VERSION_DMG
  56. if KMLightMemberManager.manager.isLogin() || type == .upgrade {
  57. KMPurchaseManager.manager.showStore()
  58. } else {
  59. KMLoginWindowController.show(window: window)
  60. }
  61. #endif
  62. #if VERSION_FREE
  63. let controller: KMComparativeTableViewController = KMComparativeTableViewController.init(windowNibName: "KMComparativeTableViewController")
  64. controller.inputType = type
  65. controller.loginAction = { controller in
  66. comparativeController = nil
  67. comparativeMainWindow?.endSheet(controller.window!)
  68. comparativeMainWindow = nil
  69. controller.close()
  70. if NSApp.mainWindow != nil {
  71. KMLoginWindowController.show(window: NSApp.mainWindow!, .ComparativeTable, .login)
  72. }
  73. }
  74. controller.signUpAction = { controller in
  75. comparativeController = nil
  76. comparativeMainWindow?.endSheet(controller.window!)
  77. comparativeMainWindow = nil
  78. controller.close()
  79. if NSApp.mainWindow != nil {
  80. KMLoginWindowController.show(window: NSApp.mainWindow!, .ComparativeTable, .register)
  81. }
  82. }
  83. controller.subscriptionAction = { controller in
  84. controller.subscribeAction(controller: controller)
  85. }
  86. controller.restoreAction = { controller in
  87. controller.subscribeAction(controller: controller, isRestore:true)
  88. }
  89. if controller.window != nil {
  90. window.beginSheet(controller.window!)
  91. }
  92. comparativeController = controller
  93. comparativeMainWindow = window
  94. if type == .loginSuccess && KMLightMemberManager.manager.purchaseState != .subscription {
  95. controller.subscribeAction(controller: controller)
  96. }
  97. return controller
  98. #endif
  99. return nil
  100. }
  101. override func windowDidLoad() {
  102. super.windowDidLoad()
  103. // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
  104. self.setup()
  105. KMPrint("订阅状态: \(KMInAppPurchaseManager.manager.state)")
  106. }
  107. func setup() {
  108. self.window?.contentView?.backgroundColor(NSColor.km_init(hex: "FFFFFF"))
  109. self.comparativeView.closeAction = { view in
  110. KMPrint("关闭")
  111. comparativeMainWindow?.endSheet(view.window!)
  112. view.window?.close()
  113. comparativeController = nil
  114. comparativeMainWindow = nil
  115. }
  116. self.comparativeView.signUpAction = { [weak self] view in
  117. KMPrint("注册")
  118. guard let callBack = self?.signUpAction else { return }
  119. callBack(self!)
  120. }
  121. // self.comparativeTableView.loginAction = { [weak self] view in
  122. // print("登录")
  123. // guard let callBack = self?.loginAction else { return }
  124. //
  125. // callBack(self!)
  126. // }
  127. //
  128. self.comparativeView.subscriptionAction = { [weak self] view in
  129. KMPrint("订阅")
  130. guard let callBack = self?.subscriptionAction else { return }
  131. callBack(self!)
  132. }
  133. self.comparativeView.restoreAction = { [weak self] view in
  134. KMPrint("restore订阅")
  135. guard let callBack = self?.restoreAction else { return }
  136. callBack(self!)
  137. }
  138. }
  139. static func isSampleController() -> KMComparativeTableViewController {
  140. for window in NSApp.windows {
  141. let controller = window.windowController
  142. if controller is KMComparativeTableViewController {
  143. return controller as! KMComparativeTableViewController
  144. }
  145. }
  146. return KMComparativeTableViewController()
  147. }
  148. func subscribeAction(controller: KMComparativeTableViewController, isRestore: Bool = false) {
  149. //loading
  150. controller.comparativeView.beginLoading(backgroundColor: NSColor.black.withAlphaComponent(0.2))
  151. if isRestore {
  152. KMPurchaseManager.manager.restorePurchases { isSuccess, error in
  153. if isSuccess {
  154. comparativeController = nil
  155. comparativeMainWindow?.endSheet(controller.window!)
  156. comparativeMainWindow = nil
  157. controller.close()
  158. } else {
  159. if error != .restoreSuccess {
  160. KMComparativeTableViewController.purchasefailed(view: comparativeController?.comparativeView, state: error)
  161. }
  162. }
  163. controller.comparativeView.endLoading()
  164. }
  165. } else {
  166. KMPurchaseManager.manager.purchaseProduct(productIdentifier: PRODUCT_1) { isSuccess, error in
  167. if isSuccess {
  168. KMPrint("订阅成功")
  169. comparativeController = nil
  170. comparativeMainWindow?.endSheet(controller.window!)
  171. controller.close()
  172. if comparativeMainWindow != nil {
  173. _ = KMSubscribeSuccessWindowController.show(window: comparativeMainWindow!)
  174. comparativeMainWindow = nil
  175. }
  176. } else {
  177. KMComparativeTableViewController.purchasefailed(view: comparativeController?.comparativeView, state: error)
  178. }
  179. //loading
  180. controller.comparativeView.endLoading()
  181. }
  182. }
  183. }
  184. static func purchasefailed(view: NSView?, state: KMInAppPurchaseState) {
  185. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1, execute: {
  186. if let showView = view {
  187. KMPurchaseAlertView.show(view: showView, string: state.rawValue)
  188. }
  189. })
  190. }
  191. static func purchaseSuccess() {
  192. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1, execute: {
  193. let alert = NSAlert()
  194. alert.messageText = NSLocalizedString("restore success", comment: "")
  195. alert.informativeText = NSLocalizedString("", comment: "")
  196. // alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  197. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  198. var window = NSApp.mainWindow
  199. if NSApp.mainWindow?.sheets.first != nil {
  200. window = NSApp.mainWindow?.sheets.first
  201. }
  202. if window != nil {
  203. alert.beginSheetModal(for: window!) { result in
  204. if (result == .alertFirstButtonReturn) {
  205. } else if result == .alertSecondButtonReturn {
  206. return
  207. }
  208. }
  209. }
  210. })
  211. }
  212. static func loginAlertView() {
  213. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1, execute: {
  214. let alert = NSAlert()
  215. alert.messageText = NSLocalizedString("Please login or register to proceed to the next step", comment: "")
  216. alert.informativeText = NSLocalizedString("As a PDF Master member, you can get unlimited conversion, merge function, AI free use times and other advanced rights.", comment: "")
  217. // alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  218. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  219. var window = NSApp.mainWindow
  220. if NSApp.mainWindow?.sheets.first != nil {
  221. window = NSApp.mainWindow?.sheets.first
  222. }
  223. if window != nil {
  224. alert.beginSheetModal(for: window!) { result in
  225. if (result == .alertFirstButtonReturn) {
  226. comparativeMainWindow?.endSheet(comparativeController!.window!)
  227. comparativeMainWindow = nil
  228. comparativeController!.close()
  229. comparativeController = nil
  230. if NSApp.mainWindow != nil {
  231. var email: String = UserDefaults.standard.value(forKey: "kLoginEmail") as? String ?? ""
  232. if email.count == 0 {
  233. let tempController = KMLoginWindowController.show(window: NSApp.mainWindow!, .ComparativeTable, .register)
  234. } else {
  235. let tempController = KMLoginWindowController.show(window: NSApp.mainWindow!, .ComparativeTable)
  236. }
  237. }
  238. } else if result == .alertSecondButtonReturn {
  239. return
  240. }
  241. }
  242. }
  243. })
  244. }
  245. }