mirror of
https://github.com/element-hq/element-android
synced 2024-11-25 02:45:37 +03:00
Reduce code duplication
This commit is contained in:
parent
4995c14f69
commit
f2f94c4a93
1 changed files with 23 additions and 44 deletions
|
@ -221,55 +221,34 @@ internal class DefaultSendService @AssistedInject constructor(
|
|||
* We use the roomId of the local echo event
|
||||
*/
|
||||
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()
|
||||
|
||||
if (encryptedLocalEchoes.isNotEmpty()) {
|
||||
val uploadWork = createUploadMediaWork(encryptedLocalEchoes, attachment, true, compressBeforeSending, startChain = true)
|
||||
allLocalEchoes.groupBy { cryptoService.isRoomEncrypted(it.roomId!!) }
|
||||
.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
|
||||
.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)
|
||||
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))
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue