From 3fcfb6e66c8b3bf92f44d9059089dd41fe5ce7a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20G=C3=B6ransson?= Date: Wed, 24 Jul 2024 16:02:17 +0200 Subject: [PATCH] Simiplify center on item --- .../compose/screen/SelectLocationScreen.kt | 14 ++++++++----- .../viewmodel/SelectLocationViewModel.kt | 20 +++++-------------- 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/screen/SelectLocationScreen.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/screen/SelectLocationScreen.kt index 0099ec6158ad..96c7c2031513 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/screen/SelectLocationScreen.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/screen/SelectLocationScreen.kt @@ -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 @@ -139,13 +140,11 @@ fun SelectLocation( ResultRecipient ) { val vm = koinViewModel() - 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) @@ -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) @@ -175,6 +174,10 @@ fun SelectLocation( } } + if (state.value is SelectLocationUiState.Content) { + LaunchedEffect(Unit) { vm.centerOnSelected() } + } + createCustomListDialogResultRecipient.OnCustomListNavResult( snackbarHostState, vm::performAction) @@ -187,7 +190,7 @@ fun SelectLocation( updateCustomListResultRecipient.OnCustomListNavResult(snackbarHostState, vm::performAction) SelectLocationScreen( - state = state, + state = state.value, lazyListState = lazyListState, snackbarHostState = snackbarHostState, onSelectRelay = vm::selectRelay, @@ -309,6 +312,7 @@ fun SelectLocationScreen( loading() } is SelectLocationUiState.Content -> { + itemsIndexed( items = state.relayListItems, key = { index: Int, item: RelayListItem -> item.key }, diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/viewmodel/SelectLocationViewModel.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/viewmodel/SelectLocationViewModel.kt index 978ac8ffc2a8..d87882370cec 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/viewmodel/SelectLocationViewModel.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/viewmodel/SelectLocationViewModel.kt @@ -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 @@ -83,18 +78,13 @@ class SelectLocationViewModel( private val _uiSideEffect = Channel() val uiSideEffect = _uiSideEffect.receiveAsFlow() - init { + fun centerOnSelected() = viewModelScope.launch { - uiState - .map { it is Content } - .filter { it } - .distinctUntilChanged() - .flatMapLatest { relayListRepository.selectedLocation.filterNotNull() } - .filterIsInstance>() - .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 = buildSet { val item = relayListRepository.selectedLocation.value.getOrNull()