From 68d53652951d26d264802e9524dc9898f2475132 Mon Sep 17 00:00:00 2001 From: sunxiaojian Date: Fri, 26 Feb 2021 15:39:05 +0800 Subject: [PATCH] chore: make code clear --- .../MastodonRegisterViewController.swift | 14 ++++---- .../Register/MastodonRegisterViewModel.swift | 33 ++++++++----------- 2 files changed, 21 insertions(+), 26 deletions(-) diff --git a/Mastodon/Scene/Authentication/Register/MastodonRegisterViewController.swift b/Mastodon/Scene/Authentication/Register/MastodonRegisterViewController.swift index 7d92651a1..58415b5fa 100644 --- a/Mastodon/Scene/Authentication/Register/MastodonRegisterViewController.swift +++ b/Mastodon/Scene/Authentication/Register/MastodonRegisterViewController.swift @@ -97,7 +97,7 @@ final class MastodonRegisterViewController: UIViewController, NeedsDependency, O textField.autocapitalizationType = .none textField.autocorrectionType = .no textField.backgroundColor = .white - textField.textColor = .black + textField.textColor = Asset.Colors.Label.secondary.color textField.attributedPlaceholder = NSAttributedString(string: L10n.Scene.Register.Input.Username.placeholder, attributes: [NSAttributedString.Key.foregroundColor: Asset.Colors.lightSecondaryText.color, NSAttributedString.Key.font: UIFont.preferredFont(forTextStyle: .headline)]) @@ -118,7 +118,7 @@ final class MastodonRegisterViewController: UIViewController, NeedsDependency, O textField.autocapitalizationType = .none textField.autocorrectionType = .no textField.backgroundColor = .white - textField.textColor = .black + textField.textColor = Asset.Colors.Label.secondary.color textField.attributedPlaceholder = NSAttributedString(string: L10n.Scene.Register.Input.DisplayName.placeholder, attributes: [NSAttributedString.Key.foregroundColor: Asset.Colors.lightSecondaryText.color, NSAttributedString.Key.font: UIFont.preferredFont(forTextStyle: .headline)]) @@ -135,7 +135,7 @@ final class MastodonRegisterViewController: UIViewController, NeedsDependency, O textField.autocorrectionType = .no textField.keyboardType = .emailAddress textField.backgroundColor = .white - textField.textColor = .black + textField.textColor = Asset.Colors.Label.secondary.color textField.attributedPlaceholder = NSAttributedString(string: L10n.Scene.Register.Input.Email.placeholder, attributes: [NSAttributedString.Key.foregroundColor: Asset.Colors.lightSecondaryText.color, NSAttributedString.Key.font: UIFont.preferredFont(forTextStyle: .headline)]) @@ -159,7 +159,7 @@ final class MastodonRegisterViewController: UIViewController, NeedsDependency, O textField.keyboardType = .asciiCapable textField.isSecureTextEntry = true textField.backgroundColor = .white - textField.textColor = .black + textField.textColor = Asset.Colors.Label.secondary.color textField.attributedPlaceholder = NSAttributedString(string: L10n.Scene.Register.Input.Password.placeholder, attributes: [NSAttributedString.Key.foregroundColor: Asset.Colors.lightSecondaryText.color, NSAttributedString.Key.font: UIFont.preferredFont(forTextStyle: .headline)]) @@ -175,7 +175,7 @@ final class MastodonRegisterViewController: UIViewController, NeedsDependency, O textField.autocapitalizationType = .none textField.autocorrectionType = .no textField.backgroundColor = .white - textField.textColor = .black + textField.textColor = Asset.Colors.Label.secondary.color textField.attributedPlaceholder = NSAttributedString(string: L10n.Scene.Register.Input.Invite.registrationUserInviteRequest, attributes: [NSAttributedString.Key.foregroundColor: Asset.Colors.lightSecondaryText.color, NSAttributedString.Key.font: UIFont.preferredFont(forTextStyle: .headline)]) @@ -242,7 +242,7 @@ extension MastodonRegisterViewController { stackView.addArrangedSubview(emailTextField) stackView.addArrangedSubview(passwordTextField) stackView.addArrangedSubview(passwordCheckLabel) - if self.viewModel.inviteEnabled { + if self.viewModel.approvalRequired { stackView.addArrangedSubview(inviteTextField) } // scrollView @@ -463,7 +463,7 @@ extension MastodonRegisterViewController { } .store(in: &disposeBag) - if self.viewModel.inviteEnabled { + if self.viewModel.approvalRequired { inviteTextField.delegate = self NSLayoutConstraint.activate([ diff --git a/Mastodon/Scene/Authentication/Register/MastodonRegisterViewModel.swift b/Mastodon/Scene/Authentication/Register/MastodonRegisterViewModel.swift index 298ecc565..9afa531f8 100644 --- a/Mastodon/Scene/Authentication/Register/MastodonRegisterViewModel.swift +++ b/Mastodon/Scene/Authentication/Register/MastodonRegisterViewModel.swift @@ -33,9 +33,9 @@ final class MastodonRegisterViewModel { let isRegistering = CurrentValueSubject(false) // output - lazy var inviteEnabled: Bool = { - if let inviteEnabled = instance.invitesEnabled { - return inviteEnabled + lazy var approvalRequired: Bool = { + if let approvalRequired = instance.approvalRequired { + return approvalRequired } return false }() @@ -106,7 +106,7 @@ final class MastodonRegisterViewModel { } .assign(to: \.value, on: passwordValidateState) .store(in: &disposeBag) - if inviteEnabled { + if approvalRequired { invite .map { invite in guard !invite.isEmpty else { return .empty } @@ -115,26 +115,21 @@ final class MastodonRegisterViewModel { .assign(to: \.value, on: inviteValidateState) .store(in: &disposeBag) } - let publisherOne = Publishers.CombineLatest( + let publisherOne = Publishers.CombineLatest4( usernameValidateState.eraseToAnyPublisher(), - displayNameValidateState.eraseToAnyPublisher() - ) - let publisherTwo = Publishers.CombineLatest3( + displayNameValidateState.eraseToAnyPublisher(), emailValidateState.eraseToAnyPublisher(), - passwordValidateState.eraseToAnyPublisher(), - inviteValidateState.eraseToAnyPublisher() - ) + passwordValidateState.eraseToAnyPublisher() + ).map { + $0.0 == .valid && $0.1 == .valid && $0.2 == .valid && $0.3 == .valid + } + Publishers.CombineLatest( publisherOne, - publisherTwo + approvalRequired ? inviteValidateState.map {$0 == .valid}.eraseToAnyPublisher() : Just(true).eraseToAnyPublisher() ) - .map { [weak self] in - guard let self = self else { return false } - if self.inviteEnabled { - return $0.0.0 == .valid && $0.0.1 == .valid && $0.1.0 == .valid && $0.1.1 == .valid && $0.1.2 == .valid - } else { - return $0.0.0 == .valid && $0.0.1 == .valid && $0.1.0 == .valid && $0.1.1 == .valid - } + .map { + return $0 && $1 } .assign(to: \.value, on: isAllValid) .store(in: &disposeBag)