From 8d73e8c905af98878a0d3aebf5ae8f92928b3db3 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Fri, 23 Oct 2020 15:20:26 +0200 Subject: [PATCH] Low priority and favorite are exclusive (iso Element Web) --- .../bottomsheet/BottomSheetRoomPreviewItem.kt | 8 ++++ .../home/room/list/RoomListViewModel.kt | 42 +++++++++++++++---- 2 files changed, 41 insertions(+), 9 deletions(-) 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 } }