KMEnterVerificationCodeView.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. //
  2. // KMEnterVerificationCodeView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by wanjun on 2024/10/29.
  6. //
  7. /**
  8. 重置密码(输入验证码)
  9. */
  10. import Cocoa
  11. import Combine
  12. class KMEnterVerificationCodeView: KMBaseXibView {
  13. @IBOutlet weak var resetPasswordsLabel: NSTextField!
  14. @IBOutlet weak var tipLabel: NSTextField!
  15. @IBOutlet weak var verifficationView: NSView!
  16. @IBOutlet weak var verifficationBox: NSBox!
  17. @IBOutlet weak var verifficationTextField: NSTextField!
  18. @IBOutlet weak var sendBox: KMBox!
  19. @IBOutlet weak var sendLabel: NSTextField!
  20. @IBOutlet weak var verifficationErrorLabel: NSTextField!
  21. @IBOutlet weak var nextBox: NSBox!
  22. @IBOutlet weak var nextButton: NSButton!
  23. @IBOutlet weak var backButton: NSButton!
  24. private var viewModel = KMSignUpViewModel()
  25. private var cancellables = Set<AnyCancellable>()
  26. convenience init(model: KMSignUpViewModel, superView: NSView) {
  27. self.init(frame: superView.bounds)
  28. viewModel = model
  29. loadUI()
  30. }
  31. public override init(frame frameRect: NSRect) {
  32. super.init(frame: frameRect)
  33. }
  34. public required init?(coder decoder: NSCoder) {
  35. fatalError("init(coder:) has not been implemented")
  36. }
  37. override func updateUI() {
  38. super.updateUI()
  39. loadUI()
  40. }
  41. // MARK: Private Method
  42. private func loadUI() -> Void {
  43. viewModel.sendContent = "60"
  44. bindViewModel()
  45. languageLocalized()
  46. initializeUI()
  47. sendBoxRefresh()
  48. viewModel.countDown(type: .reset)
  49. sendBox.moveCallback = { [weak self](mouseEntered: Bool, mouseBox: KMBox) -> Void in
  50. guard let self = self else { return }
  51. if self.viewModel.email.count <= 0 { return }
  52. if self.viewModel.sendBoxSelect { return }
  53. if mouseEntered {
  54. self.sendBox.fillColor = NSColor(named: "000000_0.1") ?? NSColor.blue
  55. self.sendBox.borderWidth = 1
  56. } else {
  57. self.sendBox.fillColor = NSColor(named: "273C62") ?? NSColor.blue
  58. }
  59. }
  60. sendBox.downCallback = { [weak self](downEntered: Bool, mouseBox: KMBox, event) -> Void in
  61. guard let self = self else { return }
  62. if self.viewModel.email.count <= 0 { return }
  63. if self.viewModel.sendBoxSelect { return }
  64. if downEntered {
  65. self.sendBox.fillColor = NSColor(named: "273C62_0.4") ?? NSColor.blue
  66. viewModel.countDown(type: .reset)
  67. }
  68. }
  69. }
  70. private func languageLocalized() -> Void {
  71. resetPasswordsLabel.stringValue = NSLocalizedString("Reset Passwords", tableName: "MemberCenterLocalizable", comment: "")
  72. tipLabel.stringValue = String(format: "%@%@", NSLocalizedString("验证码已通过邮件发送至", tableName: "MemberCenterLocalizable", comment: ""), viewModel.email)
  73. verifficationTextField.placeholderString = NSLocalizedString("Enter Verification code", tableName: "MemberCenterLocalizable", comment: "")
  74. verifficationErrorLabel.stringValue = viewModel.passwordErrorMessage
  75. nextButton.title = NSLocalizedString("Next", tableName: "MemberCenterLocalizable", comment: "")
  76. backButton.title = NSLocalizedString("Back", tableName: "MemberCenterLocalizable", comment: "")
  77. }
  78. private func initializeUI() -> Void {
  79. verifficationTextField.delegate = self
  80. resetPasswordsLabel.textColor = NSColor(named: "000000")
  81. resetPasswordsLabel.font = NSFont.SFMediumFontWithSize(20)
  82. tipLabel.textColor = NSColor(named: "0E1114")
  83. tipLabel.font = NSFont.SFProTextRegularFont(12)
  84. sendLabel.textColor = NSColor(named: "FFFFFF") ?? NSColor.white
  85. sendLabel.font = NSFont.SFProTextRegularFont(16)
  86. verifficationErrorLabel.isHidden = viewModel.passwordError()
  87. verifficationErrorLabel.textColor = NSColor(named: "FA1E5D")
  88. verifficationErrorLabel.font = NSFont.SFProTextRegularFont(9)
  89. nextBox.fillColor = NSColor(named: "273C62") ?? NSColor.blue
  90. nextButton.setTitleColor(color: NSColor(named: "FFFFFF") ?? NSColor.white, font: NSFont.SFProTextRegularFont(16))
  91. backButton.setTitleColor(color: NSColor(named: "4982E6") ?? NSColor.blue, font: NSFont.SFProTextRegularFont(12))
  92. }
  93. private func sendBoxRefresh() -> Void {
  94. sendLabel.stringValue = viewModel.sendContent
  95. if viewModel.sendContent == NSLocalizedString("Send", tableName: "MemberCenterLocalizable", comment: "") ||
  96. viewModel.sendContent == NSLocalizedString("Resend", tableName: "MemberCenterLocalizable", comment: "") {
  97. if viewModel.email.count > 0 {
  98. if viewModel.isValidEmail() {
  99. sendBox.fillColor = NSColor(named: "273C62") ?? NSColor.blue
  100. } else {
  101. sendBox.fillColor = NSColor(named: "273C62_0.4") ?? NSColor.blue
  102. }
  103. } else {
  104. sendBox.fillColor = NSColor(named: "273C62_0.4") ?? NSColor.blue
  105. }
  106. sendLabel.textColor = NSColor(named: "FFFFFF") ?? NSColor.white
  107. } else {
  108. sendBox.fillColor = NSColor(named: "DADBDE") ?? NSColor.gray
  109. sendLabel.stringValue = String(format: "%@s", viewModel.sendContent)
  110. sendLabel.textColor = NSColor(named: "0E1114") ?? NSColor.black
  111. }
  112. }
  113. private func skipEnterNewPasswordView() -> Void {
  114. guard let parentView = self.superview else { return }
  115. if parentView is NSBox {
  116. let codeView = KMEnterNewPasswordView(model: viewModel, superView: parentView)
  117. NSAnimationContext.runAnimationGroup { context in
  118. context.duration = 0.3
  119. self.animator().alphaValue = 0
  120. } completionHandler: {
  121. self.removeFromSuperview()
  122. codeView.alphaValue = 0
  123. (parentView as! NSBox).contentView = codeView
  124. NSAnimationContext.runAnimationGroup({ context in
  125. context.duration = 0.3
  126. codeView.animator().alphaValue = 1
  127. }, completionHandler: nil)
  128. }
  129. } else {
  130. guard let parentView = self.superview else { return }
  131. let codeView = KMEnterNewPasswordView(model: viewModel, superView: parentView)
  132. NSAnimationContext.runAnimationGroup { context in
  133. context.duration = 0.3
  134. self.animator().alphaValue = 0
  135. } completionHandler: {
  136. self.removeFromSuperview()
  137. codeView.alphaValue = 0
  138. parentView.addSubview(codeView)
  139. NSAnimationContext.runAnimationGroup({ context in
  140. context.duration = 0.3
  141. codeView.animator().alphaValue = 1
  142. }, completionHandler: nil)
  143. }
  144. }
  145. }
  146. // MARK: Bind Method
  147. func bindViewModel() -> Void {
  148. viewModel.$sendContent
  149. .receive(on: RunLoop.main)
  150. .sink { [weak self] newValue in
  151. self?.sendBoxRefresh()
  152. }
  153. .store(in: &cancellables)
  154. viewModel.$passwordErrorMessage
  155. .receive(on: RunLoop.main)
  156. .sink { [weak self] newValue in
  157. self?.verifficationErrorLabel.stringValue = newValue
  158. if self?.viewModel.passwordErrorMessage == "" {
  159. self?.verifficationErrorLabel.isHidden = true
  160. } else {
  161. self?.verifficationErrorLabel.isHidden = false
  162. }
  163. }
  164. .store(in: &cancellables)
  165. viewModel.$verificationCode
  166. .receive(on: RunLoop.main)
  167. .sink { [weak self] newValue in
  168. if newValue.count <= 6 && newValue.count >= 0 {
  169. self?.viewModel.passwordErrorMessage = ""
  170. } else {
  171. self?.viewModel.passwordErrorMessage = NSLocalizedString("*Please enter right Verification code", tableName: "MemberCenterLocalizable", comment: "")
  172. }
  173. }
  174. .store(in: &cancellables)
  175. }
  176. // MARK: Action Method
  177. @IBAction func nextButtonAction(_ sender: NSButton) {
  178. if viewModel.verificationCode.count <= 0 || viewModel.verificationCode.count > 6 || !viewModel.isValidVerificationCode() {
  179. viewModel.passwordErrorMessage = NSLocalizedString("Please enter the correct email format", tableName: "MemberCenterLocalizable", comment: "")
  180. return
  181. }
  182. viewModel.passwordErrorMessage = ""
  183. viewModel.enterVerificationCodeNextAction() { [weak self] success, msg in
  184. guard let self = self else { return }
  185. if success {
  186. self.skipEnterNewPasswordView()
  187. }
  188. }
  189. }
  190. @IBAction func backButtonAction(_ sender: NSButton) {
  191. guard let parentView = self.superview else { return }
  192. if parentView is NSBox {
  193. let model = KMSignUpViewModel()
  194. model.email = viewModel.email
  195. let forgotView = KMForgotPasswordView(model: model, superView: parentView)
  196. NSAnimationContext.runAnimationGroup { context in
  197. context.duration = 0.3
  198. self.animator().alphaValue = 0
  199. } completionHandler: {
  200. self.removeFromSuperview()
  201. forgotView.alphaValue = 0
  202. (parentView as! NSBox).contentView = forgotView
  203. NSAnimationContext.runAnimationGroup({ context in
  204. context.duration = 0.3
  205. forgotView.animator().alphaValue = 1
  206. }, completionHandler: nil)
  207. }
  208. } else {
  209. let model = KMSignUpViewModel()
  210. model.email = viewModel.email
  211. let forgotView = KMForgotPasswordView(model: model, superView: parentView)
  212. NSAnimationContext.runAnimationGroup { context in
  213. context.duration = 0.3
  214. self.animator().alphaValue = 0
  215. } completionHandler: {
  216. self.removeFromSuperview()
  217. forgotView.alphaValue = 0
  218. parentView.addSubview(forgotView)
  219. NSAnimationContext.runAnimationGroup({ context in
  220. context.duration = 0.3
  221. forgotView.animator().alphaValue = 1
  222. }, completionHandler: nil)
  223. }
  224. }
  225. }
  226. }
  227. extension KMEnterVerificationCodeView: NSTextFieldDelegate {
  228. func controlTextDidEndEditing(_ obj: Notification) {
  229. let textField = obj.object as? NSTextField
  230. if textField == verifficationTextField {
  231. viewModel.verificationCode = textField!.stringValue
  232. }
  233. }
  234. func controlTextDidChange(_ obj: Notification) {
  235. let textField = obj.object as? NSTextField
  236. if textField == verifficationTextField {
  237. viewModel.verificationCode = textField!.stringValue
  238. }
  239. }
  240. }