Reduce code duplication

This commit is contained in:
Benoit Marty 2020-02-17 17:56:11 +01:00
parent 4995c14f69
commit f2f94c4a93
1 changed files with 23 additions and 44 deletions

View File

@ -221,17 +221,16 @@ internal class DefaultSendService @AssistedInject constructor(
* We use the roomId of the local echo event * We use the roomId of the local echo event
*/ */
private fun internalSendMedia(allLocalEchoes: List<Event>, attachment: ContentAttachmentData, compressBeforeSending: Boolean): Cancelable { private fun internalSendMedia(allLocalEchoes: List<Event>, attachment: ContentAttachmentData, compressBeforeSending: Boolean): Cancelable {
val splitLocalEchoes = allLocalEchoes.groupBy { cryptoService.isRoomEncrypted(it.roomId!!) }
val encryptedLocalEchoes = splitLocalEchoes[true].orEmpty()
val clearLocalEchoes = splitLocalEchoes[false].orEmpty()
val cancelableBag = CancelableBag() val cancelableBag = CancelableBag()
if (encryptedLocalEchoes.isNotEmpty()) { allLocalEchoes.groupBy { cryptoService.isRoomEncrypted(it.roomId!!) }
val uploadWork = createUploadMediaWork(encryptedLocalEchoes, attachment, true, compressBeforeSending, startChain = true) .apply {
keys.forEach { isRoomEncrypted ->
// Should never be empty
val localEchoes = get(isRoomEncrypted).orEmpty()
val uploadWork = createUploadMediaWork(localEchoes, attachment, isRoomEncrypted, compressBeforeSending, startChain = true)
val dispatcherWork = createMultipleEventDispatcherWork(true) val dispatcherWork = createMultipleEventDispatcherWork(isRoomEncrypted)
workManagerProvider.workManager workManagerProvider.workManager
.beginUniqueWork(buildWorkName(UPLOAD_WORK), ExistingWorkPolicy.APPEND, uploadWork) .beginUniqueWork(buildWorkName(UPLOAD_WORK), ExistingWorkPolicy.APPEND, uploadWork)
@ -249,26 +248,6 @@ internal class DefaultSendService @AssistedInject constructor(
cancelableBag.add(CancelableWork(workManagerProvider.workManager, dispatcherWork.id)) cancelableBag.add(CancelableWork(workManagerProvider.workManager, dispatcherWork.id))
} }
if (clearLocalEchoes.isNotEmpty()) {
val uploadWork = createUploadMediaWork(clearLocalEchoes, attachment, false, compressBeforeSending, startChain = true)
val dispatcherWork = createMultipleEventDispatcherWork(false)
workManagerProvider.workManager
.beginUniqueWork(buildWorkName(UPLOAD_WORK), ExistingWorkPolicy.APPEND, uploadWork)
.then(dispatcherWork)
.enqueue()
.also { operation ->
operation.result.addListener(Runnable {
if (operation.result.isCancelled) {
Timber.e("CHAIN WAS CANCELLED")
} else if (operation.state.value is Operation.State.FAILURE) {
Timber.e("CHAIN DID FAIL")
}
}, workerFutureListenerExecutor)
}
cancelableBag.add(CancelableWork(workManagerProvider.workManager, dispatcherWork.id))
} }
return cancelableBag return cancelableBag