From b66b96899ebbac3afc3002b380dbfd5a9a0545c3 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Thu, 13 Feb 2020 22:56:11 +0100 Subject: [PATCH] Better Kotlin code --- .../session/content/UploadContentWorker.kt | 60 +++++++------------ 1 file changed, 20 insertions(+), 40 deletions(-) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/content/UploadContentWorker.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/content/UploadContentWorker.kt index 03504b6279..94bdb11edb 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/content/UploadContentWorker.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/content/UploadContentWorker.kt @@ -88,18 +88,14 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter File(attachment.path) } catch (e: Exception) { Timber.e(e) - params.events - .mapNotNull { it.eventId } - .forEach { - contentUploadStateTracker.setFailure(it, e) - } + notifyTracker(params) { contentUploadStateTracker.setFailure(it, e) } return Result.success( WorkerParamsFactory.toData(params.copy( lastFailureMessage = e.localizedMessage )) ) } - .let {originalFile -> + .let { originalFile -> if (attachment.type == ContentAttachmentData.Type.IMAGE) { if (params.compressBeforeSending) { Compressor.compress(context, originalFile) { @@ -135,22 +131,14 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter ThumbnailExtractor.extractThumbnail(params.attachment)?.let { thumbnailData -> val thumbnailProgressListener = object : ProgressRequestBody.Listener { override fun onProgress(current: Long, total: Long) { - params.events - .mapNotNull { it.eventId } - .forEach { - contentUploadStateTracker.setProgressThumbnail(it, current, total) - } + notifyTracker(params) { contentUploadStateTracker.setProgressThumbnail(it, current, total) } } } try { val contentUploadResponse = if (params.isRoomEncrypted) { Timber.v("Encrypt thumbnail") - params.events - .mapNotNull { it.eventId } - .forEach { - contentUploadStateTracker.setEncryptingThumbnail(it) - } + notifyTracker(params) { contentUploadStateTracker.setEncryptingThumbnail(it) } val encryptionResult = MXEncryptedAttachments.encryptAttachment(ByteArrayInputStream(thumbnailData.bytes), thumbnailData.mimeType) uploadedThumbnailEncryptedFileInfo = encryptionResult.encryptedFileInfo fileUploader.uploadByteArray(encryptionResult.encryptedByteArray, @@ -173,15 +161,13 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter val progressListener = object : ProgressRequestBody.Listener { override fun onProgress(current: Long, total: Long) { - params.events - .mapNotNull { it.eventId } - .forEach { - if (isStopped) { - contentUploadStateTracker.setFailure(it, Throwable("Cancelled")) - } else { - contentUploadStateTracker.setProgress(it, current, total) - } - } + notifyTracker(params) { + if (isStopped) { + contentUploadStateTracker.setFailure(it, Throwable("Cancelled")) + } else { + contentUploadStateTracker.setProgress(it, current, total) + } + } } } @@ -190,11 +176,7 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter return try { val contentUploadResponse = if (params.isRoomEncrypted) { Timber.v("Encrypt file") - params.events - .mapNotNull { it.eventId } - .forEach { - contentUploadStateTracker.setEncrypting(it) - } + notifyTracker(params) { contentUploadStateTracker.setEncrypting(it) } val encryptionResult = MXEncryptedAttachments.encryptAttachment(FileInputStream(attachmentFile), attachment.mimeType) uploadedFileEncryptedFileInfo = encryptionResult.encryptedFileInfo @@ -219,11 +201,7 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter } private fun handleFailure(params: Params, failure: Throwable): Result { - params.events - .mapNotNull { it.eventId } - .forEach { - contentUploadStateTracker.setFailure(it, failure) - } + notifyTracker(params) { contentUploadStateTracker.setFailure(it, failure) } return Result.success( WorkerParamsFactory.toData( @@ -241,11 +219,7 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter thumbnailEncryptedFileInfo: EncryptedFileInfo?, newImageAttributes: NewImageAttributes?): Result { Timber.v("handleSuccess $attachmentUrl, work is stopped $isStopped") - params.events - .mapNotNull { it.eventId } - .forEach { - contentUploadStateTracker.setSuccess(it) - } + notifyTracker(params) { contentUploadStateTracker.setSuccess(it) } val updatedEvents = params.events .map { @@ -273,6 +247,12 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter return event.copy(content = updatedContent.toContent()) } + private fun notifyTracker(params: Params, function: (String) -> Unit) { + params.events + .mapNotNull { it.eventId } + .forEach { eventId -> function.invoke(eventId) } + } + private fun MessageImageContent.update(url: String, encryptedFileInfo: EncryptedFileInfo?, newImageAttributes: NewImageAttributes?): MessageImageContent {