KMComparativeTableViewController.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 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. 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() {
  57. KMPurchaseManager.manager.showStore()
  58. } else {
  59. KMLoginWindowController.show(window: NSApp.mainWindow!)
  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. window.beginSheet(controller.window!)
  90. comparativeController = controller
  91. comparativeMainWindow = window
  92. if type == .loginSuccess && KMLightMemberManager.manager.purchaseState != .subscription {
  93. controller.subscribeAction(controller: controller)
  94. }
  95. return controller
  96. #endif
  97. return nil
  98. }
  99. override func windowDidLoad() {
  100. super.windowDidLoad()
  101. // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
  102. self.setup()
  103. KMPrint("订阅状态: \(KMInAppPurchaseManager.manager.state)")
  104. }
  105. func setup() {
  106. self.window?.contentView?.backgroundColor(NSColor(hex: "FFFFFF"))
  107. self.comparativeView.closeAction = { view in
  108. KMPrint("关闭")
  109. comparativeMainWindow?.endSheet(view.window!)
  110. view.window?.close()
  111. comparativeController = nil
  112. comparativeMainWindow = nil
  113. }
  114. self.comparativeView.signUpAction = { [weak self] view in
  115. KMPrint("注册")
  116. guard let callBack = self?.signUpAction else { return }
  117. callBack(self!)
  118. }
  119. // self.comparativeTableView.loginAction = { [weak self] view in
  120. // print("登录")
  121. // guard let callBack = self?.loginAction else { return }
  122. //
  123. // callBack(self!)
  124. // }
  125. //
  126. self.comparativeView.subscriptionAction = { [weak self] view in
  127. KMPrint("订阅")
  128. guard let callBack = self?.subscriptionAction else { return }
  129. callBack(self!)
  130. }
  131. self.comparativeView.restoreAction = { [weak self] view in
  132. KMPrint("restore订阅")
  133. guard let callBack = self?.restoreAction else { return }
  134. callBack(self!)
  135. }
  136. }
  137. static func isSampleController() -> KMComparativeTableViewController {
  138. for window in NSApp.windows {
  139. let controller = window.windowController
  140. if controller is KMComparativeTableViewController {
  141. return controller as! KMComparativeTableViewController
  142. }
  143. }
  144. return KMComparativeTableViewController()
  145. }
  146. func subscribeAction(controller: KMComparativeTableViewController, isRestore: Bool = false) {
  147. //loading
  148. controller.comparativeView.beginLoading(backgroundColor: NSColor.black.withAlphaComponent(0.2))
  149. if KMLightMemberManager.manager.isLogin() {
  150. if isRestore {
  151. KMPurchaseManager.manager.restorePurchases { isSuccess, error in
  152. if isSuccess {
  153. comparativeController = nil
  154. comparativeMainWindow?.endSheet(controller.window!)
  155. comparativeMainWindow = nil
  156. controller.close()
  157. } else {
  158. if error != .restoreSuccess {
  159. KMComparativeTableViewController.purchasefailed(view: comparativeController?.comparativeView, state: error)
  160. }
  161. }
  162. controller.comparativeView.endLoading()
  163. }
  164. } else {
  165. KMPurchaseManager.manager.purchaseProduct(productIdentifier: PRODUCT_1) { isSuccess, error in
  166. if isSuccess {
  167. KMPrint("订阅成功")
  168. comparativeController = nil
  169. comparativeMainWindow?.endSheet(controller.window!)
  170. controller.close()
  171. if comparativeMainWindow != nil {
  172. _ = KMSubscribeSuccessWindowController.show(window: comparativeMainWindow!)
  173. comparativeMainWindow = nil
  174. }
  175. } else {
  176. KMComparativeTableViewController.purchasefailed(view: comparativeController?.comparativeView, state: error)
  177. }
  178. //loading
  179. controller.comparativeView.endLoading()
  180. }
  181. }
  182. } else {
  183. KMComparativeTableViewController.loginAlertView()
  184. }
  185. }
  186. static func purchasefailed(view: NSView?, state: KMInAppPurchaseState) {
  187. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1, execute: {
  188. if let showView = view {
  189. KMPurchaseAlertView.show(view: showView, string: state.rawValue)
  190. }
  191. })
  192. }
  193. static func purchaseSuccess() {
  194. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1, execute: {
  195. let alert = NSAlert()
  196. alert.messageText = NSLocalizedString("restore success", 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. static func loginAlertView() {
  215. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1, execute: {
  216. let alert = NSAlert()
  217. alert.messageText = NSLocalizedString("Please login or register to proceed to the next step", comment: "")
  218. alert.informativeText = NSLocalizedString("As a PDF Master member, you can get unlimited conversion, merge function, AI free use times and other advanced rights.", comment: "")
  219. // alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  220. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  221. var window = NSApp.mainWindow
  222. if NSApp.mainWindow?.sheets.first != nil {
  223. window = NSApp.mainWindow?.sheets.first
  224. }
  225. if window != nil {
  226. alert.beginSheetModal(for: window!) { result in
  227. if (result == .alertFirstButtonReturn) {
  228. comparativeMainWindow?.endSheet(comparativeController!.window!)
  229. comparativeMainWindow = nil
  230. comparativeController!.close()
  231. comparativeController = nil
  232. if NSApp.mainWindow != nil {
  233. var email: String = UserDefaults.standard.value(forKey: "kLoginEmail") as? String ?? ""
  234. if email.count == 0 {
  235. let tempController = KMLoginWindowController.show(window: NSApp.mainWindow!, .ComparativeTable, .register)
  236. } else {
  237. let tempController = KMLoginWindowController.show(window: NSApp.mainWindow!, .ComparativeTable)
  238. }
  239. }
  240. } else if result == .alertSecondButtonReturn {
  241. return
  242. }
  243. }
  244. }
  245. })
  246. }
  247. }