forked from melod1n/fast-messenger
saving selected friends tab while app is working
This commit is contained in:
@@ -32,6 +32,8 @@ interface FriendsViewModel {
|
|||||||
|
|
||||||
fun onErrorConsumed()
|
fun onErrorConsumed()
|
||||||
|
|
||||||
|
fun onTabSelected(tabIndex: Int)
|
||||||
|
|
||||||
fun setScrollIndex(index: Int)
|
fun setScrollIndex(index: Int)
|
||||||
fun setScrollOffset(offset: Int)
|
fun setScrollOffset(offset: Int)
|
||||||
fun setScrollIndexOnline(index: Int)
|
fun setScrollIndexOnline(index: Int)
|
||||||
@@ -71,6 +73,10 @@ class FriendsViewModelImpl(
|
|||||||
baseError.setValue { null }
|
baseError.setValue { null }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun onTabSelected(tabIndex: Int) {
|
||||||
|
screenState.setValue { old -> old.copy(selectedTabIndex = tabIndex) }
|
||||||
|
}
|
||||||
|
|
||||||
override fun setScrollIndex(index: Int) {
|
override fun setScrollIndex(index: Int) {
|
||||||
screenState.setValue { old -> old.copy(scrollIndex = index) }
|
screenState.setValue { old -> old.copy(scrollIndex = index) }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ data class FriendsScreenState(
|
|||||||
val onlineFriends: List<UiFriend>,
|
val onlineFriends: List<UiFriend>,
|
||||||
val isPaginating: Boolean,
|
val isPaginating: Boolean,
|
||||||
val isPaginationExhausted: Boolean,
|
val isPaginationExhausted: Boolean,
|
||||||
|
val selectedTabIndex: Int,
|
||||||
val scrollIndex: Int,
|
val scrollIndex: Int,
|
||||||
val scrollOffset: Int,
|
val scrollOffset: Int,
|
||||||
val scrollIndexOnline: Int,
|
val scrollIndexOnline: Int,
|
||||||
@@ -22,6 +23,7 @@ data class FriendsScreenState(
|
|||||||
onlineFriends = emptyList(),
|
onlineFriends = emptyList(),
|
||||||
isPaginating = false,
|
isPaginating = false,
|
||||||
isPaginationExhausted = false,
|
isPaginationExhausted = false,
|
||||||
|
selectedTabIndex = 0,
|
||||||
scrollIndex = 0,
|
scrollIndex = 0,
|
||||||
scrollOffset = 0,
|
scrollOffset = 0,
|
||||||
scrollIndexOnline = 0,
|
scrollIndexOnline = 0,
|
||||||
|
|||||||
+19
-22
@@ -33,10 +33,8 @@ import androidx.compose.runtime.Composable
|
|||||||
import androidx.compose.runtime.LaunchedEffect
|
import androidx.compose.runtime.LaunchedEffect
|
||||||
import androidx.compose.runtime.derivedStateOf
|
import androidx.compose.runtime.derivedStateOf
|
||||||
import androidx.compose.runtime.getValue
|
import androidx.compose.runtime.getValue
|
||||||
import androidx.compose.runtime.mutableIntStateOf
|
|
||||||
import androidx.compose.runtime.mutableStateOf
|
import androidx.compose.runtime.mutableStateOf
|
||||||
import androidx.compose.runtime.remember
|
import androidx.compose.runtime.remember
|
||||||
import androidx.compose.runtime.saveable.rememberSaveable
|
|
||||||
import androidx.compose.runtime.setValue
|
import androidx.compose.runtime.setValue
|
||||||
import androidx.compose.runtime.snapshotFlow
|
import androidx.compose.runtime.snapshotFlow
|
||||||
import androidx.compose.ui.Alignment
|
import androidx.compose.ui.Alignment
|
||||||
@@ -101,6 +99,7 @@ fun FriendsRoute(
|
|||||||
onPaginationConditionsMet = viewModel::onPaginationConditionsMet,
|
onPaginationConditionsMet = viewModel::onPaginationConditionsMet,
|
||||||
onRefresh = viewModel::onRefresh,
|
onRefresh = viewModel::onRefresh,
|
||||||
onPhotoClicked = onPhotoClicked,
|
onPhotoClicked = onPhotoClicked,
|
||||||
|
setSelectedTabIndex = viewModel::onTabSelected,
|
||||||
setScrollIndex = viewModel::setScrollIndex,
|
setScrollIndex = viewModel::setScrollIndex,
|
||||||
setScrollOffset = viewModel::setScrollOffset,
|
setScrollOffset = viewModel::setScrollOffset,
|
||||||
setScrollIndexOnline = viewModel::setScrollIndexOnline,
|
setScrollIndexOnline = viewModel::setScrollIndexOnline,
|
||||||
@@ -121,10 +120,11 @@ fun FriendsScreen(
|
|||||||
onPaginationConditionsMet: () -> Unit = {},
|
onPaginationConditionsMet: () -> Unit = {},
|
||||||
onRefresh: () -> Unit = {},
|
onRefresh: () -> Unit = {},
|
||||||
onPhotoClicked: (url: String) -> Unit = {},
|
onPhotoClicked: (url: String) -> Unit = {},
|
||||||
setScrollIndex: (Int) -> Unit,
|
setSelectedTabIndex: (Int) -> Unit = {},
|
||||||
setScrollOffset: (Int) -> Unit,
|
setScrollIndex: (Int) -> Unit = {},
|
||||||
setScrollIndexOnline: (Int) -> Unit,
|
setScrollOffset: (Int) -> Unit = {},
|
||||||
setScrollOffsetOnline: (Int) -> Unit,
|
setScrollIndexOnline: (Int) -> Unit = {},
|
||||||
|
setScrollOffsetOnline: (Int) -> Unit = {},
|
||||||
) {
|
) {
|
||||||
val currentTheme = LocalThemeConfig.current
|
val currentTheme = LocalThemeConfig.current
|
||||||
|
|
||||||
@@ -208,10 +208,6 @@ fun FriendsScreen(
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
var selectedTabIndex by rememberSaveable {
|
|
||||||
mutableIntStateOf(0)
|
|
||||||
}
|
|
||||||
|
|
||||||
val tabItems = remember {
|
val tabItems = remember {
|
||||||
listOf(
|
listOf(
|
||||||
TabItem(
|
TabItem(
|
||||||
@@ -261,16 +257,16 @@ fun FriendsScreen(
|
|||||||
modifier = Modifier.fillMaxWidth()
|
modifier = Modifier.fillMaxWidth()
|
||||||
)
|
)
|
||||||
PrimaryTabRow(
|
PrimaryTabRow(
|
||||||
selectedTabIndex = selectedTabIndex,
|
selectedTabIndex = screenState.selectedTabIndex,
|
||||||
modifier = Modifier,
|
modifier = Modifier,
|
||||||
containerColor = Color.Transparent
|
containerColor = Color.Transparent
|
||||||
) {
|
) {
|
||||||
tabItems.forEachIndexed { index, item ->
|
tabItems.forEachIndexed { index, item ->
|
||||||
Tab(
|
Tab(
|
||||||
selected = index == selectedTabIndex,
|
selected = index == screenState.selectedTabIndex,
|
||||||
onClick = {
|
onClick = {
|
||||||
if (selectedTabIndex != index) {
|
if (screenState.selectedTabIndex != index) {
|
||||||
selectedTabIndex = index
|
setSelectedTabIndex(index)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
text = {
|
text = {
|
||||||
@@ -296,18 +292,19 @@ fun FriendsScreen(
|
|||||||
screenState.isLoading && screenState.friends.isEmpty() -> FullScreenLoader()
|
screenState.isLoading && screenState.friends.isEmpty() -> FullScreenLoader()
|
||||||
|
|
||||||
else -> {
|
else -> {
|
||||||
val pagerState = rememberPagerState { tabItems.size }
|
val pagerState = rememberPagerState(
|
||||||
|
initialPage = screenState.selectedTabIndex
|
||||||
|
) {
|
||||||
|
tabItems.size
|
||||||
|
}
|
||||||
|
|
||||||
LaunchedEffect(selectedTabIndex) {
|
LaunchedEffect(screenState.selectedTabIndex) {
|
||||||
pagerState.animateScrollToPage(selectedTabIndex)
|
pagerState.animateScrollToPage(screenState.selectedTabIndex)
|
||||||
}
|
}
|
||||||
|
|
||||||
LaunchedEffect(pagerState) {
|
LaunchedEffect(pagerState) {
|
||||||
snapshotFlow {
|
snapshotFlow { pagerState.currentPage }
|
||||||
pagerState.currentPage
|
.collect(setSelectedTabIndex)
|
||||||
}.collect { page ->
|
|
||||||
selectedTabIndex = page
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
val pullToRefreshState = rememberPullToRefreshState()
|
val pullToRefreshState = rememberPullToRefreshState()
|
||||||
|
|||||||
Reference in New Issue
Block a user