KMEnterNewPasswordView.swift 7.9 KB

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