Merge pull request #2401 from Dominaezzz/suspend_functions

Convert TagsService to suspend functions
This commit is contained in:
Benoit Marty 2020-11-16 13:38:22 +01:00 committed by GitHub
commit babbb82a49
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 30 deletions

View file

@ -16,9 +16,6 @@
package org.matrix.android.sdk.api.session.room.tags 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. * 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 * 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 * 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.Assisted
import com.squareup.inject.assisted.AssistedInject 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.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( internal class DefaultTagsService @AssistedInject constructor(
@Assisted private val roomId: String, @Assisted private val roomId: String,
private val taskExecutor: TaskExecutor,
private val addTagToRoomTask: AddTagToRoomTask, private val addTagToRoomTask: AddTagToRoomTask,
private val deleteTagFromRoomTask: DeleteTagFromRoomTask private val deleteTagFromRoomTask: DeleteTagFromRoomTask
) : TagsService { ) : TagsService {
@ -36,21 +31,13 @@ internal class DefaultTagsService @AssistedInject constructor(
fun create(roomId: String): TagsService 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) val params = AddTagToRoomTask.Params(roomId, tag, order)
return addTagToRoomTask addTagToRoomTask.execute(params)
.configureWith(params) {
this.callback = callback
}
.executeBy(taskExecutor)
} }
override fun deleteTag(tag: String, callback: MatrixCallback<Unit>): Cancelable { override suspend fun deleteTag(tag: String) {
val params = DeleteTagFromRoomTask.Params(roomId, tag) val params = DeleteTagFromRoomTask.Params(roomId, tag)
return deleteTagFromRoomTask deleteTagFromRoomTask.execute(params)
.configureWith(params) {
this.callback = callback
}
.executeBy(taskExecutor)
} }
} }

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.Membership
import org.matrix.android.sdk.api.session.room.model.RoomSummary 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.api.session.room.model.tag.RoomTag
import org.matrix.android.sdk.internal.util.awaitCallback
import org.matrix.android.sdk.rx.rx import org.matrix.android.sdk.rx.rx
import timber.log.Timber import timber.log.Timber
import java.lang.Exception import java.lang.Exception
@ -191,17 +190,13 @@ class RoomListViewModel @Inject constructor(initialState: RoomListViewState,
action.tag.otherTag() action.tag.otherTag()
?.takeIf { room.roomSummary()?.hasTag(it).orFalse() } ?.takeIf { room.roomSummary()?.hasTag(it).orFalse() }
?.let { tagToRemove -> ?.let { tagToRemove ->
awaitCallback<Unit> { room.deleteTag(tagToRemove, it) } room.deleteTag(tagToRemove)
} }
// Set the tag. We do not handle the order for the moment // Set the tag. We do not handle the order for the moment
awaitCallback<Unit> { room.addTag(action.tag, 0.5)
room.addTag(action.tag, 0.5, it)
}
} else { } else {
awaitCallback<Unit> { room.deleteTag(action.tag)
room.deleteTag(action.tag, it)
}
} }
} catch (failure: Throwable) { } catch (failure: Throwable) {
_viewEvents.post(RoomListViewEvents.Failure(failure)) _viewEvents.post(RoomListViewEvents.Failure(failure))