Better Kotlin code

This commit is contained in:
Benoit Marty 2020-02-13 22:56:11 +01:00
parent e6bd09859f
commit b66b96899e

View file

@ -88,11 +88,7 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter
File(attachment.path) File(attachment.path)
} catch (e: Exception) { } catch (e: Exception) {
Timber.e(e) Timber.e(e)
params.events notifyTracker(params) { contentUploadStateTracker.setFailure(it, e) }
.mapNotNull { it.eventId }
.forEach {
contentUploadStateTracker.setFailure(it, e)
}
return Result.success( return Result.success(
WorkerParamsFactory.toData(params.copy( WorkerParamsFactory.toData(params.copy(
lastFailureMessage = e.localizedMessage lastFailureMessage = e.localizedMessage
@ -135,22 +131,14 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter
ThumbnailExtractor.extractThumbnail(params.attachment)?.let { thumbnailData -> ThumbnailExtractor.extractThumbnail(params.attachment)?.let { thumbnailData ->
val thumbnailProgressListener = object : ProgressRequestBody.Listener { val thumbnailProgressListener = object : ProgressRequestBody.Listener {
override fun onProgress(current: Long, total: Long) { override fun onProgress(current: Long, total: Long) {
params.events notifyTracker(params) { contentUploadStateTracker.setProgressThumbnail(it, current, total) }
.mapNotNull { it.eventId }
.forEach {
contentUploadStateTracker.setProgressThumbnail(it, current, total)
}
} }
} }
try { try {
val contentUploadResponse = if (params.isRoomEncrypted) { val contentUploadResponse = if (params.isRoomEncrypted) {
Timber.v("Encrypt thumbnail") Timber.v("Encrypt thumbnail")
params.events notifyTracker(params) { contentUploadStateTracker.setEncryptingThumbnail(it) }
.mapNotNull { it.eventId }
.forEach {
contentUploadStateTracker.setEncryptingThumbnail(it)
}
val encryptionResult = MXEncryptedAttachments.encryptAttachment(ByteArrayInputStream(thumbnailData.bytes), thumbnailData.mimeType) val encryptionResult = MXEncryptedAttachments.encryptAttachment(ByteArrayInputStream(thumbnailData.bytes), thumbnailData.mimeType)
uploadedThumbnailEncryptedFileInfo = encryptionResult.encryptedFileInfo uploadedThumbnailEncryptedFileInfo = encryptionResult.encryptedFileInfo
fileUploader.uploadByteArray(encryptionResult.encryptedByteArray, fileUploader.uploadByteArray(encryptionResult.encryptedByteArray,
@ -173,9 +161,7 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter
val progressListener = object : ProgressRequestBody.Listener { val progressListener = object : ProgressRequestBody.Listener {
override fun onProgress(current: Long, total: Long) { override fun onProgress(current: Long, total: Long) {
params.events notifyTracker(params) {
.mapNotNull { it.eventId }
.forEach {
if (isStopped) { if (isStopped) {
contentUploadStateTracker.setFailure(it, Throwable("Cancelled")) contentUploadStateTracker.setFailure(it, Throwable("Cancelled"))
} else { } else {
@ -190,11 +176,7 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter
return try { return try {
val contentUploadResponse = if (params.isRoomEncrypted) { val contentUploadResponse = if (params.isRoomEncrypted) {
Timber.v("Encrypt file") Timber.v("Encrypt file")
params.events notifyTracker(params) { contentUploadStateTracker.setEncrypting(it) }
.mapNotNull { it.eventId }
.forEach {
contentUploadStateTracker.setEncrypting(it)
}
val encryptionResult = MXEncryptedAttachments.encryptAttachment(FileInputStream(attachmentFile), attachment.mimeType) val encryptionResult = MXEncryptedAttachments.encryptAttachment(FileInputStream(attachmentFile), attachment.mimeType)
uploadedFileEncryptedFileInfo = encryptionResult.encryptedFileInfo uploadedFileEncryptedFileInfo = encryptionResult.encryptedFileInfo
@ -219,11 +201,7 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter
} }
private fun handleFailure(params: Params, failure: Throwable): Result { private fun handleFailure(params: Params, failure: Throwable): Result {
params.events notifyTracker(params) { contentUploadStateTracker.setFailure(it, failure) }
.mapNotNull { it.eventId }
.forEach {
contentUploadStateTracker.setFailure(it, failure)
}
return Result.success( return Result.success(
WorkerParamsFactory.toData( WorkerParamsFactory.toData(
@ -241,11 +219,7 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter
thumbnailEncryptedFileInfo: EncryptedFileInfo?, thumbnailEncryptedFileInfo: EncryptedFileInfo?,
newImageAttributes: NewImageAttributes?): Result { newImageAttributes: NewImageAttributes?): Result {
Timber.v("handleSuccess $attachmentUrl, work is stopped $isStopped") Timber.v("handleSuccess $attachmentUrl, work is stopped $isStopped")
params.events notifyTracker(params) { contentUploadStateTracker.setSuccess(it) }
.mapNotNull { it.eventId }
.forEach {
contentUploadStateTracker.setSuccess(it)
}
val updatedEvents = params.events val updatedEvents = params.events
.map { .map {
@ -273,6 +247,12 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter
return event.copy(content = updatedContent.toContent()) 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, private fun MessageImageContent.update(url: String,
encryptedFileInfo: EncryptedFileInfo?, encryptedFileInfo: EncryptedFileInfo?,
newImageAttributes: NewImageAttributes?): MessageImageContent { newImageAttributes: NewImageAttributes?): MessageImageContent {