Create Set.toggle() method

This commit is contained in:
Benoit Marty 2020-02-18 13:33:49 +01:00
parent d730f96c41
commit 86c38ebd2d
2 changed files with 28 additions and 5 deletions

View File

@ -0,0 +1,26 @@
/*
* Copyright (c) 2020 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.riotx.core.extensions
// Create a new Set including the provided element if not already present, or removing the element if already present
fun <T> Set<T>.toggle(element: T): Set<T> {
return if (contains(element)) {
minus(element)
} else {
plus(element)
}
}

View File

@ -29,6 +29,7 @@ import im.vector.matrix.android.api.session.room.model.Membership
import im.vector.matrix.android.api.session.room.roomSummaryQueryParams
import im.vector.matrix.rx.rx
import im.vector.riotx.core.extensions.exhaustive
import im.vector.riotx.core.extensions.toggle
import im.vector.riotx.core.platform.VectorViewModel
import im.vector.riotx.features.attachments.isPreviewable
import im.vector.riotx.features.attachments.toGroupedContentAttachmentData
@ -180,11 +181,7 @@ class IncomingShareViewModel @AssistedInject constructor(
if (state.isInMultiSelectionMode) {
// One room is clicked (or long clicked) while in multi selection mode -> toggle this room
val selectedRooms = state.selectedRoomIds
val newSelectedRooms = if (selectedRooms.contains(action.roomSummary.roomId)) {
selectedRooms.minus(action.roomSummary.roomId)
} else {
selectedRooms.plus(action.roomSummary.roomId)
}
val newSelectedRooms = selectedRooms.toggle(action.roomSummary.roomId)
setState { copy(isInMultiSelectionMode = newSelectedRooms.isNotEmpty(), selectedRoomIds = newSelectedRooms) }
} else if (action.enableMultiSelect) {
// One room is long clicked, not in multi selection mode -> enable multi selection mode