KMSignUpView.swift 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  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. let email = UserDefaults.standard.value(forKey: "MemberSystemAccount")
  151. if email is String {
  152. if (email as! String).count > 0 {
  153. viewModel.email = email as! String
  154. }
  155. }
  156. emailTextField.stringValue = viewModel.email
  157. verifficationTextField.stringValue = viewModel.verificationCode
  158. passwordTextField.stringValue = viewModel.password
  159. passwordTextField1.stringValue = viewModel.password
  160. }
  161. private func initializeUI() -> Void {
  162. emailBox.fillColor = NSColor(named: "texefiedfillcolor") ?? NSColor.white
  163. passwordBox.fillColor = NSColor(named: "texefiedfillcolor") ?? NSColor.white
  164. emailTextField.delegate = self
  165. verifficationTextField.delegate = self
  166. passwordTextField.delegate = self
  167. passwordTextField1.delegate = self
  168. emailErrorLabel.isHidden = !viewModel.emailError()
  169. passwordErrorLabel.isHidden = !viewModel.passwordError()
  170. signUpLabel.textColor = NSColor(named: "000000")
  171. signUpLabel.font = NSFont.SFMediumFontWithSize(20)
  172. selectBox1.fillColor = NSColor(named: "4982E6") ?? NSColor.blue
  173. emailErrorLabel.textColor = NSColor(named: "FA1E5D")
  174. emailErrorLabel.font = NSFont.SFProTextRegularFont(9)
  175. passwordBox.borderColor = NSColor(named: "DADBDE") ?? NSColor.gray
  176. verifficationBox.borderColor = NSColor(named: "FA1E5D") ?? NSColor.gray
  177. if viewModel.isValidEmail() {
  178. sendBox.fillColor = NSColor(named: "273C62") ?? NSColor.blue
  179. } else {
  180. sendBox.fillColor = NSColor(named: "273C62_0.4") ?? NSColor.blue
  181. }
  182. sendBox.borderColor = NSColor(named: "273C62") ?? NSColor.blue
  183. sendLabel.textColor = NSColor(named: "FFFFFF") ?? NSColor.white
  184. sendLabel.font = NSFont.SFProTextRegularFont(13)
  185. passwordErrorLabel.textColor = NSColor(named: "FA1E5D")
  186. passwordErrorLabel.font = NSFont.SFProTextRegularFont(9)
  187. stayCheckButton.image = NSImage(named: "CheckBoxNor")
  188. stayLabel.textColor = NSColor(named: "0E1114") ?? NSColor.black
  189. stayLabel.font = NSFont.SFProTextRegularFont(12)
  190. forgetButton.setTitleColor(color: NSColor(named: "4982E6") ?? NSColor.blue, font: NSFont.SFProTextRegularFont(12))
  191. signUpBox.fillColor = NSColor(named: "273C62") ?? NSColor.blue
  192. signUpButton.setTitleColor(color: NSColor(named: "FFFFFF") ?? NSColor.white, font: NSFont.SFProTextRegularFont(16))
  193. privacyCheckButton.image = NSImage(named: "CheckBoxNor")
  194. privacyLabel.isEditable = false
  195. privacyLabel.isSelectable = true
  196. privacyLabel.allowsEditingTextAttributes = true
  197. privacyLabel.textColor = NSColor.black
  198. privacyLabel.font = NSFont.SFProTextRegularFont(16.0)
  199. privacyLabel.attributedStringValue = codePrivacyAttri_
  200. signUpStateChange()
  201. visibleStateChange()
  202. textfieldInputState(isEmail: true)
  203. textfieldInputState(isEmail: false)
  204. sendBoxRefresh()
  205. sendBox.moveCallback = { [weak self](mouseEntered: Bool, mouseBox: KMBox) -> Void in
  206. guard let self = self else { return }
  207. if self.viewModel.email.count <= 0 { return }
  208. if self.viewModel.sendBoxSelect { return }
  209. if !self.viewModel.isValidEmail() { return }
  210. if mouseEntered {
  211. self.sendBox.fillColor = NSColor(named: "000000_0.1") ?? NSColor.blue
  212. } else {
  213. self.sendBox.fillColor = NSColor(named: "273C62") ?? NSColor.blue
  214. }
  215. }
  216. sendBox.downCallback = { [weak self](downEntered: Bool, mouseBox: KMBox, event) -> Void in
  217. guard let self = self else { return }
  218. if self.viewModel.email.count <= 0 { return }
  219. if self.viewModel.sendBoxSelect { return }
  220. if downEntered {
  221. self.sendBox.fillColor = NSColor(named: "273C62_0.4") ?? NSColor.blue
  222. self.viewModel.countDown(type: .login, callback: nil)
  223. }
  224. }
  225. }
  226. private func signUpStateChange() -> Void {
  227. if viewModel.signUpState == .verificationCode {
  228. selectBox1.isHidden = false
  229. selectBox2.isHidden = true
  230. verificationCodeButton.setTitleColor(color: NSColor(named: "4982E6") ?? NSColor.systemBlue, font: NSFont.SFProTextRegularFont(14))
  231. passwordButton.setTitleColor(color: NSColor(named: "42464D") ?? NSColor.black, font: NSFont.SFProTextRegularFont(14))
  232. verifficationView.isHidden = false
  233. passwordView.isHidden = true
  234. verifficationTextField.placeholderString = NSLocalizedString("Please enter code", tableName: "MemberCenterLocalizable", comment: "")
  235. forgetButton.isHidden = true
  236. privacyLabel.attributedStringValue = codePrivacyAttri_
  237. } else if viewModel.signUpState == .password {
  238. selectBox1.isHidden = true
  239. selectBox2.isHidden = false
  240. verificationCodeButton.setTitleColor(color: NSColor(named: "42464D") ?? NSColor.black, font: NSFont.SFProTextRegularFont(14))
  241. passwordButton.setTitleColor(color: NSColor(named: "4982E6") ?? NSColor.black, font: NSFont.SFProTextRegularFont(14))
  242. verifficationView.isHidden = true
  243. passwordView.isHidden = false
  244. passwordTextField.placeholderString = NSLocalizedString("Please enter password", tableName: "MemberCenterLocalizable", comment: "")
  245. passwordTextField1.placeholderString = NSLocalizedString("Please enter password", tableName: "MemberCenterLocalizable", comment: "")
  246. forgetButton.isHidden = false
  247. privacyLabel.attributedStringValue = privacyAttri_
  248. }
  249. }
  250. private func checkStateChange(button: NSButton!, state: Bool) -> Void {
  251. button.state = state ? .on : .off
  252. if button.state == .on {
  253. button.image = NSImage(named: "CheckBoxSel")
  254. } else {
  255. button.image = NSImage(named: "CheckBoxNor")
  256. }
  257. }
  258. private func visibleStateChange() -> Void {
  259. if viewModel.isVisible {
  260. visibleButton.image = NSImage(named: "passwordVisible")
  261. passwordTextField.isHidden = false
  262. passwordTextField1.isHidden = true
  263. passwordTextField.stringValue = viewModel.password
  264. } else {
  265. visibleButton.image = NSImage(named: "passwordUnVisible")
  266. passwordTextField.isHidden = true
  267. passwordTextField1.isHidden = false
  268. passwordTextField1.stringValue = viewModel.password
  269. }
  270. }
  271. private func textfieldInputState(isEmail: Bool) -> Void {
  272. if isEmail {
  273. if viewModel.emailError() {
  274. emailBox.borderColor = NSColor(named: "FA1E5D") ?? NSColor.red
  275. } else {
  276. emailBox.borderColor = NSColor(named: "DADBDE") ?? NSColor.gray
  277. }
  278. emailErrorLabel.isHidden = !viewModel.emailError()
  279. } else {
  280. if viewModel.passwordError() {
  281. if viewModel.signUpState == .verificationCode {
  282. passwordBox.borderWidth = 0
  283. verifficationBox.borderWidth = 1
  284. passwordBox.borderColor = NSColor(named: "DADBDE") ?? NSColor.gray
  285. verifficationBox.borderColor = NSColor(named: "FA1E5D") ?? NSColor.red
  286. } else if viewModel.signUpState == .password {
  287. passwordBox.borderWidth = 1
  288. verifficationBox.borderWidth = 0
  289. verifficationBox.borderColor = NSColor(named: "DADBDE") ?? NSColor.gray
  290. passwordBox.borderColor = NSColor(named: "FA1E5D") ?? NSColor.red
  291. }
  292. } else {
  293. if viewModel.signUpState == .verificationCode {
  294. passwordBox.borderWidth = 1
  295. verifficationBox.borderWidth = 0
  296. } else if viewModel.signUpState == .password {
  297. passwordBox.borderWidth = 1
  298. verifficationBox.borderWidth = 0
  299. }
  300. }
  301. passwordErrorLabel.isHidden = !viewModel.passwordError()
  302. }
  303. }
  304. private func sendBoxRefresh() -> Void {
  305. sendLabel.stringValue = viewModel.sendContent
  306. if viewModel.sendContent == NSLocalizedString("Send", tableName: "MemberCenterLocalizable", comment: "") ||
  307. viewModel.sendContent == NSLocalizedString("Resend", tableName: "MemberCenterLocalizable", comment: "") {
  308. if viewModel.email.count > 0 {
  309. if viewModel.isValidEmail() {
  310. sendBox.fillColor = NSColor(named: "273C62") ?? NSColor.blue
  311. } else {
  312. sendBox.fillColor = NSColor(named: "273C62_0.4") ?? NSColor.blue
  313. }
  314. } else {
  315. sendBox.fillColor = NSColor(named: "273C62_0.4") ?? NSColor.blue
  316. }
  317. sendLabel.textColor = NSColor(named: "FFFFFF") ?? NSColor.white
  318. } else {
  319. sendBox.fillColor = NSColor(named: "DADBDE") ?? NSColor.gray
  320. sendLabel.stringValue = String(format: "%@s", viewModel.sendContent)
  321. sendLabel.textColor = NSColor(named: "0E1114") ?? NSColor.black
  322. }
  323. }
  324. private func _showPop() {
  325. guard let _ = self.window else {
  326. return
  327. }
  328. if (self.popOver_ != nil) {
  329. return
  330. }
  331. let popViewController = KMToolbarItemPopViewController()
  332. self.popOver_ = NSPopover()
  333. self.popOver_?.contentViewController = popViewController
  334. self.popOver_?.animates = false
  335. self.popOver_?.behavior = .semitransient
  336. self.popOver_?.contentSize = popViewController.view.frame.size
  337. self.popOver_?.delegate = self
  338. // 0.8
  339. var color = NSColor(hex: "#273C62").withAlphaComponent(1)
  340. if KMAppearance.isDarkMode() {
  341. color = NSColor(hex: "#4E7FDB").withAlphaComponent(1)
  342. }
  343. popViewController.view.wantsLayer = true
  344. popViewController.view.layer?.backgroundColor = color.cgColor
  345. self.popOver_?.setBackgroundColor(color)
  346. popViewController.toolbarHelpTipLabel.textColor = .white
  347. popViewController.toolbarHelpTipLabel.font = .SFProTextRegularFont(13)
  348. popViewController.contentInset = .init(top: 12, left: 8, bottom: 12, right: 8)
  349. popViewController.updateWithHelpTip(helpTip: NSLocalizedString("Please agree and check the agreement first.", tableName: "MemberCenterLocalizable", comment: ""))
  350. self.popOver_?.show(relativeTo: NSMakeRect(0, 0, 0, 12), of: privacyCheckButton, preferredEdge: .maxY)
  351. }
  352. // MARK: Bind Method
  353. func bindViewModel() -> Void {
  354. viewModel.$isVisible
  355. .receive(on: RunLoop.main)
  356. .sink { [weak self] newValue in
  357. self?.visibleStateChange()
  358. }
  359. .store(in: &cancellables)
  360. viewModel.$stayState
  361. .receive(on: RunLoop.main)
  362. .sink { [weak self] newValue in
  363. self?.checkStateChange(button: self?.stayCheckButton, state: newValue)
  364. }
  365. .store(in: &cancellables)
  366. viewModel.$privacyState
  367. .receive(on: RunLoop.main)
  368. .sink { [weak self] newValue in
  369. self?.checkStateChange(button: self?.privacyCheckButton, state: newValue)
  370. self?.popOver_?.close()
  371. }
  372. .store(in: &cancellables)
  373. viewModel.$signUpState
  374. .receive(on: RunLoop.main)
  375. .sink { [weak self] newValue in
  376. self?.signUpStateChange()
  377. }
  378. .store(in: &cancellables)
  379. viewModel.$emailErrorMessage
  380. .receive(on: RunLoop.main)
  381. .sink { [weak self] newValue in
  382. self?.emailErrorLabel.stringValue = newValue
  383. self?.emailErrorLabel.isHidden = false
  384. self?.textfieldInputState(isEmail: true)
  385. }
  386. .store(in: &cancellables)
  387. viewModel.$passwordErrorMessage
  388. .receive(on: RunLoop.main)
  389. .sink { [weak self] newValue in
  390. self?.passwordErrorLabel.stringValue = newValue
  391. if self?.viewModel.passwordErrorMessage == "" {
  392. self?.passwordErrorLabel.isHidden = true
  393. } else {
  394. self?.passwordErrorLabel.isHidden = false
  395. }
  396. }
  397. .store(in: &cancellables)
  398. viewModel.$sendContent
  399. .receive(on: RunLoop.main)
  400. .sink { [weak self] newValue in
  401. self?.sendBoxRefresh()
  402. }
  403. .store(in: &cancellables)
  404. viewModel.$email
  405. .receive(on: RunLoop.main)
  406. .sink { [weak self] newValue in
  407. self?.sendBoxRefresh()
  408. }
  409. .store(in: &cancellables)
  410. viewModel.$verificationCode
  411. .receive(on: RunLoop.main)
  412. .sink { [weak self] newValue in
  413. if newValue.count <= 6 && newValue.count >= 0 {
  414. self?.viewModel.passwordErrorMessage = ""
  415. } else {
  416. self?.viewModel.passwordErrorMessage = NSLocalizedString("Verification code error.", tableName: "MemberCenterLocalizable", comment: "")
  417. }
  418. }
  419. .store(in: &cancellables)
  420. viewModel.$password
  421. .receive(on: RunLoop.main)
  422. .sink { [weak self] newValue in
  423. if newValue.count <= 30 && newValue.count >= 0 {
  424. self?.viewModel.passwordErrorMessage = ""
  425. } else {
  426. self?.viewModel.passwordErrorMessage = NSLocalizedString("Password cannot exceed 30 characters.", tableName: "MemberCenterLocalizable", comment: "")
  427. }
  428. }
  429. .store(in: &cancellables)
  430. viewModel.$privacyPopShow
  431. .receive(on: RunLoop.main)
  432. .sink { [weak self] newValue in
  433. if newValue {
  434. KMMainThreadExecute {
  435. self?._showPop()
  436. }
  437. }
  438. }
  439. .store(in: &cancellables)
  440. }
  441. // MARK: Action Method
  442. @IBAction func verificationCodeAction(_ sender: NSButton) {
  443. viewModel.signUpStateChange(state: .verificationCode)
  444. }
  445. @IBAction func passwordAction(_ sender: NSButton) {
  446. viewModel.signUpStateChange(state: .password)
  447. }
  448. @IBAction func visibleAction(_ sender: NSButton) {
  449. viewModel.isVisible.toggle()
  450. }
  451. @IBAction func stayCheckAction(_ sender: NSButton) {
  452. viewModel.stayState.toggle()
  453. viewModel.stayStateAction()
  454. }
  455. @IBAction func signUpAction(_ sender: NSButton) {
  456. self.window?.makeFirstResponder(nil)
  457. viewModel.emailErrorMessage = ""
  458. viewModel.passwordErrorMessage = ""
  459. window?.showWaitingView()
  460. viewModel.signUpAction { [weak self] result, _ in
  461. self?.window?.hideWaitingView()
  462. #if VERSION_DMG
  463. if result == nil || result == false {
  464. return
  465. }
  466. if KMDataManager.ud_bool(forKey: KMAdvancedAnnualTrailKey) == false {
  467. return
  468. }
  469. let info = KMMemberInfo.shared
  470. if info.canTrail {
  471. DispatchQueue.main.async {
  472. let winC = KMPurchaseEmbeddedWindowController.currentCode(KMAdvancedAnnualSubscriptionTrailCodeKey_DMG)
  473. winC.showWindow(nil)
  474. winC.window?.center()
  475. }
  476. KMDataManager.ud_set(false, forKey: KMAdvancedAnnualTrailKey)
  477. }
  478. #endif
  479. }
  480. }
  481. @IBAction func forgetAction(_ sender: NSButton) {
  482. guard let parentView = self.superview else { return }
  483. if parentView is NSBox {
  484. let model = KMSignUpViewModel()
  485. model.email = viewModel.email
  486. let forgotView = KMForgotPasswordView(model: model, superView: parentView)
  487. NSAnimationContext.runAnimationGroup { context in
  488. context.duration = 0.3
  489. self.animator().alphaValue = 0
  490. } completionHandler: {
  491. self.removeFromSuperview()
  492. forgotView.alphaValue = 0
  493. (parentView as! NSBox).contentView = forgotView
  494. NSAnimationContext.runAnimationGroup({ context in
  495. context.duration = 0.3
  496. forgotView.animator().alphaValue = 1
  497. }, completionHandler: nil)
  498. }
  499. } else {
  500. let model = KMSignUpViewModel()
  501. model.email = viewModel.email
  502. let forgotView = KMForgotPasswordView(model: model, superView: parentView)
  503. NSAnimationContext.runAnimationGroup { context in
  504. context.duration = 0.3
  505. self.animator().alphaValue = 0
  506. } completionHandler: {
  507. self.removeFromSuperview()
  508. forgotView.alphaValue = 0
  509. parentView.addSubview(forgotView)
  510. NSAnimationContext.runAnimationGroup({ context in
  511. context.duration = 0.3
  512. forgotView.animator().alphaValue = 1
  513. }, completionHandler: nil)
  514. }
  515. }
  516. }
  517. @IBAction func privacyCheckAction(_ sender: NSButton) {
  518. viewModel.privacyState.toggle()
  519. }
  520. }
  521. extension KMSignUpView: NSTextFieldDelegate {
  522. func controlTextDidEndEditing(_ obj: Notification) {
  523. let textField = obj.object as? NSTextField
  524. if textField == emailTextField {
  525. viewModel.email = textField!.stringValue
  526. } else if textField == verifficationTextField {
  527. viewModel.verificationCode = textField!.stringValue
  528. } else if textField == passwordTextField {
  529. viewModel.password = textField!.stringValue
  530. } else if textField == passwordTextField1 {
  531. viewModel.password = textField!.stringValue
  532. }
  533. }
  534. func controlTextDidChange(_ obj: Notification) {
  535. let textField = obj.object as? NSTextField
  536. if textField == emailTextField {
  537. viewModel.emailErrorMessage = ""
  538. viewModel.email = textField!.stringValue
  539. emailBox.borderColor = NSColor(named: "DADBDE") ?? NSColor.gray
  540. } else if textField == verifficationTextField {
  541. viewModel.passwordErrorMessage = ""
  542. viewModel.verificationCode = textField!.stringValue
  543. verifficationBox.borderColor = NSColor(named: "DADBDE") ?? NSColor.gray
  544. } else if textField == passwordTextField {
  545. viewModel.passwordErrorMessage = ""
  546. viewModel.password = textField!.stringValue
  547. passwordBox.borderColor = NSColor(named: "DADBDE") ?? NSColor.gray
  548. } else if textField == passwordTextField1 {
  549. viewModel.passwordErrorMessage = ""
  550. viewModel.password = textField!.stringValue
  551. passwordBox.borderColor = NSColor(named: "DADBDE") ?? NSColor.gray
  552. }
  553. }
  554. func control(_ control: NSControl, textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool {
  555. if commandSelector == #selector(NSResponder.insertTab) {
  556. if control.isEqual(to: emailTextField) {
  557. if viewModel.signUpState == .password {
  558. // window?.selectNextKeyView(passwordTextField)
  559. if viewModel.isVisible {
  560. window?.makeFirstResponder(passwordTextField)
  561. } else {
  562. window?.makeFirstResponder(passwordTextField1)
  563. }
  564. return true
  565. } else if viewModel.signUpState == .verificationCode {
  566. // window?.selectNextKeyView(verifficationTextField)
  567. window?.makeFirstResponder(verifficationTextField)
  568. return true
  569. }
  570. }
  571. }
  572. return false
  573. }
  574. @objc func loginSuccessNotification() -> Void {
  575. resetTextFileData()
  576. }
  577. }
  578. extension KMSignUpView: NSPopoverDelegate {
  579. func popoverDidClose(_ notification: Notification) {
  580. if let data = self.popOver_?.isEqual(to: notification.object), data {
  581. self.popOver_ = nil
  582. }
  583. }
  584. }