[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.
pull/600/head
Mugurell 2 years ago committed by mergify[bot]
parent 29747b2095
commit 82003c9071

@ -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

Loading…
Cancel
Save