KMForgotPasswordView.swift 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. viewModel.screenType = .forgotPassword
  23. bindViewModel()
  24. languageLocalized()
  25. initializeUI()
  26. }
  27. public override init(frame frameRect: NSRect) {
  28. super.init(frame: frameRect)
  29. }
  30. public required init?(coder decoder: NSCoder) {
  31. fatalError("init(coder:) has not been implemented")
  32. }
  33. override func updateUI() {
  34. super.updateUI()
  35. bindViewModel()
  36. languageLocalized()
  37. initializeUI()
  38. }
  39. // MARK: Private Method
  40. private func languageLocalized() -> Void {
  41. resetPasswordsLabel.stringValue = NSLocalizedString("Reset Password", tableName: "MemberCenterLocalizable", comment: "")
  42. emailTextField.placeholderString = NSLocalizedString("Please enter email address", tableName: "MemberCenterLocalizable", comment: "")
  43. emailErrorLabel.stringValue = String(format: "*%@", NSLocalizedString("Email format error. Please enter the correct email.", tableName: "MemberCenterLocalizable", comment: ""))
  44. nextButton.title = NSLocalizedString("Next", tableName: "MemberCenterLocalizable", comment: "")
  45. backButton.title = NSLocalizedString("Previous Step", tableName: "MemberCenterLocalizable", comment: "")
  46. }
  47. private func initializeUI() -> Void {
  48. emailTextField.delegate = self
  49. emailBox.fillColor = NSColor(named: "texefiedfillcolor") ?? NSColor.white
  50. emailBox.borderColor = NSColor(named: "DADBDE") ?? NSColor.gray
  51. resetPasswordsLabel.textColor = NSColor(named: "000000")
  52. resetPasswordsLabel.font = NSFont.SFMediumFontWithSize(20)
  53. emailTextField.stringValue = viewModel.email
  54. emailErrorLabel.isHidden = viewModel.emailError()
  55. emailErrorLabel.textColor = NSColor(named: "FA1E5D")
  56. emailErrorLabel.font = NSFont.SFProTextRegularFont(9)
  57. nextBox.fillColor = NSColor(named: "273C62") ?? NSColor.blue
  58. nextButton.setTitleColor(color: NSColor(named: "FFFFFF") ?? NSColor.white, font: NSFont.SFProTextRegularFont(13))
  59. backButton.setTitleColor(color: NSColor(named: "4982E6") ?? NSColor.blue, font: NSFont.SFProTextRegularFont(12))
  60. }
  61. private func textfieldInputState() -> Void {
  62. if viewModel.emailError() {
  63. emailBox.borderColor = NSColor(named: "FA1E5D") ?? NSColor.red
  64. } else {
  65. emailBox.borderColor = NSColor(named: "DADBDE") ?? NSColor.gray
  66. }
  67. emailErrorLabel.isHidden = !viewModel.emailError()
  68. }
  69. private func skipSignUpView() -> Void {
  70. guard let parentView = self.superview else { return }
  71. if parentView is NSBox {
  72. let model = KMSignUpViewModel()
  73. model.email = viewModel.email
  74. let signUpView = KMSignUpView(model: model, superView: parentView)
  75. NSAnimationContext.runAnimationGroup { context in
  76. context.duration = 0.3
  77. self.animator().alphaValue = 0
  78. } completionHandler: {
  79. self.removeFromSuperview()
  80. signUpView.alphaValue = 0
  81. (parentView as! NSBox).contentView = signUpView
  82. NSAnimationContext.runAnimationGroup({ context in
  83. context.duration = 0.3
  84. signUpView.animator().alphaValue = 1
  85. }, completionHandler: nil)
  86. }
  87. } else {
  88. guard let parentView = self.superview else { return }
  89. let model = KMSignUpViewModel()
  90. model.email = viewModel.email
  91. let signUpView = KMSignUpView(model: model, superView: parentView)
  92. NSAnimationContext.runAnimationGroup { context in
  93. context.duration = 0.3
  94. self.animator().alphaValue = 0
  95. } completionHandler: {
  96. self.removeFromSuperview()
  97. signUpView.alphaValue = 0
  98. parentView.addSubview(signUpView)
  99. NSAnimationContext.runAnimationGroup({ context in
  100. context.duration = 0.3
  101. signUpView.animator().alphaValue = 1
  102. }, completionHandler: nil)
  103. }
  104. }
  105. }
  106. private func skipEnterVerificationCodeView() -> Void {
  107. guard let parentView = self.superview else { return }
  108. if parentView is NSBox {
  109. let model = KMSignUpViewModel()
  110. model.email = viewModel.email
  111. let codeView = KMEnterVerificationCodeView(model: model, superView: parentView)
  112. NSAnimationContext.runAnimationGroup { context in
  113. context.duration = 0.3
  114. self.animator().alphaValue = 0
  115. } completionHandler: {
  116. self.removeFromSuperview()
  117. codeView.alphaValue = 0
  118. (parentView as! NSBox).contentView = codeView
  119. NSAnimationContext.runAnimationGroup({ context in
  120. context.duration = 0.3
  121. codeView.animator().alphaValue = 1
  122. }, completionHandler: nil)
  123. }
  124. } else {
  125. guard let parentView = self.superview else { return }
  126. let model = KMSignUpViewModel()
  127. model.email = viewModel.email
  128. let codeView = KMEnterVerificationCodeView(model: viewModel, superView: parentView)
  129. NSAnimationContext.runAnimationGroup { context in
  130. context.duration = 0.3
  131. self.animator().alphaValue = 0
  132. } completionHandler: {
  133. self.removeFromSuperview()
  134. codeView.alphaValue = 0
  135. parentView.addSubview(codeView)
  136. NSAnimationContext.runAnimationGroup({ context in
  137. context.duration = 0.3
  138. codeView.animator().alphaValue = 1
  139. }, completionHandler: nil)
  140. }
  141. }
  142. }
  143. // MARK: Bind Method
  144. func bindViewModel() -> Void {
  145. viewModel.$emailErrorMessage
  146. .receive(on: RunLoop.main)
  147. .sink { [weak self] newValue in
  148. self?.emailErrorLabel.stringValue = newValue
  149. self?.emailErrorLabel.isHidden = false
  150. self?.textfieldInputState()
  151. }
  152. .store(in: &cancellables)
  153. }
  154. // MARK: Action Method
  155. @IBAction func nextButtonAction(_ sender: NSButton) {
  156. if viewModel.email.count <= 0 || viewModel.email.count > 100 || !viewModel.isValidEmail() {
  157. viewModel.emailErrorMessage = NSLocalizedString("Email format error. Please enter the correct email.", tableName: "MemberCenterLocalizable", comment: "")
  158. return
  159. }
  160. viewModel.emailErrorMessage = ""
  161. window?.showWaitingView()
  162. viewModel.forgotPasswordNextAction() { [weak self] success, msg in
  163. self?.window?.hideWaitingView()
  164. guard let self = self else { return }
  165. if success {
  166. self.skipEnterVerificationCodeView()
  167. } else {
  168. let alert = NSAlert()
  169. 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: "")
  170. alert.addButton(withTitle: "OK")
  171. let response = alert.runModal()
  172. if response == .alertFirstButtonReturn {
  173. self.skipSignUpView()
  174. }
  175. }
  176. }
  177. }
  178. @IBAction func backButtonAction(_ sender: NSButton) {
  179. skipSignUpView()
  180. }
  181. }
  182. extension KMForgotPasswordView: NSTextFieldDelegate {
  183. func controlTextDidEndEditing(_ obj: Notification) {
  184. let textField = obj.object as? NSTextField
  185. if textField == emailTextField {
  186. viewModel.email = textField!.stringValue
  187. }
  188. }
  189. func controlTextDidChange(_ obj: Notification) {
  190. let textField = obj.object as? NSTextField
  191. if textField == emailTextField {
  192. viewModel.email = textField!.stringValue
  193. viewModel.emailErrorMessage = ""
  194. emailBox.borderColor = NSColor(named: "DADBDE") ?? NSColor.gray
  195. }
  196. }
  197. }