From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: alina sireneva <alina@tei.pet>
Date: Thu, 23 Jul 2026 08:52:01 +0400
Subject: hopefully fix storage usage nested scroll

---
 .../main/java/org/telegram/ui/CachedMediaLayout.java  |  2 +-
 .../ui/Components/NestedSizeNotifierLayout.java       | 11 +++++++----
 2 files changed, 8 insertions(+), 5 deletions(-)

diff --git a/TMessagesProj/src/main/java/org/telegram/ui/CachedMediaLayout.java b/TMessagesProj/src/main/java/org/telegram/ui/CachedMediaLayout.java
index 0000000..0000000 100644
--- a/TMessagesProj/src/main/java/org/telegram/ui/CachedMediaLayout.java
+++ b/TMessagesProj/src/main/java/org/telegram/ui/CachedMediaLayout.java
@@ -432,7 +432,7 @@ public class CachedMediaLayout extends FrameLayout implements NestedSizeNotifier
 
     @Override
     public boolean isAttached() {
-        return true;
+        return isAttachedToWindow();
     }
 
     public void updateVisibleRows() {
diff --git a/TMessagesProj/src/main/java/org/telegram/ui/Components/NestedSizeNotifierLayout.java b/TMessagesProj/src/main/java/org/telegram/ui/Components/NestedSizeNotifierLayout.java
index 0000000..0000000 100644
--- a/TMessagesProj/src/main/java/org/telegram/ui/Components/NestedSizeNotifierLayout.java
+++ b/TMessagesProj/src/main/java/org/telegram/ui/Components/NestedSizeNotifierLayout.java
@@ -53,7 +53,7 @@ public class NestedSizeNotifierLayout extends SizeNotifierFrameLayout implements
         if (target == targetListView && childAttached()) {
             RecyclerListView innerListView = childLayout.getListView();
             int top = childLayout.getTop();
-            if (top == maxTop) {
+            if (top <= maxTop) {
                 consumed[1] = dyUnconsumed;
                 innerListView.scrollBy(0, dyUnconsumed);
             }
@@ -133,9 +133,12 @@ public class NestedSizeNotifierLayout extends SizeNotifierFrameLayout implements
     public void setChildLayout(ChildLayout childLayout, int maxTopPadding) {
         this.maxTopPadding = maxTopPadding;
         if (this.childLayout != childLayout) {
+            if (attached && this.childLayout != null) {
+                this.childLayout.removeOnLayoutChangeListener(this);
+            }
             this.childLayout = childLayout;
-            if (attached && childLayout != null && childLayout.getListView() != null) {
-                childLayout.getListView().addOnLayoutChangeListener(this);
+            if (attached && childLayout != null) {
+                childLayout.addOnLayoutChangeListener(this);
             }
         }
         updateMaxTop();
@@ -160,7 +163,7 @@ public class NestedSizeNotifierLayout extends SizeNotifierFrameLayout implements
     }
 
     public boolean isPinnedToTop() {
-        return childLayout != null && childLayout.getTop() == maxTop;
+        return childLayout != null && childLayout.getTop() <= maxTop;
     }
 
     @Override
