KMSignUpView.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. override func draw(_ dirtyRect: NSRect) {
  40. super.draw(dirtyRect)
  41. // Drawing code here.
  42. bindViewModel()
  43. languageLocalized()
  44. initializeUI()
  45. }
  46. // MARK: Private Method
  47. private func languageLocalized() -> Void {
  48. signUpLabel.stringValue = NSLocalizedString("Sign Up", tableName: "MemberCenterLocalizable", comment: "")
  49. verificationCodeButton.title = NSLocalizedString("Verification code", tableName: "MemberCenterLocalizable", comment: "")
  50. passwordButton.title = NSLocalizedString("Password", tableName: "MemberCenterLocalizable", comment: "")
  51. // sendLabel.stringValue = NSLocalizedString("Send", tableName: "MemberCenterLocalizable", comment: "")
  52. stayLabel.stringValue = NSLocalizedString("Stay logged in", tableName: "MemberCenterLocalizable", comment: "")
  53. forgetButton.title = NSLocalizedString("Forget Password?", tableName: "MemberCenterLocalizable", comment: "")
  54. signUpButton.title = NSLocalizedString("Sign Up", tableName: "MemberCenterLocalizable", comment: "")
  55. emailErrorLabel.stringValue = String(format: "*%@", NSLocalizedString("Please enter right Address", tableName: "MemberCenterLocalizable", comment: ""))
  56. passwordErrorLabel.stringValue = String(format: "*%@", NSLocalizedString("Please enter right Verification code", tableName: "MemberCenterLocalizable", comment: ""))
  57. emailTextField.placeholderString = NSLocalizedString("Please enter Email", tableName: "MemberCenterLocalizable", comment: "")
  58. verifficationTextField.placeholderString = NSLocalizedString("Enter Verification code", tableName: "MemberCenterLocalizable", comment: "")
  59. passwordTextField.placeholderString = NSLocalizedString("Password", tableName: "MemberCenterLocalizable", comment: "")
  60. passwordTextField1.placeholderString = NSLocalizedString("Password", tableName: "MemberCenterLocalizable", comment: "")
  61. privacyCheckButton.toolTip = NSLocalizedString("Please first agree and check the above agreement.", tableName: "MemberCenterLocalizable", comment: "")
  62. }
  63. private func initializeUI() -> Void {
  64. emailTextField.delegate = self
  65. verifficationTextField.delegate = self
  66. passwordTextField.delegate = self
  67. passwordTextField1.delegate = self
  68. signUpLabel.textColor = NSColor(named: "000000")
  69. signUpLabel.font = NSFont.SFMediumFontWithSize(20)
  70. selectBox1.fillColor = NSColor(named: "4982E6") ?? NSColor.blue
  71. emailErrorLabel.isHidden = true
  72. emailErrorLabel.textColor = NSColor(named: "FA1E5D")
  73. emailErrorLabel.font = NSFont.SFProTextRegularFont(9)
  74. passwordBox.borderColor = NSColor(named: "DADBDE") ?? NSColor.gray
  75. verifficationBox.borderColor = NSColor(named: "FA1E5D") ?? NSColor.gray
  76. sendBox.fillColor = NSColor(named: "273C62") ?? NSColor.blue
  77. sendBox.borderColor = NSColor(named: "273C62") ?? NSColor.blue
  78. sendLabel.textColor = NSColor(named: "FFFFFF") ?? NSColor.white
  79. sendLabel.font = NSFont.SFProTextRegularFont(16)
  80. passwordErrorLabel.isHidden = true
  81. passwordErrorLabel.textColor = NSColor(named: "FA1E5D")
  82. passwordErrorLabel.font = NSFont.SFProTextRegularFont(9)
  83. stayCheckButton.image = NSImage(named: "CheckBoxNor")
  84. stayLabel.textColor = NSColor(named: "0E1114") ?? NSColor.black
  85. stayLabel.font = NSFont.SFProTextRegularFont(12)
  86. forgetButton.setTitleColor(color: NSColor(named: "4982E6") ?? NSColor.blue, font: NSFont.SFProTextRegularFont(12))
  87. signUpBox.fillColor = NSColor(named: "273C62") ?? NSColor.blue
  88. signUpButton.setTitleColor(color: NSColor(named: "FFFFFF") ?? NSColor.white, font: NSFont.SFProTextRegularFont(16))
  89. privacyCheckButton.image = NSImage(named: "CheckBoxNor")
  90. privacyLabel.isEditable = false
  91. privacyLabel.isSelectable = true
  92. privacyLabel.allowsEditingTextAttributes = true
  93. privacyLabel.textColor = NSColor.black
  94. privacyLabel.font = NSFont.SFProTextRegularFont(16.0)
  95. 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: "")
  96. let specialOffer = NSLocalizedString("User Agreement", comment: "")
  97. let contactsUs = NSLocalizedString("Privacy Policy", comment: "")
  98. let fullString = String(format: tipsString, specialOffer, contactsUs)
  99. let attributedString = NSMutableAttributedString(string: fullString)
  100. // 定义链接的范围
  101. let specialOfferRange = (fullString as NSString).range(of: specialOffer)
  102. let contactsUsRange = (fullString as NSString).range(of: contactsUs)
  103. let linkColor = NSColor(named: "4982E6") ?? NSColor.blue
  104. let font = NSFont.SFProTextRegularFont(11.0) // 与普通文本相同的字体
  105. attributedString.addAttributes([
  106. .foregroundColor: NSColor(named: "0E1114") ?? NSColor.black as Any,
  107. .font: font
  108. ], range: (fullString as NSString).range(of: fullString))
  109. attributedString.addAttributes([
  110. .foregroundColor: linkColor,
  111. .link: NSLocalizedString("https://www.pdfreaderpro.com/store?mode=edu", comment: ""),
  112. .font: font
  113. ], range: specialOfferRange)
  114. attributedString.addAttributes([
  115. .foregroundColor: linkColor,
  116. .link: NSLocalizedString("https://www.pdfreaderpro.com/vpp-purchase-program", comment: ""),
  117. .font: font
  118. ], range: contactsUsRange)
  119. privacyLabel.attributedStringValue = attributedString
  120. signUpStateChange()
  121. visibleStateChange()
  122. textfieldInputState(isEmail: true)
  123. textfieldInputState(isEmail: false)
  124. sendBoxRefresh()
  125. sendBox.moveCallback = { [weak self](mouseEntered: Bool, mouseBox: KMBox) -> Void in
  126. guard let self = self else { return }
  127. if self.viewModel.email.count <= 0 { return }
  128. if self.viewModel.sendBoxSelect { return }
  129. if mouseEntered {
  130. self.sendBox.fillColor = NSColor(named: "000000_0.1") ?? NSColor.blue
  131. self.sendBox.borderWidth = 1
  132. } else {
  133. self.sendBox.fillColor = NSColor(named: "273C62") ?? NSColor.blue
  134. }
  135. }
  136. sendBox.downCallback = { [weak self](downEntered: Bool, mouseBox: KMBox, event) -> Void in
  137. guard let self = self else { return }
  138. if self.viewModel.email.count <= 0 { return }
  139. if self.viewModel.sendBoxSelect { return }
  140. if downEntered {
  141. self.sendBox.fillColor = NSColor(named: "273C62_0.4") ?? NSColor.blue
  142. viewModel.countDown()
  143. }
  144. }
  145. }
  146. private func signUpStateChange() -> Void {
  147. if viewModel.signUpState == .verificationCode {
  148. selectBox1.isHidden = false
  149. selectBox2.isHidden = true
  150. verificationCodeButton.setTitleColor(color: NSColor(named: "4982E6") ?? NSColor.black, font: NSFont.SFProTextRegularFont(14))
  151. passwordButton.setTitleColor(color: NSColor(named: "42464D") ?? NSColor.black, font: NSFont.SFProTextRegularFont(14))
  152. verifficationView.isHidden = false
  153. passwordView.isHidden = true
  154. verifficationTextField.placeholderString = NSLocalizedString("Enter Verification code", tableName: "MemberCenterLocalizable", comment: "")
  155. forgetButton.isHidden = true
  156. } else if viewModel.signUpState == .password {
  157. selectBox1.isHidden = true
  158. selectBox2.isHidden = false
  159. verificationCodeButton.setTitleColor(color: NSColor(named: "42464D") ?? NSColor.black, font: NSFont.SFProTextRegularFont(14))
  160. passwordButton.setTitleColor(color: NSColor(named: "4982E6") ?? NSColor.black, font: NSFont.SFProTextRegularFont(14))
  161. verifficationView.isHidden = true
  162. passwordView.isHidden = false
  163. passwordTextField.placeholderString = NSLocalizedString("Password", tableName: "MemberCenterLocalizable", comment: "")
  164. passwordTextField1.placeholderString = NSLocalizedString("Password", tableName: "MemberCenterLocalizable", comment: "")
  165. forgetButton.isHidden = false
  166. }
  167. }
  168. private func checkStateChange(button: NSButton!, state: Bool) -> Void {
  169. button.state = state ? .on : .off
  170. if button.state == .on {
  171. button.image = NSImage(named: "CheckBoxSel")
  172. } else {
  173. button.image = NSImage(named: "CheckBoxNor")
  174. }
  175. }
  176. private func visibleStateChange() -> Void {
  177. if viewModel.isVisible {
  178. visibleButton.image = NSImage(named: "passwordUnVisible")
  179. passwordTextField.isHidden = false
  180. passwordTextField1.isHidden = true
  181. passwordTextField.stringValue = viewModel.password
  182. } else {
  183. visibleButton.image = NSImage(named: "passwordVisible")
  184. passwordTextField.isHidden = true
  185. passwordTextField1.isHidden = false
  186. passwordTextField1.stringValue = viewModel.password
  187. }
  188. }
  189. private func textfieldInputState(isEmail: Bool) -> Void {
  190. if isEmail {
  191. if viewModel.emailError {
  192. emailBox.borderColor = NSColor(named: "FA1E5D") ?? NSColor.red
  193. } else {
  194. emailBox.borderColor = NSColor(named: "DADBDE") ?? NSColor.gray
  195. }
  196. emailErrorLabel.isHidden = !viewModel.emailError
  197. } else {
  198. if viewModel.passwordError {
  199. if viewModel.signUpState == .verificationCode {
  200. passwordBox.borderWidth = 0
  201. verifficationBox.borderWidth = 1
  202. } else if viewModel.signUpState == .password {
  203. passwordBox.borderWidth = 1
  204. verifficationBox.borderWidth = 0
  205. }
  206. } else {
  207. if viewModel.signUpState == .verificationCode {
  208. passwordBox.borderWidth = 1
  209. verifficationBox.borderWidth = 0
  210. } else if viewModel.signUpState == .password {
  211. passwordBox.borderWidth = 1
  212. verifficationBox.borderWidth = 0
  213. }
  214. }
  215. passwordErrorLabel.isHidden = !viewModel.passwordError
  216. }
  217. }
  218. private func sendBoxRefresh() -> Void {
  219. sendLabel.stringValue = viewModel.sendContent
  220. if viewModel.sendContent == NSLocalizedString("Send", tableName: "MemberCenterLocalizable", comment: "") {
  221. if viewModel.email.count > 0 {
  222. sendBox.fillColor = NSColor(named: "273C62") ?? NSColor.blue
  223. } else {
  224. sendBox.fillColor = NSColor(named: "273C62_0.4") ?? NSColor.blue
  225. }
  226. } else {
  227. if viewModel.sendContent == NSLocalizedString("Resend", tableName: "MemberCenterLocalizable", comment: "") {
  228. sendBox.fillColor = NSColor(named: "273C62") ?? NSColor.blue
  229. } else {
  230. sendBox.fillColor = NSColor(named: "DADBDE") ?? NSColor.gray
  231. }
  232. }
  233. }
  234. // MARK: Bind Method
  235. func bindViewModel() -> Void {
  236. viewModel.$isVisible
  237. .receive(on: RunLoop.main)
  238. .sink { [weak self] newValue in
  239. self?.visibleStateChange()
  240. }
  241. .store(in: &cancellables)
  242. viewModel.$stayState
  243. .receive(on: RunLoop.main)
  244. .sink { [weak self] newValue in
  245. self?.checkStateChange(button: self?.stayCheckButton, state: newValue)
  246. }
  247. .store(in: &cancellables)
  248. viewModel.$privacyState
  249. .receive(on: RunLoop.main)
  250. .sink { [weak self] newValue in
  251. self?.checkStateChange(button: self?.privacyCheckButton, state: newValue)
  252. }
  253. .store(in: &cancellables)
  254. viewModel.$signUpState
  255. .receive(on: RunLoop.main)
  256. .sink { [weak self] newValue in
  257. self?.signUpStateChange()
  258. }
  259. .store(in: &cancellables)
  260. viewModel.$emailError
  261. .receive(on: RunLoop.main)
  262. .sink { [weak self] newValue in
  263. self?.textfieldInputState(isEmail: true)
  264. }
  265. .store(in: &cancellables)
  266. viewModel.$passwordError
  267. .receive(on: RunLoop.main)
  268. .sink { [weak self] newValue in
  269. self?.textfieldInputState(isEmail: false)
  270. }
  271. .store(in: &cancellables)
  272. viewModel.$emailErrorMessage
  273. .receive(on: RunLoop.main)
  274. .sink { [weak self] newValue in
  275. self?.emailErrorLabel.stringValue = newValue
  276. self?.emailErrorLabel.isHidden = false
  277. }
  278. .store(in: &cancellables)
  279. viewModel.$passwordErrorMessage
  280. .receive(on: RunLoop.main)
  281. .sink { [weak self] newValue in
  282. self?.passwordErrorLabel.stringValue = newValue
  283. self?.passwordErrorLabel.isHidden = false
  284. }
  285. .store(in: &cancellables)
  286. viewModel.$sendContent
  287. .receive(on: RunLoop.main)
  288. .sink { [weak self] newValue in
  289. self?.sendBoxRefresh()
  290. }
  291. .store(in: &cancellables)
  292. viewModel.$email
  293. .receive(on: RunLoop.main)
  294. .sink { [weak self] newValue in
  295. self?.sendBoxRefresh()
  296. }
  297. .store(in: &cancellables)
  298. }
  299. // MARK: Action Method
  300. @IBAction func verificationCodeAction(_ sender: NSButton) {
  301. viewModel.signUpStateChange(state: .verificationCode)
  302. }
  303. @IBAction func passwordAction(_ sender: NSButton) {
  304. viewModel.signUpStateChange(state: .password)
  305. }
  306. @IBAction func visibleAction(_ sender: NSButton) {
  307. viewModel.isVisible.toggle()
  308. }
  309. @IBAction func stayCheckAction(_ sender: NSButton) {
  310. viewModel.stayState.toggle()
  311. }
  312. @IBAction func signUpAction(_ sender: NSButton) {
  313. viewModel.signUpAction()
  314. }
  315. @IBAction func forgetAction(_ sender: NSButton) {
  316. }
  317. @IBAction func privacyCheckAction(_ sender: NSButton) {
  318. viewModel.privacyState.toggle()
  319. }
  320. }
  321. extension KMSignUpView: NSTextFieldDelegate {
  322. func controlTextDidEndEditing(_ obj: Notification) {
  323. let textField = obj.object as? NSTextField
  324. if textField == emailTextField {
  325. viewModel.email = textField!.stringValue
  326. } else if textField == verifficationTextField {
  327. viewModel.verificationCode = textField!.stringValue
  328. } else if textField == passwordTextField {
  329. viewModel.password = textField!.stringValue
  330. } else if textField == passwordTextField1 {
  331. viewModel.password = textField!.stringValue
  332. }
  333. }
  334. func controlTextDidChange(_ obj: Notification) {
  335. let textField = obj.object as? NSTextField
  336. if textField == emailTextField {
  337. viewModel.email = textField!.stringValue
  338. } else if textField == verifficationTextField {
  339. viewModel.verificationCode = textField!.stringValue
  340. } else if textField == passwordTextField {
  341. viewModel.password = textField!.stringValue
  342. } else if textField == passwordTextField1 {
  343. viewModel.password = textField!.stringValue
  344. }
  345. }
  346. }