Skip to content

Commit e219f4f

Browse files
committed
Fix uiSideEffect crash
1 parent ddadd73 commit e219f4f

File tree

2 files changed

+17
-23
lines changed

2 files changed

+17
-23
lines changed

android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/screen/SelectLocationScreen.kt

+15-11
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import androidx.compose.material3.SnackbarHostState
3030
import androidx.compose.material3.Text
3131
import androidx.compose.material3.rememberModalBottomSheetState
3232
import androidx.compose.runtime.Composable
33-
import androidx.compose.runtime.LaunchedEffect
3433
import androidx.compose.runtime.getValue
3534
import androidx.compose.runtime.mutableStateOf
3635
import androidx.compose.runtime.remember
@@ -46,7 +45,9 @@ import androidx.compose.ui.res.painterResource
4645
import androidx.compose.ui.res.stringResource
4746
import androidx.compose.ui.text.style.TextAlign
4847
import androidx.compose.ui.tooling.preview.Preview
48+
import androidx.lifecycle.compose.LocalLifecycleOwner
4949
import androidx.lifecycle.compose.collectAsStateWithLifecycle
50+
import androidx.lifecycle.compose.currentStateAsState
5051
import androidx.lifecycle.compose.dropUnlessResumed
5152
import com.ramcosta.composedestinations.annotation.Destination
5253
import com.ramcosta.composedestinations.annotation.RootGraph
@@ -95,6 +96,7 @@ import net.mullvad.mullvadvpn.compose.test.SELECT_LOCATION_LOCATION_BOTTOM_SHEET
9596
import net.mullvad.mullvadvpn.compose.textfield.SearchTextField
9697
import net.mullvad.mullvadvpn.compose.transitions.SelectLocationTransition
9798
import net.mullvad.mullvadvpn.compose.util.CollectSideEffectWithLifecycle
99+
import net.mullvad.mullvadvpn.compose.util.RunOnKeyChange
98100
import net.mullvad.mullvadvpn.compose.util.showSnackbarImmediately
99101
import net.mullvad.mullvadvpn.lib.model.CustomListId
100102
import net.mullvad.mullvadvpn.lib.model.RelayItem
@@ -147,7 +149,9 @@ fun SelectLocation(
147149
val lazyListState = rememberLazyListState()
148150
CollectSideEffectWithLifecycle(vm.uiSideEffect) {
149151
when (it) {
150-
SelectLocationSideEffect.CloseScreen -> backNavigator.navigateBack(result = true)
152+
SelectLocationSideEffect.CloseScreen -> {
153+
backNavigator.navigateBack(result = true)
154+
}
151155
is SelectLocationSideEffect.LocationAddedToCustomList ->
152156
launch {
153157
snackbarHostState.showResultSnackbar(
@@ -171,18 +175,16 @@ fun SelectLocation(
171175
duration = SnackbarDuration.Short
172176
)
173177
}
174-
is SelectLocationSideEffect.CenterOnItem -> {
175-
val index = state.value.indexOfSelectedRelayItem()
176-
if (index != -1) {
177-
lazyListState.scrollToItem(index)
178-
lazyListState.animateScrollAndCentralizeItem(index)
179-
}
180-
}
181178
}
182179
}
183180

184-
if (state.value is SelectLocationUiState.Content) {
185-
LaunchedEffect(Unit) { vm.centerOnSelected() }
181+
val stateActual = state.value
182+
RunOnKeyChange(stateActual is SelectLocationUiState.Content) {
183+
val index = stateActual.indexOfSelectedRelayItem()
184+
if (index != -1) {
185+
lazyListState.scrollToItem(index)
186+
lazyListState.animateScrollAndCentralizeItem(index)
187+
}
186188
}
187189

188190
createCustomListDialogResultRecipient.OnCustomListNavResult(
@@ -285,6 +287,8 @@ fun SelectLocationScreen(
285287
)
286288
}
287289
) {
290+
val lifecycleState = LocalLifecycleOwner.current.lifecycle.currentStateAsState()
291+
Text(text = lifecycleState.value.toString())
288292
var bottomSheetState by remember { mutableStateOf<BottomSheetState?>(null) }
289293
BottomSheets(
290294
bottomSheetState = bottomSheetState,

android/app/src/main/kotlin/net/mullvad/mullvadvpn/viewmodel/SelectLocationViewModel.kt

+2-12
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,6 @@ class SelectLocationViewModel(
7878
private val _uiSideEffect = Channel<SelectLocationSideEffect>()
7979
val uiSideEffect = _uiSideEffect.receiveAsFlow()
8080

81-
fun centerOnSelected() =
82-
viewModelScope.launch {
83-
val selectedLocation = relayListRepository.selectedLocation.value.getOrNull()
84-
if (selectedLocation != null) {
85-
_uiSideEffect.send(SelectLocationSideEffect.CenterOnItem(selectedLocation))
86-
}
87-
}
88-
8981
private fun initialExpand(): Set<String> = buildSet {
9082
val item = relayListRepository.selectedLocation.value.getOrNull()
9183
when (item) {
@@ -331,8 +323,8 @@ class SelectLocationViewModel(
331323
relayListRepository
332324
.updateSelectedRelayLocation(locationConstraint)
333325
.fold(
334-
{ _uiSideEffect.trySend(SelectLocationSideEffect.GenericError) },
335-
{ _uiSideEffect.trySend(SelectLocationSideEffect.CloseScreen) },
326+
{ _uiSideEffect.send(SelectLocationSideEffect.GenericError) },
327+
{ _uiSideEffect.send(SelectLocationSideEffect.CloseScreen) },
336328
)
337329
}
338330
}
@@ -417,6 +409,4 @@ sealed interface SelectLocationSideEffect {
417409
class LocationRemovedFromCustomList(val result: LocationsChanged) : SelectLocationSideEffect
418410

419411
data object GenericError : SelectLocationSideEffect
420-
421-
data class CenterOnItem(val selectedItem: RelayItemId?) : SelectLocationSideEffect
422412
}

0 commit comments

Comments
 (0)