From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: alina sireneva <alina@tei.su>
Date: Tue, 5 May 2026 15:32:29 +0200
Subject: fix profile scroll position jumping when opening uncached user

---
 .../ui/Components/ProfileGalleryBlurView.java |  1 +
 .../java/org/telegram/ui/ProfileActivity.java | 70 ++++++++++++++++---
 2 files changed, 61 insertions(+), 10 deletions(-)

diff --git a/TMessagesProj/src/main/java/org/telegram/ui/Components/ProfileGalleryBlurView.java b/TMessagesProj/src/main/java/org/telegram/ui/Components/ProfileGalleryBlurView.java
index 0000000..0000000 100644
--- a/TMessagesProj/src/main/java/org/telegram/ui/Components/ProfileGalleryBlurView.java
+++ b/TMessagesProj/src/main/java/org/telegram/ui/Components/ProfileGalleryBlurView.java
@@ -191,6 +191,7 @@ public class ProfileGalleryBlurView extends View {
     public void setSize(int size) {
         if (this.actionSize != size) {
             invalidate();
+            requestLayout();
         }
         this.actionSize = size;
         this.size = (int) (dp(64) * 1.5f);
diff --git a/TMessagesProj/src/main/java/org/telegram/ui/ProfileActivity.java b/TMessagesProj/src/main/java/org/telegram/ui/ProfileActivity.java
index 0000000..0000000 100644
--- a/TMessagesProj/src/main/java/org/telegram/ui/ProfileActivity.java
+++ b/TMessagesProj/src/main/java/org/telegram/ui/ProfileActivity.java
@@ -1273,6 +1273,10 @@ public class ProfileActivity extends BaseFragment implements NotificationCenter.
                 color1Animated.set(color1, true);
                 color2Animated.set(color2, true);
             }
+            backgroundGradientY = (actionBar.getOccupyStatusBar() ? AndroidUtilities.statusBarHeight : 0)
+                    + ActionBar.getCurrentActionBarHeight()
+                    - 21 * AndroidUtilities.density
+                    + actionBar.getTranslationY();
             invalidate();
         }
 
