From 9fe155bafdf485da373508b343c97e75503b72e8 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Tue, 14 Jan 2020 15:24:50 +0100 Subject: [PATCH] postWork now returns the Cancellable --- .../room/relation/DefaultRelationService.kt | 10 ---------- .../session/room/send/DefaultSendService.kt | 12 ++++++------ .../room/timeline/TimelineSendEventWorkCommon.kt | 14 ++++++++++---- 3 files changed, 16 insertions(+), 20 deletions(-) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/relation/DefaultRelationService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/relation/DefaultRelationService.kt index 6804ac495f..93b122cfbb 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/relation/DefaultRelationService.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/relation/DefaultRelationService.kt @@ -38,7 +38,6 @@ import im.vector.matrix.android.internal.database.model.EventAnnotationsSummaryE import im.vector.matrix.android.internal.database.model.TimelineEventEntity import im.vector.matrix.android.internal.database.query.where import im.vector.matrix.android.internal.di.SessionId -import im.vector.matrix.android.internal.di.WorkManagerProvider import im.vector.matrix.android.internal.session.room.send.EncryptEventWorker import im.vector.matrix.android.internal.session.room.send.LocalEchoEventFactory import im.vector.matrix.android.internal.session.room.send.RedactEventWorker @@ -46,7 +45,6 @@ import im.vector.matrix.android.internal.session.room.send.SendEventWorker import im.vector.matrix.android.internal.session.room.timeline.TimelineSendEventWorkCommon import im.vector.matrix.android.internal.task.TaskExecutor import im.vector.matrix.android.internal.task.configureWith -import im.vector.matrix.android.internal.util.CancelableWork import im.vector.matrix.android.internal.util.fetchCopyMap import im.vector.matrix.android.internal.worker.WorkerParamsFactory import timber.log.Timber @@ -54,7 +52,6 @@ import timber.log.Timber internal class DefaultRelationService @AssistedInject constructor( @Assisted private val roomId: String, @SessionId private val sessionId: String, - private val workManagerProvider: WorkManagerProvider, private val timeLineEveSendEventWorkCommon: TimelineSendEventWorkCommon, private val eventFactory: LocalEchoEventFactory, private val cryptoService: CryptoService, @@ -87,7 +84,6 @@ internal class DefaultRelationService @AssistedInject constructor( .also { saveLocalEcho(it) } val sendRelationWork = createSendEventWork(event, true) timeLineEveSendEventWorkCommon.postWork(roomId, sendRelationWork) - CancelableWork(workManagerProvider.workManager, sendRelationWork.id) } else { Timber.w("Reaction already added") NoOpCancellable @@ -148,11 +144,9 @@ internal class DefaultRelationService @AssistedInject constructor( val encryptWork = createEncryptEventWork(event, listOf("m.relates_to")) val workRequest = createSendEventWork(event, false) timeLineEveSendEventWorkCommon.postSequentialWorks(roomId, encryptWork, workRequest) - CancelableWork(workManagerProvider.workManager, encryptWork.id) } else { val workRequest = createSendEventWork(event, true) timeLineEveSendEventWorkCommon.postWork(roomId, workRequest) - CancelableWork(workManagerProvider.workManager, workRequest.id) } } @@ -170,11 +164,9 @@ internal class DefaultRelationService @AssistedInject constructor( val encryptWork = createEncryptEventWork(event, listOf("m.relates_to")) val workRequest = createSendEventWork(event, false) timeLineEveSendEventWorkCommon.postSequentialWorks(roomId, encryptWork, workRequest) - CancelableWork(workManagerProvider.workManager, encryptWork.id) } else { val workRequest = createSendEventWork(event, true) timeLineEveSendEventWorkCommon.postWork(roomId, workRequest) - CancelableWork(workManagerProvider.workManager, workRequest.id) } } @@ -196,11 +188,9 @@ internal class DefaultRelationService @AssistedInject constructor( val encryptWork = createEncryptEventWork(event, listOf("m.relates_to")) val workRequest = createSendEventWork(event, false) timeLineEveSendEventWorkCommon.postSequentialWorks(roomId, encryptWork, workRequest) - CancelableWork(workManagerProvider.workManager, encryptWork.id) } else { val workRequest = createSendEventWork(event, true) timeLineEveSendEventWorkCommon.postWork(roomId, workRequest) - CancelableWork(workManagerProvider.workManager, workRequest.id) } } 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 fa86e43e36..121436a9bf 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 @@ -16,7 +16,10 @@ package im.vector.matrix.android.internal.session.room.send -import androidx.work.* +import androidx.work.BackoffPolicy +import androidx.work.ExistingWorkPolicy +import androidx.work.OneTimeWorkRequest +import androidx.work.Operation import com.squareup.inject.assisted.Assisted import com.squareup.inject.assisted.AssistedInject import com.zhuinden.monarchy.Monarchy @@ -91,11 +94,9 @@ internal class DefaultSendService @AssistedInject constructor( val encryptWork = createEncryptEventWork(event, true) val sendWork = createSendEventWork(event, false) timelineSendEventWorkCommon.postSequentialWorks(roomId, encryptWork, sendWork) - CancelableWork(workManagerProvider.workManager, encryptWork.id) } else { val sendWork = createSendEventWork(event, true) timelineSendEventWorkCommon.postWork(roomId, sendWork) - CancelableWork(workManagerProvider.workManager, sendWork.id) } } @@ -108,8 +109,7 @@ internal class DefaultSendService @AssistedInject constructor( override fun redactEvent(event: Event, reason: String?): Cancelable { // TODO manage media/attachements? val redactWork = createRedactEventWork(event, reason) - timelineSendEventWorkCommon.postWork(roomId, redactWork) - return CancelableWork(workManagerProvider.workManager, redactWork.id) + return timelineSendEventWorkCommon.postWork(roomId, redactWork) } override fun resendTextMessage(localEcho: TimelineEvent): Cancelable? { @@ -244,7 +244,7 @@ internal class DefaultSendService @AssistedInject constructor( return internalSendMedia(event, attachment) } - private fun internalSendMedia(localEcho: Event, attachment: ContentAttachmentData): CancelableWork { + private fun internalSendMedia(localEcho: Event, attachment: ContentAttachmentData): Cancelable { val isRoomEncrypted = cryptoService.isRoomEncrypted(roomId) val uploadWork = createUploadMediaWork(localEcho, attachment, isRoomEncrypted, startChain = true) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/TimelineSendEventWorkCommon.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/TimelineSendEventWorkCommon.kt index b042aa4e06..9870df2f29 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/TimelineSendEventWorkCommon.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/TimelineSendEventWorkCommon.kt @@ -16,7 +16,10 @@ package im.vector.matrix.android.internal.session.room.timeline import androidx.work.* +import im.vector.matrix.android.api.util.Cancelable +import im.vector.matrix.android.api.util.NoOpCancellable import im.vector.matrix.android.internal.di.WorkManagerProvider +import im.vector.matrix.android.internal.util.CancelableWork import im.vector.matrix.android.internal.worker.startChain import java.util.concurrent.TimeUnit import javax.inject.Inject @@ -32,9 +35,9 @@ internal class TimelineSendEventWorkCommon @Inject constructor( private val workManagerProvider: WorkManagerProvider ) { - fun postSequentialWorks(roomId: String, vararg workRequests: OneTimeWorkRequest) { - when { - workRequests.isEmpty() -> return + fun postSequentialWorks(roomId: String, vararg workRequests: OneTimeWorkRequest): Cancelable { + return when { + workRequests.isEmpty() -> NoOpCancellable workRequests.size == 1 -> postWork(roomId, workRequests.first()) else -> { val firstWork = workRequests.first() @@ -45,14 +48,17 @@ internal class TimelineSendEventWorkCommon @Inject constructor( continuation = continuation.then(workRequest) } continuation.enqueue() + CancelableWork(workManagerProvider.workManager, firstWork.id) } } } - fun postWork(roomId: String, workRequest: OneTimeWorkRequest, policy: ExistingWorkPolicy = ExistingWorkPolicy.APPEND) { + fun postWork(roomId: String, workRequest: OneTimeWorkRequest, policy: ExistingWorkPolicy = ExistingWorkPolicy.APPEND): Cancelable { workManagerProvider.workManager .beginUniqueWork(buildWorkName(roomId), policy, workRequest) .enqueue() + + return CancelableWork(workManagerProvider.workManager, workRequest.id) } inline fun createWork(data: Data, startChain: Boolean): OneTimeWorkRequest {