Skip to content

Commit

Permalink
Simiplify center on item
Browse files Browse the repository at this point in the history
  • Loading branch information
Rawa committed Jul 24, 2024
1 parent 18097d5 commit 3fcfb6e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Text
import androidx.compose.material3.rememberModalBottomSheetState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
Expand Down Expand Up @@ -139,13 +140,11 @@ fun SelectLocation(
ResultRecipient<CustomListLocationsDestination, LocationsChanged>
) {
val vm = koinViewModel<SelectLocationViewModel>()
val state = vm.uiState.collectAsStateWithLifecycle().value
val state = vm.uiState.collectAsStateWithLifecycle()

val snackbarHostState = remember { SnackbarHostState() }
val context = LocalContext.current

val lazyListState = rememberLazyListState()

CollectSideEffectWithLifecycle(vm.uiSideEffect) {
when (it) {
SelectLocationSideEffect.CloseScreen -> backNavigator.navigateBack(result = true)
Expand All @@ -166,7 +165,7 @@ fun SelectLocation(
duration = SnackbarDuration.Short)
}
is SelectLocationSideEffect.CenterOnItem -> {
val index = state.indexOfSelectedRelayItem()
val index = state.value.indexOfSelectedRelayItem()
if (index != -1) {
lazyListState.scrollToItem(index)
lazyListState.animateScrollAndCentralizeItem(index)
Expand All @@ -175,6 +174,10 @@ fun SelectLocation(
}
}

if (state.value is SelectLocationUiState.Content) {
LaunchedEffect(Unit) { vm.centerOnSelected() }
}

createCustomListDialogResultRecipient.OnCustomListNavResult(
snackbarHostState, vm::performAction)

Expand All @@ -187,7 +190,7 @@ fun SelectLocation(
updateCustomListResultRecipient.OnCustomListNavResult(snackbarHostState, vm::performAction)

SelectLocationScreen(
state = state,
state = state.value,
lazyListState = lazyListState,
snackbarHostState = snackbarHostState,
onSelectRelay = vm::selectRelay,
Expand Down Expand Up @@ -309,6 +312,7 @@ fun SelectLocationScreen(
loading()
}
is SelectLocationUiState.Content -> {

itemsIndexed(
items = state.relayListItems,
key = { index: Int, item: RelayListItem -> item.key },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.filterIsInstance
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.receiveAsFlow
Expand Down Expand Up @@ -83,18 +78,13 @@ class SelectLocationViewModel(
private val _uiSideEffect = Channel<SelectLocationSideEffect>()
val uiSideEffect = _uiSideEffect.receiveAsFlow()

init {
fun centerOnSelected() =
viewModelScope.launch {
uiState
.map { it is Content }
.filter { it }
.distinctUntilChanged()
.flatMapLatest { relayListRepository.selectedLocation.filterNotNull() }
.filterIsInstance<Constraint.Only<RelayItemId>>()
.map { it.value }
.collect { _uiSideEffect.send(SelectLocationSideEffect.CenterOnItem(it)) }
val selectedLocation = relayListRepository.selectedLocation.value.getOrNull()
if (selectedLocation != null) {
_uiSideEffect.send(SelectLocationSideEffect.CenterOnItem(selectedLocation))
}
}
}

private fun initialExpand(): Set<String> = buildSet {
val item = relayListRepository.selectedLocation.value.getOrNull()
Expand Down

0 comments on commit 3fcfb6e

Please sign in to comment.