Add the same unsaved changes alerts that Android has. (#4803)

* Add an alert to Discard or Save when there are unsaved changes on the RoomDetailsEditScreen.

* Add an alert to Discard or Save when there are unsaved changes on the UserDetailsEditScreen.

* Add an alert to Discard or Save when there are unsaved changes on the SecurityAndPrivacyScreen.

* Update strings.
This commit is contained in:
Doug
2025-12-01 13:02:50 +00:00
committed by GitHub
parent a16f6ad3e4
commit 7496f30966
22 changed files with 409 additions and 96 deletions
@@ -374,6 +374,7 @@
"dialog_default_video_quality_selector_subtitle" = "Select the default quality of videos you upload.";
"dialog_title_confirmation" = "Confirmation";
"dialog_title_warning" = "Warning";
"dialog_unsaved_changes_description" = "You have unsaved changes.";
"dialog_unsaved_changes_description_ios" = "Your changes won’t be saved";
"dialog_unsaved_changes_title" = "Save changes?";
"dialog_video_quality_selector_subtitle_file_size" = "The max file size allowed is: %1$@";
@@ -1041,7 +1042,6 @@
"screen_room_change_role_moderators_owner_section_footer" = "Owners automatically have admin privileges.";
"screen_room_change_role_moderators_title" = "Edit Moderators";
"screen_room_change_role_owners_title" = "Choose Owners";
"screen_room_change_role_unsaved_changes_description" = "You have unsaved changes.";
"screen_room_details_add_topic_title" = "Add topic";
"screen_room_details_badge_encrypted" = "Encrypted";
"screen_room_details_badge_not_encrypted" = "Not encrypted";
@@ -1425,6 +1425,7 @@
"screen_room_change_role_section_administrators" = "Admins";
"screen_room_change_role_section_moderators" = "Moderators";
"screen_room_change_role_section_users" = "Members";
"screen_room_change_role_unsaved_changes_description" = "You have unsaved changes.";
"screen_room_change_role_unsaved_changes_title" = "Save changes?";
"screen_room_details_badge_public" = "Public room";
"screen_room_details_invite_people_title" = "Invite people";
@@ -374,6 +374,7 @@
"dialog_default_video_quality_selector_subtitle" = "Select the default quality of videos you upload.";
"dialog_title_confirmation" = "Confirmation";
"dialog_title_warning" = "Warning";
"dialog_unsaved_changes_description" = "You have unsaved changes.";
"dialog_unsaved_changes_description_ios" = "Your changes won’t be saved";
"dialog_unsaved_changes_title" = "Save changes?";
"dialog_video_quality_selector_subtitle_file_size" = "The max file size allowed is: %1$@";
@@ -1041,7 +1042,6 @@
"screen_room_change_role_moderators_owner_section_footer" = "Owners automatically have admin privileges.";
"screen_room_change_role_moderators_title" = "Edit Moderators";
"screen_room_change_role_owners_title" = "Choose Owners";
"screen_room_change_role_unsaved_changes_description" = "You have unsaved changes.";
"screen_room_details_add_topic_title" = "Add topic";
"screen_room_details_badge_encrypted" = "Encrypted";
"screen_room_details_badge_not_encrypted" = "Not encrypted";
@@ -1425,6 +1425,7 @@
"screen_room_change_role_section_administrators" = "Admins";
"screen_room_change_role_section_moderators" = "Moderators";
"screen_room_change_role_section_users" = "Members";
"screen_room_change_role_unsaved_changes_description" = "You have unsaved changes.";
"screen_room_change_role_unsaved_changes_title" = "Save changes?";
"screen_room_details_badge_public" = "Public room";
"screen_room_details_invite_people_title" = "Invite people";
@@ -1355,6 +1355,8 @@ class RoomFlowCoordinator: FlowCoordinatorProtocol {
switch action {
case .displayEditAddressScreen:
presentEditAddressScreen()
case .dismiss:
navigationStackCoordinator.pop()
}
}
.store(in: &cancellables)
@@ -155,6 +155,14 @@ class SettingsFlowCoordinator: FlowCoordinatorProtocol {
navigationStackCoordinator: navigationStackCoordinator,
userIndicatorController: flowParameters.userIndicatorController,
appSettings: flowParameters.appSettings))
coordinator.actions
.sink { [weak self] action in
switch action {
case .dismiss:
self?.navigationStackCoordinator.pop()
}
}
.store(in: &cancellables)
navigationStackCoordinator.push(coordinator)
}
@@ -256,6 +256,8 @@ final class SpaceSettingsFlowCoordinator: FlowCoordinatorProtocol {
switch action {
case .displayEditAddressScreen:
self.stateMachine.tryEvent(.presentEditAddress)
case .dismiss:
navigationStackCoordinator.pop()
}
}
.store(in: &cancellables)
+2
View File
@@ -854,6 +854,8 @@ internal enum L10n {
internal static var dialogTitleSuccess: String { return L10n.tr("Localizable", "dialog_title_success") }
/// Warning
internal static var dialogTitleWarning: String { return L10n.tr("Localizable", "dialog_title_warning") }
/// You have unsaved changes.
internal static var dialogUnsavedChangesDescription: String { return L10n.tr("Localizable", "dialog_unsaved_changes_description") }
/// Your changes won’t be saved
internal static var dialogUnsavedChangesDescriptionIos: String { return L10n.tr("Localizable", "dialog_unsaved_changes_description_ios") }
/// Save changes?
+3 -3
View File
@@ -82,9 +82,9 @@ extension ClientProxyMock {
uploadMediaReturnValue = .failure(.sdkError(ClientProxyMockError.generic))
loadUserDisplayNameReturnValue = .failure(.sdkError(ClientProxyMockError.generic))
setUserDisplayNameReturnValue = .failure(.sdkError(ClientProxyMockError.generic))
loadUserAvatarURLReturnValue = .failure(.sdkError(ClientProxyMockError.generic))
setUserAvatarMediaReturnValue = .failure(.sdkError(ClientProxyMockError.generic))
removeUserAvatarReturnValue = .failure(.sdkError(ClientProxyMockError.generic))
loadUserAvatarURLReturnValue = .success(())
setUserAvatarMediaReturnValue = .success(())
removeUserAvatarReturnValue = .success(())
isAliasAvailableReturnValue = .success(true)
searchUsersSearchTermLimitReturnValue = .success(.init(results: [], limited: false))
profileForReturnValue = .success(.init(userID: "@a:b.com", displayName: "Some user"))
@@ -15,12 +15,6 @@ enum RoomDetailsEditScreenViewModelAction {
case displayMediaPicker
}
struct RoomDetailsEditScreenViewStateBindings {
var name: String
var topic: String
var showMediaSheet = false
}
struct RoomDetailsEditScreenViewState: BindableState {
let roomID: String
let isSpace: Bool
@@ -66,6 +60,18 @@ struct RoomDetailsEditScreenViewState: BindableState {
}
}
struct RoomDetailsEditScreenViewStateBindings {
var name: String
var topic: String
var showMediaSheet = false
var alertInfo: AlertInfo<RoomDetailsEditScreenAlertType>?
}
enum RoomDetailsEditScreenAlertType {
case unsavedChanges
}
enum RoomDetailsEditScreenViewAction {
case cancel
case save
@@ -60,9 +60,9 @@ class RoomDetailsEditScreenViewModel: RoomDetailsEditScreenViewModelType, RoomDe
override func process(viewAction: RoomDetailsEditScreenViewAction) {
switch viewAction {
case .cancel:
actionsSubject.send(.cancel)
cancel()
case .save:
saveRoomDetails()
Task { await saveRoomDetails() }
case .presentMediaSource:
state.bindings.showMediaSheet = true
case .displayCameraPicker:
@@ -110,50 +110,60 @@ class RoomDetailsEditScreenViewModel: RoomDetailsEditScreenViewModelType, RoomDe
}
}
private func saveRoomDetails() {
Task {
let userIndicatorID = UUID().uuidString
defer {
userIndicatorController.retractIndicatorWithId(userIndicatorID)
}
userIndicatorController.submitIndicator(UserIndicator(id: userIndicatorID,
type: .modal(progress: .indeterminate, interactiveDismissDisabled: true, allowsInteraction: false),
title: L10n.screenRoomDetailsUpdatingRoom,
persistent: true))
do {
try await withThrowingTaskGroup(of: Void.self) { group in
if state.avatarDidChange {
group.addTask {
if let localMedia = await self.state.localMedia {
try await self.roomProxy.uploadAvatar(media: localMedia).get()
} else if await self.state.avatarURL == nil {
try await self.roomProxy.removeAvatar().get()
}
private func cancel() {
if state.canSave {
state.bindings.alertInfo = .init(id: .unsavedChanges,
title: L10n.dialogUnsavedChangesTitle,
message: L10n.dialogUnsavedChangesDescription,
primaryButton: .init(title: L10n.actionSave) { Task { await self.saveRoomDetails() } },
secondaryButton: .init(title: L10n.actionDiscard, role: .cancel) { self.actionsSubject.send(.cancel) })
} else {
actionsSubject.send(.cancel)
}
}
private func saveRoomDetails() async {
let userIndicatorID = UUID().uuidString
defer {
userIndicatorController.retractIndicatorWithId(userIndicatorID)
}
userIndicatorController.submitIndicator(UserIndicator(id: userIndicatorID,
type: .modal(progress: .indeterminate, interactiveDismissDisabled: true, allowsInteraction: false),
title: L10n.screenRoomDetailsUpdatingRoom,
persistent: true))
do {
try await withThrowingTaskGroup(of: Void.self) { group in
if state.avatarDidChange {
group.addTask {
if let localMedia = await self.state.localMedia {
try await self.roomProxy.uploadAvatar(media: localMedia).get()
} else if await self.state.avatarURL == nil {
try await self.roomProxy.removeAvatar().get()
}
}
if state.nameDidChange {
group.addTask {
try await self.roomProxy.setName(self.state.bindings.name).get()
}
}
if state.topicDidChange {
group.addTask {
try await self.roomProxy.setTopic(self.state.bindings.topic).get()
}
}
try await group.waitForAll()
}
actionsSubject.send(.saveFinished)
} catch {
userIndicatorController.alertInfo = .init(id: .init(),
title: L10n.screenRoomDetailsEditionErrorTitle,
message: L10n.screenRoomDetailsEditionError)
if state.nameDidChange {
group.addTask {
try await self.roomProxy.setName(self.state.bindings.name).get()
}
}
if state.topicDidChange {
group.addTask {
try await self.roomProxy.setTopic(self.state.bindings.topic).get()
}
}
try await group.waitForAll()
}
actionsSubject.send(.saveFinished)
} catch {
userIndicatorController.alertInfo = .init(id: .init(),
title: L10n.screenRoomDetailsEditionErrorTitle,
message: L10n.screenRoomDetailsEditionError)
}
}
}
@@ -30,6 +30,7 @@ struct RoomDetailsEditScreen: View {
.navigationBarTitleDisplayMode(.inline)
.toolbar { toolbar }
.track(screen: .RoomSettings)
.alert(item: $context.alertInfo)
}
// MARK: - Private
@@ -18,6 +18,7 @@ struct SecurityAndPrivacyScreenCoordinatorParameters {
enum SecurityAndPrivacyScreenCoordinatorAction {
case displayEditAddressScreen
case dismiss
}
final class SecurityAndPrivacyScreenCoordinator: CoordinatorProtocol {
@@ -45,6 +46,8 @@ final class SecurityAndPrivacyScreenCoordinator: CoordinatorProtocol {
switch action {
case .displayEditAddressScreen:
actionsSubject.send(.displayEditAddressScreen)
case .dismiss:
actionsSubject.send(.dismiss)
}
}
.store(in: &cancellables)
@@ -10,6 +10,7 @@ import Foundation
enum SecurityAndPrivacyScreenViewModelAction {
case displayEditAddressScreen
case dismiss
}
struct SecurityAndPrivacyScreenViewState: BindableState {
@@ -188,9 +189,11 @@ enum SecurityAndPrivacyRoomAccessType: Equatable {
enum SecurityAndPrivacyAlertType {
case enableEncryption
case unsavedChanges
}
enum SecurityAndPrivacyScreenViewAction {
case cancel
case save
case tryUpdatingEncryption(Bool)
case editAddress
@@ -62,10 +62,10 @@ class SecurityAndPrivacyScreenViewModel: SecurityAndPrivacyScreenViewModelType,
MXLog.info("View model: received view action: \(viewAction)")
switch viewAction {
case .cancel:
showUnsavedChangesAlert() // The cancel button is only shown when there are unsaved changes.
case .save:
Task {
await saveDesiredSettings()
}
Task { await saveDesiredSettings() }
case .tryUpdatingEncryption(let updatedValue):
if updatedValue {
state.bindings.alertInfo = .init(id: .enableEncryption,
@@ -168,12 +168,19 @@ class SecurityAndPrivacyScreenViewModel: SecurityAndPrivacyScreenViewModelType,
}
}
private func saveDesiredSettings() async {
private func showUnsavedChangesAlert() {
state.bindings.alertInfo = .init(id: .unsavedChanges,
title: L10n.dialogUnsavedChangesTitle,
message: L10n.dialogUnsavedChangesDescription,
primaryButton: .init(title: L10n.actionSave) { Task { await self.saveDesiredSettings(shouldDismiss: true) } },
secondaryButton: .init(title: L10n.actionDiscard, role: .cancel) { self.actionsSubject.send(.dismiss) })
}
private func saveDesiredSettings(shouldDismiss: Bool = false) async {
showLoadingIndicator()
defer { hideLoadingIndicator() }
defer {
hideLoadingIndicator()
}
var hasFailures = false
if state.currentSettings.isEncryptionEnabled != state.bindings.desiredSettings.isEncryptionEnabled {
switch await roomProxy.enableEncryption() {
@@ -181,6 +188,7 @@ class SecurityAndPrivacyScreenViewModel: SecurityAndPrivacyScreenViewModelType,
state.currentSettings.isEncryptionEnabled = state.bindings.desiredSettings.isEncryptionEnabled
case .failure:
userIndicatorController.submitIndicator(.init(title: L10n.errorUnknown))
hasFailures = true
}
}
@@ -190,6 +198,7 @@ class SecurityAndPrivacyScreenViewModel: SecurityAndPrivacyScreenViewModelType,
state.currentSettings.historyVisibility = state.bindings.desiredSettings.historyVisibility
case .failure:
userIndicatorController.submitIndicator(.init(title: L10n.errorUnknown))
hasFailures = true
}
}
@@ -205,6 +214,7 @@ class SecurityAndPrivacyScreenViewModel: SecurityAndPrivacyScreenViewModelType,
state.currentSettings.accessType = state.bindings.desiredSettings.accessType
case .failure:
userIndicatorController.submitIndicator(.init(title: L10n.errorUnknown))
hasFailures = true
}
}
@@ -216,8 +226,13 @@ class SecurityAndPrivacyScreenViewModel: SecurityAndPrivacyScreenViewModelType,
state.currentSettings.isVisibileInRoomDirectory = state.bindings.desiredSettings.isVisibileInRoomDirectory
case .failure:
userIndicatorController.submitIndicator(.init(title: L10n.errorUnknown))
hasFailures = true
}
}
if shouldDismiss, !hasFailures {
actionsSubject.send(.dismiss)
}
}
private func handleSelectedSpaceMembersAccess() {
@@ -39,6 +39,7 @@ struct SecurityAndPrivacyScreen: View {
.compoundList()
.navigationBarTitleDisplayMode(.inline)
.navigationTitle(L10n.screenSecurityAndPrivacyTitle)
.navigationBarBackButtonHidden(!context.viewState.isSaveDisabled)
.toolbar { toolbar }
.alert(item: $context.alertInfo)
}
@@ -185,6 +186,14 @@ struct SecurityAndPrivacyScreen: View {
@ToolbarContentBuilder
private var toolbar: some ToolbarContent {
ToolbarItem(placement: .cancellationAction) {
if !context.viewState.isSaveDisabled {
Button(L10n.actionCancel) {
context.send(viewAction: .cancel)
}
}
}
ToolbarItem(placement: .confirmationAction) {
Button(L10n.actionSave) {
context.send(viewAction: .save)
@@ -18,11 +18,20 @@ struct UserDetailsEditScreenCoordinatorParameters {
let appSettings: AppSettings
}
enum UserDetailsEditScreenCoordinatorAction {
case dismiss
}
final class UserDetailsEditScreenCoordinator: CoordinatorProtocol {
private let parameters: UserDetailsEditScreenCoordinatorParameters
private var viewModel: UserDetailsEditScreenViewModelProtocol
private var cancellables = Set<AnyCancellable>()
private let actionsSubject: PassthroughSubject<RoomDetailsEditScreenCoordinatorAction, Never> = .init()
var actions: AnyPublisher<RoomDetailsEditScreenCoordinatorAction, Never> {
actionsSubject.eraseToAnyPublisher()
}
init(parameters: UserDetailsEditScreenCoordinatorParameters) {
self.parameters = parameters
@@ -34,13 +43,17 @@ final class UserDetailsEditScreenCoordinator: CoordinatorProtocol {
func start() {
viewModel.actions
.sink { [weak self] action in
guard let self else { return }
switch action {
case .dismiss:
actionsSubject.send(.dismiss)
case .displayCameraPicker:
self?.displayMediaPickerWithMode(.init(source: .camera, selectionType: .single))
displayMediaPickerWithMode(.init(source: .camera, selectionType: .single))
case .displayMediaPicker:
self?.displayMediaPickerWithMode(.init(source: .photoLibrary, selectionType: .single))
displayMediaPickerWithMode(.init(source: .photoLibrary, selectionType: .single))
case .displayFilePicker:
self?.displayMediaPickerWithMode(.init(source: .documents, selectionType: .single))
displayMediaPickerWithMode(.init(source: .documents, selectionType: .single))
}
}
.store(in: &cancellables)
@@ -9,6 +9,7 @@
import Foundation
enum UserDetailsEditScreenViewModelAction {
case dismiss
case displayCameraPicker
case displayMediaPicker
case displayFilePicker
@@ -46,9 +47,16 @@ struct UserDetailsEditScreenViewState: BindableState {
struct UserDetailsEditScreenViewStateBindings {
var name = ""
var showMediaSheet = false
var alertInfo: AlertInfo<UserDetailsEditScreenAlertType>?
}
enum UserDetailsEditScreenAlertType {
case unsavedChanges
}
enum UserDetailsEditScreenViewAction {
case cancel
case save
case presentMediaSource
case displayCameraPicker
@@ -65,8 +65,10 @@ class UserDetailsEditScreenViewModel: UserDetailsEditScreenViewModelType, UserDe
override func process(viewAction: UserDetailsEditScreenViewAction) {
switch viewAction {
case .cancel:
showUnsavedChangesAlert() // The cancel button is only shown when there are unsaved changes.
case .save:
saveUserDetails()
Task { await saveUserDetails() }
case .presentMediaSource:
state.bindings.showMediaSheet = true
case .displayCameraPicker:
@@ -106,42 +108,52 @@ class UserDetailsEditScreenViewModel: UserDetailsEditScreenViewModelType, UserDe
// MARK: - Private
private func saveUserDetails() {
Task {
let userIndicatorID = UUID().uuidString
defer {
userIndicatorController.retractIndicatorWithId(userIndicatorID)
}
userIndicatorController.submitIndicator(UserIndicator(id: userIndicatorID,
type: .modal(progress: .indeterminate, interactiveDismissDisabled: true, allowsInteraction: false),
title: L10n.screenEditProfileUpdatingDetails,
persistent: true))
do {
try await withThrowingTaskGroup(of: Void.self) { group in
if state.avatarDidChange {
group.addTask {
if let localMedia = await self.state.localMedia {
try await self.clientProxy.setUserAvatar(media: localMedia).get()
} else if await self.state.selectedAvatarURL == nil {
try await self.clientProxy.removeUserAvatar().get()
}
private func showUnsavedChangesAlert() {
state.bindings.alertInfo = .init(id: .unsavedChanges,
title: L10n.dialogUnsavedChangesTitle,
message: L10n.dialogUnsavedChangesDescription,
primaryButton: .init(title: L10n.actionSave) { Task { await self.saveUserDetails(shouldDismiss: true) } },
secondaryButton: .init(title: L10n.actionDiscard, role: .cancel) { self.actionsSubject.send(.dismiss) })
}
private func saveUserDetails(shouldDismiss: Bool = false) async {
let userIndicatorID = UUID().uuidString
defer {
userIndicatorController.retractIndicatorWithId(userIndicatorID)
}
userIndicatorController.submitIndicator(UserIndicator(id: userIndicatorID,
type: .modal(progress: .indeterminate, interactiveDismissDisabled: true, allowsInteraction: false),
title: L10n.screenEditProfileUpdatingDetails,
persistent: true))
do {
try await withThrowingTaskGroup(of: Void.self) { group in
if state.avatarDidChange {
group.addTask {
if let localMedia = await self.state.localMedia {
try await self.clientProxy.setUserAvatar(media: localMedia).get()
} else if await self.state.selectedAvatarURL == nil {
try await self.clientProxy.removeUserAvatar().get()
}
}
if state.nameDidChange {
group.addTask {
try await self.clientProxy.setUserDisplayName(self.state.bindings.name).get()
}
}
try await group.waitForAll()
}
} catch {
userIndicatorController.alertInfo = .init(id: .init(),
title: L10n.screenEditProfileErrorTitle,
message: L10n.screenEditProfileError)
if state.nameDidChange {
group.addTask {
try await self.clientProxy.setUserDisplayName(self.state.bindings.name).get()
}
}
try await group.waitForAll()
}
if shouldDismiss {
actionsSubject.send(.dismiss)
}
} catch {
userIndicatorController.alertInfo = .init(id: .init(),
title: L10n.screenEditProfileErrorTitle,
message: L10n.screenEditProfileError)
}
}
}
@@ -31,13 +31,22 @@ struct UserDetailsEditScreen: View {
.scrollDismissesKeyboard(.immediately)
.navigationTitle(L10n.screenEditProfileTitle)
.navigationBarTitleDisplayMode(.inline)
.navigationBarBackButtonHidden(context.viewState.canSave)
.toolbar { toolbar }
.alert(item: $context.alertInfo)
}
// MARK: - Private
@ToolbarContentBuilder
private var toolbar: some ToolbarContent {
ToolbarItem(placement: .cancellationAction) {
if context.viewState.canSave {
Button(L10n.actionCancel) {
context.send(viewAction: .cancel)
}
}
}
ToolbarItem(placement: .confirmationAction) {
Button(L10n.actionSave) {
context.send(viewAction: .save)