diff --git a/vector/src/main/java/im/vector/riotx/core/extensions/Set.kt b/vector/src/main/java/im/vector/riotx/core/extensions/Set.kt new file mode 100644 index 0000000000..43eb1b0d7c --- /dev/null +++ b/vector/src/main/java/im/vector/riotx/core/extensions/Set.kt @@ -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 Set.toggle(element: T): Set { + return if (contains(element)) { + minus(element) + } else { + plus(element) + } +} diff --git a/vector/src/main/java/im/vector/riotx/features/share/IncomingShareViewModel.kt b/vector/src/main/java/im/vector/riotx/features/share/IncomingShareViewModel.kt index 8a425d70d4..c135460ee4 100644 --- a/vector/src/main/java/im/vector/riotx/features/share/IncomingShareViewModel.kt +++ b/vector/src/main/java/im/vector/riotx/features/share/IncomingShareViewModel.kt @@ -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