Skip to content

Commit

Permalink
Fix uiSideEffect crash
Browse files Browse the repository at this point in the history
  • Loading branch information
Rawa committed Jul 25, 2024
1 parent ddadd73 commit e219f4f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ 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 All @@ -46,7 +45,9 @@ import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.lifecycle.compose.LocalLifecycleOwner
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.compose.currentStateAsState
import androidx.lifecycle.compose.dropUnlessResumed
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.annotation.RootGraph
Expand Down Expand Up @@ -95,6 +96,7 @@ import net.mullvad.mullvadvpn.compose.test.SELECT_LOCATION_LOCATION_BOTTOM_SHEET
import net.mullvad.mullvadvpn.compose.textfield.SearchTextField
import net.mullvad.mullvadvpn.compose.transitions.SelectLocationTransition
import net.mullvad.mullvadvpn.compose.util.CollectSideEffectWithLifecycle
import net.mullvad.mullvadvpn.compose.util.RunOnKeyChange
import net.mullvad.mullvadvpn.compose.util.showSnackbarImmediately
import net.mullvad.mullvadvpn.lib.model.CustomListId
import net.mullvad.mullvadvpn.lib.model.RelayItem
Expand Down Expand Up @@ -147,7 +149,9 @@ fun SelectLocation(
val lazyListState = rememberLazyListState()
CollectSideEffectWithLifecycle(vm.uiSideEffect) {
when (it) {
SelectLocationSideEffect.CloseScreen -> backNavigator.navigateBack(result = true)
SelectLocationSideEffect.CloseScreen -> {
backNavigator.navigateBack(result = true)
}
is SelectLocationSideEffect.LocationAddedToCustomList ->
launch {
snackbarHostState.showResultSnackbar(
Expand All @@ -171,18 +175,16 @@ fun SelectLocation(
duration = SnackbarDuration.Short
)
}
is SelectLocationSideEffect.CenterOnItem -> {
val index = state.value.indexOfSelectedRelayItem()
if (index != -1) {
lazyListState.scrollToItem(index)
lazyListState.animateScrollAndCentralizeItem(index)
}
}
}
}

if (state.value is SelectLocationUiState.Content) {
LaunchedEffect(Unit) { vm.centerOnSelected() }
val stateActual = state.value
RunOnKeyChange(stateActual is SelectLocationUiState.Content) {
val index = stateActual.indexOfSelectedRelayItem()
if (index != -1) {
lazyListState.scrollToItem(index)
lazyListState.animateScrollAndCentralizeItem(index)
}
}

createCustomListDialogResultRecipient.OnCustomListNavResult(
Expand Down Expand Up @@ -285,6 +287,8 @@ fun SelectLocationScreen(
)
}
) {
val lifecycleState = LocalLifecycleOwner.current.lifecycle.currentStateAsState()
Text(text = lifecycleState.value.toString())
var bottomSheetState by remember { mutableStateOf<BottomSheetState?>(null) }
BottomSheets(
bottomSheetState = bottomSheetState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,6 @@ class SelectLocationViewModel(
private val _uiSideEffect = Channel<SelectLocationSideEffect>()
val uiSideEffect = _uiSideEffect.receiveAsFlow()

fun centerOnSelected() =
viewModelScope.launch {
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()
when (item) {
Expand Down Expand Up @@ -331,8 +323,8 @@ class SelectLocationViewModel(
relayListRepository
.updateSelectedRelayLocation(locationConstraint)
.fold(
{ _uiSideEffect.trySend(SelectLocationSideEffect.GenericError) },
{ _uiSideEffect.trySend(SelectLocationSideEffect.CloseScreen) },
{ _uiSideEffect.send(SelectLocationSideEffect.GenericError) },
{ _uiSideEffect.send(SelectLocationSideEffect.CloseScreen) },
)
}
}
Expand Down Expand Up @@ -417,6 +409,4 @@ sealed interface SelectLocationSideEffect {
class LocationRemovedFromCustomList(val result: LocationsChanged) : SelectLocationSideEffect

data object GenericError : SelectLocationSideEffect

data class CenterOnItem(val selectedItem: RelayItemId?) : SelectLocationSideEffect
}

0 comments on commit e219f4f

Please sign in to comment.