123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- //
- // KMAccountInfoWindowController.swift
- // PDF Master
- //
- // Created by lizhe on 2023/2/24.
- //
- import Cocoa
- var accountInfoController: KMAccountInfoWindowController?
- var accountInfoMainWindow: NSWindow?
- typealias KMAccountInfoWindowControllerCancellationAction = (_ controller: KMAccountInfoWindowController) -> Void
- class KMAccountInfoWindowController: NSWindowController {
- @IBOutlet weak var accountInfoView: KMAccountInfoView!
- @IBOutlet weak var accountViewHeightConstraint: NSLayoutConstraint!
-
- var inputType: DataNavigationViewButtonActionType?
- var cancellAtionAction: KMAccountInfoWindowControllerCancellationAction?
-
- deinit {
- KMPrint("KMAccountInfoWindowController 释放")
- }
-
- 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()
- self.reloadData()
- }
-
- //MARK: 打开文件
- static func show(window: NSWindow, _ type: DataNavigationViewButtonActionType = .Batch, _ logType: KMRegisterLogType = .login) -> KMAccountInfoWindowController {
- if KMAccountInfoWindowController.isSampleController() {
- return accountInfoController ?? KMAccountInfoWindowController()
- } else {
- let controller: KMAccountInfoWindowController = KMAccountInfoWindowController.init(windowNibName: "KMAccountInfoWindowController")
- controller.inputType = type
- controller.cancellAtionAction = { controller in
- let verficationCodeWindowController = KMVerficationCodeWindowController.show(window: controller.window!)
- verficationCodeWindowController.didCancellation = {
- accountInfoMainWindow?.endSheet(controller.window!)
- controller.window?.close()
- if accountInfoMainWindow != nil {
- let cancellationWindowController = KMCancellationWindowController.show(window: accountInfoMainWindow!)
- }
-
- accountInfoController = nil
- accountInfoMainWindow = nil
- }
- }
-
-
- window.beginSheet(controller.window!)
- accountInfoController = controller
- accountInfoMainWindow = window
- return controller
- }
- }
-
- static func isSampleController() -> Bool {
- for window in NSApp.windows {
- let controller = window.windowController
- if controller is KMAccountInfoWindowController {
- return true
- }
- }
- return false
- }
-
- static func fetchSampleController() -> KMAccountInfoWindowController? {
- for window in NSApp.windows {
- let controller = window.windowController
- if controller is KMAccountInfoWindowController {
- return controller as! KMAccountInfoWindowController
- }
- }
-
- return nil
- }
-
- func setup() {
- self.window?.contentView?.backgroundColor(NSColor.km_init(hex: "#FFFFFF"))
-
- self.accountInfoView.closeAction = { view in
- KMPrint("关闭")
- accountInfoMainWindow?.endSheet(view.window!)
- view.window?.close()
- accountInfoController = nil
- accountInfoMainWindow = nil
- }
-
- self.accountInfoView.logOutAction = { view in
- KMPrint("登出")
- KMRequestServerManager.manager.logout { success, result in
- if success {
- KMPrint("登出成功")
- }
- }
- KMLightMemberManager.manager.logOut()
- accountInfoMainWindow?.endSheet(view.window!)
- view.window?.close()
- accountInfoController = nil
- accountInfoMainWindow = nil
- }
-
- self.accountInfoView.cancellationAction = { [unowned self] view in
- KMPrint("注销")
-
- if KMLightMemberManager.manager.purchaseState == .subscription ||
- KMLightMemberManager.manager.purchaseState == .trial {
- let alert = NSAlert()
- alert.messageText = NSLocalizedString("Are you sure you want to cancel?", comment: "")
- alert.informativeText = NSLocalizedString("You are still in the subscription and cannot cancel account. Please click Cancel Account after the subscription period ends.", comment: "")
- alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
- alert.beginSheetModal(for: view.window!) { [unowned self] result in
- }
- } else {
- let alert = NSAlert()
- alert.messageText = NSLocalizedString("Are you sure you want to cancel?", comment: "")
- alert.informativeText = NSLocalizedString("Cancellation cannot be withdrawn, it will be completed within 3 working days, until then your account will be frozen and cannot be logged in, are you sure you want to delete your account?", comment: "")
- alert.addButton(withTitle: NSLocalizedString("No", comment: ""))
- alert.addButton(withTitle: NSLocalizedString("Yes", comment: ""))
- alert.beginSheetModal(for: view.window!) { [unowned self] result in
- if (result == .alertFirstButtonReturn) { /// 取消
- return
- } else if result == .alertSecondButtonReturn {
- guard let callBack = cancellAtionAction else { return }
- // accountInfoMainWindow?.endSheet(view.window!)
- // view.window?.close()
- // accountInfoController = nil
- // accountInfoMainWindow = nil
- callBack(self)
- }
-
- }
- }
- }
-
- self.accountInfoView.purchaseInfoAction = { view in
- KMPurchaseManager.manager.showPurchasesInfo()
- }
-
- self.accountInfoView.subcriptionAction = { view in
- #if VERSION_DMG
- KMPurchaseManager.manager.purchaseProduct(productIdentifier: "") { isSuccess, error in
-
- }
- #endif
-
- #if VERSION_FREE
- accountInfoMainWindow?.endSheet(view.window!)
- view.window?.close()
- accountInfoController = nil
- accountInfoMainWindow = nil
-
- //跳转订阅比较表
- let _ = KMComparativeTableViewController.show(window: NSApp.mainWindow ?? NSWindow(), .accountInfo)
- #endif
- }
- }
-
- func reloadData() {
- let state = KMDMGPurchaseManager.manager.state
- if state == .unknow {
- self.accountViewHeightConstraint.constant = 274
- #if VERSION_DMG
- if KMLightMemberManager.manager.existOrderInfo() {
- self.accountViewHeightConstraint.constant = 274 + 44
- }
- #endif
- } else if state == .subscription ||
- state == .trial {
- self.accountViewHeightConstraint.constant = 411 - 60
- #if VERSION_DMG
- if KMLightMemberManager.manager.info.subscriptionInfoList[0].payType == 1 {
- self.accountViewHeightConstraint.constant = 483 - 52 - 20
- } else {
- self.accountViewHeightConstraint.constant = 483
- }
- #endif
- } else if state == .subscriptionExpired ||
- state == .trialExpired {
- self.accountViewHeightConstraint.constant = 483 - 52
- #if VERSION_DMG
- self.accountViewHeightConstraint.constant = 483
- #endif
- }
- self.accountInfoView.purchaseState = state
-
- KMRequestServerManager.manager.getUserInfo { [weak self] success, data, error, isLocal in
- if success {
- self?.accountInfoView.userInfo = data
- KMLightMemberManager.manager.reloadUserInfo()
- } else {
- if error?.code == 304 {
- DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
- if self?.window != nil {
- accountInfoMainWindow?.endSheet((self?.window)!)
- }
- self?.window?.close()
- KMLightMemberManager.manager.logOut()
- accountInfoController = nil
- accountInfoMainWindow = nil
- }
- }
- }
- }
- }
- }
|