mirror of
https://github.com/simplex-chat/simplex-chat.git
synced 2026-03-31 18:25:56 +00:00
ios: adapted UserExists error (#1864)
* ios: adapted UserExists alert * updated texts * refactor --------- Co-authored-by: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
13bd51b97d
commit
f21fc76ced
@@ -91,8 +91,15 @@ struct UserPicker: View {
|
||||
let user = u.user
|
||||
return Button(action: {
|
||||
if !user.activeUser {
|
||||
changeActiveUser(user.userId)
|
||||
userPickerVisible = false
|
||||
do {
|
||||
try changeActiveUser_(user.userId)
|
||||
userPickerVisible = false
|
||||
} catch {
|
||||
AlertManager.shared.showAlertMsg(
|
||||
title: "Error switching profile!",
|
||||
message: "Error: \(responseError(error))"
|
||||
)
|
||||
}
|
||||
}
|
||||
}, label: {
|
||||
HStack(spacing: 0) {
|
||||
|
||||
@@ -16,6 +16,19 @@ struct CreateProfile: View {
|
||||
@State private var fullName: String = ""
|
||||
@FocusState private var focusDisplayName
|
||||
@FocusState private var focusFullName
|
||||
@State private var alert: CreateProfileAlert?
|
||||
|
||||
private enum CreateProfileAlert: Identifiable {
|
||||
case duplicateUserError
|
||||
case createUserError(error: LocalizedStringKey)
|
||||
|
||||
var id: String {
|
||||
switch self {
|
||||
case .duplicateUserError: return "duplicateUserError"
|
||||
case .createUserError: return "createUserError"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
VStack(alignment: .leading) {
|
||||
@@ -82,6 +95,12 @@ struct CreateProfile: View {
|
||||
focusDisplayName = true
|
||||
setLastVersionDefault()
|
||||
}
|
||||
.alert(item: $alert) { a in
|
||||
switch a {
|
||||
case .duplicateUserError: return duplicateUserAlert
|
||||
case let .createUserError(err): return creatUserErrorAlert(err)
|
||||
}
|
||||
}
|
||||
.padding()
|
||||
}
|
||||
|
||||
@@ -109,14 +128,44 @@ struct CreateProfile: View {
|
||||
m.users = try listUsers()
|
||||
try getUserChatData()
|
||||
}
|
||||
} catch {
|
||||
fatalError("Failed to create user or start chat: \(responseError(error))")
|
||||
} catch let error {
|
||||
switch error as? ChatResponse {
|
||||
case .chatCmdError(_, .errorStore(.duplicateName)),
|
||||
.chatCmdError(_, .error(.userExists)):
|
||||
if m.currentUser == nil {
|
||||
AlertManager.shared.showAlert(duplicateUserAlert)
|
||||
} else {
|
||||
alert = .duplicateUserError
|
||||
}
|
||||
default:
|
||||
let err: LocalizedStringKey = "Error: \(responseError(error))"
|
||||
if m.currentUser == nil {
|
||||
AlertManager.shared.showAlert(creatUserErrorAlert(err))
|
||||
} else {
|
||||
alert = .createUserError(error: err)
|
||||
}
|
||||
}
|
||||
logger.error("Failed to create user or start chat: \(responseError(error))")
|
||||
}
|
||||
}
|
||||
|
||||
func canCreateProfile() -> Bool {
|
||||
displayName != "" && validDisplayName(displayName)
|
||||
}
|
||||
|
||||
private var duplicateUserAlert: Alert {
|
||||
Alert(
|
||||
title: Text("Duplicate display name!"),
|
||||
message: Text("You already have a chat profile with the same display name. Please choose another name.")
|
||||
)
|
||||
}
|
||||
|
||||
private func creatUserErrorAlert(_ err: LocalizedStringKey) -> Alert {
|
||||
Alert(
|
||||
title: Text("Error creating profile!"),
|
||||
message: Text(err)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func validDisplayName(_ name: String) -> Bool {
|
||||
|
||||
@@ -15,11 +15,13 @@ struct UserProfilesView: View {
|
||||
|
||||
private enum UserProfilesAlert: Identifiable {
|
||||
case deleteUser(index: Int, delSMPQueues: Bool)
|
||||
case activateUserError(error: String)
|
||||
case error(title: LocalizedStringKey, error: LocalizedStringKey = "")
|
||||
|
||||
var id: String {
|
||||
switch self {
|
||||
case let .deleteUser(index, delSMPQueues): return "deleteUser \(index) \(delSMPQueues)"
|
||||
case let .activateUserError(err): return "activateUserError \(err)"
|
||||
case let .error(title, _): return "error \(title)"
|
||||
}
|
||||
}
|
||||
@@ -65,6 +67,11 @@ struct UserProfilesView: View {
|
||||
},
|
||||
secondaryButton: .cancel()
|
||||
)
|
||||
case let .activateUserError(error: err):
|
||||
return Alert(
|
||||
title: Text("Error switching profile!"),
|
||||
message: Text(err)
|
||||
)
|
||||
case let .error(title, error):
|
||||
return Alert(title: Text(title), message: Text(error))
|
||||
}
|
||||
@@ -104,7 +111,11 @@ struct UserProfilesView: View {
|
||||
|
||||
private func userView(_ user: User) -> some View {
|
||||
Button {
|
||||
changeActiveUser(user.userId)
|
||||
do {
|
||||
try changeActiveUser_(user.userId)
|
||||
} catch {
|
||||
alert = .activateUserError(error: responseError(error))
|
||||
}
|
||||
} label: {
|
||||
HStack {
|
||||
ProfileImage(imageStr: user.image, color: Color(uiColor: .tertiarySystemFill))
|
||||
|
||||
@@ -1078,6 +1078,7 @@ public enum ChatError: Decodable {
|
||||
public enum ChatErrorType: Decodable {
|
||||
case noActiveUser
|
||||
case activeUserExists
|
||||
case userExists
|
||||
case differentActiveUser
|
||||
case chatNotStarted
|
||||
case invalidConnReq
|
||||
|
||||
Reference in New Issue
Block a user