From 401424ff9600afbd67ef1a2d8dedac36b22ffb41 Mon Sep 17 00:00:00 2001
From: SpiritCroc <dev@spiritcroc.de>
Date: Mon, 2 Aug 2021 10:01:53 +0200
Subject: [PATCH] Scroll to top of messages again

We want to specify where to scroll to from the top of the message rather
than the bottom. To do this in a recycler view that's filled from the
bottom, let's just target the event above and move that out by 2/3 of
the screen.

Change-Id: Ica8ec2d0aaa22b654232f128992510c9d550fe35
---
 .../app/features/home/room/detail/RoomDetailFragment.kt     | 5 ++++-
 .../features/home/room/detail/ScrollOnNewMessageCallback.kt | 6 ++++--
 2 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailFragment.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailFragment.kt
index e74e3e3c7d..bad5a55861 100644
--- a/vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailFragment.kt
+++ b/vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailFragment.kt
@@ -835,7 +835,10 @@ class RoomDetailFragment @Inject constructor(
             scrollOnHighlightedEventCallback.scheduleScrollTo(action.eventId)
         } else {
             views.timelineRecyclerView.stopScroll()
-            layoutManager.scrollToPosition(scrollPosition)
+            // Scroll such that the scrolled-to event is moved down 1/3 of the screen.
+            // To do that, we actually scroll the view above out by 2/3 (since we can only control the distance
+            // from the bottom of the view, not the top).
+            layoutManager.scrollToPositionWithOffset(scrollPosition + 1, views.timelineRecyclerView.measuredHeight * 2 / 3)
         }
     }
 
diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/ScrollOnNewMessageCallback.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/ScrollOnNewMessageCallback.kt
index 7df3ed347e..eb2fa203b0 100644
--- a/vector/src/main/java/im/vector/app/features/home/room/detail/ScrollOnNewMessageCallback.kt
+++ b/vector/src/main/java/im/vector/app/features/home/room/detail/ScrollOnNewMessageCallback.kt
@@ -64,8 +64,10 @@ class ScrollOnNewMessageCallback(private val layoutManager: LinearLayoutManager,
                 layoutManager.scrollToPositionWithOffset(0, 0)
             } else {
                 timelineEventController.searchPositionOfEvent(scrollToEvent)?.let {
-                    // Scroll such that the scrolled-to event is moved up 1/4 of the screen
-                    layoutManager.scrollToPositionWithOffset(it, parentView.measuredHeight / 4)
+                    // Scroll such that the scrolled-to event is moved down 1/3 of the screen.
+                    // To do that, we actually scroll the view above out by 2/3 (since we can only control the distance
+                    // from the bottom of the view, not the top).
+                    layoutManager.scrollToPositionWithOffset(it + 1, parentView.measuredHeight * 2 / 3)
                 }
             }
             return