For #26796 - Fix PocketStoriesComposablesPreview

This commit is contained in:
Noah Bond 2022-09-06 12:15:55 -07:00 committed by mergify[bot]
parent 7c1aa011a9
commit 0ed40f239a

View File

@ -67,6 +67,7 @@ import org.mozilla.fenix.compose.ListItemTabSurface
import org.mozilla.fenix.compose.SelectableChip import org.mozilla.fenix.compose.SelectableChip
import org.mozilla.fenix.compose.StaggeredHorizontalGrid import org.mozilla.fenix.compose.StaggeredHorizontalGrid
import org.mozilla.fenix.compose.TabSubtitleWithInterdot import org.mozilla.fenix.compose.TabSubtitleWithInterdot
import org.mozilla.fenix.compose.inComposePreview
import org.mozilla.fenix.ext.settings import org.mozilla.fenix.ext.settings
import org.mozilla.fenix.theme.FirefoxTheme import org.mozilla.fenix.theme.FirefoxTheme
import org.mozilla.fenix.theme.Theme import org.mozilla.fenix.theme.Theme
@ -278,40 +279,44 @@ private fun Modifier.onShown(
var lastVisibleCoordinates: LayoutCoordinates? = null var lastVisibleCoordinates: LayoutCoordinates? = null
return composed { return composed {
val context = LocalContext.current if (inComposePreview) {
var wasEventReported by remember { mutableStateOf(false) } Modifier
} else {
val context = LocalContext.current
var wasEventReported by remember { mutableStateOf(false) }
val toolbarHeight = context.resources.getDimensionPixelSize(R.dimen.browser_toolbar_height) val toolbarHeight = context.resources.getDimensionPixelSize(R.dimen.browser_toolbar_height)
val isToolbarPlacedAtBottom = context.settings().shouldUseBottomToolbar val isToolbarPlacedAtBottom = context.settings().shouldUseBottomToolbar
// Get a Rect of the entire screen minus system insets minus the toolbar // Get a Rect of the entire screen minus system insets minus the toolbar
val screenBounds = Rect() val screenBounds = Rect()
.apply { LocalView.current.getWindowVisibleDisplayFrame(this) } .apply { LocalView.current.getWindowVisibleDisplayFrame(this) }
.apply { .apply {
when (isToolbarPlacedAtBottom) { when (isToolbarPlacedAtBottom) {
true -> bottom -= toolbarHeight true -> bottom -= toolbarHeight
false -> top += toolbarHeight false -> top += toolbarHeight
}
}
// In the event this composable starts as visible but then gets pushed offscreen
// before MINIMUM_TIME_TO_SETTLE_MS we will not report is as being visible.
// In the LaunchedEffect we add support for when the composable starts as visible and then
// it's position isn't changed after MINIMUM_TIME_TO_SETTLE_MS so it must be reported as visible.
LaunchedEffect(initialTime) {
delay(MINIMUM_TIME_TO_SETTLE_MS.toLong())
if (!wasEventReported && lastVisibleCoordinates?.isVisible(screenBounds, threshold) == true) {
wasEventReported = true
onVisible()
} }
} }
// In the event this composable starts as visible but then gets pushed offscreen onGloballyPositioned { coordinates ->
// before MINIMUM_TIME_TO_SETTLE_MS we will not report is as being visible. if (!wasEventReported && coordinates.isVisible(screenBounds, threshold)) {
// In the LaunchedEffect we add support for when the composable starts as visible and then if (System.currentTimeMillis() - initialTime > MINIMUM_TIME_TO_SETTLE_MS) {
// it's position isn't changed after MINIMUM_TIME_TO_SETTLE_MS so it must be reported as visible. wasEventReported = true
LaunchedEffect(initialTime) { onVisible()
delay(MINIMUM_TIME_TO_SETTLE_MS.toLong()) } else {
if (!wasEventReported && lastVisibleCoordinates?.isVisible(screenBounds, threshold) == true) { lastVisibleCoordinates = coordinates
wasEventReported = true }
onVisible()
}
}
onGloballyPositioned { coordinates ->
if (!wasEventReported && coordinates.isVisible(screenBounds, threshold)) {
if (System.currentTimeMillis() - initialTime > MINIMUM_TIME_TO_SETTLE_MS) {
wasEventReported = true
onVisible()
} else {
lastVisibleCoordinates = coordinates
} }
} }
} }