From 82003c9071215126ee1813f21657c81b1dc1d333 Mon Sep 17 00:00:00 2001 From: Mugurell Date: Fri, 27 May 2022 16:09:25 +0300 Subject: [PATCH] [fenix] For https://github.com/mozilla-mobile/fenix/issues/25401 - Improve how sponsored stories impressions are reported While testing I observed that the Rect returned from `boundsInWindow()` - will have the full height and width of the composable even if scrolled offscreen vertically - will only have the height and width of what part of the composable is shown pn the screen. Possibly because of the interactions of a vertically scrollable RecyclerView and the LazyRow the composable is shown in. To account for this the method calculating how much of the composable is shown on screen will also now receive the real composable size as an argument. --- .../fenix/home/pocket/PocketStoriesComposables.kt | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/org/mozilla/fenix/home/pocket/PocketStoriesComposables.kt b/app/src/main/java/org/mozilla/fenix/home/pocket/PocketStoriesComposables.kt index 3d8c0786b..099262613 100644 --- a/app/src/main/java/org/mozilla/fenix/home/pocket/PocketStoriesComposables.kt +++ b/app/src/main/java/org/mozilla/fenix/home/pocket/PocketStoriesComposables.kt @@ -49,6 +49,7 @@ import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.PreviewParameter import androidx.compose.ui.tooling.preview.PreviewParameterProvider import androidx.compose.ui.unit.Dp +import androidx.compose.ui.unit.IntSize import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import kotlinx.coroutines.delay @@ -333,15 +334,21 @@ private fun LayoutCoordinates.isVisible( ): Boolean { if (!isAttached) return false - return boundsInWindow().toAndroidRect().getIntersectPercentage(visibleRect) >= threshold + return boundsInWindow().toAndroidRect().getIntersectPercentage(size, visibleRect) >= threshold } /** * Returns the ratio of how much this intersects with [other]. + * + * @param realSize [IntSize] containing the height and with of the composable. + * (The Rect may have a smaller height / width accounting for just what is visible) + * @param other Other [Rect] for whcih to check the intersection area. + * + * @return A `0..1` float range for how much this [Rect] intersects with other. */ @FloatRange(from = 0.0, to = 1.0) -private fun Rect.getIntersectPercentage(other: Rect): Float { - val composableArea = height() * width() +private fun Rect.getIntersectPercentage(realSize: IntSize, other: Rect): Float { + val composableArea = realSize.height * realSize.width val heightOverlap = max(0, min(bottom, other.bottom) - max(top, other.top)) val widthOverlap = max(0, min(right, other.right) - max(left, other.left)) val intersectionArea = heightOverlap * widthOverlap