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(largeTitleLabel)
stackView.addArrangedSubview(photoView) stackView.addArrangedSubview(photoView)
stackView.addArrangedSubview(usernameTextField) stackView.addArrangedSubview(usernameTextField)
stackView.addArrangedSubview(usernameIsTakenLabel)
stackView.addArrangedSubview(displayNameTextField) stackView.addArrangedSubview(displayNameTextField)
stackView.addArrangedSubview(emailTextField) stackView.addArrangedSubview(emailTextField)
stackView.addArrangedSubview(passwordTextField) stackView.addArrangedSubview(passwordTextField)
stackView.addArrangedSubview(passwordCheckLabel) stackView.addArrangedSubview(passwordCheckLabel)
if self.viewModel.approvalRequired { if viewModel.approvalRequired {
stackView.addArrangedSubview(inviteTextField) stackView.addArrangedSubview(inviteTextField)
} }
// scrollView // scrollView
@ -389,24 +390,48 @@ extension MastodonRegisterViewController {
guard let self = self else { return } guard let self = self else { return }
self.setTextFieldValidAppearance(self.passwordTextField, validateState: validateState) self.setTextFieldValidAppearance(self.passwordTextField, validateState: validateState)
self.passwordCheckLabel.attributedText = self.viewModel.attributeStringForPassword(eightCharacters: validateState == .valid) self.passwordCheckLabel.attributedText = self.viewModel.attributeStringForPassword(eightCharacters: validateState == .valid)
} }
.store(in: &disposeBag) .store(in: &disposeBag)
viewModel.isAllValid viewModel.isAllValid
.receive(on: DispatchQueue.main) .receive(on: DispatchQueue.main)
.sink { [weak self] isAllValid in .sink { [weak self] isAllValid in
guard let self = self else { return } guard let self = self else { return }
self.signUpButton.isEnabled = isAllValid self.signUpButton.isEnabled = isAllValid
} }
.store(in: &disposeBag) .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 viewModel.error
.compactMap { $0 } .compactMap { $0 }
.receive(on: DispatchQueue.main) .receive(on: DispatchQueue.main)
.sink { [weak self] error in .sink { [weak self] error in
guard let self = self else { return } guard let self = self else { return }
guard let error = error as? Mastodon.API.Error 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 alertController = UIAlertController(for: error, title: "Sign Up Failure", preferredStyle: .alert)
let okAction = UIAlertAction(title: L10n.Common.Controls.Actions.ok, style: .default, handler: nil) let okAction = UIAlertAction(title: L10n.Common.Controls.Actions.ok, style: .default, handler: nil)
alertController.addAction(okAction) alertController.addAction(okAction)
@ -454,7 +479,7 @@ extension MastodonRegisterViewController {
} }
.store(in: &disposeBag) .store(in: &disposeBag)
if self.viewModel.approvalRequired { if viewModel.approvalRequired {
inviteTextField.delegate = self inviteTextField.delegate = self
NSLayoutConstraint.activate([ NSLayoutConstraint.activate([

View File

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