diff --git a/app/build.gradle b/app/build.gradle index 8bfee140..bc195796 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -49,6 +49,8 @@ dependencies { implementation 'androidx.appcompat:appcompat:1.1.0-alpha01' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' + implementation 'androidx.core:core-ktx:1.0.1' + // Paging implementation 'androidx.paging:paging-runtime:2.0.0' diff --git a/app/src/main/java/im/vector/riotredesign/features/home/HomeModule.kt b/app/src/main/java/im/vector/riotredesign/features/home/HomeModule.kt index 85aaa5e8..f00e1d65 100644 --- a/app/src/main/java/im/vector/riotredesign/features/home/HomeModule.kt +++ b/app/src/main/java/im/vector/riotredesign/features/home/HomeModule.kt @@ -10,6 +10,7 @@ import im.vector.riotredesign.features.home.room.detail.timeline.RoomTopicItemFa import im.vector.riotredesign.features.home.room.detail.timeline.TimelineDateFormatter import im.vector.riotredesign.features.home.room.detail.timeline.TimelineEventController import im.vector.riotredesign.features.home.room.detail.timeline.TimelineItemFactory +import im.vector.riotredesign.features.home.room.detail.timeline.helper.TimelineMediaSizeProvider import org.koin.dsl.module.module class HomeModule { @@ -21,7 +22,7 @@ class HomeModule { } single { - MessageItemFactory(get()) + MessageItemFactory(get(), get()) } single { @@ -49,7 +50,11 @@ class HomeModule { } factory { (roomId: String) -> - TimelineEventController(roomId, get(), get()) + TimelineEventController(roomId, get(), get(), get()) + } + + single { + TimelineMediaSizeProvider() } single { diff --git a/app/src/main/java/im/vector/riotredesign/features/home/room/detail/timeline/MessageItemFactory.kt b/app/src/main/java/im/vector/riotredesign/features/home/room/detail/timeline/MessageItemFactory.kt index 42646478..afe43952 100644 --- a/app/src/main/java/im/vector/riotredesign/features/home/room/detail/timeline/MessageItemFactory.kt +++ b/app/src/main/java/im/vector/riotredesign/features/home/room/detail/timeline/MessageItemFactory.kt @@ -11,9 +11,11 @@ import im.vector.matrix.android.api.session.room.model.message.MessageContent import im.vector.matrix.android.api.session.room.model.message.MessageImageContent import im.vector.matrix.android.api.session.room.model.message.MessageTextContent import im.vector.riotredesign.core.extensions.localDateTime +import im.vector.riotredesign.features.home.room.detail.timeline.helper.TimelineMediaSizeProvider import im.vector.riotredesign.features.media.MediaContentRenderer -class MessageItemFactory(private val timelineDateFormatter: TimelineDateFormatter) { +class MessageItemFactory(private val timelineMediaSizeProvider: TimelineMediaSizeProvider, + private val timelineDateFormatter: TimelineDateFormatter) { private val messagesDisplayedWithInformation = HashSet() @@ -29,12 +31,12 @@ class MessageItemFactory(private val timelineDateFormatter: TimelineDateFormatte val nextDate = nextEvent?.root?.localDateTime() val addDaySeparator = date.toLocalDate() != nextDate?.toLocalDate() val isNextMessageReceivedMoreThanOneHourAgo = nextDate?.isBefore(date.minusMinutes(60)) - ?: false + ?: false if (addDaySeparator - || nextRoomMember != roomMember - || nextEvent?.root?.type != EventType.MESSAGE - || isNextMessageReceivedMoreThanOneHourAgo) { + || nextRoomMember != roomMember + || nextEvent?.root?.type != EventType.MESSAGE + || isNextMessageReceivedMoreThanOneHourAgo) { messagesDisplayedWithInformation.add(event.root.eventId) } @@ -54,13 +56,14 @@ class MessageItemFactory(private val timelineDateFormatter: TimelineDateFormatte private fun buildImageMessageItem(messageContent: MessageImageContent, informationData: MessageInformationData): MessageImageItem? { - // TODO : manage maxHeight/maxWidth + + val (maxWidth, maxHeight) = timelineMediaSizeProvider.getMaxSize() val data = MediaContentRenderer.Data( url = messageContent.url, height = messageContent.info.height, - maxHeight = 800, + maxHeight = maxHeight, width = messageContent.info.width, - maxWidth = 800, + maxWidth = maxWidth, rotation = messageContent.info.rotation, orientation = messageContent.info.orientation ) diff --git a/app/src/main/java/im/vector/riotredesign/features/home/room/detail/timeline/TimelineEventController.kt b/app/src/main/java/im/vector/riotredesign/features/home/room/detail/timeline/TimelineEventController.kt index caede520..1894731a 100644 --- a/app/src/main/java/im/vector/riotredesign/features/home/room/detail/timeline/TimelineEventController.kt +++ b/app/src/main/java/im/vector/riotredesign/features/home/room/detail/timeline/TimelineEventController.kt @@ -1,5 +1,6 @@ package im.vector.riotredesign.features.home.room.detail.timeline +import androidx.recyclerview.widget.RecyclerView import com.airbnb.epoxy.EpoxyAsyncUtil import com.airbnb.epoxy.EpoxyModel import im.vector.matrix.android.api.session.events.model.EventType @@ -7,11 +8,13 @@ import im.vector.matrix.android.api.session.events.model.TimelineEvent import im.vector.matrix.android.api.session.room.timeline.TimelineData import im.vector.riotredesign.core.extensions.localDateTime import im.vector.riotredesign.features.home.LoadingItemModel_ +import im.vector.riotredesign.features.home.room.detail.timeline.helper.TimelineMediaSizeProvider import im.vector.riotredesign.features.home.room.detail.timeline.paging.PagedListEpoxyController class TimelineEventController(private val roomId: String, private val dateFormatter: TimelineDateFormatter, - private val timelineItemFactory: TimelineItemFactory + private val timelineItemFactory: TimelineItemFactory, + private val timelineMediaSizeProvider: TimelineMediaSizeProvider ) : PagedListEpoxyController( EpoxyAsyncUtil.getAsyncBackgroundHandler(), EpoxyAsyncUtil.getAsyncBackgroundHandler() @@ -36,6 +39,10 @@ class TimelineEventController(private val roomId: String, } } + override fun onAttachedToRecyclerView(recyclerView: RecyclerView) { + super.onAttachedToRecyclerView(recyclerView) + timelineMediaSizeProvider.recyclerView = recyclerView + } override fun buildItemModels(currentPosition: Int, items: List): List> { if (items.isNullOrEmpty()) { diff --git a/app/src/main/java/im/vector/riotredesign/features/home/room/detail/timeline/helper/TimelineMediaSizeProvider.kt b/app/src/main/java/im/vector/riotredesign/features/home/room/detail/timeline/helper/TimelineMediaSizeProvider.kt new file mode 100644 index 00000000..a4251b6f --- /dev/null +++ b/app/src/main/java/im/vector/riotredesign/features/home/room/detail/timeline/helper/TimelineMediaSizeProvider.kt @@ -0,0 +1,31 @@ +package im.vector.riotredesign.features.home.room.detail.timeline.helper + +import androidx.recyclerview.widget.RecyclerView + +class TimelineMediaSizeProvider { + + lateinit var recyclerView: RecyclerView + private var cachedSize: Pair? = null + + fun getMaxSize(): Pair { + return cachedSize ?: computeMaxSize().also { cachedSize = it } + } + + private fun computeMaxSize(): Pair { + val width = recyclerView.width + val height = recyclerView.height + val maxImageWidth: Int + val maxImageHeight: Int + // landscape / portrait + if (width < height) { + maxImageWidth = Math.round(width * 0.7f) + maxImageHeight = Math.round(height * 0.5f) + } else { + maxImageWidth = Math.round(width * 0.5f) + maxImageHeight = Math.round(height * 0.7f) + } + return Pair(maxImageWidth, maxImageHeight) + } + + +} \ No newline at end of file