KMEnterNewPasswordView.swift 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. //
  2. // KMEnterNewPasswordView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by wanjun on 2024/10/29.
  6. //
  7. import Cocoa
  8. import Combine
  9. class KMEnterNewPasswordView: KMBaseXibView {
  10. @IBOutlet weak var resetPasswordsLabel: NSTextField!
  11. @IBOutlet weak var passwordBox: NSBox!
  12. @IBOutlet weak var passwordView: NSView!
  13. @IBOutlet weak var passwordTextField: NSTextField!
  14. @IBOutlet weak var passwordTextField1: NSSecureTextField!
  15. @IBOutlet weak var visibleButton: NSButton!
  16. @IBOutlet weak var passwordErrorLabel: NSTextField!
  17. @IBOutlet weak var finshBox: NSBox!
  18. @IBOutlet weak var finshButton: NSButton!
  19. private var viewModel = KMSignUpViewModel()
  20. private var cancellables = Set<AnyCancellable>()
  21. convenience init(model: KMSignUpViewModel, superView: NSView) {
  22. self.init(frame: superView.bounds)
  23. viewModel = model
  24. viewModel.screenType = .enterNewPassword
  25. bindViewModel()
  26. languageLocalized()
  27. initializeUI()
  28. }
  29. public override init(frame frameRect: NSRect) {
  30. super.init(frame: frameRect)
  31. }
  32. public required init?(coder decoder: NSCoder) {
  33. fatalError("init(coder:) has not been implemented")
  34. }
  35. override func updateUI() {
  36. super.updateUI()
  37. bindViewModel()
  38. languageLocalized()
  39. initializeUI()
  40. }
  41. // MARK: Private Method
  42. private func languageLocalized() -> Void {
  43. resetPasswordsLabel.stringValue = NSLocalizedString("Reset Password", tableName: "MemberCenterLocalizable", comment: "")
  44. finshButton.title = NSLocalizedString("Finish", tableName: "MemberCenterLocalizable", comment: "")
  45. passwordErrorLabel.stringValue = String(format: "*%@", NSLocalizedString("Verification code error.", tableName: "MemberCenterLocalizable", comment: ""))
  46. passwordTextField.placeholderString = NSLocalizedString("Please enter a new password", tableName: "MemberCenterLocalizable", comment: "")
  47. passwordTextField1.placeholderString = NSLocalizedString("Please enter a new password", tableName: "MemberCenterLocalizable", comment: "")
  48. }
  49. private func initializeUI() -> Void {
  50. passwordTextField.delegate = self
  51. passwordTextField1.delegate = self
  52. passwordBox.fillColor = NSColor(named: "texefiedfillcolor") ?? NSColor.white
  53. passwordBox.borderColor = NSColor(named: "DADBDE") ?? NSColor.gray
  54. // passwordTextField.stringValue = viewModel.password
  55. // passwordTextField1.stringValue = viewModel.password
  56. passwordErrorLabel.isHidden = !viewModel.passwordError()
  57. resetPasswordsLabel.textColor = NSColor(named: "000000")
  58. resetPasswordsLabel.font = NSFont.SFMediumFontWithSize(20)
  59. passwordBox.borderColor = NSColor(named: "DADBDE") ?? NSColor.gray
  60. passwordErrorLabel.textColor = NSColor(named: "FA1E5D")
  61. passwordErrorLabel.font = NSFont.SFProTextRegularFont(9)
  62. finshBox.fillColor = NSColor(named: "273C62") ?? NSColor.blue
  63. finshButton.setTitleColor(color: NSColor(named: "FFFFFF") ?? NSColor.white, font: NSFont.SFProTextRegularFont(13))
  64. }
  65. private func visibleStateChange() -> Void {
  66. if viewModel.isVisible {
  67. visibleButton.image = NSImage(named: "passwordVisible")
  68. passwordTextField.isHidden = false
  69. passwordTextField1.isHidden = true
  70. passwordTextField.stringValue = viewModel.password
  71. } else {
  72. visibleButton.image = NSImage(named: "passwordUnVisible")
  73. passwordTextField.isHidden = true
  74. passwordTextField1.isHidden = false
  75. passwordTextField1.stringValue = viewModel.password
  76. }
  77. }
  78. private func skipSignUpView() {
  79. DispatchQueue.main.async { [weak self] in
  80. guard let self = self, let parentView = self.superview else { return }
  81. let model = KMSignUpViewModel()
  82. model.email = self.viewModel.email
  83. model.password = self.viewModel.password
  84. model.signUpState = .password
  85. let signUpView = KMSignUpView(model: model, superView: parentView)
  86. if let parentBox = parentView as? NSBox {
  87. NSAnimationContext.runAnimationGroup { context in
  88. context.duration = 0.3
  89. self.animator().alphaValue = 0
  90. } completionHandler: {
  91. self.removeFromSuperview()
  92. signUpView.alphaValue = 0
  93. parentBox.contentView = signUpView
  94. NSAnimationContext.runAnimationGroup({ context in
  95. context.duration = 0.3
  96. signUpView.animator().alphaValue = 1
  97. }, completionHandler: nil)
  98. }
  99. } else {
  100. NSAnimationContext.runAnimationGroup { context in
  101. context.duration = 0.3
  102. self.animator().alphaValue = 0
  103. } completionHandler: {
  104. self.removeFromSuperview()
  105. signUpView.alphaValue = 0
  106. parentView.addSubview(signUpView)
  107. NSAnimationContext.runAnimationGroup({ context in
  108. context.duration = 0.3
  109. signUpView.animator().alphaValue = 1
  110. }, completionHandler: nil)
  111. }
  112. }
  113. }
  114. }
  115. // MARK: Bind Method
  116. func bindViewModel() -> Void {
  117. viewModel.$isVisible
  118. .receive(on: RunLoop.main)
  119. .sink { [weak self] newValue in
  120. self?.visibleStateChange()
  121. }
  122. .store(in: &cancellables)
  123. viewModel.$passwordErrorMessage
  124. .receive(on: RunLoop.main)
  125. .sink { [weak self] newValue in
  126. self?.passwordErrorLabel.stringValue = newValue
  127. self?.passwordErrorLabel.isHidden = false
  128. }
  129. .store(in: &cancellables)
  130. viewModel.$password
  131. .receive(on: RunLoop.main)
  132. .sink { [weak self] newValue in
  133. if newValue.count <= 30 && newValue.count >= 0 {
  134. self?.viewModel.passwordErrorMessage = ""
  135. } else {
  136. self?.viewModel.passwordErrorMessage = NSLocalizedString("Password error.", tableName: "MemberCenterLocalizable", comment: "")
  137. }
  138. }
  139. .store(in: &cancellables)
  140. }
  141. // MARK: Action Method
  142. @IBAction func visibleAction(_ sender: NSButton) {
  143. viewModel.isVisible.toggle()
  144. }
  145. @IBAction func finishAction(_ sender: NSButton) {
  146. viewModel.passwordErrorMessage = ""
  147. viewModel.resetPassword() { [weak self] success, msg in
  148. guard let self = self else { return }
  149. if success {
  150. self.skipSignUpView()
  151. } else {
  152. DispatchQueue.main.async {
  153. let alert = NSAlert()
  154. alert.messageText = NSLocalizedString(msg, comment: "")
  155. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  156. let result = alert.runModal()
  157. if (result == .alertFirstButtonReturn) {
  158. self.skipSignUpView()
  159. }
  160. }
  161. }
  162. }
  163. }
  164. }
  165. extension KMEnterNewPasswordView: NSTextFieldDelegate {
  166. func controlTextDidEndEditing(_ obj: Notification) {
  167. let textField = obj.object as? NSTextField
  168. if textField == passwordTextField {
  169. viewModel.password = textField!.stringValue
  170. } else if textField == passwordTextField1 {
  171. viewModel.password = textField!.stringValue
  172. }
  173. }
  174. func controlTextDidChange(_ obj: Notification) {
  175. let textField = obj.object as? NSTextField
  176. if textField == passwordTextField {
  177. viewModel.password = textField!.stringValue
  178. } else if textField == passwordTextField1 {
  179. viewModel.password = textField!.stringValue
  180. }
  181. }
  182. }