chore: display UI when username is taken error return by sign up

This commit is contained in:
sunxiaojian 2021-03-01 17:45:02 +08:00
parent 732c5392d4
commit a659b35577
2 changed files with 36 additions and 9 deletions

View File

@ -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,7 +390,6 @@ 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)
@ -401,12 +401,37 @@ extension MastodonRegisterViewController {
}
.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([

View File

@ -34,6 +34,8 @@ final class MastodonRegisterViewModel {
let passwordValidateState = CurrentValueSubject<ValidateState, Never>(.empty)
let inviteValidateState = CurrentValueSubject<ValidateState, Never>(.empty)
let isUsernameTaken = CurrentValueSubject<Bool, Never>(false)
let isRegistering = CurrentValueSubject<Bool, Never>(false)
let isAllValid = CurrentValueSubject<Bool, Never>(false)
let error = CurrentValueSubject<Error?, Never>(nil)