From a659b35577511db24d2851d18dcb4689942349db Mon Sep 17 00:00:00 2001 From: sunxiaojian Date: Mon, 1 Mar 2021 17:45:02 +0800 Subject: [PATCH] chore: display UI when username is taken error return by sign up --- .../MastodonRegisterViewController.swift | 43 +++++++++++++++---- .../Register/MastodonRegisterViewModel.swift | 2 + 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/Mastodon/Scene/Onboarding/Register/MastodonRegisterViewController.swift b/Mastodon/Scene/Onboarding/Register/MastodonRegisterViewController.swift index 9bdaafd0..2f92e44c 100644 --- a/Mastodon/Scene/Onboarding/Register/MastodonRegisterViewController.swift +++ b/Mastodon/Scene/Onboarding/Register/MastodonRegisterViewController.swift @@ -241,11 +241,12 @@ extension MastodonRegisterViewController { stackView.addArrangedSubview(largeTitleLabel) stackView.addArrangedSubview(photoView) stackView.addArrangedSubview(usernameTextField) + stackView.addArrangedSubview(usernameIsTakenLabel) stackView.addArrangedSubview(displayNameTextField) stackView.addArrangedSubview(emailTextField) stackView.addArrangedSubview(passwordTextField) stackView.addArrangedSubview(passwordCheckLabel) - if self.viewModel.approvalRequired { + if viewModel.approvalRequired { stackView.addArrangedSubview(inviteTextField) } // scrollView @@ -389,24 +390,48 @@ extension MastodonRegisterViewController { guard let self = self else { return } self.setTextFieldValidAppearance(self.passwordTextField, validateState: validateState) self.passwordCheckLabel.attributedText = self.viewModel.attributeStringForPassword(eightCharacters: validateState == .valid) - } .store(in: &disposeBag) viewModel.isAllValid - .receive(on: DispatchQueue.main) - .sink { [weak self] isAllValid in - guard let self = self else { return } - self.signUpButton.isEnabled = isAllValid - } - .store(in: &disposeBag) + .receive(on: DispatchQueue.main) + .sink { [weak self] isAllValid in + guard let self = self else { return } + self.signUpButton.isEnabled = isAllValid + } + .store(in: &disposeBag) + viewModel.isUsernameTaken + .receive(on: DispatchQueue.main) + .sink {[weak self] isUsernameTaken in + guard let self = self else { return } + if isUsernameTaken { + self.usernameIsTakenLabel.isHidden = false + stackView.setCustomSpacing(6, after: self.usernameTextField) + stackView.setCustomSpacing(16, after: self.usernameIsTakenLabel) + } else { + self.usernameIsTakenLabel.isHidden = true + stackView.setCustomSpacing(40, after: self.usernameTextField) + } + } + .store(in: &disposeBag) viewModel.error .compactMap { $0 } .receive(on: DispatchQueue.main) .sink { [weak self] error in guard let self = self else { return } guard let error = error as? Mastodon.API.Error else { return } + switch error.mastodonError { + case .generic(let mastodonEntityError): + if let usernameTakenError = mastodonEntityError.details?.username { + let isUsernameAvaliable = usernameTakenError.filter { errorDetailReason -> Bool in + errorDetailReason.error == .ERR_TAKEN + }.isEmpty + self.viewModel.isUsernameTaken.value = !isUsernameAvaliable + } + default: + break + } let alertController = UIAlertController(for: error, title: "Sign Up Failure", preferredStyle: .alert) let okAction = UIAlertAction(title: L10n.Common.Controls.Actions.ok, style: .default, handler: nil) alertController.addAction(okAction) @@ -454,7 +479,7 @@ extension MastodonRegisterViewController { } .store(in: &disposeBag) - if self.viewModel.approvalRequired { + if viewModel.approvalRequired { inviteTextField.delegate = self NSLayoutConstraint.activate([ diff --git a/Mastodon/Scene/Onboarding/Register/MastodonRegisterViewModel.swift b/Mastodon/Scene/Onboarding/Register/MastodonRegisterViewModel.swift index 5a909834..8f930771 100644 --- a/Mastodon/Scene/Onboarding/Register/MastodonRegisterViewModel.swift +++ b/Mastodon/Scene/Onboarding/Register/MastodonRegisterViewModel.swift @@ -34,6 +34,8 @@ final class MastodonRegisterViewModel { let passwordValidateState = CurrentValueSubject(.empty) let inviteValidateState = CurrentValueSubject(.empty) + let isUsernameTaken = CurrentValueSubject(false) + let isRegistering = CurrentValueSubject(false) let isAllValid = CurrentValueSubject(false) let error = CurrentValueSubject(nil)