diff --git a/vector/src/main/java/im/vector/app/core/epoxy/bottomsheet/BottomSheetRoomPreviewItem.kt b/vector/src/main/java/im/vector/app/core/epoxy/bottomsheet/BottomSheetRoomPreviewItem.kt index 105388d87b..1c80e6a85c 100644 --- a/vector/src/main/java/im/vector/app/core/epoxy/bottomsheet/BottomSheetRoomPreviewItem.kt +++ b/vector/src/main/java/im/vector/app/core/epoxy/bottomsheet/BottomSheetRoomPreviewItem.kt @@ -61,12 +61,20 @@ abstract class BottomSheetRoomPreviewItem : VectorEpoxyModel { - override fun onFailure(failure: Throwable) { + session.getRoom(action.roomId)?.let { room -> + viewModelScope.launch(Dispatchers.IO) { + try { + if (room.roomSummary()?.hasTag(action.tag) == false) { + // Favorite and low priority tags are exclusive, so maybe delete the other tag first + action.tag.otherTag() + ?.takeIf { room.roomSummary()?.hasTag(it).orFalse() } + ?.let { tagToRemove -> + awaitCallback { room.deleteTag(tagToRemove, it) } + } + + // Set the tag. We do not handle the order for the moment + awaitCallback { + room.addTag(action.tag, 0.5, it) + } + } else { + awaitCallback { + room.deleteTag(action.tag, it) + } + } + } catch (failure: Throwable) { _viewEvents.post(RoomListViewEvents.Failure(failure)) } } - if (it.roomSummary()?.hasTag(action.tag) == false) { - // Set the tag. We do not handle the order for the moment - it.addTag(action.tag, 0.5, callback) - } else { - it.deleteTag(action.tag, callback) - } + } + } + + private fun String.otherTag(): String? { + return when (this) { + RoomTag.ROOM_TAG_FAVOURITE -> RoomTag.ROOM_TAG_LOW_PRIORITY + RoomTag.ROOM_TAG_LOW_PRIORITY -> RoomTag.ROOM_TAG_FAVOURITE + else -> null } }