Skip to content

Commit

Permalink
Fix errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Rawa committed Jul 19, 2024
1 parent 6c54a13 commit b0d9885
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import com.ramcosta.composedestinations.result.ResultBackNavigator
import net.mullvad.mullvadvpn.R
import net.mullvad.mullvadvpn.compose.cell.HeaderCell
import net.mullvad.mullvadvpn.compose.cell.IconCell
import net.mullvad.mullvadvpn.compose.communication.LocationsChanged
import net.mullvad.mullvadvpn.compose.communication.CustomListActionResult
import net.mullvad.mullvadvpn.compose.component.MullvadModalBottomContainer
import net.mullvad.mullvadvpn.compose.util.CollectSideEffectWithLifecycle
import net.mullvad.mullvadvpn.lib.model.CustomListId
Expand All @@ -35,14 +35,13 @@ data class CustomListEntrySheetNavArgs(
style = DestinationStyleBottomSheet::class
)
@Composable
fun CustomListEntrySheet(backNavigator: ResultBackNavigator<LocationsChanged>) {
fun CustomListEntrySheet(backNavigator: ResultBackNavigator<CustomListActionResult>) {
val vm = koinViewModel<CustomListEntrySheetViewModel>()
val state = vm.uiState.collectAsStateWithLifecycle()
CollectSideEffectWithLifecycle(vm.uiSideEffect) {
when (it) {
CustomListEntrySheetSideEffect.GenericError -> TODO("How do we handle error?")
is CustomListEntrySheetSideEffect.LocationRemovedFromCustomList ->
backNavigator.navigateBack(it.locationsChanged)
is CustomListEntrySheetSideEffect.LocationRemovedResult ->
backNavigator.navigateBack(it.result)
}
}
MullvadModalBottomContainer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import kotlin.collections.forEach
import net.mullvad.mullvadvpn.R
import net.mullvad.mullvadvpn.compose.cell.HeaderCell
import net.mullvad.mullvadvpn.compose.cell.IconCell
import net.mullvad.mullvadvpn.compose.communication.CustomListSuccess
import net.mullvad.mullvadvpn.compose.communication.CustomListActionResult
import net.mullvad.mullvadvpn.compose.component.MullvadCircularProgressIndicatorMedium
import net.mullvad.mullvadvpn.compose.component.MullvadModalBottomContainer
import net.mullvad.mullvadvpn.compose.state.LocationUiState
Expand All @@ -42,18 +42,14 @@ data class LocationNavArgs(val locationName: String, val id: GeoLocationId)
@Composable
fun LocationSheet(
navigator: DestinationsNavigator,
backNavigator: ResultBackNavigator<CustomListSuccess>,
backNavigator: ResultBackNavigator<CustomListActionResult>,
) {
val viewModel = koinViewModel<LocationSheetViewModel>()
val state = viewModel.uiState.collectAsStateWithLifecycle()

CollectSideEffectWithLifecycle(viewModel.uiSideEffect) {
when (it) {
LocationSideEffect.GenericError -> {
TODO("Handle")
}
is LocationSideEffect.LocationAddedToCustomList ->
backNavigator.navigateBack(it.locationsChanged)
is LocationSideEffect.AddLocationResult -> backNavigator.navigateBack(it.result)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import kotlinx.parcelize.Parcelize
import net.mullvad.mullvadvpn.lib.model.CustomListId
import net.mullvad.mullvadvpn.lib.model.CustomListName

sealed interface CustomListSuccess : Parcelable {
@Parcelize sealed interface CustomListActionResult : Parcelable

@Parcelize data object GenericError : CustomListActionResult, Parcelable

sealed interface CustomListSuccess : CustomListActionResult, Parcelable {
val undo: CustomListAction
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,10 @@ import net.mullvad.mullvadvpn.compose.cell.SwitchComposeSubtitleCell
import net.mullvad.mullvadvpn.compose.cell.ThreeDotCell
import net.mullvad.mullvadvpn.compose.communication.Created
import net.mullvad.mullvadvpn.compose.communication.CustomListAction
import net.mullvad.mullvadvpn.compose.communication.CustomListActionResult
import net.mullvad.mullvadvpn.compose.communication.CustomListSuccess
import net.mullvad.mullvadvpn.compose.communication.Deleted
import net.mullvad.mullvadvpn.compose.communication.GenericError
import net.mullvad.mullvadvpn.compose.communication.LocationsChanged
import net.mullvad.mullvadvpn.compose.communication.Renamed
import net.mullvad.mullvadvpn.compose.component.LocationsEmptyText
Expand Down Expand Up @@ -134,9 +136,9 @@ fun SelectLocation(
deleteCustomListDialogResultRecipient: ResultRecipient<DeleteCustomListDestination, Deleted>,
updateCustomListResultRecipient:
ResultRecipient<CustomListLocationsDestination, LocationsChanged>,
locationSheetResultRecipient: ResultRecipient<LocationSheetDestination, CustomListSuccess>,
locationSheetResultRecipient: ResultRecipient<LocationSheetDestination, CustomListActionResult>,
customListEntryResultRecipient:
ResultRecipient<CustomListEntrySheetDestination, LocationsChanged>
ResultRecipient<CustomListEntrySheetDestination, CustomListActionResult>
) {
val vm = koinViewModel<SelectLocationViewModel>()
val state = vm.uiState.collectAsStateWithLifecycle().value
Expand Down Expand Up @@ -519,7 +521,7 @@ private fun CustomListSuccess.message(context: Context): String =
}

@Composable
private fun <D : DestinationSpec, R : CustomListSuccess> ResultRecipient<D, R>
private fun <D : DestinationSpec, R : CustomListActionResult> ResultRecipient<D, R>
.OnCustomListNavResult(
snackbarHostState: SnackbarHostState,
performAction: (action: CustomListAction) -> Unit
Expand All @@ -533,12 +535,24 @@ private fun <D : DestinationSpec, R : CustomListSuccess> ResultRecipient<D, R>
}
is NavResult.Value -> {
// Handle result
scope.launch {
snackbarHostState.showResultSnackbar(
context = context,
result = result.value,
onUndo = performAction
)
val customListActionResult = result.value
when (customListActionResult) {
is GenericError -> {
scope.launch {
snackbarHostState.showSnackbarImmediately(
message = context.getString(R.string.error_occurred),
duration = SnackbarDuration.Short
)
}
}
is CustomListSuccess ->
scope.launch {
snackbarHostState.showResultSnackbar(
context = context,
result = customListActionResult,
onUndo = performAction
)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.receiveAsFlow
import kotlinx.coroutines.launch
import net.mullvad.mullvadvpn.compose.communication.CustomListAction
import net.mullvad.mullvadvpn.compose.communication.LocationsChanged
import net.mullvad.mullvadvpn.compose.communication.CustomListActionResult
import net.mullvad.mullvadvpn.compose.communication.GenericError
import net.mullvad.mullvadvpn.compose.state.CustomListEntrySheetUiState
import net.mullvad.mullvadvpn.repository.CustomListsRepository
import net.mullvad.mullvadvpn.usecase.customlists.CustomListActionUseCase
import net.mullvad.mullvadvpn.viewmodel.CustomListEntrySheetSideEffect.GenericError

class CustomListEntrySheetViewModel(
val customListsRepository: CustomListsRepository,
Expand All @@ -32,29 +32,22 @@ class CustomListEntrySheetViewModel(

fun removeLocationFromList() =
viewModelScope.launch {
either {
val customList =
customListsRepository.getCustomListById(navArgs.customListId).bind()
val newLocations = (customList.locations - navArgs.location)
customListActionUseCase(
CustomListAction.UpdateLocations(customList.id, newLocations)
)
.bind()
}
.fold(
{ _uiSideEffect.send(GenericError) },
{
_uiSideEffect.send(
CustomListEntrySheetSideEffect.LocationRemovedFromCustomList(it)
)
val result =
either {
val customList =
customListsRepository.getCustomListById(navArgs.customListId).bind()
val newLocations = (customList.locations - navArgs.location)
customListActionUseCase(
CustomListAction.UpdateLocations(customList.id, newLocations)
)
.bind()
}
)
.fold({ GenericError }, { it })
_uiSideEffect.send(CustomListEntrySheetSideEffect.LocationRemovedResult(result))
}
}

sealed interface CustomListEntrySheetSideEffect {
data object GenericError : CustomListEntrySheetSideEffect

data class LocationRemovedFromCustomList(val locationsChanged: LocationsChanged) :
data class LocationRemovedResult(val result: CustomListActionResult) :
CustomListEntrySheetSideEffect
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import kotlinx.coroutines.flow.receiveAsFlow
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import net.mullvad.mullvadvpn.compose.communication.CustomListAction
import net.mullvad.mullvadvpn.compose.communication.LocationsChanged
import net.mullvad.mullvadvpn.compose.communication.CustomListActionResult
import net.mullvad.mullvadvpn.compose.communication.GenericError
import net.mullvad.mullvadvpn.compose.state.CustomListEntry
import net.mullvad.mullvadvpn.compose.state.LocationUiState
import net.mullvad.mullvadvpn.lib.model.RelayItem
Expand Down Expand Up @@ -57,18 +58,19 @@ class LocationSheetViewModel(
viewModelScope.launch {
val newLocations =
(customList.locations + item).filter { it !in item.descendants() }.map { it.id }
customListActionUseCase(CustomListAction.UpdateLocations(customList.id, newLocations))
.fold(
{ _uiSideEffect.send(LocationSideEffect.GenericError) },
{ _uiSideEffect.send(LocationSideEffect.LocationAddedToCustomList(it)) },
)
val result =
customListActionUseCase(
CustomListAction.UpdateLocations(customList.id, newLocations)
)
.fold(
{ GenericError },
{ it },
)
_uiSideEffect.send(LocationSideEffect.AddLocationResult(result))
}
}
}

sealed interface LocationSideEffect {
data object GenericError : LocationSideEffect

data class LocationAddedToCustomList(val locationsChanged: LocationsChanged) :
LocationSideEffect
data class AddLocationResult(val result: CustomListActionResult) : LocationSideEffect
}

0 comments on commit b0d9885

Please sign in to comment.