Skip to content

Commit

Permalink
Fix preview screen parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
Pururun committed Nov 1, 2024
1 parent 5cbe233 commit 73863e3
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package net.mullvad.mullvadvpn.compose.preview

import net.mullvad.mullvadvpn.compose.state.RelayListItem
import net.mullvad.mullvadvpn.compose.state.RelayListItemState
import net.mullvad.mullvadvpn.lib.model.CustomList
import net.mullvad.mullvadvpn.lib.model.CustomListId
import net.mullvad.mullvadvpn.lib.model.CustomListName
import net.mullvad.mullvadvpn.lib.model.RelayItem

object RelayListItemPreviewData {
fun generateRelayListItems(
includeCustomLists: Boolean,
isSearching: Boolean,
): List<RelayListItem> = buildList {
if (!isSearching || includeCustomLists) {
add(RelayListItem.CustomListHeader)
// Add custom list items
if (includeCustomLists) {
RelayListItem.CustomListItem(
item =
RelayItem.CustomList(
customList =
CustomList(
id = CustomListId("custom_list_id"),
name = CustomListName.fromString("Custom List"),
locations = emptyList(),
),
locations =
listOf(
RelayItemPreviewData.generateRelayItemCountry(
name = "Country",
cityNames = listOf("City"),
relaysPerCity = 2,
active = true,
)
),
),
isSelected = false,
state = null,
expanded = false,
)
}
if (!isSearching) {
add(RelayListItem.CustomListFooter(hasCustomList = includeCustomLists))
}
}
add(RelayListItem.LocationHeader)
val locations =
listOf(
RelayItemPreviewData.generateRelayItemCountry(
name = "A relay",
cityNames = listOf("City 1", "City 2"),
relaysPerCity = 2,
active = true,
),
RelayItemPreviewData.generateRelayItemCountry(
name = "Another relay",
cityNames = listOf("City X", "City Y", "City Z"),
relaysPerCity = 1,
active = false,
),
)
addAll(
listOf(
RelayListItem.GeoLocationItem(
item = locations[0],
isSelected = false,
depth = 0,
expanded = true,
state = null,
),
RelayListItem.GeoLocationItem(
item = locations[0].cities[0],
isSelected = true,
depth = 1,
expanded = false,
state = null,
),
RelayListItem.GeoLocationItem(
item = locations[0].cities[1],
isSelected = false,
depth = 1,
expanded = true,
state = null,
),
RelayListItem.GeoLocationItem(
item = locations[0].cities[1].relays[0],
isSelected = false,
depth = 2,
expanded = false,
state = RelayListItemState.USED_AS_EXIT,
),
RelayListItem.GeoLocationItem(
item = locations[0].cities[1].relays[0],
isSelected = false,
depth = 2,
expanded = false,
state = null,
),
RelayListItem.GeoLocationItem(
item = locations[1],
isSelected = false,
depth = 0,
expanded = false,
state = null,
),
)
)
}

fun generateEmptyList(searchTerm: String) = listOf(RelayListItem.LocationsEmptyText(searchTerm))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package net.mullvad.mullvadvpn.compose.preview

import androidx.compose.ui.tooling.preview.PreviewParameterProvider
import net.mullvad.mullvadvpn.compose.state.SearchLocationUiState
import net.mullvad.mullvadvpn.usecase.FilterChip

class SearchLocationsUiStatePreviewParameterProvider :
PreviewParameterProvider<SearchLocationUiState> {
override val values =
sequenceOf(
SearchLocationUiState.NoQuery(searchTerm = "", filterChips = listOf(FilterChip.Entry)),
SearchLocationUiState.Content(
searchTerm = "Mullvad",
filterChips = listOf(FilterChip.Entry),
relayListItems = RelayListItemPreviewData.generateEmptyList("Mullvad"),
customLists = emptyList(),
),
SearchLocationUiState.Content(
searchTerm = "Germany",
filterChips = listOf(FilterChip.Entry),
relayListItems =
RelayListItemPreviewData.generateRelayListItems(
includeCustomLists = true,
isSearching = true,
),
customLists = emptyList(),
),
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,44 @@ package net.mullvad.mullvadvpn.compose.preview
import androidx.compose.ui.tooling.preview.PreviewParameterProvider
import net.mullvad.mullvadvpn.compose.state.RelayListType
import net.mullvad.mullvadvpn.compose.state.SelectLocationUiState
import net.mullvad.mullvadvpn.lib.model.GeoLocationId
import net.mullvad.mullvadvpn.lib.model.Ownership
import net.mullvad.mullvadvpn.lib.model.Provider
import net.mullvad.mullvadvpn.lib.model.ProviderId
import net.mullvad.mullvadvpn.lib.model.RelayItem
import net.mullvad.mullvadvpn.usecase.FilterChip
import net.mullvad.mullvadvpn.usecase.ModelOwnership

private val RELAY =
RelayItem.Location.Relay(
id =
GeoLocationId.Hostname(
city = GeoLocationId.City(country = GeoLocationId.Country("se"), code = "code"),
code = "code",
),
provider = Provider(providerId = ProviderId("providerId"), ownership = Ownership.Rented),
active = true,
daita = true,
)

class SelectLocationsUiStatePreviewParameterProvider :
PreviewParameterProvider<SelectLocationUiState> {
override val values =
sequenceOf(
SelectLocationUiState(
// searchTerm = "search term",
listOf(FilterChip.Ownership(ownership = ModelOwnership.MullvadOwned)),
filterChips = emptyList(),
multihopEnabled = false,
relayListType = RelayListType.EXIT,
),
SelectLocationUiState(
filterChips =
listOf(
FilterChip.Ownership(ownership = ModelOwnership.Rented),
FilterChip.Provider(PROVIDER_COUNT),
),
multihopEnabled = false,
relayListType = RelayListType.EXIT,
),
SelectLocationUiState(
filterChips = emptyList(),
multihopEnabled = true,
relayListType = RelayListType.ENTRY,
)
),
SelectLocationUiState(
filterChips =
listOf(
FilterChip.Ownership(ownership = ModelOwnership.MullvadOwned),
FilterChip.Provider(PROVIDER_COUNT),
),
multihopEnabled = true,
relayListType = RelayListType.ENTRY,
),
)
}

private const val PROVIDER_COUNT = 3
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import androidx.compose.ui.platform.LocalSoftwareKeyboardController
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.tooling.preview.PreviewParameter
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.compose.dropUnlessResumed
import com.ramcosta.composedestinations.annotation.Destination
Expand All @@ -56,6 +57,7 @@ import net.mullvad.mullvadvpn.compose.component.MullvadSnackbar
import net.mullvad.mullvadvpn.compose.component.drawVerticalScrollbar
import net.mullvad.mullvadvpn.compose.constant.ContentType
import net.mullvad.mullvadvpn.compose.extensions.dropUnlessResumed
import net.mullvad.mullvadvpn.compose.preview.SearchLocationsUiStatePreviewParameterProvider
import net.mullvad.mullvadvpn.compose.state.RelayListType
import net.mullvad.mullvadvpn.compose.state.SearchLocationUiState
import net.mullvad.mullvadvpn.compose.transitions.SearchTransition
Expand All @@ -72,14 +74,18 @@ import net.mullvad.mullvadvpn.viewmodel.location.SearchLocationSideEffect
import net.mullvad.mullvadvpn.viewmodel.location.SearchLocationViewModel
import org.koin.androidx.compose.koinViewModel

@Preview
@Preview("Default|Not found|Results")
@Composable
private fun PreviewSearchLocationScreen() {
AppTheme { SearchLocationScreen(state = SearchLocationUiState.NoQuery("", emptyList())) }
private fun PreviewSearchLocationScreen(
@PreviewParameter(SearchLocationsUiStatePreviewParameterProvider::class)
state: SearchLocationUiState
) {
AppTheme { SearchLocationScreen(state = state) }
}

data class SearchLocationNavArgs(val relayListType: RelayListType)

@Suppress("LongMethod")
@Composable
@Destination<RootGraph>(style = SearchTransition::class, navArgs = SearchLocationNavArgs::class)
fun SearchLocation(
Expand Down Expand Up @@ -187,6 +193,7 @@ fun SearchLocation(
)
}

@Suppress("LongMethod")
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun SearchLocationScreen(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ import net.mullvad.mullvadvpn.viewmodel.location.SelectLocationSideEffect
import net.mullvad.mullvadvpn.viewmodel.location.SelectLocationViewModel
import org.koin.androidx.compose.koinViewModel

@Preview("Content|Loading")
@Preview("Default|Filters|Multihop|Multihop and Filters")
@Composable
private fun PreviewSelectLocationScreen(
@PreviewParameter(SelectLocationsUiStatePreviewParameterProvider::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,5 @@ sealed interface SearchLocationUiState {
override val filterChips: List<FilterChip>,
val relayListItems: List<RelayListItem>,
val customLists: List<RelayItem.CustomList>,
val relayListType: RelayListType,
) : SearchLocationUiState
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import net.mullvad.mullvadvpn.usecase.customlists.CustomListsRelayItemUseCase
import net.mullvad.mullvadvpn.usecase.customlists.FilterCustomListsRelayItemUseCase
import net.mullvad.mullvadvpn.util.combine

@Suppress("LongParameterList")
class SearchLocationViewModel(
private val wireguardConstraintsRepository: WireguardConstraintsRepository,
private val relayListRepository: RelayListRepository,
Expand Down Expand Up @@ -93,7 +94,6 @@ class SearchLocationViewModel(
expandedItems = expandedItems,
),
customLists = customLists,
relayListType = relayListType,
filterChips = filterChips,
)
} else {
Expand Down

0 comments on commit 73863e3

Please sign in to comment.