@@ -3224,12 +3228,21 @@ public class ProfileActivity extends BaseFragment implements NotificationCenter.
                         layout = true;
                     } else if (invalidateScroll || currentPaddingTop != paddingTop) {
                         if (savedScrollPosition >= 0) {
-                            layoutManager.scrollToPositionWithOffset(savedScrollPosition, savedScrollOffset - paddingTop);
+                            int headerExtraDelta = (savedScrollPosition == 0 && !allowPullingDown) ? (getHeaderExtraHeight() - savedScrollHeaderExtra) : 0;
+                            int savedOffset = savedScrollOffset + headerExtraDelta - paddingTop;
+                            if (savedScrollPosition == 0 && allowPullingDown && currentExpandAnimatorValue >= 1f) {
+                                savedOffset = -actionBarHeight;
+                            }
+                            layoutManager.scrollToPositionWithOffset(savedScrollPosition, savedOffset);
                         } else if ((!changed || !allowPullingDown) && view != null) {
-                            if (pos == 0 && !allowPullingDown && top > getHeaderExtraHeight()) {
+                            if (pos == 0 && !allowPullingDown && top > 0 && top != getHeaderExtraHeight()) {
                                 top = getHeaderExtraHeight();
                             }
-                            layoutManager.scrollToPositionWithOffset(pos, top - paddingTop);
+                            int scrollOffset = top - paddingTop;
+                            if (pos == 0 && allowPullingDown && currentExpandAnimatorValue >= 1f) {
+                                scrollOffset = -actionBarHeight;
+                            }
+                            layoutManager.scrollToPositionWithOffset(pos, scrollOffset);
                             layout = true;
                         } else {
                             layoutManager.scrollToPositionWithOffset(0, getHeaderExtraHeight() - paddingTop);
@@ -4277,7 +4290,7 @@ public class ProfileActivity extends BaseFragment implements NotificationCenter.
             public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) {
                 final View view = layoutManager.findViewByPosition(0);
                 if (view != null && !openingAvatar) {
-                    final int canScroll = view.getTop() - getHeaderExtraHeight();
+                    final int canScroll = layoutManager.getDecoratedTop(view) - getHeaderExtraHeight();
                     if (!allowPullingDown && canScroll > dy) {
                         dy = canScroll;
                         if (avatarsViewPager.hasImages() && avatarImage.getImageReceiver().hasNotThumb() && !AndroidUtilities.isAccessibilityScreenReaderEnabled() && (!isInLandscapeMode && !AndroidUtilities.isTablet() || hasMainTabs)) {
@@ -8011,7 +8024,7 @@ public class ProfileActivity extends BaseFragment implements NotificationCenter.
             }
         }
         RecyclerListView.Holder holder = child == null ? null : (RecyclerListView.Holder) listView.findContainingViewHolder(child);
-        int top = child == null ? 0 : child.getTop();
+        int top = child == null ? 0 : layoutManager.getDecoratedTop(child);
         int adapterPosition = holder != null ? holder.getAdapterPosition() : RecyclerView.NO_POSITION;
         if (top >= 0 && adapterPosition == 0) {
             newOffset = top;
@@ -11049,6 +11062,22 @@ public class ProfileActivity extends BaseFragment implements NotificationCenter.
             }
             musicView.setVisibility(hasMusic ? View.VISIBLE : View.GONE);
         }
+        if (avatarsBlurView != null) {
+            avatarsBlurView.setSize(getActionsExtraHeight());
+        }
+        if (avatarsViewPager != null) {
+            avatarsViewPager.setPadding(0, 0, 0, getActionsExtraHeight());
+        }
+        if (isPulledDown && listView != null) {
+            final int abh = ActionBar.getCurrentActionBarHeight() + (actionBar.getOccupyStatusBar() ? AndroidUtilities.statusBarHeight : 0);
+            final int target = listView.getMeasuredWidth() + getActionsExtraHeight() - abh;
+            if (target > 0 && extraHeight != target) {
+                extraHeight = target;
+                initialAnimationExtraHeight = target;
+                if (topView != null) topView.invalidate();
+                needLayout(false);
+            }
+        }
     }
 
     private Drawable getScamDrawable(int type) {
@@ -15456,37 +15485,58 @@ public class ProfileActivity extends BaseFragment implements NotificationCenter.
             listAdapter.notifyDataSetChanged();
         }
         if (savedScrollPosition >= 0) {
-            layoutManager.scrollToPositionWithOffset(savedScrollPosition, savedScrollOffset - listView.getPaddingTop());
+            int headerExtraDelta = (savedScrollPosition == 0 && !allowPullingDown) ? (getHeaderExtraHeight() - savedScrollHeaderExtra) : 0;
+            if (headerExtraDelta != 0 && transitionAnimationInProress && extraHeight < getHeaderExtraHeight()) {
+                extraHeight = getHeaderExtraHeight();
+                if (listView.getPaddingTop() < getHeaderExtraHeight()) {
+                    listView.setPadding(0, getHeaderExtraHeight(), 0, listView.getPaddingBottom());
+                }
+                needLayout(true);
+                topView.invalidate();
+            }
+            layoutManager.scrollToPositionWithOffset(savedScrollPosition, savedScrollOffset + headerExtraDelta - listView.getPaddingTop());
         }
         AndroidUtilities.updateVisibleRows(listView);
     }
 
     int savedScrollPosition = -1;
     int savedScrollOffset;
+    int savedScrollHeaderExtra;
     boolean savedScrollToSharedMedia;
 
     private void saveScrollPosition() {
         if (listView != null && layoutManager != null && listView.getChildCount() > 0 && !savedScrollToSharedMedia) {
+            if (savedScrollPosition >= 0) {
+                return;
+            }
             View view = null;
             int position = -1;
             int top = Integer.MAX_VALUE;
+            int topmostTop = Integer.MAX_VALUE;
+            int topmostPos = RecyclerListView.NO_POSITION;
             for (int i = 0; i < listView.getChildCount(); i++) {
                 int childPosition = listView.getChildAdapterPosition(listView.getChildAt(i));
                 View child = listView.getChildAt(i);
+                if (child.getTop() < topmostTop) {
+                    topmostTop = child.getTop();
+                    topmostPos = childPosition;
+                }
                 if (childPosition != RecyclerListView.NO_POSITION && child.getTop() < top) {
                     view = child;
                     position = childPosition;
                     top = child.getTop();
                 }
             }
+            if (topmostPos == RecyclerListView.NO_POSITION) return;
             if (view != null) {
                 savedScrollPosition = position;
-                savedScrollOffset = view.getTop();
-                if (savedScrollPosition == 0 && !allowPullingDown && savedScrollOffset > getHeaderExtraHeight()) {
-                    savedScrollOffset = getHeaderExtraHeight();
+                savedScrollOffset = layoutManager.getDecoratedTop(view);
+                savedScrollHeaderExtra = getHeaderExtraHeight();
+                if (savedScrollPosition == 0 && !allowPullingDown && savedScrollOffset > listView.getPaddingTop()) {
+                    savedScrollOffset = listView.getPaddingTop();
                 }
 
-                layoutManager.scrollToPositionWithOffset(position, view.getTop() - listView.getPaddingTop());
+                layoutManager.scrollToPositionWithOffset(position, savedScrollOffset - listView.getPaddingTop());
             }
         }
     }
