KMEnterVerificationCodeView.swift 11 KB

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