KMSignUpView.swift 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. convenience init(model: KMSignUpViewModel, superView: NSView) {
  40. self.init(frame: superView.bounds)
  41. viewModel = model
  42. bindViewModel()
  43. languageLocalized()
  44. initializeUI()
  45. }
  46. public override init(frame frameRect: NSRect) {
  47. super.init(frame: frameRect)
  48. }
  49. public required init?(coder decoder: NSCoder) {
  50. fatalError("init(coder:) has not been implemented")
  51. }
  52. override func updateUI() {
  53. super.updateUI()
  54. bindViewModel()
  55. languageLocalized()
  56. initializeUI()
  57. }
  58. // MARK: Private Method
  59. private func languageLocalized() -> Void {
  60. signUpLabel.stringValue = NSLocalizedString("Sign in(titile)", tableName: "MemberCenterLocalizable", comment: "")
  61. verificationCodeButton.title = NSLocalizedString("Verification Code", tableName: "MemberCenterLocalizable", comment: "")
  62. passwordButton.title = NSLocalizedString("Password", tableName: "MemberCenterLocalizable", comment: "")
  63. stayLabel.stringValue = NSLocalizedString("Stay signed in", tableName: "MemberCenterLocalizable", comment: "")
  64. forgetButton.title = NSLocalizedString("Forgot password?", tableName: "MemberCenterLocalizable", comment: "")
  65. signUpButton.title = NSLocalizedString("Sign in(button)", tableName: "MemberCenterLocalizable", comment: "")
  66. emailErrorLabel.stringValue = String(format: "*%@", NSLocalizedString("Email format error. Please enter the correct email.", tableName: "MemberCenterLocalizable", comment: ""))
  67. passwordErrorLabel.stringValue = String(format: "*%@", NSLocalizedString("Verification code error.", tableName: "MemberCenterLocalizable", comment: ""))
  68. emailTextField.placeholderString = NSLocalizedString("Please enter email address", tableName: "MemberCenterLocalizable", comment: "")
  69. verifficationTextField.placeholderString = NSLocalizedString("Please enter code", tableName: "MemberCenterLocalizable", comment: "")
  70. passwordTextField.placeholderString = NSLocalizedString("Please enter password", tableName: "MemberCenterLocalizable", comment: "")
  71. passwordTextField1.placeholderString = NSLocalizedString("Please enter password", tableName: "MemberCenterLocalizable", comment: "")
  72. privacyCheckButton.toolTip = NSLocalizedString("Please agree and check the above agreement first.", tableName: "MemberCenterLocalizable", comment: "")
  73. emailTextField.stringValue = viewModel.email
  74. verifficationTextField.stringValue = viewModel.verificationCode
  75. passwordTextField.stringValue = viewModel.password
  76. passwordTextField1.stringValue = viewModel.password
  77. }
  78. private func initializeUI() -> Void {
  79. emailTextField.delegate = self
  80. verifficationTextField.delegate = self
  81. passwordTextField.delegate = self
  82. passwordTextField1.delegate = self
  83. emailErrorLabel.isHidden = !viewModel.emailError()
  84. passwordErrorLabel.isHidden = !viewModel.passwordError()
  85. signUpLabel.textColor = NSColor(named: "000000")
  86. signUpLabel.font = NSFont.SFMediumFontWithSize(20)
  87. selectBox1.fillColor = NSColor(named: "4982E6") ?? NSColor.blue
  88. emailErrorLabel.textColor = NSColor(named: "FA1E5D")
  89. emailErrorLabel.font = NSFont.SFProTextRegularFont(9)
  90. passwordBox.borderColor = NSColor(named: "DADBDE") ?? NSColor.gray
  91. verifficationBox.borderColor = NSColor(named: "FA1E5D") ?? NSColor.gray
  92. if viewModel.isValidEmail() {
  93. sendBox.fillColor = NSColor(named: "273C62") ?? NSColor.blue
  94. } else {
  95. sendBox.fillColor = NSColor(named: "273C62_0.4") ?? NSColor.blue
  96. }
  97. sendBox.borderColor = NSColor(named: "273C62") ?? NSColor.blue
  98. sendLabel.textColor = NSColor(named: "FFFFFF") ?? NSColor.white
  99. sendLabel.font = NSFont.SFProTextRegularFont(16)
  100. passwordErrorLabel.textColor = NSColor(named: "FA1E5D")
  101. passwordErrorLabel.font = NSFont.SFProTextRegularFont(9)
  102. stayCheckButton.image = NSImage(named: "CheckBoxNor")
  103. stayLabel.textColor = NSColor(named: "0E1114") ?? NSColor.black
  104. stayLabel.font = NSFont.SFProTextRegularFont(12)
  105. forgetButton.setTitleColor(color: NSColor(named: "4982E6") ?? NSColor.blue, font: NSFont.SFProTextRegularFont(12))
  106. signUpBox.fillColor = NSColor(named: "273C62") ?? NSColor.blue
  107. signUpButton.setTitleColor(color: NSColor(named: "FFFFFF") ?? NSColor.white, font: NSFont.SFProTextRegularFont(16))
  108. privacyCheckButton.image = NSImage(named: "CheckBoxNor")
  109. privacyLabel.isEditable = false
  110. privacyLabel.isSelectable = true
  111. privacyLabel.allowsEditingTextAttributes = true
  112. privacyLabel.textColor = NSColor.black
  113. privacyLabel.font = NSFont.SFProTextRegularFont(16.0)
  114. 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: "")
  115. let specialOffer = NSLocalizedString("Terms of Service", tableName: "MemberCenterLocalizable", comment: "")
  116. let contactsUs = NSLocalizedString("Privacy Policy", tableName: "MemberCenterLocalizable", comment: "")
  117. let fullString = String(format: tipsString, specialOffer, contactsUs)
  118. let attributedString = NSMutableAttributedString(string: fullString)
  119. // 定义链接的范围
  120. let specialOfferRange = (fullString as NSString).range(of: specialOffer)
  121. let contactsUsRange = (fullString as NSString).range(of: contactsUs)
  122. let linkColor = NSColor(named: "4982E6") ?? NSColor.blue
  123. let font = NSFont.SFProTextRegularFont(11.0) // 与普通文本相同的字体
  124. attributedString.addAttributes([
  125. .foregroundColor: NSColor(named: "0E1114") ?? NSColor.black as Any,
  126. .font: font
  127. ], range: (fullString as NSString).range(of: fullString))
  128. attributedString.addAttributes([
  129. .foregroundColor: linkColor,
  130. .link: NSLocalizedString("https://www.pdfreaderpro.com/terms_of_service", comment: ""),
  131. .font: font
  132. ], range: specialOfferRange)
  133. attributedString.addAttributes([
  134. .foregroundColor: linkColor,
  135. .link: NSLocalizedString("https://www.pdfreaderpro.com/privacy-policy", comment: ""),
  136. .font: font
  137. ], range: contactsUsRange)
  138. privacyLabel.attributedStringValue = attributedString
  139. signUpStateChange()
  140. visibleStateChange()
  141. textfieldInputState(isEmail: true)
  142. textfieldInputState(isEmail: false)
  143. sendBoxRefresh()
  144. sendBox.moveCallback = { [weak self](mouseEntered: Bool, mouseBox: KMBox) -> Void in
  145. guard let self = self else { return }
  146. if self.viewModel.email.count <= 0 { return }
  147. if self.viewModel.sendBoxSelect { return }
  148. if !self.viewModel.isValidEmail() { return }
  149. if mouseEntered {
  150. self.sendBox.fillColor = NSColor(named: "000000_0.1") ?? NSColor.blue
  151. self.sendBox.borderWidth = 1
  152. } else {
  153. self.sendBox.fillColor = NSColor(named: "273C62") ?? NSColor.blue
  154. }
  155. }
  156. sendBox.downCallback = { [weak self](downEntered: Bool, mouseBox: KMBox, event) -> Void in
  157. guard let self = self else { return }
  158. if self.viewModel.email.count <= 0 { return }
  159. if self.viewModel.sendBoxSelect { return }
  160. if downEntered {
  161. self.sendBox.fillColor = NSColor(named: "273C62_0.4") ?? NSColor.blue
  162. viewModel.countDown(type: .login)
  163. }
  164. }
  165. }
  166. private func signUpStateChange() -> Void {
  167. if viewModel.signUpState == .verificationCode {
  168. selectBox1.isHidden = false
  169. selectBox2.isHidden = true
  170. verificationCodeButton.setTitleColor(color: NSColor(named: "4982E6") ?? NSColor.black, font: NSFont.SFProTextRegularFont(14))
  171. passwordButton.setTitleColor(color: NSColor(named: "42464D") ?? NSColor.black, font: NSFont.SFProTextRegularFont(14))
  172. verifficationView.isHidden = false
  173. passwordView.isHidden = true
  174. verifficationTextField.placeholderString = NSLocalizedString("Please enter code", tableName: "MemberCenterLocalizable", comment: "")
  175. forgetButton.isHidden = true
  176. } else if viewModel.signUpState == .password {
  177. selectBox1.isHidden = true
  178. selectBox2.isHidden = false
  179. verificationCodeButton.setTitleColor(color: NSColor(named: "42464D") ?? NSColor.black, font: NSFont.SFProTextRegularFont(14))
  180. passwordButton.setTitleColor(color: NSColor(named: "4982E6") ?? NSColor.black, font: NSFont.SFProTextRegularFont(14))
  181. verifficationView.isHidden = true
  182. passwordView.isHidden = false
  183. passwordTextField.placeholderString = NSLocalizedString("Please enter password", tableName: "MemberCenterLocalizable", comment: "")
  184. passwordTextField1.placeholderString = NSLocalizedString("Please enter password", tableName: "MemberCenterLocalizable", comment: "")
  185. forgetButton.isHidden = false
  186. }
  187. }
  188. private func checkStateChange(button: NSButton!, state: Bool) -> Void {
  189. button.state = state ? .on : .off
  190. if button.state == .on {
  191. button.image = NSImage(named: "CheckBoxSel")
  192. } else {
  193. button.image = NSImage(named: "CheckBoxNor")
  194. }
  195. }
  196. private func visibleStateChange() -> Void {
  197. if viewModel.isVisible {
  198. visibleButton.image = NSImage(named: "passwordUnVisible")
  199. passwordTextField.isHidden = false
  200. passwordTextField1.isHidden = true
  201. passwordTextField.stringValue = viewModel.password
  202. } else {
  203. visibleButton.image = NSImage(named: "passwordVisible")
  204. passwordTextField.isHidden = true
  205. passwordTextField1.isHidden = false
  206. passwordTextField1.stringValue = viewModel.password
  207. }
  208. }
  209. private func textfieldInputState(isEmail: Bool) -> Void {
  210. if isEmail {
  211. if viewModel.emailError() {
  212. emailBox.borderColor = NSColor(named: "FA1E5D") ?? NSColor.red
  213. } else {
  214. emailBox.borderColor = NSColor(named: "DADBDE") ?? NSColor.gray
  215. }
  216. emailErrorLabel.isHidden = !viewModel.emailError()
  217. } else {
  218. if viewModel.passwordError() {
  219. if viewModel.signUpState == .verificationCode {
  220. passwordBox.borderWidth = 0
  221. verifficationBox.borderWidth = 1
  222. } else if viewModel.signUpState == .password {
  223. passwordBox.borderWidth = 1
  224. verifficationBox.borderWidth = 0
  225. }
  226. } else {
  227. if viewModel.signUpState == .verificationCode {
  228. passwordBox.borderWidth = 1
  229. verifficationBox.borderWidth = 0
  230. } else if viewModel.signUpState == .password {
  231. passwordBox.borderWidth = 1
  232. verifficationBox.borderWidth = 0
  233. }
  234. }
  235. passwordErrorLabel.isHidden = !viewModel.passwordError()
  236. }
  237. }
  238. private func sendBoxRefresh() -> Void {
  239. sendLabel.stringValue = viewModel.sendContent
  240. if viewModel.sendContent == NSLocalizedString("Send", tableName: "MemberCenterLocalizable", comment: "") ||
  241. viewModel.sendContent == NSLocalizedString("Resend", tableName: "MemberCenterLocalizable", comment: "") {
  242. if viewModel.email.count > 0 {
  243. if viewModel.isValidEmail() {
  244. sendBox.fillColor = NSColor(named: "273C62") ?? NSColor.blue
  245. } else {
  246. sendBox.fillColor = NSColor(named: "273C62_0.4") ?? NSColor.blue
  247. }
  248. } else {
  249. sendBox.fillColor = NSColor(named: "273C62_0.4") ?? NSColor.blue
  250. }
  251. sendLabel.textColor = NSColor(named: "FFFFFF") ?? NSColor.white
  252. } else {
  253. sendBox.fillColor = NSColor(named: "DADBDE") ?? NSColor.gray
  254. sendLabel.stringValue = String(format: "%@s", viewModel.sendContent)
  255. sendLabel.textColor = NSColor(named: "0E1114") ?? NSColor.black
  256. }
  257. }
  258. // MARK: Bind Method
  259. func bindViewModel() -> Void {
  260. viewModel.$isVisible
  261. .receive(on: RunLoop.main)
  262. .sink { [weak self] newValue in
  263. self?.visibleStateChange()
  264. }
  265. .store(in: &cancellables)
  266. viewModel.$stayState
  267. .receive(on: RunLoop.main)
  268. .sink { [weak self] newValue in
  269. self?.checkStateChange(button: self?.stayCheckButton, state: newValue)
  270. }
  271. .store(in: &cancellables)
  272. viewModel.$privacyState
  273. .receive(on: RunLoop.main)
  274. .sink { [weak self] newValue in
  275. self?.checkStateChange(button: self?.privacyCheckButton, state: newValue)
  276. }
  277. .store(in: &cancellables)
  278. viewModel.$signUpState
  279. .receive(on: RunLoop.main)
  280. .sink { [weak self] newValue in
  281. self?.signUpStateChange()
  282. }
  283. .store(in: &cancellables)
  284. viewModel.$emailErrorMessage
  285. .receive(on: RunLoop.main)
  286. .sink { [weak self] newValue in
  287. self?.emailErrorLabel.stringValue = newValue
  288. self?.emailErrorLabel.isHidden = false
  289. }
  290. .store(in: &cancellables)
  291. viewModel.$passwordErrorMessage
  292. .receive(on: RunLoop.main)
  293. .sink { [weak self] newValue in
  294. self?.passwordErrorLabel.stringValue = newValue
  295. if self?.viewModel.passwordErrorMessage == "" {
  296. self?.passwordErrorLabel.isHidden = true
  297. } else {
  298. self?.passwordErrorLabel.isHidden = false
  299. }
  300. }
  301. .store(in: &cancellables)
  302. viewModel.$sendContent
  303. .receive(on: RunLoop.main)
  304. .sink { [weak self] newValue in
  305. self?.sendBoxRefresh()
  306. }
  307. .store(in: &cancellables)
  308. viewModel.$email
  309. .receive(on: RunLoop.main)
  310. .sink { [weak self] newValue in
  311. self?.sendBoxRefresh()
  312. }
  313. .store(in: &cancellables)
  314. viewModel.$verificationCode
  315. .receive(on: RunLoop.main)
  316. .sink { [weak self] newValue in
  317. if newValue.count <= 6 && newValue.count >= 0 {
  318. self?.viewModel.passwordErrorMessage = ""
  319. } else {
  320. self?.viewModel.passwordErrorMessage = NSLocalizedString("Verification code error.", tableName: "MemberCenterLocalizable", comment: "")
  321. }
  322. }
  323. .store(in: &cancellables)
  324. viewModel.$password
  325. .receive(on: RunLoop.main)
  326. .sink { [weak self] newValue in
  327. if newValue.count <= 30 && newValue.count >= 0 {
  328. self?.viewModel.passwordErrorMessage = ""
  329. } else {
  330. self?.viewModel.passwordErrorMessage = NSLocalizedString("Account or password error.", tableName: "MemberCenterLocalizable", comment: "")
  331. }
  332. }
  333. .store(in: &cancellables)
  334. }
  335. // MARK: Action Method
  336. @IBAction func verificationCodeAction(_ sender: NSButton) {
  337. viewModel.signUpStateChange(state: .verificationCode)
  338. }
  339. @IBAction func passwordAction(_ sender: NSButton) {
  340. viewModel.signUpStateChange(state: .password)
  341. }
  342. @IBAction func visibleAction(_ sender: NSButton) {
  343. viewModel.isVisible.toggle()
  344. }
  345. @IBAction func stayCheckAction(_ sender: NSButton) {
  346. viewModel.stayState.toggle()
  347. viewModel.stayStateAction()
  348. }
  349. @IBAction func signUpAction(_ sender: NSButton) {
  350. viewModel.emailErrorMessage = ""
  351. viewModel.passwordErrorMessage = ""
  352. viewModel.signUpAction()
  353. }
  354. @IBAction func forgetAction(_ sender: NSButton) {
  355. guard let parentView = self.superview else { return }
  356. if parentView is NSBox {
  357. let model = KMSignUpViewModel()
  358. model.email = viewModel.email
  359. let forgotView = KMForgotPasswordView(model: model, superView: parentView)
  360. NSAnimationContext.runAnimationGroup { context in
  361. context.duration = 0.3
  362. self.animator().alphaValue = 0
  363. } completionHandler: {
  364. self.removeFromSuperview()
  365. forgotView.alphaValue = 0
  366. (parentView as! NSBox).contentView = forgotView
  367. NSAnimationContext.runAnimationGroup({ context in
  368. context.duration = 0.3
  369. forgotView.animator().alphaValue = 1
  370. }, completionHandler: nil)
  371. }
  372. } else {
  373. let model = KMSignUpViewModel()
  374. model.email = viewModel.email
  375. let forgotView = KMForgotPasswordView(model: model, superView: parentView)
  376. NSAnimationContext.runAnimationGroup { context in
  377. context.duration = 0.3
  378. self.animator().alphaValue = 0
  379. } completionHandler: {
  380. self.removeFromSuperview()
  381. forgotView.alphaValue = 0
  382. parentView.addSubview(forgotView)
  383. NSAnimationContext.runAnimationGroup({ context in
  384. context.duration = 0.3
  385. forgotView.animator().alphaValue = 1
  386. }, completionHandler: nil)
  387. }
  388. }
  389. }
  390. @IBAction func privacyCheckAction(_ sender: NSButton) {
  391. viewModel.privacyState.toggle()
  392. }
  393. }
  394. extension KMSignUpView: NSTextFieldDelegate {
  395. func controlTextDidEndEditing(_ obj: Notification) {
  396. let textField = obj.object as? NSTextField
  397. if textField == emailTextField {
  398. viewModel.email = textField!.stringValue
  399. } else if textField == verifficationTextField {
  400. viewModel.verificationCode = textField!.stringValue
  401. } else if textField == passwordTextField {
  402. viewModel.password = textField!.stringValue
  403. } else if textField == passwordTextField1 {
  404. viewModel.password = textField!.stringValue
  405. }
  406. }
  407. func controlTextDidChange(_ obj: Notification) {
  408. let textField = obj.object as? NSTextField
  409. if textField == emailTextField {
  410. viewModel.email = textField!.stringValue
  411. } else if textField == verifficationTextField {
  412. viewModel.verificationCode = textField!.stringValue
  413. } else if textField == passwordTextField {
  414. viewModel.password = textField!.stringValue
  415. } else if textField == passwordTextField1 {
  416. viewModel.password = textField!.stringValue
  417. }
  418. }
  419. }