From d87b951403fc83c3ed1a83a1c068d03e394856b6 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Thu, 13 Feb 2020 17:40:14 +0100 Subject: [PATCH] Improve code --- .../attachments/ContentAttachmentData.kt | 18 ++++++++++++------ .../home/room/detail/RoomDetailFragment.kt | 14 ++++++-------- .../features/share/IncomingShareViewModel.kt | 16 +++++++--------- 3 files changed, 25 insertions(+), 23 deletions(-) diff --git a/vector/src/main/java/im/vector/riotx/features/attachments/ContentAttachmentData.kt b/vector/src/main/java/im/vector/riotx/features/attachments/ContentAttachmentData.kt index ecd979f900..8fb717f96e 100644 --- a/vector/src/main/java/im/vector/riotx/features/attachments/ContentAttachmentData.kt +++ b/vector/src/main/java/im/vector/riotx/features/attachments/ContentAttachmentData.kt @@ -22,11 +22,17 @@ fun ContentAttachmentData.isPreviewable(): Boolean { return type == ContentAttachmentData.Type.IMAGE || type == ContentAttachmentData.Type.VIDEO } -fun List.filterPreviewables(): List { - return filter { it.isPreviewable() } -} +data class GroupedContentAttachmentData( + val previewables: List, + val notPreviewables: List +) -fun List.filterNonPreviewables(): List { - return filter { it.isPreviewable().not() } +fun List.toGroupedContentAttachmentData(): GroupedContentAttachmentData { + return groupBy { it.isPreviewable() } + .let { + GroupedContentAttachmentData( + it[true].orEmpty(), + it[false].orEmpty() + ) + } } - diff --git a/vector/src/main/java/im/vector/riotx/features/home/room/detail/RoomDetailFragment.kt b/vector/src/main/java/im/vector/riotx/features/home/room/detail/RoomDetailFragment.kt index ffd0417d05..7d45db2aae 100644 --- a/vector/src/main/java/im/vector/riotx/features/home/room/detail/RoomDetailFragment.kt +++ b/vector/src/main/java/im/vector/riotx/features/home/room/detail/RoomDetailFragment.kt @@ -116,10 +116,9 @@ import im.vector.riotx.core.utils.toast import im.vector.riotx.features.attachments.AttachmentTypeSelectorView import im.vector.riotx.features.attachments.AttachmentsHelper import im.vector.riotx.features.attachments.ContactAttachment -import im.vector.riotx.features.attachments.filterNonPreviewables -import im.vector.riotx.features.attachments.filterPreviewables import im.vector.riotx.features.attachments.preview.AttachmentsPreviewActivity import im.vector.riotx.features.attachments.preview.AttachmentsPreviewArgs +import im.vector.riotx.features.attachments.toGroupedContentAttachmentData import im.vector.riotx.features.command.Command import im.vector.riotx.features.crypto.util.toImageRes import im.vector.riotx.features.crypto.verification.VerificationBottomSheet @@ -1351,14 +1350,13 @@ class RoomDetailFragment @Inject constructor( // AttachmentsHelper.Callback override fun onContentAttachmentsReady(attachments: List) { - val previewable = attachments.filterPreviewables() - val nonPreviewable = attachments.filterNonPreviewables() - if (nonPreviewable.isNotEmpty()) { + val grouped = attachments.toGroupedContentAttachmentData() + if (grouped.notPreviewables.isNotEmpty()) { // Send the non previewable attachment right now (?) - roomDetailViewModel.handle(RoomDetailAction.SendMedia(nonPreviewable, false)) + roomDetailViewModel.handle(RoomDetailAction.SendMedia(grouped.notPreviewables, false)) } - if (previewable.isNotEmpty()) { - val intent = AttachmentsPreviewActivity.newIntent(requireContext(), AttachmentsPreviewArgs(previewable)) + if (grouped.previewables.isNotEmpty()) { + val intent = AttachmentsPreviewActivity.newIntent(requireContext(), AttachmentsPreviewArgs(grouped.previewables)) startActivityForResult(intent, AttachmentsPreviewActivity.REQUEST_CODE) } } diff --git a/vector/src/main/java/im/vector/riotx/features/share/IncomingShareViewModel.kt b/vector/src/main/java/im/vector/riotx/features/share/IncomingShareViewModel.kt index eb5b1bda78..e4cd47f624 100644 --- a/vector/src/main/java/im/vector/riotx/features/share/IncomingShareViewModel.kt +++ b/vector/src/main/java/im/vector/riotx/features/share/IncomingShareViewModel.kt @@ -30,8 +30,7 @@ import im.vector.matrix.android.api.session.room.roomSummaryQueryParams import im.vector.matrix.rx.rx import im.vector.riotx.core.extensions.exhaustive import im.vector.riotx.core.platform.VectorViewModel -import im.vector.riotx.features.attachments.filterNonPreviewables -import im.vector.riotx.features.attachments.filterPreviewables +import im.vector.riotx.features.attachments.toGroupedContentAttachmentData import im.vector.riotx.features.home.room.list.ChronologicalRoomComparator import java.util.concurrent.TimeUnit @@ -140,18 +139,17 @@ class IncomingShareViewModel @AssistedInject constructor(@Assisted initialState: ?.let { roomId -> session.getRoom(roomId) } ?.sendMedias(attachmentData, compressMediaBeforeSending, selectedRoomIds) } else { - val previewable = attachmentData.filterPreviewables() - val nonPreviewable = attachmentData.filterNonPreviewables() - if (nonPreviewable.isNotEmpty()) { - // Send the non previewable attachment right now (?) + val grouped = attachmentData.toGroupedContentAttachmentData() + if (grouped.notPreviewables.isNotEmpty()) { + // Send the not previewable attachment right now (?) // Pick the first room to send the media selectedRoomIds.firstOrNull() ?.let { roomId -> session.getRoom(roomId) } - ?.sendMedias(nonPreviewable, compressMediaBeforeSending, selectedRoomIds) + ?.sendMedias(grouped.notPreviewables, compressMediaBeforeSending, selectedRoomIds) } - if (previewable.isNotEmpty()) { + if (grouped.previewables.isNotEmpty()) { // In case of multiple share of media, edit them first - _viewEvents.post(IncomingShareViewEvents.EditMediaBeforeSending(previewable)) + _viewEvents.post(IncomingShareViewEvents.EditMediaBeforeSending(grouped.previewables)) } } }