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.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)
}
}

View File

@ -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)

View File

@ -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 <reified W : ListenableWorker> createWork(data: Data, startChain: Boolean): OneTimeWorkRequest {