From 4887f678c59246fcb04ca3ca77b5e3111d029abc Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Wed, 28 Oct 2020 14:27:09 +0100 Subject: [PATCH] Delete temporary file if user cancel room setting change --- .../roomdirectory/createroom/CreateRoomViewModel.kt | 1 - .../roomprofile/settings/RoomSettingsAction.kt | 1 + .../roomprofile/settings/RoomSettingsFragment.kt | 9 ++++++--- .../roomprofile/settings/RoomSettingsViewEvents.kt | 1 + .../roomprofile/settings/RoomSettingsViewModel.kt | 13 +++++++++++++ 5 files changed, 21 insertions(+), 4 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/roomdirectory/createroom/CreateRoomViewModel.kt b/vector/src/main/java/im/vector/app/features/roomdirectory/createroom/CreateRoomViewModel.kt index 97d8ba9386..36ea0f402a 100644 --- a/vector/src/main/java/im/vector/app/features/roomdirectory/createroom/CreateRoomViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/roomdirectory/createroom/CreateRoomViewModel.kt @@ -106,7 +106,6 @@ class CreateRoomViewModel @AssistedInject constructor(@Assisted initialState: Cr private fun doReset() { setState { // Delete temporary file with the avatar - // TODO BMA Do this also in the other PR avatarUri?.let { tryOrNull { it.toFile().delete() }} CreateRoomViewState( diff --git a/vector/src/main/java/im/vector/app/features/roomprofile/settings/RoomSettingsAction.kt b/vector/src/main/java/im/vector/app/features/roomprofile/settings/RoomSettingsAction.kt index 62852562b0..80bb8813cf 100644 --- a/vector/src/main/java/im/vector/app/features/roomprofile/settings/RoomSettingsAction.kt +++ b/vector/src/main/java/im/vector/app/features/roomprofile/settings/RoomSettingsAction.kt @@ -27,4 +27,5 @@ sealed class RoomSettingsAction : VectorViewModelAction { data class SetRoomCanonicalAlias(val newCanonicalAlias: String) : RoomSettingsAction() object EnableEncryption : RoomSettingsAction() object Save : RoomSettingsAction() + object Cancel : RoomSettingsAction() } diff --git a/vector/src/main/java/im/vector/app/features/roomprofile/settings/RoomSettingsFragment.kt b/vector/src/main/java/im/vector/app/features/roomprofile/settings/RoomSettingsFragment.kt index efc4e1280a..57521f7d80 100644 --- a/vector/src/main/java/im/vector/app/features/roomprofile/settings/RoomSettingsFragment.kt +++ b/vector/src/main/java/im/vector/app/features/roomprofile/settings/RoomSettingsFragment.kt @@ -79,7 +79,11 @@ class RoomSettingsFragment @Inject constructor( viewModel.observeViewEvents { when (it) { is RoomSettingsViewEvents.Failure -> showFailure(it.throwable) - is RoomSettingsViewEvents.Success -> showSuccess() + RoomSettingsViewEvents.Success -> showSuccess() + RoomSettingsViewEvents.GoBack -> { + ignoreChanges = true + vectorBaseActivity.onBackPressed() + } }.exhaustive } } @@ -218,8 +222,7 @@ class RoomSettingsFragment @Inject constructor( .setTitle(R.string.dialog_title_warning) .setMessage(R.string.warning_unsaved_change) .setPositiveButton(R.string.warning_unsaved_change_discard) { _, _ -> - ignoreChanges = true - vectorBaseActivity.onBackPressed() + viewModel.handle(RoomSettingsAction.Cancel) } .setNegativeButton(R.string.cancel, null) .show() diff --git a/vector/src/main/java/im/vector/app/features/roomprofile/settings/RoomSettingsViewEvents.kt b/vector/src/main/java/im/vector/app/features/roomprofile/settings/RoomSettingsViewEvents.kt index 952ca791c9..83a768fb34 100644 --- a/vector/src/main/java/im/vector/app/features/roomprofile/settings/RoomSettingsViewEvents.kt +++ b/vector/src/main/java/im/vector/app/features/roomprofile/settings/RoomSettingsViewEvents.kt @@ -25,4 +25,5 @@ import im.vector.app.core.platform.VectorViewEvents sealed class RoomSettingsViewEvents : VectorViewEvents { data class Failure(val throwable: Throwable) : RoomSettingsViewEvents() object Success : RoomSettingsViewEvents() + object GoBack : RoomSettingsViewEvents() } diff --git a/vector/src/main/java/im/vector/app/features/roomprofile/settings/RoomSettingsViewModel.kt b/vector/src/main/java/im/vector/app/features/roomprofile/settings/RoomSettingsViewModel.kt index 6090209b1a..7a7bc88599 100644 --- a/vector/src/main/java/im/vector/app/features/roomprofile/settings/RoomSettingsViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/roomprofile/settings/RoomSettingsViewModel.kt @@ -16,6 +16,7 @@ package im.vector.app.features.roomprofile.settings +import androidx.core.net.toFile import com.airbnb.mvrx.FragmentViewModelContext import com.airbnb.mvrx.MvRxViewModelFactory import com.airbnb.mvrx.ViewModelContext @@ -27,6 +28,7 @@ import im.vector.app.features.powerlevel.PowerLevelsObservableFactory import io.reactivex.Completable import io.reactivex.Observable import org.matrix.android.sdk.api.MatrixCallback +import org.matrix.android.sdk.api.extensions.tryOrNull import org.matrix.android.sdk.api.query.QueryStringValue import org.matrix.android.sdk.api.session.Session import org.matrix.android.sdk.api.session.events.model.EventType @@ -146,9 +148,20 @@ class RoomSettingsViewModel @AssistedInject constructor(@Assisted initialState: is RoomSettingsAction.SetRoomHistoryVisibility -> setState { copy(newHistoryVisibility = action.visibility) } is RoomSettingsAction.SetRoomCanonicalAlias -> setState { copy(newCanonicalAlias = action.newCanonicalAlias) } is RoomSettingsAction.Save -> saveSettings() + is RoomSettingsAction.Cancel -> cancel() }.exhaustive } + private fun cancel() { + // Maybe delete the pending avatar + withState { + (it.avatarAction as? RoomSettingsViewState.AvatarAction.UpdateAvatar) + ?.let { tryOrNull { it.newAvatarUri.toFile().delete() } } + } + + _viewEvents.post(RoomSettingsViewEvents.GoBack) + } + private fun saveSettings() = withState { state -> postLoading(true)