From 2fc2665ff31c994c334522638d0d1b036b4852e2 Mon Sep 17 00:00:00 2001 From: Maxime NATUREL Date: Wed, 19 Oct 2022 12:51:59 +0200 Subject: [PATCH] Deselect all sessions when leaving select mode --- .../v2/othersessions/OtherSessionsFragment.kt | 2 +- .../othersessions/OtherSessionsViewModel.kt | 23 +++++++++++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/othersessions/OtherSessionsFragment.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/othersessions/OtherSessionsFragment.kt index 1a0d63f04a..73e528b358 100644 --- a/vector/src/main/java/im/vector/app/features/settings/devices/v2/othersessions/OtherSessionsFragment.kt +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/othersessions/OtherSessionsFragment.kt @@ -154,7 +154,7 @@ class OtherSessionsFragment : override fun invalidate() = withState(viewModel) { state -> if (state.devices is Success) { - val devices = state.devices().orEmpty() + val devices = state.devices.invoke() renderDevices(devices, state.currentFilter) updateToolbar(devices, state.isSelectModeEnabled) } diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/othersessions/OtherSessionsViewModel.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/othersessions/OtherSessionsViewModel.kt index 4a00f0ab2b..4ad2ab96e8 100644 --- a/vector/src/main/java/im/vector/app/features/settings/devices/v2/othersessions/OtherSessionsViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/othersessions/OtherSessionsViewModel.kt @@ -85,16 +85,15 @@ class OtherSessionsViewModel @AssistedInject constructor( } private fun handleDisableSelectMode() { - // TODO deselect all selected sessions - setState { copy(isSelectModeEnabled = false) } + setSelectionForAllDevices(isSelected = false, enableSelectMode = false) } private fun handleEnableSelectMode(deviceId: String?) { - toggleSelectionForDevice(deviceId, true) + toggleSelectionForDevice(deviceId, enableSelectMode = true) } private fun handleToggleSelectionForDevice(deviceId: String) = withState { state -> - toggleSelectionForDevice(deviceId, state.isSelectModeEnabled) + toggleSelectionForDevice(deviceId, enableSelectMode = state.isSelectModeEnabled) } private fun toggleSelectionForDevice(deviceId: String?, enableSelectMode: Boolean) = withState { state -> @@ -118,4 +117,20 @@ class OtherSessionsViewModel @AssistedInject constructor( ) } } + + private fun setSelectionForAllDevices(isSelected: Boolean, enableSelectMode: Boolean) = withState { state -> + val updatedDevices = if (state.devices is Success) { + val updatedDevices = state.devices.invoke().map { it.copy(isSelected = isSelected) } + Success(updatedDevices) + } else { + state.devices + } + + setState { + copy( + devices = updatedDevices, + isSelectModeEnabled = enableSelectMode + ) + } + } }