KMEnterVerificationCodeView.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 Password", tableName: "MemberCenterLocalizable", comment: "")
  72. tipLabel.stringValue = String(format: "%@%@", NSLocalizedString("We have sent you a code via email to", tableName: "MemberCenterLocalizable", comment: ""), viewModel.email)
  73. verifficationTextField.placeholderString = NSLocalizedString("Please enter code", tableName: "MemberCenterLocalizable", comment: "")
  74. verifficationErrorLabel.stringValue = viewModel.passwordErrorMessage
  75. nextButton.title = NSLocalizedString("Next", tableName: "MemberCenterLocalizable", comment: "")
  76. backButton.title = NSLocalizedString("Previous Step", tableName: "MemberCenterLocalizable", comment: "")
  77. }
  78. private func initializeUI() -> Void {
  79. verifficationBox.fillColor = NSColor(named: "texefiedfillcolor") ?? NSColor.white
  80. verifficationBox.borderColor = NSColor(named: "DADBDE") ?? NSColor.gray
  81. verifficationTextField.delegate = self
  82. resetPasswordsLabel.textColor = NSColor(named: "000000")
  83. resetPasswordsLabel.font = NSFont.SFMediumFontWithSize(20)
  84. tipLabel.textColor = NSColor(named: "0E1114")
  85. tipLabel.font = NSFont.SFProTextRegularFont(12)
  86. sendLabel.textColor = NSColor(named: "FFFFFF") ?? NSColor.white
  87. sendLabel.font = NSFont.SFProTextRegularFont(13)
  88. verifficationErrorLabel.isHidden = viewModel.passwordError()
  89. verifficationErrorLabel.textColor = NSColor(named: "FA1E5D")
  90. verifficationErrorLabel.font = NSFont.SFProTextRegularFont(9)
  91. nextBox.fillColor = NSColor(named: "273C62") ?? NSColor.blue
  92. nextButton.setTitleColor(color: NSColor(named: "FFFFFF") ?? NSColor.white, font: NSFont.SFProTextRegularFont(13))
  93. backButton.setTitleColor(color: NSColor(named: "4982E6") ?? NSColor.blue, font: NSFont.SFProTextRegularFont(12))
  94. }
  95. private func sendBoxRefresh() -> Void {
  96. sendLabel.stringValue = viewModel.sendContent
  97. if viewModel.sendContent == NSLocalizedString("Send", tableName: "MemberCenterLocalizable", comment: "") ||
  98. viewModel.sendContent == NSLocalizedString("Resend", tableName: "MemberCenterLocalizable", comment: "") {
  99. if viewModel.email.count > 0 {
  100. if viewModel.isValidEmail() {
  101. sendBox.fillColor = NSColor(named: "273C62") ?? NSColor.blue
  102. } else {
  103. sendBox.fillColor = NSColor(named: "273C62_0.4") ?? NSColor.blue
  104. }
  105. } else {
  106. sendBox.fillColor = NSColor(named: "273C62_0.4") ?? NSColor.blue
  107. }
  108. sendLabel.textColor = NSColor(named: "FFFFFF") ?? NSColor.white
  109. } else {
  110. sendBox.fillColor = NSColor(named: "DADBDE") ?? NSColor.gray
  111. sendLabel.stringValue = String(format: "%@s", viewModel.sendContent)
  112. sendLabel.textColor = NSColor(named: "0E1114") ?? NSColor.black
  113. }
  114. }
  115. private func skipEnterNewPasswordView() -> Void {
  116. guard let parentView = self.superview else { return }
  117. if parentView is NSBox {
  118. let codeView = KMEnterNewPasswordView(model: viewModel, superView: parentView)
  119. NSAnimationContext.runAnimationGroup { context in
  120. context.duration = 0.3
  121. self.animator().alphaValue = 0
  122. } completionHandler: {
  123. self.removeFromSuperview()
  124. codeView.alphaValue = 0
  125. (parentView as! NSBox).contentView = codeView
  126. NSAnimationContext.runAnimationGroup({ context in
  127. context.duration = 0.3
  128. codeView.animator().alphaValue = 1
  129. }, completionHandler: nil)
  130. }
  131. } else {
  132. guard let parentView = self.superview else { return }
  133. let codeView = KMEnterNewPasswordView(model: viewModel, superView: parentView)
  134. NSAnimationContext.runAnimationGroup { context in
  135. context.duration = 0.3
  136. self.animator().alphaValue = 0
  137. } completionHandler: {
  138. self.removeFromSuperview()
  139. codeView.alphaValue = 0
  140. parentView.addSubview(codeView)
  141. NSAnimationContext.runAnimationGroup({ context in
  142. context.duration = 0.3
  143. codeView.animator().alphaValue = 1
  144. }, completionHandler: nil)
  145. }
  146. }
  147. }
  148. // MARK: Bind Method
  149. func bindViewModel() -> Void {
  150. viewModel.$sendContent
  151. .receive(on: RunLoop.main)
  152. .sink { [weak self] newValue in
  153. self?.sendBoxRefresh()
  154. }
  155. .store(in: &cancellables)
  156. viewModel.$passwordErrorMessage
  157. .receive(on: RunLoop.main)
  158. .sink { [weak self] newValue in
  159. self?.verifficationErrorLabel.stringValue = newValue
  160. if self?.viewModel.passwordErrorMessage.isEmpty == false {
  161. self?.verifficationErrorLabel.isHidden = true
  162. self?.verifficationBox.borderColor = NSColor(named: "FA1E5D") ?? NSColor.red
  163. } else {
  164. self?.verifficationErrorLabel.isHidden = false
  165. self?.verifficationBox.borderColor = NSColor(named: "DADBDE") ?? NSColor.red
  166. }
  167. }
  168. .store(in: &cancellables)
  169. viewModel.$verificationCode
  170. .receive(on: RunLoop.main)
  171. .sink { [weak self] newValue in
  172. if newValue.count <= 6 && newValue.count >= 0 {
  173. self?.viewModel.passwordErrorMessage = ""
  174. } else {
  175. self?.viewModel.passwordErrorMessage = NSLocalizedString("Verification code error.", tableName: "MemberCenterLocalizable", comment: "")
  176. }
  177. }
  178. .store(in: &cancellables)
  179. }
  180. // MARK: Action Method
  181. @IBAction func nextButtonAction(_ sender: NSButton) {
  182. if viewModel.verificationCode.count <= 0 || viewModel.verificationCode.count > 6 || !viewModel.isValidVerificationCode() {
  183. viewModel.passwordErrorMessage = NSLocalizedString("Verification code error.", tableName: "MemberCenterLocalizable", comment: "")
  184. return
  185. }
  186. viewModel.passwordErrorMessage = ""
  187. viewModel.enterVerificationCodeNextAction() { [weak self] success, msg in
  188. guard let self = self else { return }
  189. if success {
  190. self.skipEnterNewPasswordView()
  191. }
  192. }
  193. }
  194. @IBAction func backButtonAction(_ sender: NSButton) {
  195. guard let parentView = self.superview else { return }
  196. if parentView is NSBox {
  197. let model = KMSignUpViewModel()
  198. model.email = viewModel.email
  199. let forgotView = KMForgotPasswordView(model: model, superView: parentView)
  200. NSAnimationContext.runAnimationGroup { context in
  201. context.duration = 0.3
  202. self.animator().alphaValue = 0
  203. } completionHandler: {
  204. self.removeFromSuperview()
  205. forgotView.alphaValue = 0
  206. (parentView as! NSBox).contentView = forgotView
  207. NSAnimationContext.runAnimationGroup({ context in
  208. context.duration = 0.3
  209. forgotView.animator().alphaValue = 1
  210. }, completionHandler: nil)
  211. }
  212. } else {
  213. let model = KMSignUpViewModel()
  214. model.email = viewModel.email
  215. let forgotView = KMForgotPasswordView(model: model, superView: parentView)
  216. NSAnimationContext.runAnimationGroup { context in
  217. context.duration = 0.3
  218. self.animator().alphaValue = 0
  219. } completionHandler: {
  220. self.removeFromSuperview()
  221. forgotView.alphaValue = 0
  222. parentView.addSubview(forgotView)
  223. NSAnimationContext.runAnimationGroup({ context in
  224. context.duration = 0.3
  225. forgotView.animator().alphaValue = 1
  226. }, completionHandler: nil)
  227. }
  228. }
  229. }
  230. }
  231. extension KMEnterVerificationCodeView: NSTextFieldDelegate {
  232. func controlTextDidEndEditing(_ obj: Notification) {
  233. let textField = obj.object as? NSTextField
  234. if textField == verifficationTextField {
  235. viewModel.verificationCode = textField!.stringValue
  236. }
  237. }
  238. func controlTextDidChange(_ obj: Notification) {
  239. let textField = obj.object as? NSTextField
  240. if textField == verifficationTextField {
  241. viewModel.verificationCode = textField!.stringValue
  242. viewModel.passwordErrorMessage = ""
  243. }
  244. }
  245. }