postWork now returns the Cancellable

This commit is contained in:
Benoit Marty 2020-01-14 15:24:50 +01:00 committed by Valere
parent 3c982866d8
commit 9fe155bafd
3 changed files with 16 additions and 20 deletions

View File

@ -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.model.TimelineEventEntity
import im.vector.matrix.android.internal.database.query.where import im.vector.matrix.android.internal.database.query.where
import im.vector.matrix.android.internal.di.SessionId 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.EncryptEventWorker
import im.vector.matrix.android.internal.session.room.send.LocalEchoEventFactory import im.vector.matrix.android.internal.session.room.send.LocalEchoEventFactory
import im.vector.matrix.android.internal.session.room.send.RedactEventWorker 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.session.room.timeline.TimelineSendEventWorkCommon
import im.vector.matrix.android.internal.task.TaskExecutor import im.vector.matrix.android.internal.task.TaskExecutor
import im.vector.matrix.android.internal.task.configureWith 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.util.fetchCopyMap
import im.vector.matrix.android.internal.worker.WorkerParamsFactory import im.vector.matrix.android.internal.worker.WorkerParamsFactory
import timber.log.Timber import timber.log.Timber
@ -54,7 +52,6 @@ import timber.log.Timber
internal class DefaultRelationService @AssistedInject constructor( internal class DefaultRelationService @AssistedInject constructor(
@Assisted private val roomId: String, @Assisted private val roomId: String,
@SessionId private val sessionId: String, @SessionId private val sessionId: String,
private val workManagerProvider: WorkManagerProvider,
private val timeLineEveSendEventWorkCommon: TimelineSendEventWorkCommon, private val timeLineEveSendEventWorkCommon: TimelineSendEventWorkCommon,
private val eventFactory: LocalEchoEventFactory, private val eventFactory: LocalEchoEventFactory,
private val cryptoService: CryptoService, private val cryptoService: CryptoService,
@ -87,7 +84,6 @@ internal class DefaultRelationService @AssistedInject constructor(
.also { saveLocalEcho(it) } .also { saveLocalEcho(it) }
val sendRelationWork = createSendEventWork(event, true) val sendRelationWork = createSendEventWork(event, true)
timeLineEveSendEventWorkCommon.postWork(roomId, sendRelationWork) timeLineEveSendEventWorkCommon.postWork(roomId, sendRelationWork)
CancelableWork(workManagerProvider.workManager, sendRelationWork.id)
} else { } else {
Timber.w("Reaction already added") Timber.w("Reaction already added")
NoOpCancellable NoOpCancellable
@ -148,11 +144,9 @@ internal class DefaultRelationService @AssistedInject constructor(
val encryptWork = createEncryptEventWork(event, listOf("m.relates_to")) val encryptWork = createEncryptEventWork(event, listOf("m.relates_to"))
val workRequest = createSendEventWork(event, false) val workRequest = createSendEventWork(event, false)
timeLineEveSendEventWorkCommon.postSequentialWorks(roomId, encryptWork, workRequest) timeLineEveSendEventWorkCommon.postSequentialWorks(roomId, encryptWork, workRequest)
CancelableWork(workManagerProvider.workManager, encryptWork.id)
} else { } else {
val workRequest = createSendEventWork(event, true) val workRequest = createSendEventWork(event, true)
timeLineEveSendEventWorkCommon.postWork(roomId, workRequest) 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 encryptWork = createEncryptEventWork(event, listOf("m.relates_to"))
val workRequest = createSendEventWork(event, false) val workRequest = createSendEventWork(event, false)
timeLineEveSendEventWorkCommon.postSequentialWorks(roomId, encryptWork, workRequest) timeLineEveSendEventWorkCommon.postSequentialWorks(roomId, encryptWork, workRequest)
CancelableWork(workManagerProvider.workManager, encryptWork.id)
} else { } else {
val workRequest = createSendEventWork(event, true) val workRequest = createSendEventWork(event, true)
timeLineEveSendEventWorkCommon.postWork(roomId, workRequest) 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 encryptWork = createEncryptEventWork(event, listOf("m.relates_to"))
val workRequest = createSendEventWork(event, false) val workRequest = createSendEventWork(event, false)
timeLineEveSendEventWorkCommon.postSequentialWorks(roomId, encryptWork, workRequest) timeLineEveSendEventWorkCommon.postSequentialWorks(roomId, encryptWork, workRequest)
CancelableWork(workManagerProvider.workManager, encryptWork.id)
} else { } else {
val workRequest = createSendEventWork(event, true) val workRequest = createSendEventWork(event, true)
timeLineEveSendEventWorkCommon.postWork(roomId, workRequest) timeLineEveSendEventWorkCommon.postWork(roomId, workRequest)
CancelableWork(workManagerProvider.workManager, workRequest.id)
} }
} }

