KMCloseVerificationWC.swift 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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.email = KMMemberInfo.shared.userEmail
  35. signUpModel.countDown(type: .logout, callback: nil)
  36. nextButton.isEnabled = false
  37. NotificationCenter.default.addObserver(self, selector: #selector(changeEffectiveAppearance), name: NSNotification.Name(rawValue: "kEffectiveAppearance"), object: nil)
  38. }
  39. @objc func changeEffectiveAppearance() {
  40. self.initializeUI()
  41. }
  42. // MARK: Private Method
  43. private func languageLocalized() -> Void {
  44. self.window?.title = NSLocalizedString("Remove Account", tableName: "MemberCenterLocalizable", comment: "")
  45. titleLabel.stringValue = NSLocalizedString("Verify your identity", tableName: "MemberCenterLocalizable", comment: "")
  46. subTitleLabel1.stringValue = NSLocalizedString("We have sent you a code via email to", tableName: "MemberCenterLocalizable", comment: "")
  47. subTitleLabel2.stringValue = KMMemberInfo.shared.userEmail
  48. nextButton.title = NSLocalizedString("Next", tableName: "MemberCenterLocalizable", comment: "")
  49. verifficationErrorLabel.stringValue = String(format: "%@", NSLocalizedString("Verification code error.", tableName: "MemberCenterLocalizable", comment: ""))
  50. verifficationTextField.placeholderString = NSLocalizedString("Please enter code", tableName: "MemberCenterLocalizable", comment: "")
  51. }
  52. private func initializeUI() -> Void {
  53. self.window?.contentView?.wantsLayer = true
  54. let isDarkModel = KMAdvertisementConfig.isDarkModel()
  55. if isDarkModel {
  56. self.window?.contentView?.layer?.backgroundColor = NSColor(hex: "0E1114").cgColor;
  57. } else {
  58. self.window?.contentView?.layer?.backgroundColor = NSColor(hex: "FFFFFF").cgColor;
  59. }
  60. verifficationBox.fillColor = NSColor(named: "texefiedfillcolor") ?? NSColor.white
  61. verifficationErrorLabel.isHidden = !signUpModel.passwordError()
  62. titleLabel.textColor = NSColor(named: "000000_0.85")
  63. titleLabel.font = NSFont.SFProTextSemiboldFont(13)
  64. subTitleLabel1.textColor = NSColor(named: "000000_0.85")
  65. subTitleLabel1.font = NSFont.SFProTextRegularFont(12)
  66. subTitleLabel2.textColor = NSColor(named: "000000_0.85")
  67. subTitleLabel2.font = NSFont.SFProTextRegularFont(12)
  68. nextButton.setTitleColor(color: NSColor(named: "FFFFFF") ?? NSColor.white, font: NSFont.SFProTextRegularFont(13))
  69. verifficationTextField.delegate = self
  70. verifficationBox2.borderColor = NSColor(named: "DADBDE") ?? NSColor.gray
  71. verifficationBox.borderColor = NSColor(named: "FA1E5D") ?? NSColor.gray
  72. sendBox.fillColor = NSColor(named: "273C62") ?? NSColor.blue
  73. sendBox.borderColor = NSColor(named: "273C62") ?? NSColor.blue
  74. sendLabel.textColor = NSColor(named: "FFFFFF") ?? NSColor.white
  75. sendLabel.font = NSFont.SFProTextRegularFont(13)
  76. verifficationErrorLabel.textColor = NSColor(named: "FA1E5D")
  77. verifficationErrorLabel.font = NSFont.SFProTextRegularFont(9)
  78. textfieldInputState()
  79. sendBoxRefresh()
  80. sendBox.moveCallback = { [weak self](mouseEntered: Bool, mouseBox: KMBox) -> Void in
  81. guard let self = self else { return }
  82. if self.signUpModel.sendBoxSelect { return }
  83. if mouseEntered {
  84. self.sendBox.fillColor = NSColor(named: "000000_0.1") ?? NSColor.blue
  85. } else {
  86. self.sendBox.fillColor = NSColor(named: "273C62") ?? NSColor.blue
  87. }
  88. }
  89. sendBox.downCallback = { [weak self](downEntered: Bool, mouseBox: KMBox, event) -> Void in
  90. guard let self = self else { return }
  91. if self.signUpModel.email.count <= 0 { return }
  92. if self.signUpModel.sendBoxSelect { return }
  93. if downEntered {
  94. self.sendBox.fillColor = NSColor(named: "273C62_0.4") ?? NSColor.blue
  95. self.signUpModel.countDown(type: .logout, callback: nil)
  96. }
  97. }
  98. }
  99. private func textfieldInputState() -> Void {
  100. if signUpModel.passwordError() {
  101. if signUpModel.signUpState == .verificationCode {
  102. verifficationBox2.borderWidth = 0
  103. verifficationBox.borderWidth = 1
  104. }
  105. } else {
  106. if signUpModel.signUpState == .verificationCode {
  107. verifficationBox2.borderWidth = 1
  108. verifficationBox.borderWidth = 0
  109. }
  110. }
  111. verifficationErrorLabel.isHidden = !signUpModel.passwordError()
  112. }
  113. private func sendBoxRefresh() -> Void {
  114. sendLabel.stringValue = signUpModel.sendContent
  115. if signUpModel.sendContent == NSLocalizedString("Send", tableName: "MemberCenterLocalizable", comment: "") {
  116. sendBox.fillColor = NSColor(named: "273C62") ?? NSColor.blue
  117. } else {
  118. if signUpModel.sendContent == NSLocalizedString("Resend", tableName: "MemberCenterLocalizable", comment: "") {
  119. sendBox.fillColor = NSColor(named: "273C62") ?? NSColor.blue
  120. } else {
  121. sendBox.fillColor = NSColor(named: "DADBDE") ?? NSColor.gray
  122. sendLabel.stringValue = String(format: "%@s", signUpModel.sendContent)
  123. }
  124. }
  125. }
  126. // MARK: Bind Method
  127. func bindViewModel() -> Void {
  128. signUpModel.$passwordErrorMessage
  129. .receive(on: RunLoop.main)
  130. .sink { [weak self] newValue in
  131. self?.verifficationErrorLabel.stringValue = newValue
  132. self?.verifficationErrorLabel.isHidden = false
  133. self?.verifficationBox.borderColor = NSColor(named: "FA1E5D") ?? NSColor.red
  134. }
  135. .store(in: &cancellables)
  136. signUpModel.$sendContent
  137. .receive(on: RunLoop.main)
  138. .sink { [weak self] newValue in
  139. self?.sendBoxRefresh()
  140. }
  141. .store(in: &cancellables)
  142. }
  143. // MARK: Button Action
  144. @IBAction func yesButtonAction(_ sender: NSButton) {
  145. window?.showWaitingView()
  146. userInfoModel.closeAccountApplyWC(code: signUpModel.verificationCode) { [weak self] result, resultDict in
  147. DispatchQueue.main.async {
  148. if(result == true) {
  149. KMCloseApplyWC.shared.showWindow(nil)
  150. self?.window?.close()
  151. } else {
  152. if(resultDict != nil) {
  153. let msg = resultDict?.msg ?? NSLocalizedString("Unknown error", comment: "")
  154. let alert = NSAlert()
  155. alert.alertStyle = .critical
  156. alert.messageText = NSLocalizedString("Error Information", comment: "")
  157. alert.informativeText = NSLocalizedString("Please make sure your internet connection is available.", comment: "")
  158. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  159. alert.runModal()
  160. }
  161. }
  162. self?.window?.hideWaitingView()
  163. }
  164. }
  165. }
  166. }
  167. extension KMCloseVerificationWC: NSTextFieldDelegate {
  168. func controlTextDidEndEditing(_ obj: Notification) {
  169. let textField = obj.object as? NSTextField
  170. if textField == verifficationTextField {
  171. signUpModel.verificationCode = textField!.stringValue
  172. }
  173. }
  174. func controlTextDidChange(_ obj: Notification) {
  175. let textField = obj.object as? NSTextField
  176. if textField == verifficationTextField {
  177. signUpModel.verificationCode = textField!.stringValue
  178. signUpModel.passwordErrorMessage = ""
  179. verifficationBox.borderColor = NSColor(named: "DADBDE") ?? NSColor.gray
  180. if(textField?.stringValue.isEmpty == true) {
  181. nextButton.isEnabled = false
  182. } else {
  183. nextButton.isEnabled = true
  184. }
  185. }
  186. }
  187. }