From f21fc76ced0ab4c3307c50af3a701d0dee9f13bb Mon Sep 17 00:00:00 2001 From: Stanislav Dmitrenko <7953703+avently@users.noreply.github.com> Date: Tue, 31 Jan 2023 15:55:41 +0000 Subject: [PATCH] ios: adapted UserExists error (#1864) * ios: adapted UserExists alert * updated texts * refactor --------- Co-authored-by: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> --- .../Shared/Views/ChatList/UserPicker.swift | 11 +++- .../Views/Onboarding/CreateProfile.swift | 53 ++++++++++++++++++- .../Views/UserSettings/UserProfilesView.swift | 13 ++++- apps/ios/SimpleXChat/APITypes.swift | 1 + 4 files changed, 73 insertions(+), 5 deletions(-) diff --git a/apps/ios/Shared/Views/ChatList/UserPicker.swift b/apps/ios/Shared/Views/ChatList/UserPicker.swift index 2c2fd82ff6..c0b2462651 100644 --- a/apps/ios/Shared/Views/ChatList/UserPicker.swift +++ b/apps/ios/Shared/Views/ChatList/UserPicker.swift @@ -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) { diff --git a/apps/ios/Shared/Views/Onboarding/CreateProfile.swift b/apps/ios/Shared/Views/Onboarding/CreateProfile.swift index ce0a6160b8..d6b8c6b123 100644 --- a/apps/ios/Shared/Views/Onboarding/CreateProfile.swift +++ b/apps/ios/Shared/Views/Onboarding/CreateProfile.swift @@ -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 { diff --git a/apps/ios/Shared/Views/UserSettings/UserProfilesView.swift b/apps/ios/Shared/Views/UserSettings/UserProfilesView.swift index c6be670f2a..d41982b706 100644 --- a/apps/ios/Shared/Views/UserSettings/UserProfilesView.swift +++ b/apps/ios/Shared/Views/UserSettings/UserProfilesView.swift @@ -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)) diff --git a/apps/ios/SimpleXChat/APITypes.swift b/apps/ios/SimpleXChat/APITypes.swift index 23c2958c17..69b8ce8514 100644 --- a/apps/ios/SimpleXChat/APITypes.swift +++ b/apps/ios/SimpleXChat/APITypes.swift @@ -1078,6 +1078,7 @@ public enum ChatError: Decodable { public enum ChatErrorType: Decodable { case noActiveUser case activeUserExists + case userExists case differentActiveUser case chatNotStarted case invalidConnReq