Skip to content

Commit

Permalink
show error alert if setting username fails
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuatbrown committed Oct 18, 2024
1 parent ae0af31 commit 82fb793
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 38 deletions.
24 changes: 12 additions & 12 deletions Nos/Assets/Localization/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -5236,18 +5236,6 @@
}
}
},
"displayNameError" : {
"comment" : "error message for when we can't set the display name in onboarding",
"extractionState" : "manual",
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "There is a problem connecting to the servers. You can skip for now and visit your Profile to update later."
}
}
}
},
"displayNameHeadline" : {
"comment" : "headline for the display name screen in onboarding",
"extractionState" : "manual",
Expand Down Expand Up @@ -5782,6 +5770,18 @@
}
}
},
"errorConnecting" : {
"comment" : "error message for when we can't set the display name or username in onboarding",
"extractionState" : "manual",
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "There is a problem connecting to the servers. You can skip for now and visit your Profile to update later."
}
}
}
},
"eventSource" : {
"extractionState" : "manual",
"localizations" : {
Expand Down
2 changes: 1 addition & 1 deletion Nos/Views/Onboarding/DisplayNameView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ struct DisplayNameView: View {
}
}
.navigationBarHidden(true)
.alert("displayNameError", isPresented: $showError) {
.alert("errorConnecting", isPresented: $showError) {
Button {
nextStep()
} label: {
Expand Down
41 changes: 16 additions & 25 deletions Nos/Views/Onboarding/UsernameView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fileprivate enum UsernameViewState {
case loading
case verificationFailed
case claimed
case claimFailed
case errorAlert
}

/// The Username view in the onboarding.
Expand All @@ -26,9 +26,9 @@ struct UsernameView: View {

private var showAlert: Binding<Bool> {
Binding {
saveError != nil
usernameState == .errorAlert
} set: { _ in
saveError = nil
usernameState = .idle
}
}

Expand Down Expand Up @@ -57,19 +57,11 @@ struct UsernameView: View {
}
}
.navigationBarHidden(true)
.alert(isPresented: showAlert, error: saveError) {
.alert("errorConnecting", isPresented: showAlert) {
Button {
saveError = nil
Task {
await save()
}
} label: {
Text("retry")
}
Button {
saveError = nil
nextStep()
} label: {
Text("cancel")
Text("skipForNow")
}
}
}
Expand Down Expand Up @@ -111,7 +103,7 @@ struct UsernameView: View {
}
Spacer()
BigActionButton("next") {
await next()
await verifyAndSave()
}
.disabled(nextButtonDisabled)
}
Expand All @@ -126,12 +118,16 @@ struct UsernameView: View {
.foregroundStyle(Color.error)
}

func nextStep() {
state.step = .buildYourNetwork
}

/// Checks whether the username is available and saves it. Updates `usernameState` based on the result.
func next() async {
func verifyAndSave() async {
usernameState = .loading

guard !username.isEmpty, let keyPair = currentUser.keyPair else {
usernameState = .claimFailed
usernameState = .errorAlert
return
}

Expand All @@ -157,8 +153,7 @@ struct UsernameView: View {

guard let author = await currentUser.author,
let keyPair = currentUser.keyPair else {
saveError = SaveProfileError.unexpectedError
usernameState = .claimFailed
usernameState = .errorAlert
return
}

Expand All @@ -175,14 +170,10 @@ struct UsernameView: View {
relays: relays
)
usernameState = .claimed
state.step = .buildYourNetwork
} catch CurrentUserError.errorWhilePublishingToRelays {
saveError = SaveProfileError.unableToPublishChanges
usernameState = .claimFailed
nextStep()
} catch {
crashReporting.report(error)
saveError = SaveProfileError.unexpectedError
usernameState = .claimFailed
usernameState = .errorAlert
}
}
}
Expand Down

0 comments on commit 82fb793

Please sign in to comment.