Low priority and favorite are exclusive (iso Element Web)

This commit is contained in:
Benoit Marty 2020-10-23 15:20:26 +02:00
parent 5a6683574c
commit 8d73e8c905
2 changed files with 41 additions and 9 deletions

View file

@ -61,12 +61,20 @@ abstract class BottomSheetRoomPreviewItem : VectorEpoxyModel<BottomSheetRoomPrev
holder.roomLowPriority.setOnClickListener {
// Immediate echo
setLowPriorityState(holder, !izLowPriority)
if (!izLowPriority) {
// If we put the room in low priority, it will also remove the favorite tag
setFavoriteState(holder, false)
}
// And do the action
lowPriorityClickListener?.invoke()
}
holder.roomFavorite.setOnClickListener {
// Immediate echo
setFavoriteState(holder, !izFavorite)
if (!izFavorite) {
// If we put the room in favorite, it will also remove the low priority tag
setLowPriorityState(holder, false)
}
// And do the action
favoriteClickListener?.invoke()
}

View file

@ -16,6 +16,7 @@
package im.vector.app.features.home.room.list
import androidx.lifecycle.viewModelScope
import com.airbnb.mvrx.FragmentViewModelContext
import com.airbnb.mvrx.MvRxViewModelFactory
import com.airbnb.mvrx.ViewModelContext
@ -23,6 +24,8 @@ import im.vector.app.core.extensions.exhaustive
import im.vector.app.core.platform.VectorViewModel
import im.vector.app.core.utils.DataSource
import io.reactivex.schedulers.Schedulers
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.MatrixCallback
import org.matrix.android.sdk.api.NoOpMatrixCallback
import org.matrix.android.sdk.api.extensions.orFalse
@ -30,6 +33,7 @@ import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.session.room.model.Membership
import org.matrix.android.sdk.api.session.room.model.RoomSummary
import org.matrix.android.sdk.api.session.room.model.tag.RoomTag
import org.matrix.android.sdk.internal.util.awaitCallback
import org.matrix.android.sdk.rx.rx
import timber.log.Timber
import javax.inject.Inject
@ -173,18 +177,38 @@ class RoomListViewModel @Inject constructor(initialState: RoomListViewState,
}
private fun handleToggleTag(action: RoomListAction.ToggleTag) {
session.getRoom(action.roomId)?.let {
val callback = object : MatrixCallback<Unit> {
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<Unit> { room.deleteTag(tagToRemove, it) }
}
// Set the tag. We do not handle the order for the moment
awaitCallback<Unit> {
room.addTag(action.tag, 0.5, it)
}
} else {
awaitCallback<Unit> {
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
}
}