KMCloseVerificationWC.swift 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. //
  2. // KMCloseVerificationWC.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by wanjun on 2024/11/5.
  6. // 手动撤销验证码WindowController
  7. //
  8. import Cocoa
  9. import Combine
  10. class KMCloseVerificationWC: NSWindowController {
  11. @IBOutlet weak var titleLabel: NSTextField!
  12. @IBOutlet weak var subTitleLabel1: NSTextField!
  13. @IBOutlet weak var subTitleLabel2: NSTextField!
  14. @IBOutlet weak var verifficationBox: NSBox!
  15. @IBOutlet weak var verifficationBox2: NSBox!
  16. @IBOutlet weak var verifficationTextField: NSTextField!
  17. @IBOutlet weak var sendBox: KMBox!
  18. @IBOutlet weak var sendLabel: NSTextField!
  19. @IBOutlet weak var verifficationErrorLabel: NSTextField!
  20. @IBOutlet weak var nextButton: NSButton!
  21. private var userInfoModel = KMUserInfoVCModel()
  22. private var signUpModel = KMSignUpViewModel()
  23. private var cancellables = Set<AnyCancellable>()
  24. static let shared: KMCloseVerificationWC = {
  25. let windowC = KMCloseVerificationWC(windowNibName: "KMCloseVerificationWC")
  26. return windowC
  27. }()
  28. override func windowDidLoad() {
  29. super.windowDidLoad()
  30. // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
  31. bindViewModel()
  32. languageLocalized()
  33. initializeUI()
  34. signUpModel.countDown(type: .logout)
  35. }
  36. // MARK: Private Method
  37. private func languageLocalized() -> Void {
  38. titleLabel.stringValue = NSLocalizedString("Verify your identity", tableName: "MemberCenterLocalizable", comment: "")
  39. subTitleLabel1.stringValue = NSLocalizedString("We have sent you a code via email to", tableName: "MemberCenterLocalizable", comment: "")
  40. subTitleLabel2.stringValue = KMMemberInfo.shared.userEmail
  41. nextButton.title = NSLocalizedString("Next", tableName: "MemberCenterLocalizable", comment: "")
  42. verifficationErrorLabel.stringValue = String(format: "%@", NSLocalizedString("Verification code error.", tableName: "MemberCenterLocalizable", comment: ""))
  43. verifficationTextField.placeholderString = NSLocalizedString("Please enter code", tableName: "MemberCenterLocalizable", comment: "")
  44. }
  45. private func initializeUI() -> Void {
  46. self.window?.contentView?.wantsLayer = true
  47. self.window?.contentView?.layer?.backgroundColor = (NSColor(named: "membercentercolor") ?? NSColor.white).cgColor
  48. verifficationBox.fillColor = NSColor(named: "texefiedfillcolor") ?? NSColor.white
  49. verifficationErrorLabel.isHidden = !signUpModel.passwordError()
  50. titleLabel.textColor = NSColor(named: "000000_0.85")
  51. titleLabel.font = NSFont.SFProTextSemiboldFont(13)
  52. subTitleLabel1.textColor = NSColor(named: "000000_0.85")
  53. subTitleLabel1.font = NSFont.SFProTextRegularFont(12)
  54. subTitleLabel2.textColor = NSColor(named: "000000_0.85")
  55. subTitleLabel2.font = NSFont.SFProTextRegularFont(12)
  56. nextButton.setTitleColor(color: NSColor(named: "FFFFFF") ?? NSColor.white, font: NSFont.SFProTextRegularFont(13))
  57. verifficationTextField.delegate = self
  58. verifficationBox2.borderColor = NSColor(named: "DADBDE") ?? NSColor.gray
  59. verifficationBox.borderColor = NSColor(named: "FA1E5D") ?? NSColor.gray
  60. sendBox.fillColor = NSColor(named: "273C62") ?? NSColor.blue
  61. sendBox.borderColor = NSColor(named: "273C62") ?? NSColor.blue
  62. sendLabel.textColor = NSColor(named: "FFFFFF") ?? NSColor.white
  63. sendLabel.font = NSFont.SFProTextRegularFont(13)
  64. verifficationErrorLabel.textColor = NSColor(named: "FA1E5D")
  65. verifficationErrorLabel.font = NSFont.SFProTextRegularFont(9)
  66. textfieldInputState()
  67. sendBoxRefresh()
  68. sendBox.moveCallback = { [weak self](mouseEntered: Bool, mouseBox: KMBox) -> Void in
  69. guard let self = self else { return }
  70. if self.signUpModel.sendBoxSelect { return }
  71. if mouseEntered {
  72. self.sendBox.fillColor = NSColor(named: "000000_0.1") ?? NSColor.blue
  73. self.sendBox.borderWidth = 1
  74. } else {
  75. self.sendBox.fillColor = NSColor(named: "273C62") ?? NSColor.blue
  76. }
  77. }
  78. sendBox.downCallback = { [weak self](downEntered: Bool, mouseBox: KMBox, event) -> Void in
  79. guard let self = self else { return }
  80. if self.signUpModel.email.count <= 0 { return }
  81. if self.signUpModel.sendBoxSelect { return }
  82. if downEntered {
  83. self.sendBox.fillColor = NSColor(named: "273C62_0.4") ?? NSColor.blue
  84. signUpModel.countDown(type: .logout)
  85. }
  86. }
  87. }
  88. private func textfieldInputState() -> Void {
  89. if signUpModel.passwordError() {
  90. if signUpModel.signUpState == .verificationCode {
  91. verifficationBox2.borderWidth = 0
  92. verifficationBox.borderWidth = 1
  93. }
  94. } else {
  95. if signUpModel.signUpState == .verificationCode {
  96. verifficationBox2.borderWidth = 1
  97. verifficationBox.borderWidth = 0
  98. }
  99. }
  100. verifficationErrorLabel.isHidden = !signUpModel.passwordError()
  101. }
  102. private func sendBoxRefresh() -> Void {
  103. sendLabel.stringValue = signUpModel.sendContent
  104. if signUpModel.sendContent == NSLocalizedString("Send", tableName: "MemberCenterLocalizable", comment: "") {
  105. sendBox.fillColor = NSColor(named: "273C62") ?? NSColor.blue
  106. } else {
  107. if signUpModel.sendContent == NSLocalizedString("Resend", tableName: "MemberCenterLocalizable", comment: "") {
  108. sendBox.fillColor = NSColor(named: "273C62") ?? NSColor.blue
  109. } else {
  110. sendBox.fillColor = NSColor(named: "DADBDE") ?? NSColor.gray
  111. sendLabel.stringValue = String(format: "%@s", signUpModel.sendContent)
  112. }
  113. }
  114. }
  115. // MARK: Bind Method
  116. func bindViewModel() -> Void {
  117. signUpModel.$passwordErrorMessage
  118. .receive(on: RunLoop.main)
  119. .sink { [weak self] newValue in
  120. self?.verifficationErrorLabel.stringValue = newValue
  121. self?.verifficationErrorLabel.isHidden = false
  122. }
  123. .store(in: &cancellables)
  124. signUpModel.$sendContent
  125. .receive(on: RunLoop.main)
  126. .sink { [weak self] newValue in
  127. self?.sendBoxRefresh()
  128. }
  129. .store(in: &cancellables)
  130. }
  131. // MARK: Button Action
  132. @IBAction func yesButtonAction(_ sender: NSButton) {
  133. userInfoModel.closeAccountApplyWC(code: signUpModel.verificationCode)
  134. }
  135. }
  136. extension KMCloseVerificationWC: NSTextFieldDelegate {
  137. func controlTextDidEndEditing(_ obj: Notification) {
  138. let textField = obj.object as? NSTextField
  139. if textField == verifficationTextField {
  140. signUpModel.verificationCode = textField!.stringValue
  141. }
  142. }
  143. func controlTextDidChange(_ obj: Notification) {
  144. let textField = obj.object as? NSTextField
  145. if textField == verifficationTextField {
  146. signUpModel.verificationCode = textField!.stringValue
  147. }
  148. }
  149. }