KMEnterNewPasswordView.swift 7.7 KB

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