KMSignUpView.swift 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. //
  2. // KMSignUpView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by wanjun on 2024/10/23.
  6. //
  7. import Cocoa
  8. import Combine
  9. class KMSignUpView: KMBaseXibView {
  10. @IBOutlet weak var signUpLabel: NSTextField!
  11. @IBOutlet weak var loginModeBox: NSBox!
  12. @IBOutlet weak var verificationCodeButton: NSButton!
  13. @IBOutlet weak var selectBox1: NSBox!
  14. @IBOutlet weak var passwordButton: NSButton!
  15. @IBOutlet weak var selectBox2: NSBox!
  16. @IBOutlet weak var emailBox: NSBox!
  17. @IBOutlet weak var emailTextField: NSTextField!
  18. @IBOutlet weak var emailErrorLabel: NSTextField!
  19. @IBOutlet weak var passwordBox: NSBox!
  20. @IBOutlet weak var verifficationView: NSView!
  21. @IBOutlet weak var verifficationBox: NSBox!
  22. @IBOutlet weak var verifficationTextField: NSTextField!
  23. @IBOutlet weak var sendBox: KMBox!
  24. @IBOutlet weak var sendLabel: NSTextField!
  25. @IBOutlet weak var passwordView: NSView!
  26. @IBOutlet weak var passwordTextField: NSTextField!
  27. @IBOutlet weak var passwordTextField1: NSSecureTextField!
  28. @IBOutlet weak var visibleButton: NSButton!
  29. @IBOutlet weak var passwordErrorLabel: NSTextField!
  30. @IBOutlet weak var stayCheckButton: NSButton!
  31. @IBOutlet weak var stayLabel: NSTextField!
  32. @IBOutlet weak var forgetButton: NSButton!
  33. @IBOutlet weak var signUpBox: NSBox!
  34. @IBOutlet weak var signUpButton: NSButton!
  35. @IBOutlet weak var privacyCheckButton: NSButton!
  36. @IBOutlet weak var privacyLabel: NSTextField!
  37. private var viewModel = KMSignUpViewModel()
  38. private var cancellables = Set<AnyCancellable>()
  39. private var popOver_: NSPopover?
  40. private lazy var codePrivacyAttri_: NSAttributedString = {
  41. let tipsString = NSLocalizedString("I have read and agreed to the %@ and %@. An account will be automatically created after signing in with an unregistered email address.", tableName: "MemberCenterLocalizable", comment: "")
  42. let specialOffer = NSLocalizedString("Terms of Service", tableName: "MemberCenterLocalizable", comment: "")
  43. let contactsUs = NSLocalizedString("Privacy Policy", tableName: "MemberCenterLocalizable", comment: "")
  44. let fullString = String(format: tipsString, specialOffer, contactsUs)
  45. let attributedString = NSMutableAttributedString(string: fullString)
  46. // 定义链接的范围
  47. let specialOfferRange = (fullString as NSString).range(of: specialOffer)
  48. let contactsUsRange = (fullString as NSString).range(of: contactsUs)
  49. let linkColor = NSColor(named: "4982E6") ?? NSColor.blue
  50. let font = NSFont.SFProTextRegularFont(11.0) // 与普通文本相同的字体
  51. attributedString.addAttributes([
  52. .foregroundColor: NSColor(named: "0E1114") ?? NSColor.black as Any,
  53. .font: font
  54. ], range: (fullString as NSString).range(of: fullString))
  55. attributedString.addAttributes([
  56. .foregroundColor: linkColor,
  57. .link: NSLocalizedString("https://www.pdfreaderpro.com/terms_of_service", comment: ""),
  58. .font: font
  59. ], range: specialOfferRange)
  60. attributedString.addAttributes([
  61. .foregroundColor: linkColor,
  62. .link: NSLocalizedString("https://www.pdfreaderpro.com/privacy-policy", comment: ""),
  63. .font: font
  64. ], range: contactsUsRange)
  65. return attributedString
  66. }()
  67. private lazy var privacyAttri_: NSAttributedString = {
  68. let tipsString = NSLocalizedString("I have read and agreed to the %@ and %@.", tableName: "MemberCenterLocalizable", comment: "")
  69. let specialOffer = NSLocalizedString("Terms of Service", tableName: "MemberCenterLocalizable", comment: "")
  70. let contactsUs = NSLocalizedString("Privacy Policy", tableName: "MemberCenterLocalizable", comment: "")
  71. let fullString = String(format: tipsString, specialOffer, contactsUs)
  72. let attributedString = NSMutableAttributedString(string: fullString)
  73. // 定义链接的范围
  74. let specialOfferRange = (fullString as NSString).range(of: specialOffer)
  75. let contactsUsRange = (fullString as NSString).range(of: contactsUs)
  76. let linkColor = NSColor(named: "4982E6") ?? NSColor.blue
  77. let font = NSFont.SFProTextRegularFont(11.0) // 与普通文本相同的字体
  78. attributedString.addAttributes([
  79. .foregroundColor: NSColor(named: "0E1114") ?? NSColor.black as Any,
  80. .font: font
  81. ], range: (fullString as NSString).range(of: fullString))
  82. attributedString.addAttributes([
  83. .foregroundColor: linkColor,
  84. .link: NSLocalizedString("https://www.pdfreaderpro.com/terms_of_service", comment: ""),
  85. .font: font
  86. ], range: specialOfferRange)
  87. attributedString.addAttributes([
  88. .foregroundColor: linkColor,
  89. .link: NSLocalizedString("https://www.pdfreaderpro.com/privacy-policy", comment: ""),
  90. .font: font
  91. ], range: contactsUsRange)
  92. return attributedString
  93. }()
  94. convenience init(model: KMSignUpViewModel, superView: NSView) {
  95. self.init(frame: superView.bounds)
  96. viewModel = model
  97. viewModel.screenType = .signUp
  98. bindViewModel()
  99. languageLocalized()
  100. initializeUI()
  101. if model.sendCode {
  102. self.viewModel.countDown(type: .login, callback: nil)
  103. model.sendCode = false
  104. }
  105. }
  106. public override init(frame frameRect: NSRect) {
  107. super.init(frame: frameRect)
  108. }
  109. public required init?(coder decoder: NSCoder) {
  110. fatalError("init(coder:) has not been implemented")
  111. }
  112. public func resetTextFileData() {
  113. if(self.superview != nil) {
  114. emailTextField.stringValue = ""
  115. passwordTextField.stringValue = ""
  116. passwordTextField1.stringValue = ""
  117. verifficationTextField.stringValue = ""
  118. emailErrorLabel.isHidden = true
  119. passwordErrorLabel.isHidden = true
  120. sendLabel.stringValue = NSLocalizedString("Send", tableName: "MemberCenterLocalizable", comment: "")
  121. viewModel.sendContent = NSLocalizedString("Send", tableName: "MemberCenterLocalizable", comment: "")
  122. viewModel.email = ""
  123. sendBoxRefresh()
  124. }
  125. }
  126. override func updateUI() {
  127. super.updateUI()
  128. NotificationCenter.default.addObserver(self, selector: #selector(loginSuccessNotification), name: NSNotification.Name(rawValue: "MemberCenterLoginSuccess"), object: nil)
  129. DispatchQueue.main.async { [weak self] in
  130. self?.bindViewModel()
  131. self?.languageLocalized()
  132. self?.initializeUI()
  133. }
  134. }
  135. // MARK: Private Method
  136. private func languageLocalized() -> Void {
  137. signUpLabel.stringValue = NSLocalizedString("Sign in(titile)", tableName: "MemberCenterLocalizable", comment: "")
  138. verificationCodeButton.title = NSLocalizedString("Verification Code", tableName: "MemberCenterLocalizable", comment: "")
  139. passwordButton.title = NSLocalizedString("Password", tableName: "MemberCenterLocalizable", comment: "")
  140. stayLabel.stringValue = NSLocalizedString("Stay signed in", tableName: "MemberCenterLocalizable", comment: "")
  141. forgetButton.title = NSLocalizedString("Forgot password?", tableName: "MemberCenterLocalizable", comment: "")
  142. signUpButton.title = NSLocalizedString("Sign in(button)", tableName: "MemberCenterLocalizable", comment: "")
  143. emailErrorLabel.stringValue = String(format: "*%@", NSLocalizedString("Email format error. Please enter the correct email.", tableName: "MemberCenterLocalizable", comment: ""))
  144. passwordErrorLabel.stringValue = String(format: "*%@", NSLocalizedString("Verification code error.", tableName: "MemberCenterLocalizable", comment: ""))
  145. emailTextField.placeholderString = NSLocalizedString("Please enter email address", tableName: "MemberCenterLocalizable", comment: "")
  146. verifficationTextField.placeholderString = NSLocalizedString("Please enter code", tableName: "MemberCenterLocalizable", comment: "")
  147. passwordTextField.placeholderString = NSLocalizedString("Please enter password", tableName: "MemberCenterLocalizable", comment: "")
  148. passwordTextField1.placeholderString = NSLocalizedString("Please enter password", tableName: "MemberCenterLocalizable", comment: "")
  149. privacyCheckButton.toolTip = NSLocalizedString("Please agree and check the agreement first.", tableName: "MemberCenterLocalizable", comment: "")
  150. emailTextField.stringValue = viewModel.email
  151. verifficationTextField.stringValue = viewModel.verificationCode
  152. passwordTextField.stringValue = viewModel.password
  153. passwordTextField1.stringValue = viewModel.password
  154. }
  155. private func initializeUI() -> Void {
  156. emailBox.fillColor = NSColor(named: "texefiedfillcolor") ?? NSColor.white
  157. passwordBox.fillColor = NSColor(named: "texefiedfillcolor") ?? NSColor.white
  158. emailTextField.delegate = self
  159. verifficationTextField.delegate = self
  160. passwordTextField.delegate = self
  161. passwordTextField1.delegate = self
  162. emailErrorLabel.isHidden = !viewModel.emailError()
  163. passwordErrorLabel.isHidden = !viewModel.passwordError()
  164. signUpLabel.textColor = NSColor(named: "000000")
  165. signUpLabel.font = NSFont.SFMediumFontWithSize(20)
  166. selectBox1.fillColor = NSColor(named: "4982E6") ?? NSColor.blue
  167. emailErrorLabel.textColor = NSColor(named: "FA1E5D")
  168. emailErrorLabel.font = NSFont.SFProTextRegularFont(9)
  169. passwordBox.borderColor = NSColor(named: "DADBDE") ?? NSColor.gray
  170. verifficationBox.borderColor = NSColor(named: "FA1E5D") ?? NSColor.gray
  171. if viewModel.isValidEmail() {
  172. sendBox.fillColor = NSColor(named: "273C62") ?? NSColor.blue
  173. } else {
  174. sendBox.fillColor = NSColor(named: "273C62_0.4") ?? NSColor.blue
  175. }
  176. sendBox.borderColor = NSColor(named: "273C62") ?? NSColor.blue
  177. sendLabel.textColor = NSColor(named: "FFFFFF") ?? NSColor.white
  178. sendLabel.font = NSFont.SFProTextRegularFont(13)
  179. passwordErrorLabel.textColor = NSColor(named: "FA1E5D")
  180. passwordErrorLabel.font = NSFont.SFProTextRegularFont(9)
  181. stayCheckButton.image = NSImage(named: "CheckBoxNor")
  182. stayLabel.textColor = NSColor(named: "0E1114") ?? NSColor.black
  183. stayLabel.font = NSFont.SFProTextRegularFont(12)
  184. forgetButton.setTitleColor(color: NSColor(named: "4982E6") ?? NSColor.blue, font: NSFont.SFProTextRegularFont(12))
  185. signUpBox.fillColor = NSColor(named: "273C62") ?? NSColor.blue
  186. signUpButton.setTitleColor(color: NSColor(named: "FFFFFF") ?? NSColor.white, font: NSFont.SFProTextRegularFont(16))
  187. privacyCheckButton.image = NSImage(named: "CheckBoxNor")
  188. privacyLabel.isEditable = false
  189. privacyLabel.isSelectable = true
  190. privacyLabel.allowsEditingTextAttributes = true
  191. privacyLabel.textColor = NSColor.black
  192. privacyLabel.font = NSFont.SFProTextRegularFont(16.0)
  193. privacyLabel.attributedStringValue = codePrivacyAttri_
  194. signUpStateChange()
  195. visibleStateChange()
  196. textfieldInputState(isEmail: true)
  197. textfieldInputState(isEmail: false)
  198. sendBoxRefresh()
  199. sendBox.moveCallback = { [weak self](mouseEntered: Bool, mouseBox: KMBox) -> Void in
  200. guard let self = self else { return }
  201. if self.viewModel.email.count <= 0 { return }
  202. if self.viewModel.sendBoxSelect { return }
  203. if !self.viewModel.isValidEmail() { return }
  204. if mouseEntered {
  205. self.sendBox.fillColor = NSColor(named: "000000_0.1") ?? NSColor.blue
  206. } else {
  207. self.sendBox.fillColor = NSColor(named: "273C62") ?? NSColor.blue
  208. }
  209. }
  210. sendBox.downCallback = { [weak self](downEntered: Bool, mouseBox: KMBox, event) -> Void in
  211. guard let self = self else { return }
  212. if self.viewModel.email.count <= 0 { return }
  213. if self.viewModel.sendBoxSelect { return }
  214. if downEntered {
  215. self.sendBox.fillColor = NSColor(named: "273C62_0.4") ?? NSColor.blue
  216. self.viewModel.countDown(type: .login, callback: nil)
  217. }
  218. }
  219. }
  220. private func signUpStateChange() -> Void {
  221. if viewModel.signUpState == .verificationCode {
  222. selectBox1.isHidden = false
  223. selectBox2.isHidden = true
  224. verificationCodeButton.setTitleColor(color: NSColor(named: "4982E6") ?? NSColor.systemBlue, font: NSFont.SFProTextRegularFont(14))
  225. passwordButton.setTitleColor(color: NSColor(named: "42464D") ?? NSColor.black, font: NSFont.SFProTextRegularFont(14))
  226. verifficationView.isHidden = false
  227. passwordView.isHidden = true
  228. verifficationTextField.placeholderString = NSLocalizedString("Please enter code", tableName: "MemberCenterLocalizable", comment: "")
  229. forgetButton.isHidden = true
  230. privacyLabel.attributedStringValue = codePrivacyAttri_
  231. } else if viewModel.signUpState == .password {
  232. selectBox1.isHidden = true
  233. selectBox2.isHidden = false
  234. verificationCodeButton.setTitleColor(color: NSColor(named: "42464D") ?? NSColor.black, font: NSFont.SFProTextRegularFont(14))
  235. passwordButton.setTitleColor(color: NSColor(named: "4982E6") ?? NSColor.black, font: NSFont.SFProTextRegularFont(14))
  236. verifficationView.isHidden = true
  237. passwordView.isHidden = false
  238. passwordTextField.placeholderString = NSLocalizedString("Please enter password", tableName: "MemberCenterLocalizable", comment: "")
  239. passwordTextField1.placeholderString = NSLocalizedString("Please enter password", tableName: "MemberCenterLocalizable", comment: "")
  240. forgetButton.isHidden = false
  241. privacyLabel.attributedStringValue = privacyAttri_
  242. }
  243. }
  244. private func checkStateChange(button: NSButton!, state: Bool) -> Void {
  245. button.state = state ? .on : .off
  246. if button.state == .on {
  247. button.image = NSImage(named: "CheckBoxSel")
  248. } else {
  249. button.image = NSImage(named: "CheckBoxNor")
  250. }
  251. }
  252. private func visibleStateChange() -> Void {
  253. if viewModel.isVisible {
  254. visibleButton.image = NSImage(named: "passwordVisible")
  255. passwordTextField.isHidden = false
  256. passwordTextField1.isHidden = true
  257. passwordTextField.stringValue = viewModel.password
  258. } else {
  259. visibleButton.image = NSImage(named: "passwordUnVisible")
  260. passwordTextField.isHidden = true
  261. passwordTextField1.isHidden = false
  262. passwordTextField1.stringValue = viewModel.password
  263. }
  264. }
  265. private func textfieldInputState(isEmail: Bool) -> Void {
  266. if isEmail {
  267. if viewModel.emailError() {
  268. emailBox.borderColor = NSColor(named: "FA1E5D") ?? NSColor.red
  269. } else {
  270. emailBox.borderColor = NSColor(named: "DADBDE") ?? NSColor.gray
  271. }
  272. emailErrorLabel.isHidden = !viewModel.emailError()
  273. } else {
  274. if viewModel.passwordError() {
  275. if viewModel.signUpState == .verificationCode {
  276. passwordBox.borderWidth = 0
  277. verifficationBox.borderWidth = 1
  278. passwordBox.borderColor = NSColor(named: "DADBDE") ?? NSColor.gray
  279. verifficationBox.borderColor = NSColor(named: "FA1E5D") ?? NSColor.red
  280. } else if viewModel.signUpState == .password {
  281. passwordBox.borderWidth = 1
  282. verifficationBox.borderWidth = 0
  283. verifficationBox.borderColor = NSColor(named: "DADBDE") ?? NSColor.gray
  284. passwordBox.borderColor = NSColor(named: "FA1E5D") ?? NSColor.red
  285. }
  286. } else {
  287. if viewModel.signUpState == .verificationCode {
  288. passwordBox.borderWidth = 1
  289. verifficationBox.borderWidth = 0
  290. } else if viewModel.signUpState == .password {
  291. passwordBox.borderWidth = 1
  292. verifficationBox.borderWidth = 0
  293. }
  294. }
  295. passwordErrorLabel.isHidden = !viewModel.passwordError()
  296. }
  297. }
  298. private func sendBoxRefresh() -> Void {
  299. sendLabel.stringValue = viewModel.sendContent
  300. if viewModel.sendContent == NSLocalizedString("Send", tableName: "MemberCenterLocalizable", comment: "") ||
  301. viewModel.sendContent == NSLocalizedString("Resend", tableName: "MemberCenterLocalizable", comment: "") {
  302. if viewModel.email.count > 0 {
  303. if viewModel.isValidEmail() {
  304. sendBox.fillColor = NSColor(named: "273C62") ?? NSColor.blue
  305. } else {
  306. sendBox.fillColor = NSColor(named: "273C62_0.4") ?? NSColor.blue
  307. }
  308. } else {
  309. sendBox.fillColor = NSColor(named: "273C62_0.4") ?? NSColor.blue
  310. }
  311. sendLabel.textColor = NSColor(named: "FFFFFF") ?? NSColor.white
  312. } else {
  313. sendBox.fillColor = NSColor(named: "DADBDE") ?? NSColor.gray
  314. sendLabel.stringValue = String(format: "%@s", viewModel.sendContent)
  315. sendLabel.textColor = NSColor(named: "0E1114") ?? NSColor.black
  316. }
  317. }
  318. private func _showPop() {
  319. guard let _ = self.window else {
  320. return
  321. }
  322. if (self.popOver_ != nil) {
  323. return
  324. }
  325. let popViewController = KMToolbarItemPopViewController()
  326. self.popOver_ = NSPopover()
  327. self.popOver_?.contentViewController = popViewController
  328. self.popOver_?.animates = false
  329. self.popOver_?.behavior = .semitransient
  330. self.popOver_?.contentSize = popViewController.view.frame.size
  331. self.popOver_?.delegate = self
  332. // 0.8
  333. var color = NSColor(hex: "#273C62").withAlphaComponent(1)
  334. if KMAppearance.isDarkMode() {
  335. color = NSColor(hex: "#4E7FDB").withAlphaComponent(1)
  336. }
  337. popViewController.view.wantsLayer = true
  338. popViewController.view.layer?.backgroundColor = color.cgColor
  339. self.popOver_?.setBackgroundColor(color)
  340. popViewController.toolbarHelpTipLabel.textColor = .white
  341. popViewController.toolbarHelpTipLabel.font = .SFProTextRegularFont(13)
  342. popViewController.contentInset = .init(top: 12, left: 8, bottom: 12, right: 8)
  343. popViewController.updateWithHelpTip(helpTip: NSLocalizedString("Please agree and check the agreement first.", tableName: "MemberCenterLocalizable", comment: ""))
  344. self.popOver_?.show(relativeTo: NSMakeRect(0, 0, 0, 12), of: privacyCheckButton, preferredEdge: .maxY)
  345. }
  346. // MARK: Bind Method
  347. func bindViewModel() -> Void {
  348. viewModel.$isVisible
  349. .receive(on: RunLoop.main)
  350. .sink { [weak self] newValue in
  351. self?.visibleStateChange()
  352. }
  353. .store(in: &cancellables)
  354. viewModel.$stayState
  355. .receive(on: RunLoop.main)
  356. .sink { [weak self] newValue in
  357. self?.checkStateChange(button: self?.stayCheckButton, state: newValue)
  358. }
  359. .store(in: &cancellables)
  360. viewModel.$privacyState
  361. .receive(on: RunLoop.main)
  362. .sink { [weak self] newValue in
  363. self?.checkStateChange(button: self?.privacyCheckButton, state: newValue)
  364. self?.popOver_?.close()
  365. }
  366. .store(in: &cancellables)
  367. viewModel.$signUpState
  368. .receive(on: RunLoop.main)
  369. .sink { [weak self] newValue in
  370. self?.signUpStateChange()
  371. }
  372. .store(in: &cancellables)
  373. viewModel.$emailErrorMessage
  374. .receive(on: RunLoop.main)
  375. .sink { [weak self] newValue in
  376. self?.emailErrorLabel.stringValue = newValue
  377. self?.emailErrorLabel.isHidden = false
  378. self?.textfieldInputState(isEmail: true)
  379. }
  380. .store(in: &cancellables)
  381. viewModel.$passwordErrorMessage
  382. .receive(on: RunLoop.main)
  383. .sink { [weak self] newValue in
  384. self?.passwordErrorLabel.stringValue = newValue
  385. if self?.viewModel.passwordErrorMessage == "" {
  386. self?.passwordErrorLabel.isHidden = true
  387. } else {
  388. self?.passwordErrorLabel.isHidden = false
  389. }
  390. }
  391. .store(in: &cancellables)
  392. viewModel.$sendContent
  393. .receive(on: RunLoop.main)
  394. .sink { [weak self] newValue in
  395. self?.sendBoxRefresh()
  396. }
  397. .store(in: &cancellables)
  398. viewModel.$email
  399. .receive(on: RunLoop.main)
  400. .sink { [weak self] newValue in
  401. self?.sendBoxRefresh()
  402. }
  403. .store(in: &cancellables)
  404. viewModel.$verificationCode
  405. .receive(on: RunLoop.main)
  406. .sink { [weak self] newValue in
  407. if newValue.count <= 6 && newValue.count >= 0 {
  408. self?.viewModel.passwordErrorMessage = ""
  409. } else {
  410. self?.viewModel.passwordErrorMessage = NSLocalizedString("Verification code error.", tableName: "MemberCenterLocalizable", comment: "")
  411. }
  412. }
  413. .store(in: &cancellables)
  414. viewModel.$password
  415. .receive(on: RunLoop.main)
  416. .sink { [weak self] newValue in
  417. if newValue.count <= 30 && newValue.count >= 0 {
  418. self?.viewModel.passwordErrorMessage = ""
  419. } else {
  420. self?.viewModel.passwordErrorMessage = NSLocalizedString("Password cannot exceed 30 characters.", tableName: "MemberCenterLocalizable", comment: "")
  421. }
  422. }
  423. .store(in: &cancellables)
  424. viewModel.$privacyPopShow
  425. .receive(on: RunLoop.main)
  426. .sink { [weak self] newValue in
  427. if newValue {
  428. KMMainThreadExecute {
  429. self?._showPop()
  430. }
  431. }
  432. }
  433. .store(in: &cancellables)
  434. }
  435. // MARK: Action Method
  436. @IBAction func verificationCodeAction(_ sender: NSButton) {
  437. viewModel.signUpStateChange(state: .verificationCode)
  438. }
  439. @IBAction func passwordAction(_ sender: NSButton) {
  440. viewModel.signUpStateChange(state: .password)
  441. }
  442. @IBAction func visibleAction(_ sender: NSButton) {
  443. viewModel.isVisible.toggle()
  444. }
  445. @IBAction func stayCheckAction(_ sender: NSButton) {
  446. viewModel.stayState.toggle()
  447. viewModel.stayStateAction()
  448. }
  449. @IBAction func signUpAction(_ sender: NSButton) {
  450. self.window?.makeFirstResponder(nil)
  451. viewModel.emailErrorMessage = ""
  452. viewModel.passwordErrorMessage = ""
  453. window?.showWaitingView()
  454. viewModel.signUpAction { [weak self] result, _ in
  455. self?.window?.hideWaitingView()
  456. }
  457. }
  458. @IBAction func forgetAction(_ sender: NSButton) {
  459. guard let parentView = self.superview else { return }
  460. if parentView is NSBox {
  461. let model = KMSignUpViewModel()
  462. model.email = viewModel.email
  463. let forgotView = KMForgotPasswordView(model: model, superView: parentView)
  464. NSAnimationContext.runAnimationGroup { context in
  465. context.duration = 0.3
  466. self.animator().alphaValue = 0
  467. } completionHandler: {
  468. self.removeFromSuperview()
  469. forgotView.alphaValue = 0
  470. (parentView as! NSBox).contentView = forgotView
  471. NSAnimationContext.runAnimationGroup({ context in
  472. context.duration = 0.3
  473. forgotView.animator().alphaValue = 1
  474. }, completionHandler: nil)
  475. }
  476. } else {
  477. let model = KMSignUpViewModel()
  478. model.email = viewModel.email
  479. let forgotView = KMForgotPasswordView(model: model, superView: parentView)
  480. NSAnimationContext.runAnimationGroup { context in
  481. context.duration = 0.3
  482. self.animator().alphaValue = 0
  483. } completionHandler: {
  484. self.removeFromSuperview()
  485. forgotView.alphaValue = 0
  486. parentView.addSubview(forgotView)
  487. NSAnimationContext.runAnimationGroup({ context in
  488. context.duration = 0.3
  489. forgotView.animator().alphaValue = 1
  490. }, completionHandler: nil)
  491. }
  492. }
  493. }
  494. @IBAction func privacyCheckAction(_ sender: NSButton) {
  495. viewModel.privacyState.toggle()
  496. }
  497. }
  498. extension KMSignUpView: NSTextFieldDelegate {
  499. func controlTextDidEndEditing(_ obj: Notification) {
  500. let textField = obj.object as? NSTextField
  501. if textField == emailTextField {
  502. viewModel.email = textField!.stringValue
  503. } else if textField == verifficationTextField {
  504. viewModel.verificationCode = textField!.stringValue
  505. } else if textField == passwordTextField {
  506. viewModel.password = textField!.stringValue
  507. } else if textField == passwordTextField1 {
  508. viewModel.password = textField!.stringValue
  509. }
  510. }
  511. func controlTextDidChange(_ obj: Notification) {
  512. let textField = obj.object as? NSTextField
  513. if textField == emailTextField {
  514. viewModel.emailErrorMessage = ""
  515. viewModel.email = textField!.stringValue
  516. emailBox.borderColor = NSColor(named: "DADBDE") ?? NSColor.gray
  517. } else if textField == verifficationTextField {
  518. viewModel.passwordErrorMessage = ""
  519. viewModel.verificationCode = textField!.stringValue
  520. verifficationBox.borderColor = NSColor(named: "DADBDE") ?? NSColor.gray
  521. } else if textField == passwordTextField {
  522. viewModel.passwordErrorMessage = ""
  523. viewModel.password = textField!.stringValue
  524. passwordBox.borderColor = NSColor(named: "DADBDE") ?? NSColor.gray
  525. } else if textField == passwordTextField1 {
  526. viewModel.passwordErrorMessage = ""
  527. viewModel.password = textField!.stringValue
  528. passwordBox.borderColor = NSColor(named: "DADBDE") ?? NSColor.gray
  529. }
  530. }
  531. func control(_ control: NSControl, textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool {
  532. if commandSelector == #selector(NSResponder.insertTab) {
  533. if control.isEqual(to: emailTextField) {
  534. if viewModel.signUpState == .password {
  535. // window?.selectNextKeyView(passwordTextField)
  536. if viewModel.isVisible {
  537. window?.makeFirstResponder(passwordTextField)
  538. } else {
  539. window?.makeFirstResponder(passwordTextField1)
  540. }
  541. return true
  542. } else if viewModel.signUpState == .verificationCode {
  543. // window?.selectNextKeyView(verifficationTextField)
  544. window?.makeFirstResponder(verifficationTextField)
  545. return true
  546. }
  547. }
  548. }
  549. return false
  550. }
  551. @objc func loginSuccessNotification() -> Void {
  552. resetTextFileData()
  553. }
  554. }
  555. extension KMSignUpView: NSPopoverDelegate {
  556. func popoverDidClose(_ notification: Notification) {
  557. if let data = self.popOver_?.isEqual(to: notification.object), data {
  558. self.popOver_ = nil
  559. }
  560. }
  561. }