Deselect all sessions when leaving select mode

This commit is contained in:
Maxime NATUREL 2022-10-19 12:51:59 +02:00
parent 5b1bf8a68e
commit 2fc2665ff3
2 changed files with 20 additions and 5 deletions

View File

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

View File

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