KMSignUpView.swift 21 KB

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