// // AccountCenterWindowController.swift // PDF Reader Pro // // Created by User-Tangchao on 2024/10/22. // import Cocoa let kAccountTokenKey = "AccountToken" let kAccountEmailKey = "AccountEmail" let kTermsOfUseUrlString = "https://www.anyrecover.com/company/terms-conditions/" let kPrivacyPolicyUrlString = "https://www.anyrecover.com/company/privacy-policy/" let kResetpasswordUrlString = "https://account.anyrecover.com/reset-password/" let kCancelSubscriptionUrlString = "https://www.anyrecover.com/support/cancel-subscription/" let kPwdInputStrings = "!\"#$%&'()*+,-./:;<>=?@[\\]^_`{|}~0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM" extension NSNotification.Name { static let loginStatusChanged = NSNotification.Name("KMLoginStatusChangedNotification") } class AccountCenterWindowController: NSWindowController { @IBOutlet weak var contentBox: NSBox! private lazy var rightDatas_: [String] = { return [ NSLocalizedString("Handle PDF Documents with AI", comment: ""), NSLocalizedString("Unlimited file conversion", comment: ""), NSLocalizedString("PDF text and image editing", comment: ""), NSLocalizedString("Batch PDF processing", comment: ""), NSLocalizedString("Advanced PDF management", comment: ""), NSLocalizedString("PDF annotations", comment: ""), NSLocalizedString("Create&fill forms", comment: ""), NSLocalizedString("PDF Protect", comment: ""), NSLocalizedString("Advanced OCR technology", comment: "")] }() private var inputC_: AccountInputController? private var centerC: AccountCenterController? private var pwdChangedwinC_: AccountPwdChangedWindowController? private lazy var closeButton_: NSButton = { let view = NSButton() view.isBordered = false view.title = "" return view }() convenience init() { self.init(windowNibName: "AccountCenterWindowController") } override func windowDidLoad() { super.windowDidLoad() self.window?.appearance = NSAppearance(named: .aqua) self.contentBox.borderWidth = 0 self.window?.contentView?.addSubview(self.closeButton_) self.closeButton_.km_add_right_constraint(constant: -10) self.closeButton_.km_add_top_constraint(constant: 10) self.closeButton_.km_add_size_constraint(size: .init(width: 24, height: 24)) self.closeButton_.target = self self.closeButton_.action = #selector(_closeAction) self.closeButton_.image = NSImage(named: "KMImageNameAccountClose") if let token = KMDataManager.ud_string(forKey: kAccountTokenKey), token.isEmpty == false { // 外面处理激活设备 AccountTools.refreshRights(isActivaDevice: false, isDealCode: true) { model in guard let _ = model else { self.gotoLogin() return } let rightM = AccountRightModel(dict: (model?.data as? [String : Any]) ?? [:]) self.gotoCenter(model: rightM, infoModel: nil) // 激活设备 if let data = rightM.memberInfo?.email, data.isEmpty == false { AccountTools.activateDevice(email: data, callback: nil) } } } else { self.gotoLogin() } } func gotoLogin() { self.closeButton_.isHidden = false self.inputC_ = AccountInputController() self.inputC_?.rightDatas = self.rightDatas_ self.contentBox.contentView = self.inputC_?.view self.inputC_?.gotoLogin() self.inputC_?.itemClick = { idx, params in guard let model = params.first as? AccountInfoModel else { return } if let data = model.token, data.isEmpty == false { KMDataManager.ud_set(data, forKey: kAccountTokenKey) } if let data = model.email, data.isEmpty == false { KMDataManager.ud_set(data, forKey: kAccountEmailKey) } let state = self._isConnectionAvailable() if !state { self._showHud(msg: NSLocalizedString("Unable to connect to server, please check your connection.", comment: "")) return } AccountTools.refreshRights(isActivaDevice: true, isDealCode: true) { dataModel in guard let _ = dataModel else { return } let rmodel = AccountRightModel(dict: dataModel?.data as? [String : Any] ?? [:]) self.gotoCenter(model: rmodel, infoModel: model) } } } func gotoSignin() { self.closeButton_.isHidden = false self.inputC_ = AccountInputController() self.contentBox.contentView = self.inputC_?.view self.inputC_?.goToSignIn() self.inputC_?.rightDatas = self.rightDatas_ self.inputC_?.itemClick = { idx, params in guard let model = params.first as? AccountInfoModel else { return } if let data = model.token, data.isEmpty == false { KMDataManager.ud_set(data, forKey: kAccountTokenKey) } if let data = model.email, data.isEmpty == false { KMDataManager.ud_set(data, forKey: kAccountEmailKey) } let state = self._isConnectionAvailable() if !state { self._showHud(msg: NSLocalizedString("Unable to connect to server, please check your connection.", comment: "")) return } AccountTools.refreshRights(isActivaDevice: true, isDealCode: true) { dataModel in guard let _ = dataModel else { return } let rmodel = AccountRightModel(dict: dataModel?.data as? [String : Any] ?? [:]) self.gotoCenter(model: rmodel, infoModel: model) } } } func gotoCenter(model: AccountRightModel?, infoModel: AccountInfoModel?) { self.closeButton_.isHidden = true self.centerC = AccountCenterController() self.centerC?.rightDatas = self.rightDatas_ self.centerC?.model = model self.centerC?.infoModel = infoModel self.centerC?.goLoginBlock = { [weak self] _ in self?.gotoLogin() } self.contentBox.contentView = self.centerC?.view } func pwdChangedAction() { if let _ = self.pwdChangedwinC_ { NSSound.beep() return } KMDataManager.ud_set("", forKey: kAccountTokenKey) AccountManager.manager.saveRights(model: nil) NotificationCenter.default.post(name: .loginStatusChanged, object: nil) let winC = AccountPwdChangedWindowController() self.pwdChangedwinC_ = winC self.window?.addChildWindow(winC.window!, ordered: .above) let winFrame = self.window?.sheetParent?.frame ?? .zero var frame = self.pwdChangedwinC_?.window?.frame ?? .zero frame.origin.x = winFrame.origin.x + (winFrame.size.width-frame.size.width)*0.5 frame.origin.y = winFrame.origin.y + (winFrame.size.height-frame.size.height)*0.5 self.pwdChangedwinC_?.window?.setFrame(frame, display: true) winC.itemClick = { [weak self] idx, _ in for win in self?.window?.childWindows ?? [] { if win.isEqual(to: self?.pwdChangedwinC_?.window) { self?.window?.removeChildWindow(win) break } } self?.pwdChangedwinC_?.window?.orderOut(nil) self?.pwdChangedwinC_ = nil self?.window?.windowController?.km_quick_endSheet() } } // MARK: - Private Methods @objc private func _closeAction() { self.km_quick_endSheet() } private func _isConnectionAvailable() -> Bool { if Reachability.forInternetConnection().currentReachabilityStatus().rawValue == 0 { return false } return true } private func _showHud(msg: String) { DispatchQueue.main.async { if let data = self.window?.contentView { _ = CustomAlertView.alertView(message: msg, fromView: data, withStyle: .black) } } } }