Skip to content

Commit

Permalink
Remade actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Pururun committed Mar 1, 2024
1 parent 907629c commit 4a8e8a8
Show file tree
Hide file tree
Showing 21 changed files with 375 additions and 407 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,33 @@ package net.mullvad.mullvadvpn.compose.communication
import android.os.Parcelable
import kotlinx.parcelize.Parcelize

sealed interface CustomListAction : Parcelable {
sealed interface
CustomListAction : Parcelable {

@Parcelize
data class Rename(val customListId: String, val name: String) : CustomListAction {
fun not(): CustomListAction = this
data class Rename(val customListId: String, val name: String, val newName: String) : CustomListAction {
fun not() = this.copy(name = newName, newName = name)
}

@Parcelize
data class Delete(val customListId: String, val name: String) : CustomListAction {
fun not(locations: List<String>): CustomListAction = Create(name, locations)
data class Delete(val customListId: String) : CustomListAction {
fun not(name: String, locations: List<String>) = Create(name, locations)
}

@Parcelize
data class Create(
val name: String = "",
val locations: List<String> = emptyList(),
val locationNames: List<String> = emptyList()
val locations: List<String> = emptyList()
) : CustomListAction, Parcelable {
fun not(customListId: String) = Delete(customListId, name)
fun not(customListId: String) = Delete(customListId)
}

@Parcelize
data class UpdateLocations(
val customListId: String,
val newList: Boolean = false,
val locations: List<String> = emptyList()
) : CustomListAction {
fun not(locations: List<String>): CustomListAction =
fun not(locations: List<String>): UpdateLocations =
UpdateLocations(customListId = customListId, locations = locations)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,31 @@ import android.os.Parcelable
import kotlinx.parcelize.Parcelize

sealed interface CustomListResult : Parcelable {
val reverseAction: CustomListAction
val undo: CustomListAction

@Parcelize
data class ListCreated(
val locationName: String,
val customListName: String,
override val reverseAction: CustomListAction
data class Created(
val id: String,
val name: String,
val locationName: String?,
override val undo: CustomListAction.Delete
) : CustomListResult

@Parcelize
data class ListDeleted(val name: String, override val reverseAction: CustomListAction) :
CustomListResult
data class Deleted(override val undo: CustomListAction.Create) : CustomListResult {
val name
get() = undo.name
}

@Parcelize
data class ListRenamed(val name: String, override val reverseAction: CustomListAction) :
CustomListResult
data class Renamed(override val undo: CustomListAction.Rename) : CustomListResult {
val name: String
get() = undo.name
}

@Parcelize
data class ListUpdated(val name: String, override val reverseAction: CustomListAction) :
CustomListResult
data class LocationsChanged(
val name: String,
override val undo: CustomListAction.UpdateLocations
) : CustomListResult
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ import com.ramcosta.composedestinations.result.ResultBackNavigator
import com.ramcosta.composedestinations.spec.DestinationStyle
import net.mullvad.mullvadvpn.R
import net.mullvad.mullvadvpn.compose.button.PrimaryButton
import net.mullvad.mullvadvpn.compose.communication.CustomListAction
import net.mullvad.mullvadvpn.compose.communication.CustomListRequest
import net.mullvad.mullvadvpn.compose.communication.CustomListResult
import net.mullvad.mullvadvpn.compose.communication.parsedAction
import net.mullvad.mullvadvpn.compose.destinations.CustomListLocationsDestination
import net.mullvad.mullvadvpn.compose.state.CreateCustomListUiState
import net.mullvad.mullvadvpn.compose.textfield.CustomTextField
Expand Down Expand Up @@ -57,28 +54,21 @@ fun PreviewCreateCustomListDialogError() {
@Destination(style = DestinationStyle.Dialog::class)
fun CreateCustomList(
navigator: DestinationsNavigator,
backNavigator: ResultBackNavigator<CustomListResult.ListCreated>,
request: CustomListRequest
backNavigator: ResultBackNavigator<CustomListResult.Created>,
locationCode: String = ""
) {
val vm: CreateCustomListDialogViewModel =
koinViewModel(
parameters = { parametersOf(request.parsedAction<CustomListAction.Create>()) }
parameters = { parametersOf(locationCode) }
)
LaunchedEffect(key1 = Unit) {
vm.uiSideEffect.collect { sideEffect ->
when (sideEffect) {
is CreateCustomListDialogSideEffect.NavigateToCustomListLocationsScreen -> {
navigator.popBackStack()
navigator.navigate(
CustomListLocationsDestination(
request =
CustomListRequest(
action =
CustomListAction.UpdateLocations(
customListId = sideEffect.customListId,
newList = true
)
)
customListId = sideEffect.customListId,
newList = true
)
) {
launchSingleTop = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ import com.ramcosta.composedestinations.spec.DestinationStyle
import net.mullvad.mullvadvpn.R
import net.mullvad.mullvadvpn.compose.button.NegativeButton
import net.mullvad.mullvadvpn.compose.button.PrimaryButton
import net.mullvad.mullvadvpn.compose.communication.CustomListAction
import net.mullvad.mullvadvpn.compose.communication.CustomListRequest
import net.mullvad.mullvadvpn.compose.communication.CustomListResult
import net.mullvad.mullvadvpn.compose.communication.parsedAction
import net.mullvad.mullvadvpn.lib.theme.AppTheme
import net.mullvad.mullvadvpn.lib.theme.Dimens
import net.mullvad.mullvadvpn.viewmodel.DeleteCustomListConfirmationSideEffect
Expand All @@ -41,11 +38,12 @@ private fun PreviewRemoveDeviceConfirmationDialog() {
@Composable
@Destination(style = DestinationStyle.Dialog::class)
fun DeleteCustomList(
navigator: ResultBackNavigator<CustomListResult.ListDeleted>,
request: CustomListRequest
navigator: ResultBackNavigator<CustomListResult.Deleted>,
customListId: String,
name: String
) {
val viewModel: DeleteCustomListConfirmationViewModel =
koinViewModel(parameters = { parametersOf(request.parsedAction()) })
koinViewModel(parameters = { parametersOf(customListId) })

LaunchedEffect(Unit) {
viewModel.uiSideEffect.collect {
Expand All @@ -57,7 +55,7 @@ fun DeleteCustomList(
}

DeleteCustomListConfirmationDialog(
name = request.parsedAction<CustomListAction.Delete>().name,
name = name,
onDelete = viewModel::deleteCustomList,
onBack = { navigator.navigateBack() }
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ import com.ramcosta.composedestinations.result.ResultBackNavigator
import com.ramcosta.composedestinations.spec.DestinationStyle
import net.mullvad.mullvadvpn.R
import net.mullvad.mullvadvpn.compose.button.PrimaryButton
import net.mullvad.mullvadvpn.compose.communication.CustomListRequest
import net.mullvad.mullvadvpn.compose.communication.CustomListResult
import net.mullvad.mullvadvpn.compose.communication.parsedAction
import net.mullvad.mullvadvpn.compose.state.UpdateCustomListUiState
import net.mullvad.mullvadvpn.compose.textfield.CustomTextField
import net.mullvad.mullvadvpn.lib.theme.AppTheme
Expand All @@ -43,11 +41,12 @@ fun PreviewEditCustomListNameDialog() {
@Composable
@Destination(style = DestinationStyle.Dialog::class)
fun EditCustomListName(
backNavigator: ResultBackNavigator<CustomListResult.ListRenamed>,
request: CustomListRequest
backNavigator: ResultBackNavigator<CustomListResult.Renamed>,
customListId: String,
initialName: String
) {
val vm: EditCustomListNameDialogViewModel =
koinViewModel(parameters = { parametersOf(request.parsedAction()) })
koinViewModel(parameters = { parametersOf(customListId, initialName) })
LaunchedEffect(Unit) {
vm.uiSideEffect.collect { sideEffect ->
when (sideEffect) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ import com.ramcosta.composedestinations.result.ResultBackNavigator
import com.ramcosta.composedestinations.result.ResultRecipient
import net.mullvad.mullvadvpn.R
import net.mullvad.mullvadvpn.compose.cell.CheckableRelayLocationCell
import net.mullvad.mullvadvpn.compose.communication.CustomListRequest
import net.mullvad.mullvadvpn.compose.communication.CustomListResult
import net.mullvad.mullvadvpn.compose.communication.parsedAction
import net.mullvad.mullvadvpn.compose.component.LocationsEmptyText
import net.mullvad.mullvadvpn.compose.component.MullvadCircularProgressIndicatorLarge
import net.mullvad.mullvadvpn.compose.component.NavigateBackIconButton
Expand Down Expand Up @@ -59,13 +57,14 @@ fun PreviewCustomListLocationScreen() {
@Destination(style = SlideInFromRightTransition::class)
fun CustomListLocations(
navigator: DestinationsNavigator,
backNavigator: ResultBackNavigator<CustomListResult.ListUpdated>,
request: CustomListRequest,
discardChangesResultRecipient: ResultRecipient<DiscardChangesDialogDestination, Boolean>
backNavigator: ResultBackNavigator<CustomListResult.LocationsChanged>,
discardChangesResultRecipient: ResultRecipient<DiscardChangesDialogDestination, Boolean>,
customListId: String,
newList: Boolean,
) {
val customListsViewModel =
koinViewModel<CustomListLocationsViewModel>(
parameters = { parametersOf(request.parsedAction()) }
parameters = { parametersOf(customListId, newList) }
)

discardChangesResultRecipient.onNavResult(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.navigation.DestinationsNavigator
import com.ramcosta.composedestinations.result.NavResult
Expand All @@ -32,7 +35,6 @@ import kotlinx.coroutines.launch
import net.mullvad.mullvadvpn.R
import net.mullvad.mullvadvpn.compose.cell.NavigationComposeCell
import net.mullvad.mullvadvpn.compose.communication.CustomListAction
import net.mullvad.mullvadvpn.compose.communication.CustomListRequest
import net.mullvad.mullvadvpn.compose.communication.CustomListResult
import net.mullvad.mullvadvpn.compose.component.MullvadCircularProgressIndicatorLarge
import net.mullvad.mullvadvpn.compose.component.NavigateBackIconButton
Expand Down Expand Up @@ -62,7 +64,7 @@ fun PreviewCustomListsScreen() {
fun CustomLists(
navigator: DestinationsNavigator,
editCustomListResultRecipient:
ResultRecipient<EditCustomListDestination, CustomListResult.ListDeleted>
ResultRecipient<EditCustomListDestination, CustomListResult.Deleted>
) {
val viewModel = koinViewModel<CustomListsViewModel>()
val uiState by viewModel.uiState.collectAsState()
Expand All @@ -89,7 +91,7 @@ fun CustomLists(
duration = SnackbarDuration.Long,
onAction = {
viewModel.undoDeleteCustomList(
result.value.reverseAction as CustomListAction.Create
result.value.undo as CustomListAction.Create
)
}
)
Expand All @@ -104,7 +106,7 @@ fun CustomLists(
snackbarHostState = snackbarHostState,
addCustomList = {
navigator.navigate(
CreateCustomListDestination(CustomListRequest(CustomListAction.Create())),
CreateCustomListDestination(),
) {
launchSingleTop = true
}
Expand All @@ -131,6 +133,17 @@ fun CustomListsScreen(
navigationIcon = { NavigateBackIconButton(onBackClick) },
floatingActionButton = {
ExtendedFloatingActionButton(
modifier =
Modifier.shadow(
elevation = 3.dp,
spotColor = Color(0x4D000000),
ambientColor = Color(0x4D000000)
)
.shadow(
elevation = 8.dp,
spotColor = Color(0x26000000),
ambientColor = Color(0x26000000)
),
onClick = addCustomList,
containerColor = MaterialTheme.colorScheme.primary,
contentColor = MaterialTheme.colorScheme.onPrimary,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ import com.ramcosta.composedestinations.result.ResultBackNavigator
import com.ramcosta.composedestinations.result.ResultRecipient
import net.mullvad.mullvadvpn.R
import net.mullvad.mullvadvpn.compose.cell.TwoRowCell
import net.mullvad.mullvadvpn.compose.communication.CustomListAction
import net.mullvad.mullvadvpn.compose.communication.CustomListRequest
import net.mullvad.mullvadvpn.compose.communication.CustomListResult
import net.mullvad.mullvadvpn.compose.component.MullvadCircularProgressIndicatorLarge
import net.mullvad.mullvadvpn.compose.component.NavigateBackIconButton
Expand Down Expand Up @@ -76,10 +74,10 @@ fun PreviewEditCustomListScreen() {
@Destination(style = SlideInFromRightTransition::class)
fun EditCustomList(
navigator: DestinationsNavigator,
backNavigator: ResultBackNavigator<CustomListResult.ListDeleted>,
backNavigator: ResultBackNavigator<CustomListResult.Deleted>,
customListId: String,
confirmDeleteListResultRecipient:
ResultRecipient<DeleteCustomListDestination, CustomListResult.ListDeleted>
ResultRecipient<DeleteCustomListDestination, CustomListResult.Deleted>
) {
val viewModel =
koinViewModel<EditCustomListViewModel>(parameters = { parametersOf(customListId) })
Expand All @@ -98,32 +96,20 @@ fun EditCustomList(
uiState = uiState,
onDeleteList = { name ->
navigator.navigate(
DeleteCustomListDestination(
CustomListRequest(action = CustomListAction.Delete(customListId, name))
)
DeleteCustomListDestination(customListId = customListId, name = name)
) {
launchSingleTop = true
}
},
onNameClicked = { id, name ->
navigator.navigate(
EditCustomListNameDestination(
request = CustomListRequest(action = CustomListAction.Rename(id, name))
)
EditCustomListNameDestination(customListId = id, initialName = name)
) {
launchSingleTop = true
}
},
onLocationsClicked = {
navigator.navigate(
CustomListLocationsDestination(
request =
CustomListRequest(
action =
CustomListAction.UpdateLocations(customListId = it, newList = false)
)
)
) {
navigator.navigate(CustomListLocationsDestination(customListId = it, newList = false)) {
launchSingleTop = true
}
},
Expand Down
Loading

0 comments on commit 4a8e8a8

Please sign in to comment.