123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- //
- // 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<AnyCancellable>()
-
- 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(sendCode: Bool) -> Void {
- guard let parentView = self.superview else { return }
- if parentView is NSBox {
- let model = KMSignUpViewModel()
- model.sendCode = sendCode
- 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 = ""
- window?.showWaitingView()
- viewModel.forgotPasswordNextAction() { [weak self] success, msg in
- self?.window?.hideWaitingView()
-
- guard let self = self else { return }
- if success {
- self.skipEnterVerificationCodeView()
- } else {
- DispatchQueue.main.async {
- 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(sendCode: true)
- }
- }
- }
- }
- }
-
- @IBAction func backButtonAction(_ sender: NSButton) {
- skipSignUpView(sendCode: false)
- }
- }
- 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
- }
- }
- }
|