Start chain: create extension

This commit is contained in:
Benoit Marty 2019-08-06 15:23:13 +02:00
parent d82fd10f3b
commit 7a6fc4936b
4 changed files with 18 additions and 17 deletions

View File

@ -42,6 +42,7 @@ import im.vector.matrix.android.internal.worker.AlwaysSuccessfulWorker
import im.vector.matrix.android.internal.worker.WorkManagerUtil
import im.vector.matrix.android.internal.worker.WorkManagerUtil.matrixOneTimeWorkRequestBuilder
import im.vector.matrix.android.internal.worker.WorkerParamsFactory
import im.vector.matrix.android.internal.worker.startChain
import timber.log.Timber
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit
@ -288,11 +289,7 @@ internal class DefaultSendService @Inject constructor(private val context: Conte
return matrixOneTimeWorkRequestBuilder<EncryptEventWorker>()
.setConstraints(WorkManagerUtil.workConstraints)
.setInputData(sendWorkData)
.apply {
if (startChain) {
setInputMerger(NoMerger::class.java)
}
}
.startChain(startChain)
.setBackoffCriteria(BackoffPolicy.LINEAR, BACKOFF_DELAY, TimeUnit.MILLISECONDS)
.build()
}
@ -322,11 +319,7 @@ internal class DefaultSendService @Inject constructor(private val context: Conte

return matrixOneTimeWorkRequestBuilder<UploadContentWorker>()
.setConstraints(WorkManagerUtil.workConstraints)
.apply {
if (startChain) {
setInputMerger(NoMerger::class.java)
}
}
.startChain(startChain)
.setInputData(uploadWorkData)
.setBackoffCriteria(BackoffPolicy.LINEAR, BACKOFF_DELAY, TimeUnit.MILLISECONDS)
.build()

View File

@ -19,7 +19,7 @@ import androidx.work.Data
import androidx.work.InputMerger

/**
* InputMerger which takes only the first input, to ensure an appended work with only have the specified parameters
* InputMerger which takes only the first input, to ensure an appended work will only have the specified parameters
*/
internal class NoMerger : InputMerger() {
override fun merge(inputs: MutableList<Data>): Data {

View File

@ -17,9 +17,9 @@ package im.vector.matrix.android.internal.session.room.timeline

import android.content.Context
import androidx.work.*
import im.vector.matrix.android.internal.session.room.send.NoMerger
import im.vector.matrix.android.internal.worker.WorkManagerUtil
import im.vector.matrix.android.internal.worker.WorkManagerUtil.matrixOneTimeWorkRequestBuilder
import im.vector.matrix.android.internal.worker.startChain
import java.util.concurrent.TimeUnit


@ -61,11 +61,7 @@ internal object TimelineSendEventWorkCommon {
inline fun <reified W : ListenableWorker> createWork(data: Data, startChain: Boolean): OneTimeWorkRequest {
return matrixOneTimeWorkRequestBuilder<W>()
.setConstraints(WorkManagerUtil.workConstraints)
.apply {
if (startChain) {
setInputMerger(NoMerger::class.java)
}
}
.startChain(startChain)
.setInputData(data)
.setBackoffCriteria(BackoffPolicy.LINEAR, BACKOFF_DELAY, TimeUnit.MILLISECONDS)
.build()

View File

@ -16,3 +16,15 @@

package im.vector.matrix.android.internal.worker

import androidx.work.OneTimeWorkRequest
import im.vector.matrix.android.internal.session.room.send.NoMerger

/**
* If startChain parameter is true, the builder will have a inputMerger set to [NoMerger]
*/
internal fun OneTimeWorkRequest.Builder.startChain(startChain: Boolean): OneTimeWorkRequest.Builder {
if (startChain) {
setInputMerger(NoMerger::class.java)
}
return this
}