KMSignUpView.swift 33 KB

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