@@ -7,6 +7,7 @@ import androidx.compose.foundation.layout.Column
77import androidx.compose.foundation.layout.PaddingValues
88import androidx.compose.foundation.layout.fillMaxSize
99import androidx.compose.foundation.layout.fillMaxWidth
10+ import androidx.compose.foundation.layout.navigationBarsPadding
1011import androidx.compose.foundation.layout.padding
1112import androidx.compose.foundation.lazy.LazyColumn
1213import androidx.compose.foundation.lazy.rememberLazyListState
@@ -15,11 +16,13 @@ import androidx.compose.material.Text
1516import androidx.compose.runtime.Composable
1617import androidx.compose.runtime.LaunchedEffect
1718import androidx.compose.runtime.SideEffect
19+ import androidx.compose.runtime.collectAsState
1820import androidx.compose.runtime.getValue
1921import androidx.compose.runtime.mutableStateOf
2022import androidx.compose.runtime.rememberCoroutineScope
2123import androidx.compose.runtime.saveable.rememberSaveable
2224import androidx.compose.runtime.setValue
25+ import androidx.compose.runtime.snapshots.Snapshot
2326import androidx.compose.ui.Alignment.Companion.CenterHorizontally
2427import androidx.compose.ui.Alignment.Companion.CenterVertically
2528import androidx.compose.ui.Modifier
@@ -45,6 +48,7 @@ import com.getcode.ui.theme.ButtonState
4548import com.getcode.ui.theme.CodeButton
4649import com.getcode.ui.theme.CodeCircularProgressIndicator
4750import com.getcode.ui.theme.CodeScaffold
51+ import com.getcode.ui.utils.addIf
4852import io.grpc.Status.Code
4953import kotlinx.coroutines.flow.filter
5054import kotlinx.coroutines.flow.filterIsInstance
@@ -110,21 +114,14 @@ private fun ChatListScreenContent(
110114) {
111115 val navigator = LocalCodeNavigator .current
112116 val context = LocalContext .current
117+ val state by viewModel.stateFlow.collectAsState()
113118 val chats = viewModel.chats.collectAsLazyPagingItems()
114119 val isLoading = chats.loadState.refresh is LoadState .Loading
115120 var isInitialLoad by rememberSaveable { mutableStateOf(true ) }
116121 val listState = rememberLazyListState()
117122 val composeScope = rememberCoroutineScope()
118123
119124 CodeScaffold { padding ->
120- SideEffect {
121- if (! listState.canScrollBackward) {
122- composeScope.launch {
123- listState.scrollToItem(0 )
124- }
125- }
126- }
127-
128125 LazyColumn (
129126 modifier = Modifier .fillMaxSize().padding(padding),
130127 contentPadding = PaddingValues (bottom = CodeTheme .dimens.inset),
@@ -178,7 +175,8 @@ private fun ChatListScreenContent(
178175 modifier = Modifier
179176 .fillMaxWidth()
180177 .padding(top = CodeTheme .dimens.grid.x6)
181- .padding(horizontal = CodeTheme .dimens.inset),
178+ .padding(horizontal = CodeTheme .dimens.inset)
179+ .addIf(! state.isLoggedIn) { Modifier .navigationBarsPadding() },
182180 buttonState = ButtonState .Filled ,
183181 text = stringResource(R .string.action_findRoom)
184182 ) {
@@ -191,6 +189,15 @@ private fun ChatListScreenContent(
191189 }
192190 }
193191 }
192+
193+ // opts out of the list maintaining
194+ // scroll position when adding elements before the first item
195+ Snapshot .withoutReadObservation {
196+ listState.requestScrollToItem(
197+ index = listState.firstVisibleItemIndex,
198+ scrollOffset = listState.firstVisibleItemScrollOffset
199+ )
200+ }
194201 }
195202 }
196203
0 commit comments