KMEnterVerificationCodeView.swift 12 KB

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