Skip to content

Commit 0954eaa

Browse files
committed
chore(fc/chat/list): apply nav bar padding to find room button conditionally
Signed-off-by: Brandon McAnsh <[email protected]>
1 parent d1905f0 commit 0954eaa

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

flipchatApp/src/main/kotlin/xyz/flipchat/app/features/chat/list/ChatListScreen.kt

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import androidx.compose.foundation.layout.Column
77
import androidx.compose.foundation.layout.PaddingValues
88
import androidx.compose.foundation.layout.fillMaxSize
99
import androidx.compose.foundation.layout.fillMaxWidth
10+
import androidx.compose.foundation.layout.navigationBarsPadding
1011
import androidx.compose.foundation.layout.padding
1112
import androidx.compose.foundation.lazy.LazyColumn
1213
import androidx.compose.foundation.lazy.rememberLazyListState
@@ -15,11 +16,13 @@ import androidx.compose.material.Text
1516
import androidx.compose.runtime.Composable
1617
import androidx.compose.runtime.LaunchedEffect
1718
import androidx.compose.runtime.SideEffect
19+
import androidx.compose.runtime.collectAsState
1820
import androidx.compose.runtime.getValue
1921
import androidx.compose.runtime.mutableStateOf
2022
import androidx.compose.runtime.rememberCoroutineScope
2123
import androidx.compose.runtime.saveable.rememberSaveable
2224
import androidx.compose.runtime.setValue
25+
import androidx.compose.runtime.snapshots.Snapshot
2326
import androidx.compose.ui.Alignment.Companion.CenterHorizontally
2427
import androidx.compose.ui.Alignment.Companion.CenterVertically
2528
import androidx.compose.ui.Modifier
@@ -45,6 +48,7 @@ import com.getcode.ui.theme.ButtonState
4548
import com.getcode.ui.theme.CodeButton
4649
import com.getcode.ui.theme.CodeCircularProgressIndicator
4750
import com.getcode.ui.theme.CodeScaffold
51+
import com.getcode.ui.utils.addIf
4852
import io.grpc.Status.Code
4953
import kotlinx.coroutines.flow.filter
5054
import 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

flipchatApp/src/main/kotlin/xyz/flipchat/app/features/chat/list/ChatListViewModel.kt

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import com.getcode.view.BaseViewModel2
1313
import dagger.hilt.android.lifecycle.HiltViewModel
1414
import kotlinx.coroutines.delay
1515
import kotlinx.coroutines.flow.Flow
16-
import kotlinx.coroutines.flow.SharingStarted
1716
import kotlinx.coroutines.flow.distinctUntilChanged
1817
import kotlinx.coroutines.flow.filter
1918
import kotlinx.coroutines.flow.filterIsInstance
@@ -24,7 +23,6 @@ import kotlinx.coroutines.flow.launchIn
2423
import kotlinx.coroutines.flow.map
2524
import kotlinx.coroutines.flow.mapNotNull
2625
import kotlinx.coroutines.flow.onEach
27-
import kotlinx.coroutines.flow.stateIn
2826
import kotlinx.coroutines.flow.take
2927
import xyz.flipchat.app.R
3028
import xyz.flipchat.app.features.chat.conversation.ConversationViewModel.Event
@@ -59,11 +57,13 @@ class ChatListViewModel @Inject constructor(
5957
val showFullscreenSpinner: Boolean = false,
6058
val networkConnected: Boolean = true,
6159
val chatTapCount: Int = 0,
60+
val isLoggedIn: Boolean = false,
6261
val isLogOutEnabled: Boolean = false,
6362
)
6463

6564
sealed interface Event {
6665
data class OnSelfIdChanged(val id: ID?) : Event
66+
data class OnLoggedInStateChanged(val loggedIn: Boolean): Event
6767
data class ShowFullScreenSpinner(
6868
val showScrim: Boolean = true,
6969
val showSpinner: Boolean = true
@@ -87,9 +87,14 @@ class ChatListViewModel @Inject constructor(
8787
userManager.state
8888
.map { it.userId }
8989
.distinctUntilChanged()
90-
.onEach {
91-
dispatchEvent(Event.OnSelfIdChanged(it))
92-
}.launchIn(viewModelScope)
90+
.onEach { dispatchEvent(Event.OnSelfIdChanged(it)) }
91+
.launchIn(viewModelScope)
92+
93+
userManager.state
94+
.map { it.authState }
95+
.distinctUntilChanged()
96+
.onEach { dispatchEvent(Event.OnLoggedInStateChanged(it is AuthState.LoggedIn)) }
97+
.launchIn(viewModelScope)
9398

9499
networkObserver.state
95100
.map { it.connected }
@@ -246,6 +251,8 @@ class ChatListViewModel @Inject constructor(
246251

247252
is Event.OnSelfIdChanged -> { state -> state.copy(selfId = event.id) }
248253

254+
is Event.OnLoggedInStateChanged -> { state -> state.copy(isLoggedIn = event.loggedIn) }
255+
249256
is Event.NeedsAccountCreated,
250257
is Event.OnAccountCreated,
251258
is Event.OnOpen,

0 commit comments

Comments
 (0)