Select all/Deselect all actions

This commit is contained in:
Maxime NATUREL 2022-10-19 14:16:31 +02:00
parent 2fc2665ff3
commit a703b8ae10
3 changed files with 20 additions and 0 deletions

View file

@ -24,4 +24,6 @@ sealed class OtherSessionsAction : VectorViewModelAction {
data class EnableSelectMode(val deviceId: String?) : OtherSessionsAction()
object DisableSelectMode : OtherSessionsAction()
data class ToggleSelectionForDevice(val deviceId: String) : OtherSessionsAction()
object SelectAll : OtherSessionsAction()
object DeselectAll : OtherSessionsAction()
}

View file

@ -85,6 +85,14 @@ class OtherSessionsFragment :
enableSelectMode(true)
true
}
R.id.otherSessionsSelectAll -> {
viewModel.handle(OtherSessionsAction.SelectAll)
true
}
R.id.otherSessionsDeselectAll -> {
viewModel.handle(OtherSessionsAction.DeselectAll)
true
}
else -> false
}
}

View file

@ -72,6 +72,8 @@ class OtherSessionsViewModel @AssistedInject constructor(
OtherSessionsAction.DisableSelectMode -> handleDisableSelectMode()
is OtherSessionsAction.EnableSelectMode -> handleEnableSelectMode(action.deviceId)
is OtherSessionsAction.ToggleSelectionForDevice -> handleToggleSelectionForDevice(action.deviceId)
OtherSessionsAction.DeselectAll -> handleDeselectAll()
OtherSessionsAction.SelectAll -> handleSelectAll()
}
}
@ -118,6 +120,14 @@ class OtherSessionsViewModel @AssistedInject constructor(
}
}
private fun handleSelectAll() = withState { state ->
setSelectionForAllDevices(isSelected = true, enableSelectMode = state.isSelectModeEnabled)
}
private fun handleDeselectAll() = withState { state ->
setSelectionForAllDevices(isSelected = false, enableSelectMode = state.isSelectModeEnabled)
}
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) }