Chips row scroll fix

This commit is contained in:
Sad Ellie 2022-09-04 21:22:28 +03:00
parent 8ac244289a
commit a5152e8acb
3 changed files with 22 additions and 38 deletions

View File

@ -120,6 +120,9 @@ fun UnittoApp(
}
composable(LEFT_LIST_SCREEN) {
// Initial group
secondViewModel.setSelectedChip(mainViewModel.unitFrom.group, true)
LeftSideScreen(
viewModel = secondViewModel,
currentUnit = mainViewModel.unitFrom,
@ -130,6 +133,9 @@ fun UnittoApp(
}
composable(RIGHT_LIST_SCREEN) {
// Initial group
secondViewModel.setSelectedChip(mainViewModel.unitFrom.group, false)
RightSideScreen(
viewModel = secondViewModel,
currentUnit = mainViewModel.unitTo,

View File

@ -98,15 +98,9 @@ fun LeftSideScreen(
SearchBar(
title = stringResource(R.string.units_screen_from),
value = uiState.value.searchQuery,
onValueChange = {
viewModel.onSearchQueryChange(it)
viewModel.loadUnitsToShow(true)
},
onValueChange = { viewModel.onSearchQueryChange(it) },
favoritesOnly = uiState.value.favoritesOnly,
favoriteAction = {
viewModel.toggleFavoritesOnly()
viewModel.loadUnitsToShow(true)
},
favoriteAction = { viewModel.toggleFavoritesOnly() },
navigateUpAction = navigateUp,
focusManager = focusManager,
scrollBehavior = scrollBehavior
@ -114,10 +108,7 @@ fun LeftSideScreen(
ChipsRow(
chosenUnitGroup = uiState.value.chosenUnitGroup,
items = uiState.value.shownUnitGroups,
selectAction = {
viewModel.toggleSelectedChip(it)
viewModel.loadUnitsToShow(true)
},
selectAction = { viewModel.toggleSelectedChip(it) },
lazyListState = chipsRowLazyListState,
navigateToSettingsAction = navigateToSettingsAction
)
@ -157,14 +148,8 @@ fun LeftSideScreen(
}
// This block is called only once on initial composition
LaunchedEffect(Unit) {
/**
* Telling viewModel that it needs to update the list
*/
viewModel.setSelectedChip(currentUnit.group)
viewModel.loadUnitsToShow(true)
val groupToSelect = uiState.value.shownUnitGroups.indexOf(currentUnit.group)
LaunchedEffect(uiState.value.shownUnitGroups) {
val groupToSelect = uiState.value.shownUnitGroups.indexOf(uiState.value.chosenUnitGroup)
if (groupToSelect > -1) {
chipsRowLazyListState.animateScrollToItem(groupToSelect)
}
@ -203,13 +188,11 @@ fun RightSideScreen(
title = stringResource(R.string.units_screen_to),
value = uiState.value.searchQuery,
onValueChange = {
viewModel.onSearchQueryChange(it)
viewModel.loadUnitsToShow(false)
viewModel.onSearchQueryChange(it, false)
},
favoritesOnly = uiState.value.favoritesOnly,
favoriteAction = {
viewModel.toggleFavoritesOnly()
viewModel.loadUnitsToShow(false)
viewModel.toggleFavoritesOnly(false)
},
navigateUpAction = navigateUp,
focusManager = focusManager,
@ -253,15 +236,6 @@ fun RightSideScreen(
}
}
}
// This block is called only once on initial composition
LaunchedEffect(Unit) {
/**
* Telling viewModel that it needs to update the list
*/
viewModel.setSelectedChip(currentUnit.group)
viewModel.loadUnitsToShow(false)
}
}
@Composable

View File

@ -67,12 +67,14 @@ class SecondViewModel @Inject constructor(
}
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), SecondScreenUIState())
fun toggleFavoritesOnly() {
fun toggleFavoritesOnly(hideBrokenCurrencies: Boolean = true) {
_favoritesOnly.update { !_favoritesOnly.value }
loadUnitsToShow(hideBrokenCurrencies)
}
fun onSearchQueryChange(newValue: String) {
fun onSearchQueryChange(newValue: String, hideBrokenCurrencies: Boolean = true) {
_searchQuery.update { newValue }
loadUnitsToShow(hideBrokenCurrencies)
}
/**
@ -80,8 +82,9 @@ class SecondViewModel @Inject constructor(
*
* @param unitGroup Chip to change to.
*/
fun setSelectedChip(unitGroup: UnitGroup) {
fun setSelectedChip(unitGroup: UnitGroup, hideBrokenCurrencies: Boolean = true) {
_chosenUnitGroup.update { unitGroup }
loadUnitsToShow(hideBrokenCurrencies)
}
/**
@ -92,9 +95,10 @@ class SecondViewModel @Inject constructor(
*
* @param unitGroup [UnitGroup], currently selected chip.
*/
fun toggleSelectedChip(unitGroup: UnitGroup) {
fun toggleSelectedChip(unitGroup: UnitGroup, hideBrokenCurrencies: Boolean = true) {
val newUnitGroup = if (_chosenUnitGroup.value == unitGroup) null else unitGroup
_chosenUnitGroup.update { newUnitGroup }
loadUnitsToShow(hideBrokenCurrencies)
}
/**
@ -103,7 +107,7 @@ class SecondViewModel @Inject constructor(
* @param hideBrokenCurrencies Decide whether or not we are on left side. Need it because right side requires
* us to mark disabled currency units
*/
fun loadUnitsToShow(
private fun loadUnitsToShow(
hideBrokenCurrencies: Boolean
) {
viewModelScope.launch {