KMSignUpView.swift 19 KB

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