View File

@ -16,7 +16,10 @@
package im.vector.matrix.android.internal.session.room.send 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.Assisted
import com.squareup.inject.assisted.AssistedInject import com.squareup.inject.assisted.AssistedInject
import com.zhuinden.monarchy.Monarchy import com.zhuinden.monarchy.Monarchy
@ -91,11 +94,9 @@ internal class DefaultSendService @AssistedInject constructor(
val encryptWork = createEncryptEventWork(event, true) val encryptWork = createEncryptEventWork(event, true)
val sendWork = createSendEventWork(event, false) val sendWork = createSendEventWork(event, false)
timelineSendEventWorkCommon.postSequentialWorks(roomId, encryptWork, sendWork) timelineSendEventWorkCommon.postSequentialWorks(roomId, encryptWork, sendWork)
CancelableWork(workManagerProvider.workManager, encryptWork.id)
} else { } else {
val sendWork = createSendEventWork(event, true) val sendWork = createSendEventWork(event, true)
timelineSendEventWorkCommon.postWork(roomId, sendWork) 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 { override fun redactEvent(event: Event, reason: String?): Cancelable {
// TODO manage media/attachements? // TODO manage media/attachements?
val redactWork = createRedactEventWork(event, reason) val redactWork = createRedactEventWork(event, reason)
timelineSendEventWorkCommon.postWork(roomId, redactWork) return timelineSendEventWorkCommon.postWork(roomId, redactWork)
return CancelableWork(workManagerProvider.workManager, redactWork.id)
} }
override fun resendTextMessage(localEcho: TimelineEvent): Cancelable? { override fun resendTextMessage(localEcho: TimelineEvent): Cancelable? {
@ -244,7 +244,7 @@ internal class DefaultSendService @AssistedInject constructor(
return internalSendMedia(event, attachment) 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 isRoomEncrypted = cryptoService.isRoomEncrypted(roomId)
val uploadWork = createUploadMediaWork(localEcho, attachment, isRoomEncrypted, startChain = true) val uploadWork = createUploadMediaWork(localEcho, attachment, isRoomEncrypted, startChain = true)

View File

@ -16,7 +16,10 @@
package im.vector.matrix.android.internal.session.room.timeline package im.vector.matrix.android.internal.session.room.timeline
import androidx.work.* 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.di.WorkManagerProvider
import im.vector.matrix.android.internal.util.CancelableWork
import im.vector.matrix.android.internal.worker.startChain import im.vector.matrix.android.internal.worker.startChain
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
import javax.inject.Inject import javax.inject.Inject
@ -32,9 +35,9 @@ internal class TimelineSendEventWorkCommon @Inject constructor(
private val workManagerProvider: WorkManagerProvider private val workManagerProvider: WorkManagerProvider
) { ) {
fun postSequentialWorks(roomId: String, vararg workRequests: OneTimeWorkRequest) { fun postSequentialWorks(roomId: String, vararg workRequests: OneTimeWorkRequest): Cancelable {
when { return when {
workRequests.isEmpty() -> return workRequests.isEmpty() -> NoOpCancellable
workRequests.size == 1 -> postWork(roomId, workRequests.first()) workRequests.size == 1 -> postWork(roomId, workRequests.first())
else -> { else -> {
val firstWork = workRequests.first() val firstWork = workRequests.first()
@ -45,14 +48,17 @@ internal class TimelineSendEventWorkCommon @Inject constructor(
continuation = continuation.then(workRequest) continuation = continuation.then(workRequest)
} }
continuation.enqueue() 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 workManagerProvider.workManager
.beginUniqueWork(buildWorkName(roomId), policy, workRequest) .beginUniqueWork(buildWorkName(roomId), policy, workRequest)
.enqueue() .enqueue()
return CancelableWork(workManagerProvider.workManager, workRequest.id)
} }
inline fun <reified W : ListenableWorker> createWork(data: Data, startChain: Boolean): OneTimeWorkRequest { inline fun <reified W : ListenableWorker> createWork(data: Data, startChain: Boolean): OneTimeWorkRequest {