123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- //
- // 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<AnyCancellable>()
-
- 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 {
- 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
- }
- }
- }
|