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

View File

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

View File

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