KMEnterVerificationCodeView.swift 12 KB

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