KMForgotPasswordView.swift 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. //
  2. // KMForgotPasswordView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by wanjun on 2024/10/28.
  6. //
  7. import Cocoa
  8. import Combine
  9. class KMForgotPasswordView: KMBaseXibView {
  10. @IBOutlet weak var resetPasswordsLabel: NSTextField!
  11. @IBOutlet weak var emailBox: NSBox!
  12. @IBOutlet weak var emailTextField: NSTextField!
  13. @IBOutlet weak var nextBox: NSBox!
  14. @IBOutlet weak var nextButton: NSButton!
  15. @IBOutlet weak var backButton: NSButton!
  16. @IBOutlet weak var emailErrorLabel: NSTextField!
  17. private var viewModel = KMSignUpViewModel()
  18. private var cancellables = Set<AnyCancellable>()
  19. convenience init(model: KMSignUpViewModel, superView: NSView) {
  20. self.init(frame: superView.bounds)
  21. viewModel = model
  22. bindViewModel()
  23. languageLocalized()
  24. initializeUI()
  25. }
  26. public override init(frame frameRect: NSRect) {
  27. super.init(frame: frameRect)
  28. }
  29. public required init?(coder decoder: NSCoder) {
  30. fatalError("init(coder:) has not been implemented")
  31. }
  32. override func updateUI() {
  33. super.updateUI()
  34. bindViewModel()
  35. languageLocalized()
  36. initializeUI()
  37. }
  38. // MARK: Private Method
  39. private func languageLocalized() -> Void {
  40. resetPasswordsLabel.stringValue = NSLocalizedString("Reset Passwords", tableName: "MemberCenterLocalizable", comment: "")
  41. emailTextField.placeholderString = NSLocalizedString("Please enter Email", tableName: "MemberCenterLocalizable", comment: "")
  42. emailErrorLabel.stringValue = String(format: "*%@", NSLocalizedString("Please enter right Address", tableName: "MemberCenterLocalizable", comment: ""))
  43. nextButton.title = NSLocalizedString("Next", tableName: "MemberCenterLocalizable", comment: "")
  44. backButton.title = NSLocalizedString("Back", tableName: "MemberCenterLocalizable", comment: "")
  45. }
  46. private func initializeUI() -> Void {
  47. emailTextField.delegate = self
  48. resetPasswordsLabel.textColor = NSColor(named: "000000")
  49. resetPasswordsLabel.font = NSFont.SFMediumFontWithSize(20)
  50. emailTextField.stringValue = viewModel.email
  51. emailErrorLabel.isHidden = viewModel.emailError()
  52. emailErrorLabel.textColor = NSColor(named: "FA1E5D")
  53. emailErrorLabel.font = NSFont.SFProTextRegularFont(9)
  54. nextBox.fillColor = NSColor(named: "273C62") ?? NSColor.blue
  55. nextButton.setTitleColor(color: NSColor(named: "FFFFFF") ?? NSColor.white, font: NSFont.SFProTextRegularFont(16))
  56. backButton.setTitleColor(color: NSColor(named: "4982E6") ?? NSColor.blue, font: NSFont.SFProTextRegularFont(12))
  57. }
  58. private func textfieldInputState() -> Void {
  59. if viewModel.emailError() {
  60. emailBox.borderColor = NSColor(named: "FA1E5D") ?? NSColor.red
  61. } else {
  62. emailBox.borderColor = NSColor(named: "DADBDE") ?? NSColor.gray
  63. }
  64. emailErrorLabel.isHidden = !viewModel.emailError()
  65. }
  66. private func skipSignUpView() -> Void {
  67. guard let parentView = self.superview else { return }
  68. if parentView is NSBox {
  69. let model = KMSignUpViewModel()
  70. model.email = viewModel.email
  71. let signUpView = KMSignUpView(model: model, superView: parentView)
  72. NSAnimationContext.runAnimationGroup { context in
  73. context.duration = 0.3
  74. self.animator().alphaValue = 0
  75. } completionHandler: {
  76. self.removeFromSuperview()
  77. signUpView.alphaValue = 0
  78. (parentView as! NSBox).contentView = signUpView
  79. NSAnimationContext.runAnimationGroup({ context in
  80. context.duration = 0.3
  81. signUpView.animator().alphaValue = 1
  82. }, completionHandler: nil)
  83. }
  84. } else {
  85. guard let parentView = self.superview else { return }
  86. let model = KMSignUpViewModel()
  87. model.email = viewModel.email
  88. let signUpView = KMSignUpView(model: model, superView: parentView)
  89. NSAnimationContext.runAnimationGroup { context in
  90. context.duration = 0.3
  91. self.animator().alphaValue = 0
  92. } completionHandler: {
  93. self.removeFromSuperview()
  94. signUpView.alphaValue = 0
  95. parentView.addSubview(signUpView)
  96. NSAnimationContext.runAnimationGroup({ context in
  97. context.duration = 0.3
  98. signUpView.animator().alphaValue = 1
  99. }, completionHandler: nil)
  100. }
  101. }
  102. }
  103. private func skipEnterVerificationCodeView() -> Void {
  104. guard let parentView = self.superview else { return }
  105. if parentView is NSBox {
  106. let model = KMSignUpViewModel()
  107. model.email = viewModel.email
  108. let codeView = KMEnterVerificationCodeView(model: model, superView: parentView)
  109. NSAnimationContext.runAnimationGroup { context in
  110. context.duration = 0.3
  111. self.animator().alphaValue = 0
  112. } completionHandler: {
  113. self.removeFromSuperview()
  114. codeView.alphaValue = 0
  115. (parentView as! NSBox).contentView = codeView
  116. NSAnimationContext.runAnimationGroup({ context in
  117. context.duration = 0.3
  118. codeView.animator().alphaValue = 1
  119. }, completionHandler: nil)
  120. }
  121. } else {
  122. guard let parentView = self.superview else { return }
  123. let model = KMSignUpViewModel()
  124. model.email = viewModel.email
  125. let codeView = KMEnterVerificationCodeView(model: viewModel, superView: parentView)
  126. NSAnimationContext.runAnimationGroup { context in
  127. context.duration = 0.3
  128. self.animator().alphaValue = 0
  129. } completionHandler: {
  130. self.removeFromSuperview()
  131. codeView.alphaValue = 0
  132. parentView.addSubview(codeView)
  133. NSAnimationContext.runAnimationGroup({ context in
  134. context.duration = 0.3
  135. codeView.animator().alphaValue = 1
  136. }, completionHandler: nil)
  137. }
  138. }
  139. }
  140. // MARK: Bind Method
  141. func bindViewModel() -> Void {
  142. viewModel.$emailErrorMessage
  143. .receive(on: RunLoop.main)
  144. .sink { [weak self] newValue in
  145. self?.emailErrorLabel.stringValue = newValue
  146. self?.emailErrorLabel.isHidden = false
  147. }
  148. .store(in: &cancellables)
  149. }
  150. // MARK: Action Method
  151. @IBAction func nextButtonAction(_ sender: NSButton) {
  152. if viewModel.email.count <= 0 || viewModel.email.count > 100 || !viewModel.isValidEmail() {
  153. viewModel.emailErrorMessage = NSLocalizedString("Please enter the correct email format", tableName: "MemberCenterLocalizable", comment: "")
  154. return
  155. }
  156. viewModel.emailErrorMessage = ""
  157. viewModel.forgotPasswordNextAction() { [weak self] success, msg in
  158. guard let self = self else { return }
  159. if success {
  160. self.skipEnterVerificationCodeView()
  161. } else {
  162. let alert = NSAlert()
  163. alert.messageText = NSLocalizedString("该邮箱还未注册,请先通过账号验证码登录,我们将自动为您注册账号", tableName: "MemberCenterLocalizable", comment: "")
  164. alert.addButton(withTitle: "OK")
  165. let response = alert.runModal()
  166. if response == .alertFirstButtonReturn {
  167. self.skipSignUpView()
  168. }
  169. }
  170. }
  171. }
  172. @IBAction func backButtonAction(_ sender: NSButton) {
  173. skipSignUpView()
  174. }
  175. }
  176. extension KMForgotPasswordView: NSTextFieldDelegate {
  177. func controlTextDidEndEditing(_ obj: Notification) {
  178. let textField = obj.object as? NSTextField
  179. if textField == emailTextField {
  180. viewModel.email = textField!.stringValue
  181. }
  182. }
  183. func controlTextDidChange(_ obj: Notification) {
  184. let textField = obj.object as? NSTextField
  185. if textField == emailTextField {
  186. viewModel.email = textField!.stringValue
  187. }
  188. }
  189. }