123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494 |
- //
- // KMSignUpView.swift
- // PDF Reader Pro
- //
- // Created by wanjun on 2024/10/23.
- //
- import Cocoa
- import Combine
- class KMSignUpView: KMBaseXibView {
-
- @IBOutlet weak var signUpLabel: NSTextField!
- @IBOutlet weak var loginModeBox: NSBox!
- @IBOutlet weak var verificationCodeButton: NSButton!
- @IBOutlet weak var selectBox1: NSBox!
- @IBOutlet weak var passwordButton: NSButton!
- @IBOutlet weak var selectBox2: NSBox!
- @IBOutlet weak var emailBox: NSBox!
- @IBOutlet weak var emailTextField: NSTextField!
- @IBOutlet weak var emailErrorLabel: NSTextField!
- @IBOutlet weak var passwordBox: NSBox!
- @IBOutlet weak var verifficationView: NSView!
- @IBOutlet weak var verifficationBox: NSBox!
- @IBOutlet weak var verifficationTextField: NSTextField!
- @IBOutlet weak var sendBox: KMBox!
- @IBOutlet weak var sendLabel: NSTextField!
- @IBOutlet weak var passwordView: NSView!
- @IBOutlet weak var passwordTextField: NSTextField!
- @IBOutlet weak var passwordTextField1: NSSecureTextField!
-
- @IBOutlet weak var visibleButton: NSButton!
- @IBOutlet weak var passwordErrorLabel: NSTextField!
- @IBOutlet weak var stayCheckButton: NSButton!
- @IBOutlet weak var stayLabel: NSTextField!
- @IBOutlet weak var forgetButton: NSButton!
- @IBOutlet weak var signUpBox: NSBox!
- @IBOutlet weak var signUpButton: NSButton!
- @IBOutlet weak var privacyCheckButton: NSButton!
- @IBOutlet weak var privacyLabel: NSTextField!
-
- private var viewModel = KMSignUpViewModel()
- private var cancellables = Set<AnyCancellable>()
-
- convenience init(model: KMSignUpViewModel, superView: NSView) {
- self.init(frame: superView.bounds)
- viewModel = model
- viewModel.screenType = .signUp
-
- bindViewModel()
- languageLocalized()
- initializeUI()
- }
-
- public override init(frame frameRect: NSRect) {
- super.init(frame: frameRect)
- }
-
- public required init?(coder decoder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
-
- public func resetTextFileData() {
- if(self.superview != nil) {
- emailTextField.stringValue = ""
- passwordTextField.stringValue = ""
- passwordTextField1.stringValue = ""
- verifficationTextField.stringValue = ""
- emailErrorLabel.isHidden = true
- passwordErrorLabel.isHidden = true
- sendLabel.stringValue = NSLocalizedString("Send", tableName: "MemberCenterLocalizable", comment: "")
- viewModel.sendContent = NSLocalizedString("Send", tableName: "MemberCenterLocalizable", comment: "")
- viewModel.email = ""
- sendBoxRefresh()
- }
- }
-
- override func updateUI() {
- super.updateUI()
-
- NotificationCenter.default.addObserver(self, selector: #selector(loginSuccessNotification), name: NSNotification.Name(rawValue: "MemberCenterLoginSuccess"), object: nil)
-
- bindViewModel()
- languageLocalized()
- initializeUI()
- }
-
- // MARK: Private Method
-
- private func languageLocalized() -> Void {
- signUpLabel.stringValue = NSLocalizedString("Sign in(titile)", tableName: "MemberCenterLocalizable", comment: "")
- verificationCodeButton.title = NSLocalizedString("Verification Code", tableName: "MemberCenterLocalizable", comment: "")
- passwordButton.title = NSLocalizedString("Password", tableName: "MemberCenterLocalizable", comment: "")
- stayLabel.stringValue = NSLocalizedString("Stay signed in", tableName: "MemberCenterLocalizable", comment: "")
- forgetButton.title = NSLocalizedString("Forgot password?", tableName: "MemberCenterLocalizable", comment: "")
- signUpButton.title = NSLocalizedString("Sign in(button)", tableName: "MemberCenterLocalizable", comment: "")
- emailErrorLabel.stringValue = String(format: "*%@", NSLocalizedString("Email format error. Please enter the correct email.", tableName: "MemberCenterLocalizable", comment: ""))
- passwordErrorLabel.stringValue = String(format: "*%@", NSLocalizedString("Verification code error.", tableName: "MemberCenterLocalizable", comment: ""))
- emailTextField.placeholderString = NSLocalizedString("Please enter email address", tableName: "MemberCenterLocalizable", comment: "")
- verifficationTextField.placeholderString = NSLocalizedString("Please enter code", tableName: "MemberCenterLocalizable", comment: "")
- passwordTextField.placeholderString = NSLocalizedString("Please enter password", tableName: "MemberCenterLocalizable", comment: "")
- passwordTextField1.placeholderString = NSLocalizedString("Please enter password", tableName: "MemberCenterLocalizable", comment: "")
- privacyCheckButton.toolTip = NSLocalizedString("Please agree and check the above agreement first.", tableName: "MemberCenterLocalizable", comment: "")
- emailTextField.stringValue = viewModel.email
- verifficationTextField.stringValue = viewModel.verificationCode
- passwordTextField.stringValue = viewModel.password
- passwordTextField1.stringValue = viewModel.password
- }
-
- private func initializeUI() -> Void {
- emailBox.fillColor = NSColor(named: "texefiedfillcolor") ?? NSColor.white
- passwordBox.fillColor = NSColor(named: "texefiedfillcolor") ?? NSColor.white
- emailTextField.delegate = self
- verifficationTextField.delegate = self
- passwordTextField.delegate = self
- passwordTextField1.delegate = self
-
- emailErrorLabel.isHidden = !viewModel.emailError()
- passwordErrorLabel.isHidden = !viewModel.passwordError()
- signUpLabel.textColor = NSColor(named: "000000")
- signUpLabel.font = NSFont.SFMediumFontWithSize(20)
- selectBox1.fillColor = NSColor(named: "4982E6") ?? NSColor.blue
- emailErrorLabel.textColor = NSColor(named: "FA1E5D")
- emailErrorLabel.font = NSFont.SFProTextRegularFont(9)
- passwordBox.borderColor = NSColor(named: "DADBDE") ?? NSColor.gray
- verifficationBox.borderColor = NSColor(named: "FA1E5D") ?? NSColor.gray
- if viewModel.isValidEmail() {
- sendBox.fillColor = NSColor(named: "273C62") ?? NSColor.blue
- } else {
- sendBox.fillColor = NSColor(named: "273C62_0.4") ?? NSColor.blue
- }
- sendBox.borderColor = NSColor(named: "273C62") ?? NSColor.blue
- sendLabel.textColor = NSColor(named: "FFFFFF") ?? NSColor.white
- sendLabel.font = NSFont.SFProTextRegularFont(13)
- passwordErrorLabel.textColor = NSColor(named: "FA1E5D")
- passwordErrorLabel.font = NSFont.SFProTextRegularFont(9)
- stayCheckButton.image = NSImage(named: "CheckBoxNor")
- stayLabel.textColor = NSColor(named: "0E1114") ?? NSColor.black
- stayLabel.font = NSFont.SFProTextRegularFont(12)
- forgetButton.setTitleColor(color: NSColor(named: "4982E6") ?? NSColor.blue, font: NSFont.SFProTextRegularFont(12))
- signUpBox.fillColor = NSColor(named: "273C62") ?? NSColor.blue
- signUpButton.setTitleColor(color: NSColor(named: "FFFFFF") ?? NSColor.white, font: NSFont.SFProTextRegularFont(16))
- privacyCheckButton.image = NSImage(named: "CheckBoxNor")
- privacyLabel.isEditable = false
- privacyLabel.isSelectable = true
- privacyLabel.allowsEditingTextAttributes = true
- privacyLabel.textColor = NSColor.black
- privacyLabel.font = NSFont.SFProTextRegularFont(16.0)
- 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: "")
- let specialOffer = NSLocalizedString("Terms of Service", tableName: "MemberCenterLocalizable", comment: "")
- let contactsUs = NSLocalizedString("Privacy Policy", tableName: "MemberCenterLocalizable", comment: "")
- let fullString = String(format: tipsString, specialOffer, contactsUs)
- let attributedString = NSMutableAttributedString(string: fullString)
- // 定义链接的范围
- let specialOfferRange = (fullString as NSString).range(of: specialOffer)
- let contactsUsRange = (fullString as NSString).range(of: contactsUs)
- let linkColor = NSColor(named: "4982E6") ?? NSColor.blue
- let font = NSFont.SFProTextRegularFont(11.0) // 与普通文本相同的字体
- attributedString.addAttributes([
- .foregroundColor: NSColor(named: "0E1114") ?? NSColor.black as Any,
- .font: font
- ], range: (fullString as NSString).range(of: fullString))
- attributedString.addAttributes([
- .foregroundColor: linkColor,
- .link: NSLocalizedString("https://www.pdfreaderpro.com/terms_of_service", comment: ""),
- .font: font
- ], range: specialOfferRange)
- attributedString.addAttributes([
- .foregroundColor: linkColor,
- .link: NSLocalizedString("https://www.pdfreaderpro.com/privacy-policy", comment: ""),
- .font: font
- ], range: contactsUsRange)
- privacyLabel.attributedStringValue = attributedString
-
- signUpStateChange()
- visibleStateChange()
- textfieldInputState(isEmail: true)
- textfieldInputState(isEmail: false)
- sendBoxRefresh()
-
- sendBox.moveCallback = { [weak self](mouseEntered: Bool, mouseBox: KMBox) -> Void in
- guard let self = self else { return }
- if self.viewModel.email.count <= 0 { return }
- if self.viewModel.sendBoxSelect { return }
- if !self.viewModel.isValidEmail() { return }
- if mouseEntered {
- self.sendBox.fillColor = NSColor(named: "000000_0.1") ?? NSColor.blue
- } else {
- self.sendBox.fillColor = NSColor(named: "273C62") ?? NSColor.blue
- }
- }
- sendBox.downCallback = { [weak self](downEntered: Bool, mouseBox: KMBox, event) -> Void in
- guard let self = self else { return }
- if self.viewModel.email.count <= 0 { return }
- if self.viewModel.sendBoxSelect { return }
- if downEntered {
- self.sendBox.fillColor = NSColor(named: "273C62_0.4") ?? NSColor.blue
- viewModel.countDown(type: .login)
- }
- }
- }
-
- private func signUpStateChange() -> Void {
- if viewModel.signUpState == .verificationCode {
- selectBox1.isHidden = false
- selectBox2.isHidden = true
- verificationCodeButton.setTitleColor(color: NSColor(named: "4982E6") ?? NSColor.systemBlue, font: NSFont.SFProTextRegularFont(14))
- passwordButton.setTitleColor(color: NSColor(named: "42464D") ?? NSColor.black, font: NSFont.SFProTextRegularFont(14))
- verifficationView.isHidden = false
- passwordView.isHidden = true
- verifficationTextField.placeholderString = NSLocalizedString("Please enter code", tableName: "MemberCenterLocalizable", comment: "")
- forgetButton.isHidden = true
- } else if viewModel.signUpState == .password {
- selectBox1.isHidden = true
- selectBox2.isHidden = false
- verificationCodeButton.setTitleColor(color: NSColor(named: "42464D") ?? NSColor.black, font: NSFont.SFProTextRegularFont(14))
- passwordButton.setTitleColor(color: NSColor(named: "4982E6") ?? NSColor.black, font: NSFont.SFProTextRegularFont(14))
- verifficationView.isHidden = true
- passwordView.isHidden = false
- passwordTextField.placeholderString = NSLocalizedString("Please enter password", tableName: "MemberCenterLocalizable", comment: "")
- passwordTextField1.placeholderString = NSLocalizedString("Please enter password", tableName: "MemberCenterLocalizable", comment: "")
- forgetButton.isHidden = false
- }
- }
-
- private func checkStateChange(button: NSButton!, state: Bool) -> Void {
- button.state = state ? .on : .off
- if button.state == .on {
- button.image = NSImage(named: "CheckBoxSel")
- } else {
- button.image = NSImage(named: "CheckBoxNor")
- }
- }
-
- private func visibleStateChange() -> Void {
- if viewModel.isVisible {
- visibleButton.image = NSImage(named: "passwordVisible")
- passwordTextField.isHidden = false
- passwordTextField1.isHidden = true
- passwordTextField.stringValue = viewModel.password
- } else {
- visibleButton.image = NSImage(named: "passwordUnVisible")
- passwordTextField.isHidden = true
- passwordTextField1.isHidden = false
- passwordTextField1.stringValue = viewModel.password
- }
- }
-
- private func textfieldInputState(isEmail: Bool) -> Void {
- if isEmail {
- if viewModel.emailError() {
- emailBox.borderColor = NSColor(named: "FA1E5D") ?? NSColor.red
- } else {
- emailBox.borderColor = NSColor(named: "DADBDE") ?? NSColor.gray
- }
- emailErrorLabel.isHidden = !viewModel.emailError()
- } else {
- if viewModel.passwordError() {
- if viewModel.signUpState == .verificationCode {
- passwordBox.borderWidth = 0
- verifficationBox.borderWidth = 1
- passwordBox.borderColor = NSColor(named: "DADBDE") ?? NSColor.gray
- verifficationBox.borderColor = NSColor(named: "FA1E5D") ?? NSColor.red
- } else if viewModel.signUpState == .password {
- passwordBox.borderWidth = 1
- verifficationBox.borderWidth = 0
- verifficationBox.borderColor = NSColor(named: "DADBDE") ?? NSColor.gray
- passwordBox.borderColor = NSColor(named: "FA1E5D") ?? NSColor.red
- }
- } else {
- if viewModel.signUpState == .verificationCode {
- passwordBox.borderWidth = 1
- verifficationBox.borderWidth = 0
- } else if viewModel.signUpState == .password {
- passwordBox.borderWidth = 1
- verifficationBox.borderWidth = 0
- }
- }
- passwordErrorLabel.isHidden = !viewModel.passwordError()
- }
- }
-
- private func sendBoxRefresh() -> Void {
- sendLabel.stringValue = viewModel.sendContent
- if viewModel.sendContent == NSLocalizedString("Send", tableName: "MemberCenterLocalizable", comment: "") ||
- viewModel.sendContent == NSLocalizedString("Resend", tableName: "MemberCenterLocalizable", comment: "") {
- if viewModel.email.count > 0 {
- if viewModel.isValidEmail() {
- sendBox.fillColor = NSColor(named: "273C62") ?? NSColor.blue
- } else {
- sendBox.fillColor = NSColor(named: "273C62_0.4") ?? NSColor.blue
- }
- } else {
- sendBox.fillColor = NSColor(named: "273C62_0.4") ?? NSColor.blue
- }
- sendLabel.textColor = NSColor(named: "FFFFFF") ?? NSColor.white
- } else {
- sendBox.fillColor = NSColor(named: "DADBDE") ?? NSColor.gray
- sendLabel.stringValue = String(format: "%@s", viewModel.sendContent)
- sendLabel.textColor = NSColor(named: "0E1114") ?? NSColor.black
- }
- }
-
- // MARK: Bind Method
-
- func bindViewModel() -> Void {
- viewModel.$isVisible
- .receive(on: RunLoop.main)
- .sink { [weak self] newValue in
- self?.visibleStateChange()
- }
- .store(in: &cancellables)
- viewModel.$stayState
- .receive(on: RunLoop.main)
- .sink { [weak self] newValue in
- self?.checkStateChange(button: self?.stayCheckButton, state: newValue)
- }
- .store(in: &cancellables)
- viewModel.$privacyState
- .receive(on: RunLoop.main)
- .sink { [weak self] newValue in
- self?.checkStateChange(button: self?.privacyCheckButton, state: newValue)
- }
- .store(in: &cancellables)
- viewModel.$signUpState
- .receive(on: RunLoop.main)
- .sink { [weak self] newValue in
- self?.signUpStateChange()
- }
- .store(in: &cancellables)
- viewModel.$emailErrorMessage
- .receive(on: RunLoop.main)
- .sink { [weak self] newValue in
- self?.emailErrorLabel.stringValue = newValue
- self?.emailErrorLabel.isHidden = false
- self?.textfieldInputState(isEmail: true)
- }
- .store(in: &cancellables)
- viewModel.$passwordErrorMessage
- .receive(on: RunLoop.main)
- .sink { [weak self] newValue in
- self?.passwordErrorLabel.stringValue = newValue
- if self?.viewModel.passwordErrorMessage == "" {
- self?.passwordErrorLabel.isHidden = true
- } else {
- self?.passwordErrorLabel.isHidden = false
- }
- }
- .store(in: &cancellables)
- viewModel.$sendContent
- .receive(on: RunLoop.main)
- .sink { [weak self] newValue in
- self?.sendBoxRefresh()
- }
- .store(in: &cancellables)
- viewModel.$email
- .receive(on: RunLoop.main)
- .sink { [weak self] newValue in
- self?.sendBoxRefresh()
- }
- .store(in: &cancellables)
- viewModel.$verificationCode
- .receive(on: RunLoop.main)
- .sink { [weak self] newValue in
- if newValue.count <= 6 && newValue.count >= 0 {
- self?.viewModel.passwordErrorMessage = ""
- } else {
- self?.viewModel.passwordErrorMessage = NSLocalizedString("Verification code error.", tableName: "MemberCenterLocalizable", comment: "")
- }
- }
- .store(in: &cancellables)
- viewModel.$password
- .receive(on: RunLoop.main)
- .sink { [weak self] newValue in
- if newValue.count <= 30 && newValue.count >= 0 {
- self?.viewModel.passwordErrorMessage = ""
- } else {
- self?.viewModel.passwordErrorMessage = NSLocalizedString("Account or password error.", tableName: "MemberCenterLocalizable", comment: "")
- }
- }
- .store(in: &cancellables)
- }
-
-
- // MARK: Action Method
-
- @IBAction func verificationCodeAction(_ sender: NSButton) {
- viewModel.signUpStateChange(state: .verificationCode)
- }
-
- @IBAction func passwordAction(_ sender: NSButton) {
- viewModel.signUpStateChange(state: .password)
- }
-
- @IBAction func visibleAction(_ sender: NSButton) {
- viewModel.isVisible.toggle()
- }
-
- @IBAction func stayCheckAction(_ sender: NSButton) {
- viewModel.stayState.toggle()
- viewModel.stayStateAction()
- }
-
- @IBAction func signUpAction(_ sender: NSButton) {
- self.window?.makeFirstResponder(nil)
-
- viewModel.emailErrorMessage = ""
- viewModel.passwordErrorMessage = ""
- viewModel.signUpAction()
- }
-
- @IBAction func forgetAction(_ sender: NSButton) {
- guard let parentView = self.superview else { return }
- if parentView is NSBox {
- let model = KMSignUpViewModel()
- model.email = viewModel.email
- let forgotView = KMForgotPasswordView(model: model, superView: parentView)
- NSAnimationContext.runAnimationGroup { context in
- context.duration = 0.3
- self.animator().alphaValue = 0
- } completionHandler: {
- self.removeFromSuperview()
- forgotView.alphaValue = 0
- (parentView as! NSBox).contentView = forgotView
- NSAnimationContext.runAnimationGroup({ context in
- context.duration = 0.3
- forgotView.animator().alphaValue = 1
- }, completionHandler: nil)
- }
- } else {
- let model = KMSignUpViewModel()
- model.email = viewModel.email
- let forgotView = KMForgotPasswordView(model: model, superView: parentView)
- NSAnimationContext.runAnimationGroup { context in
- context.duration = 0.3
- self.animator().alphaValue = 0
- } completionHandler: {
- self.removeFromSuperview()
- forgotView.alphaValue = 0
- parentView.addSubview(forgotView)
- NSAnimationContext.runAnimationGroup({ context in
- context.duration = 0.3
- forgotView.animator().alphaValue = 1
- }, completionHandler: nil)
- }
- }
- }
-
- @IBAction func privacyCheckAction(_ sender: NSButton) {
- viewModel.privacyState.toggle()
- }
- }
- extension KMSignUpView: NSTextFieldDelegate {
- func controlTextDidEndEditing(_ obj: Notification) {
- let textField = obj.object as? NSTextField
- if textField == emailTextField {
- viewModel.email = textField!.stringValue
- } else if textField == verifficationTextField {
- viewModel.verificationCode = textField!.stringValue
- } else if textField == passwordTextField {
- viewModel.password = textField!.stringValue
- } else if textField == passwordTextField1 {
- viewModel.password = textField!.stringValue
- }
- }
-
- func controlTextDidChange(_ obj: Notification) {
- let textField = obj.object as? NSTextField
- if textField == emailTextField {
- viewModel.emailErrorMessage = ""
- viewModel.email = textField!.stringValue
- emailBox.borderColor = NSColor(named: "DADBDE") ?? NSColor.gray
- } else if textField == verifficationTextField {
- viewModel.passwordErrorMessage = ""
- viewModel.verificationCode = textField!.stringValue
- verifficationBox.borderColor = NSColor(named: "DADBDE") ?? NSColor.gray
- } else if textField == passwordTextField {
- viewModel.passwordErrorMessage = ""
- viewModel.password = textField!.stringValue
- passwordBox.borderColor = NSColor(named: "DADBDE") ?? NSColor.gray
- } else if textField == passwordTextField1 {
- viewModel.passwordErrorMessage = ""
- viewModel.password = textField!.stringValue
- passwordBox.borderColor = NSColor(named: "DADBDE") ?? NSColor.gray
- }
- }
-
- @objc func loginSuccessNotification() -> Void {
- resetTextFileData()
- }
- }
|