KMSignUpView.swift 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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 Up", 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 logged in", tableName: "MemberCenterLocalizable", comment: "")
  64. forgetButton.title = NSLocalizedString("Forget Password?", tableName: "MemberCenterLocalizable", comment: "")
  65. signUpButton.title = NSLocalizedString("Sign Up", tableName: "MemberCenterLocalizable", comment: "")
  66. emailErrorLabel.stringValue = String(format: "*%@", NSLocalizedString("Please enter right Address", tableName: "MemberCenterLocalizable", comment: ""))
  67. passwordErrorLabel.stringValue = String(format: "*%@", NSLocalizedString("Please enter right Verification code", tableName: "MemberCenterLocalizable", comment: ""))
  68. emailTextField.placeholderString = NSLocalizedString("Please enter Email", tableName: "MemberCenterLocalizable", comment: "")
  69. verifficationTextField.placeholderString = NSLocalizedString("Enter Verification code", tableName: "MemberCenterLocalizable", comment: "")
  70. passwordTextField.placeholderString = NSLocalizedString("Password", tableName: "MemberCenterLocalizable", comment: "")
  71. passwordTextField1.placeholderString = NSLocalizedString("Password", tableName: "MemberCenterLocalizable", comment: "")
  72. privacyCheckButton.toolTip = NSLocalizedString("Please first agree and check the above agreement.", 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 agree to the %@ and %@, and an account will be automatically created after I log in to an unregistered email address", comment: "")
  115. let specialOffer = NSLocalizedString("User Agreement", comment: "")
  116. let contactsUs = NSLocalizedString("Privacy Policy", 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("Enter Verification 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("Password", tableName: "MemberCenterLocalizable", comment: "")
  184. passwordTextField1.placeholderString = NSLocalizedString("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. } else {
  252. sendBox.fillColor = NSColor(named: "DADBDE") ?? NSColor.gray
  253. sendLabel.stringValue = String(format: "%@s", viewModel.sendContent)
  254. }
  255. }
  256. // MARK: Bind Method
  257. func bindViewModel() -> Void {
  258. viewModel.$isVisible
  259. .receive(on: RunLoop.main)
  260. .sink { [weak self] newValue in
  261. self?.visibleStateChange()
  262. }
  263. .store(in: &cancellables)
  264. viewModel.$stayState
  265. .receive(on: RunLoop.main)
  266. .sink { [weak self] newValue in
  267. self?.checkStateChange(button: self?.stayCheckButton, state: newValue)
  268. }
  269. .store(in: &cancellables)
  270. viewModel.$privacyState
  271. .receive(on: RunLoop.main)
  272. .sink { [weak self] newValue in
  273. self?.checkStateChange(button: self?.privacyCheckButton, state: newValue)
  274. }
  275. .store(in: &cancellables)
  276. viewModel.$signUpState
  277. .receive(on: RunLoop.main)
  278. .sink { [weak self] newValue in
  279. self?.signUpStateChange()
  280. }
  281. .store(in: &cancellables)
  282. viewModel.$emailErrorMessage
  283. .receive(on: RunLoop.main)
  284. .sink { [weak self] newValue in
  285. self?.emailErrorLabel.stringValue = newValue
  286. self?.emailErrorLabel.isHidden = false
  287. }
  288. .store(in: &cancellables)
  289. viewModel.$passwordErrorMessage
  290. .receive(on: RunLoop.main)
  291. .sink { [weak self] newValue in
  292. self?.passwordErrorLabel.stringValue = newValue
  293. self?.passwordErrorLabel.isHidden = false
  294. }
  295. .store(in: &cancellables)
  296. viewModel.$sendContent
  297. .receive(on: RunLoop.main)
  298. .sink { [weak self] newValue in
  299. self?.sendBoxRefresh()
  300. }
  301. .store(in: &cancellables)
  302. viewModel.$email
  303. .receive(on: RunLoop.main)
  304. .sink { [weak self] newValue in
  305. self?.sendBoxRefresh()
  306. }
  307. .store(in: &cancellables)
  308. viewModel.$verificationCode
  309. .receive(on: RunLoop.main)
  310. .sink { [weak self] newValue in
  311. if newValue.count <= 6 && newValue.count >= 0 {
  312. self?.viewModel.passwordErrorMessage = ""
  313. } else {
  314. self?.viewModel.passwordErrorMessage = NSLocalizedString("*Please enter right Verification code", tableName: "MemberCenterLocalizable", comment: "")
  315. }
  316. }
  317. .store(in: &cancellables)
  318. }
  319. // MARK: Action Method
  320. @IBAction func verificationCodeAction(_ sender: NSButton) {
  321. viewModel.signUpStateChange(state: .verificationCode)
  322. }
  323. @IBAction func passwordAction(_ sender: NSButton) {
  324. viewModel.signUpStateChange(state: .password)
  325. }
  326. @IBAction func visibleAction(_ sender: NSButton) {
  327. viewModel.isVisible.toggle()
  328. }
  329. @IBAction func stayCheckAction(_ sender: NSButton) {
  330. viewModel.stayState.toggle()
  331. }
  332. @IBAction func signUpAction(_ sender: NSButton) {
  333. viewModel.emailErrorMessage = ""
  334. viewModel.passwordErrorMessage = ""
  335. viewModel.signUpAction()
  336. }
  337. @IBAction func forgetAction(_ sender: NSButton) {
  338. guard let parentView = self.superview else { return }
  339. if parentView is NSBox {
  340. let model = KMSignUpViewModel()
  341. model.email = viewModel.email
  342. let forgotView = KMForgotPasswordView(model: model, superView: parentView)
  343. NSAnimationContext.runAnimationGroup { context in
  344. context.duration = 0.3
  345. self.animator().alphaValue = 0
  346. } completionHandler: {
  347. self.removeFromSuperview()
  348. forgotView.alphaValue = 0
  349. (parentView as! NSBox).contentView = forgotView
  350. NSAnimationContext.runAnimationGroup({ context in
  351. context.duration = 0.3
  352. forgotView.animator().alphaValue = 1
  353. }, completionHandler: nil)
  354. }
  355. } else {
  356. let model = KMSignUpViewModel()
  357. model.email = viewModel.email
  358. let forgotView = KMForgotPasswordView(model: model, superView: parentView)
  359. NSAnimationContext.runAnimationGroup { context in
  360. context.duration = 0.3
  361. self.animator().alphaValue = 0
  362. } completionHandler: {
  363. self.removeFromSuperview()
  364. forgotView.alphaValue = 0
  365. parentView.addSubview(forgotView)
  366. NSAnimationContext.runAnimationGroup({ context in
  367. context.duration = 0.3
  368. forgotView.animator().alphaValue = 1
  369. }, completionHandler: nil)
  370. }
  371. }
  372. }
  373. @IBAction func privacyCheckAction(_ sender: NSButton) {
  374. viewModel.privacyState.toggle()
  375. }
  376. }
  377. extension KMSignUpView: NSTextFieldDelegate {
  378. func controlTextDidEndEditing(_ obj: Notification) {
  379. let textField = obj.object as? NSTextField
  380. if textField == emailTextField {
  381. viewModel.email = textField!.stringValue
  382. } else if textField == verifficationTextField {
  383. viewModel.verificationCode = textField!.stringValue
  384. } else if textField == passwordTextField {
  385. viewModel.password = textField!.stringValue
  386. } else if textField == passwordTextField1 {
  387. viewModel.password = textField!.stringValue
  388. }
  389. }
  390. func controlTextDidChange(_ obj: Notification) {
  391. let textField = obj.object as? NSTextField
  392. if textField == emailTextField {
  393. viewModel.email = textField!.stringValue
  394. } else if textField == verifficationTextField {
  395. viewModel.verificationCode = textField!.stringValue
  396. } else if textField == passwordTextField {
  397. viewModel.password = textField!.stringValue
  398. } else if textField == passwordTextField1 {
  399. viewModel.password = textField!.stringValue
  400. }
  401. }
  402. }