123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- //
- // KMComparativeTableViewController.swift
- // PDF Reader Pro
- //
- // Created by lizhe on 2023/2/24.
- //
- import Cocoa
- typealias KMComparativeTableViewControllerLoginAction = (_ controller: KMComparativeTableViewController) -> Void
- typealias KMComparativeTableViewControllerSignUpAction = (_ controller: KMComparativeTableViewController) -> Void
- typealias KMComparativeTableViewControllerSubscriptionAction = (_ controller: KMComparativeTableViewController) -> Void
- typealias KMComparativeTableViewControllerRestoreAction = (_ controller: KMComparativeTableViewController) -> Void
- var comparativeController: KMComparativeTableViewController?
- var comparativeMainWindow: NSWindow?
- @objc enum KMComparativeInputType: Int {
- case unknown = 0
- case login = 1 //登录界面
- case register = 2 //注册界面
- case resetPassword = 3 //重置密码
- case verificationCode = 4 //验证码
- case registerSuccess = 5 //注册成功
- case loginInputPassword = 6 //登录密码输入
- case accountInfo = 7 //用户信息
- case loginSuccess = 8 //用户登录成功
- case loginCancel = 9 //用户登录成功
-
- case compress = 10 // 压缩
- case convert = 11 // 转档
- case secure = 12 // 安全
- case pageEdit = 13 // 页面编辑
- case merge = 14 // 合并
- case crop = 15 // 裁切
- case thumb = 16 // 缩略图列表
- case shareFlatten = 17 // 分享副本
- //后续添加
-
- case upgrade = 18 //升级按钮
- case equity = 19 //权益弹窗
- }
- @objcMembers class KMComparativeTableViewController: NSWindowController {
- @IBOutlet weak var comparativeTableView: KMComparativeTableView!
- @IBOutlet weak var comparativeView: KMComparativeView!
-
- var inputType: KMComparativeInputType?
-
- var loginAction: KMComparativeTableViewControllerLoginAction?
- var signUpAction: KMComparativeTableViewControllerSignUpAction?
- var subscriptionAction: KMComparativeTableViewControllerSubscriptionAction?
- var restoreAction: KMComparativeTableViewControllerRestoreAction?
-
- deinit {
- KMPrint("KMComparativeTableViewController 释放")
- }
-
- //MARK: 打开文件
- /**
- @abstract 弹出比较表
- @param window 弹出window
- @param type 从什么页面跳转到比较表 默认batch
- */
- static func show(window: NSWindow, _ type: KMComparativeInputType = .unknown, inputType: KMSubscribeWaterMarkType = .none) -> KMComparativeTableViewController? {
- #if VERSION_DMG
- if KMLightMemberManager.manager.isLogin() || type == .upgrade {
- KMPurchaseManager.manager.showStore()
- } else {
- KMLoginWindowController.show(window: window)
- }
- #endif
-
- #if VERSION_FREE
- let controller: KMComparativeTableViewController = KMComparativeTableViewController.init(windowNibName: "KMComparativeTableViewController")
- controller.inputType = type
- controller.loginAction = { controller in
- comparativeController = nil
- comparativeMainWindow?.endSheet(controller.window!)
- comparativeMainWindow = nil
- controller.close()
- if NSApp.mainWindow != nil {
- KMLoginWindowController.show(window: NSApp.mainWindow!, .ComparativeTable, .login)
- }
- }
- controller.signUpAction = { controller in
- comparativeController = nil
- comparativeMainWindow?.endSheet(controller.window!)
- comparativeMainWindow = nil
- controller.close()
- if NSApp.mainWindow != nil {
- KMLoginWindowController.show(window: NSApp.mainWindow!, .ComparativeTable, .register)
- }
- }
-
- controller.subscriptionAction = { controller in
- controller.subscribeAction(controller: controller)
- }
-
- controller.restoreAction = { controller in
- controller.subscribeAction(controller: controller, isRestore:true)
- }
-
- if controller.window != nil {
- window.beginSheet(controller.window!)
- }
- comparativeController = controller
- comparativeMainWindow = window
-
- if type == .loginSuccess && KMLightMemberManager.manager.purchaseState != .subscription {
- controller.subscribeAction(controller: controller)
- }
- return controller
- #endif
- return nil
- }
-
- override func windowDidLoad() {
- super.windowDidLoad()
- // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
- self.setup()
-
- KMPrint("订阅状态: \(KMInAppPurchaseManager.manager.state)")
- }
-
- func setup() {
- self.window?.contentView?.backgroundColor(NSColor.km_init(hex: "FFFFFF"))
- self.comparativeView.closeAction = { view in
- KMPrint("关闭")
- comparativeMainWindow?.endSheet(view.window!)
- view.window?.close()
- comparativeController = nil
- comparativeMainWindow = nil
- }
-
- self.comparativeView.signUpAction = { [weak self] view in
- KMPrint("注册")
- guard let callBack = self?.signUpAction else { return }
- callBack(self!)
- }
- // self.comparativeTableView.loginAction = { [weak self] view in
- // print("登录")
- // guard let callBack = self?.loginAction else { return }
- //
- // callBack(self!)
- // }
- //
- self.comparativeView.subscriptionAction = { [weak self] view in
- KMPrint("订阅")
- guard let callBack = self?.subscriptionAction else { return }
- callBack(self!)
- }
- self.comparativeView.restoreAction = { [weak self] view in
- KMPrint("restore订阅")
- guard let callBack = self?.restoreAction else { return }
- callBack(self!)
- }
- }
-
- static func isSampleController() -> KMComparativeTableViewController {
- for window in NSApp.windows {
- let controller = window.windowController
- if controller is KMComparativeTableViewController {
- return controller as! KMComparativeTableViewController
- }
- }
- return KMComparativeTableViewController()
- }
-
- func subscribeAction(controller: KMComparativeTableViewController, isRestore: Bool = false) {
- //loading
- controller.comparativeView.beginLoading(backgroundColor: NSColor.black.withAlphaComponent(0.2))
- if isRestore {
- KMPurchaseManager.manager.restorePurchases { isSuccess, error in
- if isSuccess {
- comparativeController = nil
- comparativeMainWindow?.endSheet(controller.window!)
- comparativeMainWindow = nil
- controller.close()
- } else {
- if error != .restoreSuccess {
- KMComparativeTableViewController.purchasefailed(view: comparativeController?.comparativeView, state: error)
- }
- }
- controller.comparativeView.endLoading()
- }
- } else {
- KMPurchaseManager.manager.purchaseProduct(productIdentifier: PRODUCT_1) { isSuccess, error in
- if isSuccess {
- KMPrint("订阅成功")
- comparativeController = nil
- comparativeMainWindow?.endSheet(controller.window!)
- controller.close()
- if comparativeMainWindow != nil {
- _ = KMSubscribeSuccessWindowController.show(window: comparativeMainWindow!)
- comparativeMainWindow = nil
- }
- } else {
- KMComparativeTableViewController.purchasefailed(view: comparativeController?.comparativeView, state: error)
- }
- //loading
- controller.comparativeView.endLoading()
- }
- }
- }
-
- static func purchasefailed(view: NSView?, state: KMInAppPurchaseState) {
- DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1, execute: {
- if let showView = view {
- KMPurchaseAlertView.show(view: showView, string: state.rawValue)
- }
- })
- }
-
- static func purchaseSuccess() {
- DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1, execute: {
- let alert = NSAlert()
- alert.messageText = NSLocalizedString("restore success", comment: "")
- alert.informativeText = NSLocalizedString("", comment: "")
- // alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
- alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
- var window = NSApp.mainWindow
- if NSApp.mainWindow?.sheets.first != nil {
- window = NSApp.mainWindow?.sheets.first
- }
- if window != nil {
- alert.beginSheetModal(for: window!) { result in
- if (result == .alertFirstButtonReturn) {
- } else if result == .alertSecondButtonReturn {
- return
- }
- }
- }
- })
- }
-
- static func loginAlertView() {
- DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1, execute: {
- let alert = NSAlert()
- alert.messageText = NSLocalizedString("Please login or register to proceed to the next step", comment: "")
- alert.informativeText = NSLocalizedString("As a PDFull member, you can get unlimited conversion, merge function, AI free use times and other advanced rights.", comment: "")
- // alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
- alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
- var window = NSApp.mainWindow
- if NSApp.mainWindow?.sheets.first != nil {
- window = NSApp.mainWindow?.sheets.first
- }
- if window != nil {
- alert.beginSheetModal(for: window!) { result in
- if (result == .alertFirstButtonReturn) {
- comparativeMainWindow?.endSheet(comparativeController!.window!)
- comparativeMainWindow = nil
- comparativeController!.close()
- comparativeController = nil
- if NSApp.mainWindow != nil {
- var email: String = UserDefaults.standard.value(forKey: "kLoginEmail") as? String ?? ""
- if email.count == 0 {
- let tempController = KMLoginWindowController.show(window: NSApp.mainWindow!, .ComparativeTable, .register)
- } else {
- let tempController = KMLoginWindowController.show(window: NSApp.mainWindow!, .ComparativeTable)
- }
- }
- } else if result == .alertSecondButtonReturn {
- return
- }
- }
- }
- })
- }
- }
|