// // KMForgotPasswordView.swift // PDF Reader Pro // // Created by wanjun on 2024/10/28. // import Cocoa import Combine class KMForgotPasswordView: KMBaseXibView { @IBOutlet weak var resetPasswordsLabel: NSTextField! @IBOutlet weak var emailBox: NSBox! @IBOutlet weak var emailTextField: NSTextField! @IBOutlet weak var nextBox: NSBox! @IBOutlet weak var nextButton: NSButton! @IBOutlet weak var backButton: NSButton! @IBOutlet weak var emailErrorLabel: NSTextField! private var viewModel = KMSignUpViewModel() private var cancellables = Set() convenience init(model: KMSignUpViewModel, superView: NSView) { self.init(frame: superView.bounds) viewModel = model viewModel.screenType = .forgotPassword bindViewModel() languageLocalized() initializeUI() } public override init(frame frameRect: NSRect) { super.init(frame: frameRect) } public required init?(coder decoder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func updateUI() { super.updateUI() bindViewModel() languageLocalized() initializeUI() } // MARK: Private Method private func languageLocalized() -> Void { resetPasswordsLabel.stringValue = NSLocalizedString("Reset Password", tableName: "MemberCenterLocalizable", comment: "") emailTextField.placeholderString = NSLocalizedString("Please enter email address", tableName: "MemberCenterLocalizable", comment: "") emailErrorLabel.stringValue = String(format: "*%@", NSLocalizedString("Email format error. Please enter the correct email.", tableName: "MemberCenterLocalizable", comment: "")) nextButton.title = NSLocalizedString("Next", tableName: "MemberCenterLocalizable", comment: "") backButton.title = NSLocalizedString("Previous Step", tableName: "MemberCenterLocalizable", comment: "") } private func initializeUI() -> Void { emailTextField.delegate = self emailBox.fillColor = NSColor(named: "texefiedfillcolor") ?? NSColor.white emailBox.borderColor = NSColor(named: "DADBDE") ?? NSColor.gray resetPasswordsLabel.textColor = NSColor(named: "000000") resetPasswordsLabel.font = NSFont.SFMediumFontWithSize(20) emailTextField.stringValue = viewModel.email emailErrorLabel.isHidden = viewModel.emailError() emailErrorLabel.textColor = NSColor(named: "FA1E5D") emailErrorLabel.font = NSFont.SFProTextRegularFont(9) nextBox.fillColor = NSColor(named: "273C62") ?? NSColor.blue nextButton.setTitleColor(color: NSColor(named: "FFFFFF") ?? NSColor.white, font: NSFont.SFProTextRegularFont(13)) backButton.setTitleColor(color: NSColor(named: "4982E6") ?? NSColor.blue, font: NSFont.SFProTextRegularFont(12)) } private func textfieldInputState() -> Void { if viewModel.emailError() { emailBox.borderColor = NSColor(named: "FA1E5D") ?? NSColor.red } else { emailBox.borderColor = NSColor(named: "DADBDE") ?? NSColor.gray } emailErrorLabel.isHidden = !viewModel.emailError() } private func skipSignUpView() -> Void { guard let parentView = self.superview else { return } if parentView is NSBox { let model = KMSignUpViewModel() model.email = viewModel.email let signUpView = KMSignUpView(model: model, superView: parentView) NSAnimationContext.runAnimationGroup { context in context.duration = 0.3 self.animator().alphaValue = 0 } completionHandler: { self.removeFromSuperview() signUpView.alphaValue = 0 (parentView as! NSBox).contentView = signUpView NSAnimationContext.runAnimationGroup({ context in context.duration = 0.3 signUpView.animator().alphaValue = 1 }, completionHandler: nil) } } else { guard let parentView = self.superview else { return } let model = KMSignUpViewModel() model.email = viewModel.email let signUpView = KMSignUpView(model: model, superView: parentView) NSAnimationContext.runAnimationGroup { context in context.duration = 0.3 self.animator().alphaValue = 0 } completionHandler: { self.removeFromSuperview() signUpView.alphaValue = 0 parentView.addSubview(signUpView) NSAnimationContext.runAnimationGroup({ context in context.duration = 0.3 signUpView.animator().alphaValue = 1 }, completionHandler: nil) } } } private func skipEnterVerificationCodeView() -> Void { guard let parentView = self.superview else { return } if parentView is NSBox { let model = KMSignUpViewModel() model.email = viewModel.email let codeView = KMEnterVerificationCodeView(model: model, superView: parentView) NSAnimationContext.runAnimationGroup { context in context.duration = 0.3 self.animator().alphaValue = 0 } completionHandler: { self.removeFromSuperview() codeView.alphaValue = 0 (parentView as! NSBox).contentView = codeView NSAnimationContext.runAnimationGroup({ context in context.duration = 0.3 codeView.animator().alphaValue = 1 }, completionHandler: nil) } } else { guard let parentView = self.superview else { return } let model = KMSignUpViewModel() model.email = viewModel.email let codeView = KMEnterVerificationCodeView(model: viewModel, superView: parentView) NSAnimationContext.runAnimationGroup { context in context.duration = 0.3 self.animator().alphaValue = 0 } completionHandler: { self.removeFromSuperview() codeView.alphaValue = 0 parentView.addSubview(codeView) NSAnimationContext.runAnimationGroup({ context in context.duration = 0.3 codeView.animator().alphaValue = 1 }, completionHandler: nil) } } } // MARK: Bind Method func bindViewModel() -> Void { viewModel.$emailErrorMessage .receive(on: RunLoop.main) .sink { [weak self] newValue in self?.emailErrorLabel.stringValue = newValue self?.emailErrorLabel.isHidden = false self?.textfieldInputState() } .store(in: &cancellables) } // MARK: Action Method @IBAction func nextButtonAction(_ sender: NSButton) { if viewModel.email.count <= 0 || viewModel.email.count > 100 || !viewModel.isValidEmail() { viewModel.emailErrorMessage = NSLocalizedString("Email format error. Please enter the correct email.", tableName: "MemberCenterLocalizable", comment: "") return } viewModel.emailErrorMessage = "" viewModel.forgotPasswordNextAction() { [weak self] success, msg in guard let self = self else { return } if success { self.skipEnterVerificationCodeView() } else { let alert = NSAlert() alert.messageText = NSLocalizedString("This email has not been registered, please log in through the account verification code, we will automatically register an account for you.", tableName: "MemberCenterLocalizable", comment: "") alert.addButton(withTitle: "OK") let response = alert.runModal() if response == .alertFirstButtonReturn { self.skipSignUpView() } } } } @IBAction func backButtonAction(_ sender: NSButton) { skipSignUpView() } } extension KMForgotPasswordView: NSTextFieldDelegate { func controlTextDidEndEditing(_ obj: Notification) { let textField = obj.object as? NSTextField if textField == emailTextField { viewModel.email = textField!.stringValue } } func controlTextDidChange(_ obj: Notification) { let textField = obj.object as? NSTextField if textField == emailTextField { viewModel.email = textField!.stringValue viewModel.emailErrorMessage = "" emailBox.borderColor = NSColor(named: "DADBDE") ?? NSColor.gray } } }