From 2db38d71f97178db079279ed17deac6a28e25013 Mon Sep 17 00:00:00 2001 From: Robin Singh Date: Mon, 22 Jan 2024 22:53:12 +0530 Subject: [PATCH] fix: Ensure selected news source is visible among the initial visible sources in the viewport (#66) --- .../ui/components/NewsSourceBottomSheetContent.kt | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/rob729/newsfeed/ui/components/NewsSourceBottomSheetContent.kt b/app/src/main/java/com/rob729/newsfeed/ui/components/NewsSourceBottomSheetContent.kt index 8ca71e9..a292fc4 100644 --- a/app/src/main/java/com/rob729/newsfeed/ui/components/NewsSourceBottomSheetContent.kt +++ b/app/src/main/java/com/rob729/newsfeed/ui/components/NewsSourceBottomSheetContent.kt @@ -9,7 +9,9 @@ import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.navigationBarsPadding import androidx.compose.foundation.lazy.LazyRow +import androidx.compose.foundation.lazy.rememberLazyListState import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalConfiguration @@ -29,6 +31,11 @@ fun NewsSourceBottomSheetContent( val newsSourceList = Constants.newsSourceUiDataLists val screenWidthDp = LocalConfiguration.current.screenWidthDp val itemWidth = (screenWidthDp - (ITEM_SPACING * VISIBLE_CARDS)).div(VISIBLE_CARDS) + val rowState = rememberLazyListState() + + LaunchedEffect(currentSelectedNewsSource, newsSourceList) { + rowState.scrollToItem(newsSourceList.indexOfFirst { it.domain == currentSelectedNewsSource }) + } Box( modifier = Modifier.navigationBarsPadding(), @@ -38,7 +45,8 @@ fun NewsSourceBottomSheetContent( LazyRow( modifier = Modifier .testTag("news_source_list"), - horizontalArrangement = Arrangement.spacedBy(ITEM_SPACING.dp) + horizontalArrangement = Arrangement.spacedBy(ITEM_SPACING.dp), + state = rowState ) { items(Constants.newsSourceUiDataLists.size, { index: Int -> newsSourceList[index].domain }) { index ->