From 0f3a63e36687aafe87fd024973337e02e6703446 Mon Sep 17 00:00:00 2001 From: Dominic Fischer Date: Tue, 2 Jul 2019 21:44:02 +0100 Subject: [PATCH] Enhance CancelableBag Signed-off-by: Dominic Fischer --- .../matrix/android/api/util/CancelableBag.kt | 16 ++-------------- .../session/room/send/DefaultSendService.kt | 7 ++----- .../session/room/timeline/DefaultTimeline.kt | 4 +--- 3 files changed, 5 insertions(+), 22 deletions(-) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/util/CancelableBag.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/util/CancelableBag.kt index 95fdc3de24..f5689b4d13 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/util/CancelableBag.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/util/CancelableBag.kt @@ -16,20 +16,8 @@ package im.vector.matrix.android.api.util -class CancelableBag : Cancelable { - - private val cancelableList = ArrayList() - - fun add(cancelable: Cancelable) { - cancelableList.add(cancelable) - } - +class CancelableBag : Cancelable, MutableList by ArrayList() { override fun cancel() { - cancelableList.forEach { it.cancel() } + forEach { it.cancel() } } - } - -fun Cancelable.addTo(cancelables: CancelableBag) { - cancelables.add(this) -} \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/DefaultSendService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/DefaultSendService.kt index 27e9c62e00..85712ee277 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/DefaultSendService.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/DefaultSendService.kt @@ -26,7 +26,6 @@ import im.vector.matrix.android.api.session.events.model.Event import im.vector.matrix.android.api.session.room.send.SendService import im.vector.matrix.android.api.util.Cancelable import im.vector.matrix.android.api.util.CancelableBag -import im.vector.matrix.android.api.util.addTo import im.vector.matrix.android.internal.session.content.UploadContentWorker import im.vector.matrix.android.internal.session.room.timeline.TimelineSendEventWorkCommon import im.vector.matrix.android.internal.util.CancelableWork @@ -82,11 +81,9 @@ internal class DefaultSendService @Inject constructor(private val context: Conte } override fun sendMedias(attachments: List): Cancelable { - val cancelableBag = CancelableBag() - attachments.forEach { - sendMedia(it).addTo(cancelableBag) + return attachments.mapTo(CancelableBag()) { + sendMedia(it) } - return cancelableBag } override fun redactEvent(event: Event, reason: String?): Cancelable { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/DefaultTimeline.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/DefaultTimeline.kt index 72c07527b6..6ae965159b 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/DefaultTimeline.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/DefaultTimeline.kt @@ -26,7 +26,6 @@ import im.vector.matrix.android.api.session.events.model.toModel import im.vector.matrix.android.api.session.room.timeline.Timeline import im.vector.matrix.android.api.session.room.timeline.TimelineEvent import im.vector.matrix.android.api.util.CancelableBag -import im.vector.matrix.android.api.util.addTo import im.vector.matrix.android.internal.crypto.NewSessionListener import im.vector.matrix.android.internal.crypto.model.event.EncryptedEventContent import im.vector.matrix.android.internal.database.mapper.asDomain @@ -392,7 +391,7 @@ internal class DefaultTimeline( limit = limit) Timber.v("Should fetch $limit items $direction") - paginationTask.configureWith(params) + cancelableBag += paginationTask.configureWith(params) .enableRetry() .dispatchTo(object : MatrixCallback { override fun onSuccess(data: TokenChunkEventPersistor.Result) { @@ -411,7 +410,6 @@ internal class DefaultTimeline( } }) .executeBy(taskExecutor) - .addTo(cancelableBag) } /**