Convert TagsService to suspend functions

Signed-off-by: Dominic Fischer <dominicfischer7@gmail.com>
This commit is contained in:
Dominic Fischer 2020-11-08 12:53:32 +00:00
parent 8dff0b2c5d
commit 4dff9316c2
3 changed files with 9 additions and 30 deletions

View File

@ -16,9 +16,6 @@
package org.matrix.android.sdk.api.session.room.tags
import org.matrix.android.sdk.api.MatrixCallback
import org.matrix.android.sdk.api.util.Cancelable
/**
* This interface defines methods to handle tags of a room. It's implemented at the room level.
*/
@ -26,10 +23,10 @@ interface TagsService {
/**
* Add a tag to a room
*/
fun addTag(tag: String, order: Double?, callback: MatrixCallback<Unit>): Cancelable
suspend fun addTag(tag: String, order: Double?)
/**
* Remove tag from a room
*/
fun deleteTag(tag: String, callback: MatrixCallback<Unit>): Cancelable
suspend fun deleteTag(tag: String)
}

View File

@ -18,15 +18,10 @@ package org.matrix.android.sdk.internal.session.room.tags
import com.squareup.inject.assisted.Assisted
import com.squareup.inject.assisted.AssistedInject
import org.matrix.android.sdk.api.MatrixCallback
import org.matrix.android.sdk.api.session.room.tags.TagsService
import org.matrix.android.sdk.api.util.Cancelable
import org.matrix.android.sdk.internal.task.TaskExecutor
import org.matrix.android.sdk.internal.task.configureWith
internal class DefaultTagsService @AssistedInject constructor(
@Assisted private val roomId: String,
private val taskExecutor: TaskExecutor,
private val addTagToRoomTask: AddTagToRoomTask,
private val deleteTagFromRoomTask: DeleteTagFromRoomTask
) : TagsService {
@ -36,21 +31,13 @@ internal class DefaultTagsService @AssistedInject constructor(
fun create(roomId: String): TagsService
}
override fun addTag(tag: String, order: Double?, callback: MatrixCallback<Unit>): Cancelable {
override suspend fun addTag(tag: String, order: Double?) {
val params = AddTagToRoomTask.Params(roomId, tag, order)
return addTagToRoomTask
.configureWith(params) {
this.callback = callback
}
.executeBy(taskExecutor)
addTagToRoomTask.execute(params)
}
override fun deleteTag(tag: String, callback: MatrixCallback<Unit>): Cancelable {
override suspend fun deleteTag(tag: String) {
val params = DeleteTagFromRoomTask.Params(roomId, tag)
return deleteTagFromRoomTask
.configureWith(params) {
this.callback = callback
}
.executeBy(taskExecutor)
deleteTagFromRoomTask.execute(params)
}
}

View File

@ -33,7 +33,6 @@ 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 java.lang.Exception
@ -191,17 +190,13 @@ class RoomListViewModel @Inject constructor(initialState: RoomListViewState,
action.tag.otherTag()
?.takeIf { room.roomSummary()?.hasTag(it).orFalse() }
?.let { tagToRemove ->
awaitCallback<Unit> { room.deleteTag(tagToRemove, it) }
room.deleteTag(tagToRemove)
}
// Set the tag. We do not handle the order for the moment
awaitCallback<Unit> {
room.addTag(action.tag, 0.5, it)
}
room.addTag(action.tag, 0.5)
} else {
awaitCallback<Unit> {
room.deleteTag(action.tag, it)
}
room.deleteTag(action.tag)
}
} catch (failure: Throwable) {
_viewEvents.post(RoomListViewEvents.Failure(failure))