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
@@ -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)