Use the new setIsFavorite api
This commit is contained in:
@@ -22,25 +22,20 @@ import io.element.android.libraries.di.SessionScope
|
||||
import io.element.android.libraries.matrix.api.MatrixClient
|
||||
import io.element.android.libraries.matrix.api.core.RoomId
|
||||
import io.element.android.libraries.matrix.api.room.MatrixRoom
|
||||
import kotlinx.coroutines.flow.first
|
||||
import javax.inject.Inject
|
||||
import kotlin.coroutines.cancellation.CancellationException
|
||||
|
||||
@ContributesBinding(SessionScope::class)
|
||||
class DefaultSetRoomIsFavoriteAction @Inject constructor(private val client: MatrixClient) : SetRoomIsFavoriteAction {
|
||||
|
||||
override suspend operator fun invoke(roomId: RoomId, isFavorite: Boolean): SetRoomIsFavoriteAction.Result {
|
||||
return client.getRoom(roomId).use { room ->
|
||||
room?.setIsFavorite(isFavorite) ?: SetRoomIsFavoriteAction.Result.RoomNotFound
|
||||
}
|
||||
return client.getRoom(roomId)?.use { room ->
|
||||
invoke(room, isFavorite)
|
||||
} ?: SetRoomIsFavoriteAction.Result.RoomNotFound
|
||||
}
|
||||
|
||||
override suspend fun invoke(room: MatrixRoom, isFavorite: Boolean): SetRoomIsFavoriteAction.Result {
|
||||
return room.setIsFavorite(isFavorite)
|
||||
}
|
||||
|
||||
private suspend fun MatrixRoom.setIsFavorite(isFavorite: Boolean): SetRoomIsFavoriteAction.Result {
|
||||
val notableTags = notableTagsFlow.first().copy(isFavorite = isFavorite)
|
||||
return updateNotableTags(notableTags).fold(
|
||||
return room.setIsFavorite(isFavorite).fold(
|
||||
onSuccess = {
|
||||
SetRoomIsFavoriteAction.Result.Success
|
||||
},
|
||||
@@ -53,4 +48,5 @@ class DefaultSetRoomIsFavoriteAction @Inject constructor(private val client: Mat
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -52,7 +52,7 @@ class DefaultSetRoomIsFavoriteActionTests {
|
||||
@Test
|
||||
fun `given a room, when action is invoked and fail, then it returns Result_Exception`() = runTest {
|
||||
val action = DefaultSetRoomIsFavoriteAction(FakeMatrixClient())
|
||||
room.givenUpdateNotableTagsResult(Result.failure(Exception()))
|
||||
room.givenSetIsFavoriteResult(Result.failure(Exception()))
|
||||
val result = action(room, true)
|
||||
assert(result is SetRoomIsFavoriteAction.Result.Exception)
|
||||
}
|
||||
|
||||
@@ -156,7 +156,7 @@ interface MatrixRoom : Closeable {
|
||||
|
||||
suspend fun reportContent(eventId: EventId, reason: String, blockUserId: UserId?): Result<Unit>
|
||||
|
||||
suspend fun updateNotableTags(notableTags: RoomNotableTags): Result<Unit>
|
||||
suspend fun setIsFavorite(isFavorite: Boolean): Result<Unit>
|
||||
|
||||
/**
|
||||
* Share a location message in the room.
|
||||
|
||||
@@ -19,7 +19,9 @@ package io.element.android.libraries.matrix.api.room.tags
|
||||
/**
|
||||
* Represents the notable tags of a room.
|
||||
* @param isFavorite true if the room is marked as favorite.
|
||||
* @param isLowPriority true if the room is marked as low priority.
|
||||
*/
|
||||
data class RoomNotableTags(
|
||||
val isFavorite: Boolean = false,
|
||||
val isLowPriority: Boolean = false,
|
||||
)
|
||||
|
||||
@@ -435,14 +435,9 @@ class RustMatrixRoom(
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun updateNotableTags(notableTags: RoomNotableTags): Result<Unit> = withContext(roomDispatcher) {
|
||||
override suspend fun setIsFavorite(isFavorite: Boolean): Result<Unit> = withContext(roomDispatcher) {
|
||||
runCatching {
|
||||
Timber.i("Update notable tags with : $notableTags")
|
||||
innerRoom.updateNotableTags(notableTags.map())
|
||||
}.onFailure {
|
||||
Timber.w("Failed to update notable tags: $it")
|
||||
}.onSuccess {
|
||||
Timber.i("Successfully updated notable tags")
|
||||
innerRoom.setIsFavorite(isFavorite, null)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -20,9 +20,11 @@ import io.element.android.libraries.matrix.api.room.tags.RoomNotableTags
|
||||
import uniffi.matrix_sdk_base.RoomNotableTags as RustRoomNotableTags
|
||||
|
||||
fun RustRoomNotableTags.map() = RoomNotableTags(
|
||||
isFavorite = isFavorite
|
||||
isFavorite = isFavorite,
|
||||
isLowPriority = isLowPriority
|
||||
)
|
||||
|
||||
fun RoomNotableTags.map() = RustRoomNotableTags(
|
||||
isFavorite = isFavorite
|
||||
isFavorite = isFavorite,
|
||||
isLowPriority = isLowPriority
|
||||
)
|
||||
|
||||
@@ -114,7 +114,7 @@ class FakeMatrixRoom(
|
||||
private var getWidgetDriverResult: Result<MatrixWidgetDriver> = Result.success(FakeWidgetDriver())
|
||||
private var canUserTriggerRoomNotificationResult: Result<Boolean> = Result.success(true)
|
||||
private var canUserJoinCallResult: Result<Boolean> = Result.success(true)
|
||||
private var updateNotableTagsResult = Result.success(Unit)
|
||||
private var setIsFavoriteResult = Result.success(Unit)
|
||||
var sendMessageMentions = emptyList<Mention>()
|
||||
val editMessageCalls = mutableListOf<Pair<String, String?>>()
|
||||
private val _typingRecord = mutableListOf<Boolean>()
|
||||
@@ -171,7 +171,7 @@ class FakeMatrixRoom(
|
||||
private val _roomInfoFlow: MutableSharedFlow<MatrixRoomInfo> = MutableSharedFlow(replay = 1)
|
||||
override val roomInfoFlow: Flow<MatrixRoomInfo> = _roomInfoFlow
|
||||
|
||||
private val _notableTagsFlow: MutableSharedFlow<RoomNotableTags> = MutableStateFlow(aRoomNotableTags())
|
||||
private val _notableTagsFlow: MutableStateFlow<RoomNotableTags> = MutableStateFlow(aRoomNotableTags())
|
||||
override val notableTagsFlow: Flow<RoomNotableTags> = _notableTagsFlow
|
||||
|
||||
override val membersStateFlow: MutableStateFlow<MatrixRoomMembersState> = MutableStateFlow(MatrixRoomMembersState.Unknown)
|
||||
@@ -379,9 +379,15 @@ class FakeMatrixRoom(
|
||||
return reportContentResult
|
||||
}
|
||||
|
||||
override suspend fun updateNotableTags(notableTags: RoomNotableTags): Result<Unit> {
|
||||
return updateNotableTagsResult.also { result ->
|
||||
override suspend fun setIsFavorite(isFavorite: Boolean): Result<Unit> {
|
||||
return setIsFavoriteResult.also { result ->
|
||||
if (result.isSuccess) {
|
||||
val lowPriority = if (isFavorite) {
|
||||
false
|
||||
} else {
|
||||
_notableTagsFlow.value.isLowPriority
|
||||
}
|
||||
val notableTags = RoomNotableTags(isFavorite, lowPriority)
|
||||
_notableTagsFlow.emit(notableTags)
|
||||
}
|
||||
}
|
||||
@@ -584,8 +590,8 @@ class FakeMatrixRoom(
|
||||
getWidgetDriverResult = result
|
||||
}
|
||||
|
||||
fun givenUpdateNotableTagsResult(result: Result<Unit>) {
|
||||
updateNotableTagsResult = result
|
||||
fun givenSetIsFavoriteResult(result: Result<Unit>) {
|
||||
setIsFavoriteResult = result
|
||||
}
|
||||
|
||||
fun givenRoomInfo(roomInfo: MatrixRoomInfo) {
|
||||
|
||||
Reference in New Issue
Block a user