From f5e46df309bf5728b0f9accee30d743451ed01ff Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Wed, 28 Oct 2020 14:29:03 +0100 Subject: [PATCH] Delete temporary file if user change again the room avatar --- .../roomprofile/settings/RoomSettingsViewModel.kt | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) 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 7a7bc88599..e083b6ca0f 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 @@ -142,7 +142,7 @@ class RoomSettingsViewModel @AssistedInject constructor(@Assisted initialState: override fun handle(action: RoomSettingsAction) { when (action) { is RoomSettingsAction.EnableEncryption -> handleEnableEncryption() - is RoomSettingsAction.SetAvatarAction -> setState { copy(avatarAction = action.avatarAction) } + is RoomSettingsAction.SetAvatarAction -> handleSetAvatarAction(action) is RoomSettingsAction.SetRoomName -> setState { copy(newName = action.newName) } is RoomSettingsAction.SetRoomTopic -> setState { copy(newTopic = action.newTopic) } is RoomSettingsAction.SetRoomHistoryVisibility -> setState { copy(newHistoryVisibility = action.visibility) } @@ -152,12 +152,21 @@ class RoomSettingsViewModel @AssistedInject constructor(@Assisted initialState: }.exhaustive } - private fun cancel() { + private fun handleSetAvatarAction(action: RoomSettingsAction.SetAvatarAction) { + deletePendingAvatar() + setState { copy(avatarAction = action.avatarAction) } + } + + private fun deletePendingAvatar() { // Maybe delete the pending avatar withState { (it.avatarAction as? RoomSettingsViewState.AvatarAction.UpdateAvatar) ?.let { tryOrNull { it.newAvatarUri.toFile().delete() } } } + } + + private fun cancel() { + deletePendingAvatar() _viewEvents.post(RoomSettingsViewEvents.GoBack) }