// // KMEnterNewPasswordView.swift // PDF Reader Pro // // Created by wanjun on 2024/10/29. // import Cocoa import Combine class KMEnterNewPasswordView: KMBaseXibView { @IBOutlet weak var resetPasswordsLabel: NSTextField! @IBOutlet weak var passwordBox: NSBox! @IBOutlet weak var passwordView: NSView! @IBOutlet weak var passwordTextField: NSTextField! @IBOutlet weak var passwordTextField1: NSSecureTextField! @IBOutlet weak var visibleButton: NSButton! @IBOutlet weak var passwordErrorLabel: NSTextField! @IBOutlet weak var finshBox: NSBox! @IBOutlet weak var finshButton: NSButton! private var viewModel = KMSignUpViewModel() private var cancellables = Set() convenience init(model: KMSignUpViewModel, superView: NSView) { self.init(frame: superView.bounds) viewModel = model viewModel.screenType = .enterNewPassword 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: "") finshButton.title = NSLocalizedString("Finish", tableName: "MemberCenterLocalizable", comment: "") passwordErrorLabel.stringValue = String(format: "*%@", NSLocalizedString("Verification code error.", tableName: "MemberCenterLocalizable", comment: "")) passwordTextField.placeholderString = NSLocalizedString("Please enter a new password", tableName: "MemberCenterLocalizable", comment: "") passwordTextField1.placeholderString = NSLocalizedString("Please enter a new password", tableName: "MemberCenterLocalizable", comment: "") } private func initializeUI() -> Void { passwordTextField.delegate = self passwordTextField1.delegate = self passwordBox.fillColor = NSColor(named: "texefiedfillcolor") ?? NSColor.white passwordBox.borderColor = NSColor(named: "DADBDE") ?? NSColor.gray // passwordTextField.stringValue = viewModel.password // passwordTextField1.stringValue = viewModel.password passwordErrorLabel.isHidden = !viewModel.passwordError() resetPasswordsLabel.textColor = NSColor(named: "000000") resetPasswordsLabel.font = NSFont.SFMediumFontWithSize(20) passwordBox.borderColor = NSColor(named: "DADBDE") ?? NSColor.gray passwordErrorLabel.textColor = NSColor(named: "FA1E5D") passwordErrorLabel.font = NSFont.SFProTextRegularFont(9) finshBox.fillColor = NSColor(named: "273C62") ?? NSColor.blue finshButton.setTitleColor(color: NSColor(named: "FFFFFF") ?? NSColor.white, font: NSFont.SFProTextRegularFont(13)) } private func visibleStateChange() -> Void { if viewModel.isVisible { visibleButton.image = NSImage(named: "passwordVisible") passwordTextField.isHidden = false passwordTextField1.isHidden = true passwordTextField.stringValue = viewModel.password } else { visibleButton.image = NSImage(named: "passwordUnVisible") passwordTextField.isHidden = true passwordTextField1.isHidden = false passwordTextField1.stringValue = viewModel.password } } private func skipSignUpView() { DispatchQueue.main.async { [weak self] in guard let self = self, let parentView = self.superview else { return } let model = KMSignUpViewModel() model.email = self.viewModel.email model.password = self.viewModel.password model.signUpState = .password let signUpView = KMSignUpView(model: model, superView: parentView) if let parentBox = parentView as? NSBox { NSAnimationContext.runAnimationGroup { context in context.duration = 0.3 self.animator().alphaValue = 0 } completionHandler: { self.removeFromSuperview() signUpView.alphaValue = 0 parentBox.contentView = signUpView NSAnimationContext.runAnimationGroup({ context in context.duration = 0.3 signUpView.animator().alphaValue = 1 }, completionHandler: nil) } } else { 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) } } } } // MARK: Bind Method func bindViewModel() -> Void { viewModel.$isVisible .receive(on: RunLoop.main) .sink { [weak self] newValue in self?.visibleStateChange() } .store(in: &cancellables) viewModel.$passwordErrorMessage .receive(on: RunLoop.main) .sink { [weak self] newValue in self?.passwordErrorLabel.stringValue = newValue self?.passwordErrorLabel.isHidden = false } .store(in: &cancellables) viewModel.$password .receive(on: RunLoop.main) .sink { [weak self] newValue in if newValue.count <= 30 && newValue.count >= 0 { self?.viewModel.passwordErrorMessage = "" } else { self?.viewModel.passwordErrorMessage = NSLocalizedString("Password error.", tableName: "MemberCenterLocalizable", comment: "") } } .store(in: &cancellables) } // MARK: Action Method @IBAction func visibleAction(_ sender: NSButton) { viewModel.isVisible.toggle() } @IBAction func finishAction(_ sender: NSButton) { viewModel.passwordErrorMessage = "" viewModel.resetPassword() { [weak self] success, msg in guard let self = self else { return } if success { self.skipSignUpView() } else { DispatchQueue.main.async { let alert = NSAlert() alert.messageText = NSLocalizedString(msg, comment: "") alert.addButton(withTitle: NSLocalizedString("OK", comment: "")) let result = alert.runModal() if (result == .alertFirstButtonReturn) { self.skipSignUpView() } } } } } } extension KMEnterNewPasswordView: NSTextFieldDelegate { func controlTextDidEndEditing(_ obj: Notification) { let textField = obj.object as? NSTextField if textField == passwordTextField { viewModel.password = textField!.stringValue } else if textField == passwordTextField1 { viewModel.password = textField!.stringValue } } func controlTextDidChange(_ obj: Notification) { let textField = obj.object as? NSTextField if textField == passwordTextField { viewModel.password = textField!.stringValue } else if textField == passwordTextField1 { viewModel.password = textField!.stringValue } } }