From 0b50ee097449961bd39cbf3aee0cb110c8b60021 Mon Sep 17 00:00:00 2001 From: ganfra Date: Fri, 2 Nov 2018 10:17:37 +0100 Subject: [PATCH] Timeline : scroll to show new message --- .../platform/DefaultListUpdateCallback.kt | 22 +++++++++++++++++++ .../home/room/detail/RoomDetailFragment.kt | 5 ++++- .../room/detail/ScrollOnNewMessageCallback.kt | 14 ++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/im/vector/riotredesign/core/platform/DefaultListUpdateCallback.kt create mode 100644 app/src/main/java/im/vector/riotredesign/features/home/room/detail/ScrollOnNewMessageCallback.kt diff --git a/app/src/main/java/im/vector/riotredesign/core/platform/DefaultListUpdateCallback.kt b/app/src/main/java/im/vector/riotredesign/core/platform/DefaultListUpdateCallback.kt new file mode 100644 index 0000000000..53ec2c0472 --- /dev/null +++ b/app/src/main/java/im/vector/riotredesign/core/platform/DefaultListUpdateCallback.kt @@ -0,0 +1,22 @@ +package im.vector.riotredesign.core.platform + +import android.support.v7.util.ListUpdateCallback + +interface DefaultListUpdateCallback : ListUpdateCallback { + + override fun onChanged(position: Int, count: Int, tag: Any?) { + //no-op + } + + override fun onMoved(position: Int, count: Int) { + //no-op + } + + override fun onInserted(position: Int, count: Int) { + //no-op + } + + override fun onRemoved(position: Int, count: Int) { + //no-op + } +} \ No newline at end of file diff --git a/app/src/main/java/im/vector/riotredesign/features/home/room/detail/RoomDetailFragment.kt b/app/src/main/java/im/vector/riotredesign/features/home/room/detail/RoomDetailFragment.kt index 2e942dacf1..ae77d50f01 100644 --- a/app/src/main/java/im/vector/riotredesign/features/home/room/detail/RoomDetailFragment.kt +++ b/app/src/main/java/im/vector/riotredesign/features/home/room/detail/RoomDetailFragment.kt @@ -33,6 +33,7 @@ class RoomDetailFragment : RiotFragment() { private val matrix by inject() private val currentSession = matrix.currentSession private var roomId by FragmentArgumentDelegate() + private lateinit var timelineEventController: TimelineEventController private lateinit var room: Room @@ -59,8 +60,10 @@ class RoomDetailFragment : RiotFragment() { private fun setupRecyclerView() { val layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, true) + val listUpdateCallback = ScrollOnNewMessageCallback(layoutManager) recyclerView.layoutManager = layoutManager timelineEventController = TimelineEventController(riotActivity) + timelineEventController.addModelBuildListener { it.dispatchTo(listUpdateCallback) } recyclerView.setController(timelineEventController) } @@ -82,4 +85,4 @@ class RoomDetailFragment : RiotFragment() { timelineEventController.requestModelBuild() } -} \ No newline at end of file +} diff --git a/app/src/main/java/im/vector/riotredesign/features/home/room/detail/ScrollOnNewMessageCallback.kt b/app/src/main/java/im/vector/riotredesign/features/home/room/detail/ScrollOnNewMessageCallback.kt new file mode 100644 index 0000000000..3328d8b408 --- /dev/null +++ b/app/src/main/java/im/vector/riotredesign/features/home/room/detail/ScrollOnNewMessageCallback.kt @@ -0,0 +1,14 @@ +package im.vector.riotredesign.features.home.room.detail + +import android.support.v7.widget.LinearLayoutManager +import im.vector.riotredesign.core.platform.DefaultListUpdateCallback + +class ScrollOnNewMessageCallback(private val layoutManager: LinearLayoutManager) : DefaultListUpdateCallback { + + override fun onInserted(position: Int, count: Int) { + if (layoutManager.findFirstVisibleItemPosition() == 0) { + layoutManager.scrollToPosition(0) + } + } + +} \ No newline at end of file