KMForgotPasswordView.swift 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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(sendCode: Bool) -> Void {
  70. guard let parentView = self.superview else { return }
  71. if parentView is NSBox {
  72. let model = KMSignUpViewModel()
  73. model.sendCode = sendCode
  74. model.email = viewModel.email
  75. let signUpView = KMSignUpView(model: model, superView: parentView)
  76. NSAnimationContext.runAnimationGroup { context in
  77. context.duration = 0.3
  78. self.animator().alphaValue = 0
  79. } completionHandler: {
  80. self.removeFromSuperview()
  81. signUpView.alphaValue = 0
  82. (parentView as! NSBox).contentView = signUpView
  83. NSAnimationContext.runAnimationGroup({ context in
  84. context.duration = 0.3
  85. signUpView.animator().alphaValue = 1
  86. }, completionHandler: nil)
  87. }
  88. } else {
  89. guard let parentView = self.superview else { return }
  90. let model = KMSignUpViewModel()
  91. model.email = viewModel.email
  92. let signUpView = KMSignUpView(model: model, superView: parentView)
  93. NSAnimationContext.runAnimationGroup { context in
  94. context.duration = 0.3
  95. self.animator().alphaValue = 0
  96. } completionHandler: {
  97. self.removeFromSuperview()
  98. signUpView.alphaValue = 0
  99. parentView.addSubview(signUpView)
  100. NSAnimationContext.runAnimationGroup({ context in
  101. context.duration = 0.3
  102. signUpView.animator().alphaValue = 1
  103. }, completionHandler: nil)
  104. }
  105. }
  106. }
  107. private func skipEnterVerificationCodeView() -> Void {
  108. guard let parentView = self.superview else { return }
  109. if parentView is NSBox {
  110. let model = KMSignUpViewModel()
  111. model.email = viewModel.email
  112. let codeView = KMEnterVerificationCodeView(model: model, superView: parentView)
  113. NSAnimationContext.runAnimationGroup { context in
  114. context.duration = 0.3
  115. self.animator().alphaValue = 0
  116. } completionHandler: {
  117. self.removeFromSuperview()
  118. codeView.alphaValue = 0
  119. (parentView as! NSBox).contentView = codeView
  120. NSAnimationContext.runAnimationGroup({ context in
  121. context.duration = 0.3
  122. codeView.animator().alphaValue = 1
  123. }, completionHandler: nil)
  124. }
  125. } else {
  126. guard let parentView = self.superview else { return }
  127. let model = KMSignUpViewModel()
  128. model.email = viewModel.email
  129. let codeView = KMEnterVerificationCodeView(model: viewModel, superView: parentView)
  130. NSAnimationContext.runAnimationGroup { context in
  131. context.duration = 0.3
  132. self.animator().alphaValue = 0
  133. } completionHandler: {
  134. self.removeFromSuperview()
  135. codeView.alphaValue = 0
  136. parentView.addSubview(codeView)
  137. NSAnimationContext.runAnimationGroup({ context in
  138. context.duration = 0.3
  139. codeView.animator().alphaValue = 1
  140. }, completionHandler: nil)
  141. }
  142. }
  143. }
  144. // MARK: Bind Method
  145. func bindViewModel() -> Void {
  146. viewModel.$emailErrorMessage
  147. .receive(on: RunLoop.main)
  148. .sink { [weak self] newValue in
  149. self?.emailErrorLabel.stringValue = newValue
  150. self?.emailErrorLabel.isHidden = false
  151. self?.textfieldInputState()
  152. }
  153. .store(in: &cancellables)
  154. }
  155. // MARK: Action Method
  156. @IBAction func nextButtonAction(_ sender: NSButton) {
  157. if viewModel.email.count <= 0 || viewModel.email.count > 100 || !viewModel.isValidEmail() {
  158. viewModel.emailErrorMessage = NSLocalizedString("Email format error. Please enter the correct email.", tableName: "MemberCenterLocalizable", comment: "")
  159. return
  160. }
  161. viewModel.emailErrorMessage = ""
  162. window?.showWaitingView()
  163. viewModel.forgotPasswordNextAction() { [weak self] success, msg in
  164. self?.window?.hideWaitingView()
  165. guard let self = self else { return }
  166. if success {
  167. self.skipEnterVerificationCodeView()
  168. } else {
  169. DispatchQueue.main.async {
  170. let alert = NSAlert()
  171. 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: "")
  172. alert.addButton(withTitle: "OK")
  173. let response = alert.runModal()
  174. if response == .alertFirstButtonReturn {
  175. self.skipSignUpView(sendCode: true)
  176. }
  177. }
  178. }
  179. }
  180. }
  181. @IBAction func backButtonAction(_ sender: NSButton) {
  182. skipSignUpView(sendCode: false)
  183. }
  184. }
  185. extension KMForgotPasswordView: NSTextFieldDelegate {
  186. func controlTextDidEndEditing(_ obj: Notification) {
  187. let textField = obj.object as? NSTextField
  188. if textField == emailTextField {
  189. viewModel.email = textField!.stringValue
  190. }
  191. }
  192. func controlTextDidChange(_ obj: Notification) {
  193. let textField = obj.object as? NSTextField
  194. if textField == emailTextField {
  195. viewModel.email = textField!.stringValue
  196. viewModel.emailErrorMessage = ""
  197. emailBox.borderColor = NSColor(named: "DADBDE") ?? NSColor.gray
  198. }
  199. }
  200. }