KMEnterVerificationCodeView.swift 12 KB

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