Skip to content

Commit

Permalink
Refactor select location view even more
Browse files Browse the repository at this point in the history
  • Loading branch information
Rawa committed Jul 23, 2024
1 parent 8c0c5f1 commit bd27b43
Show file tree
Hide file tree
Showing 7 changed files with 394 additions and 443 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package net.mullvad.mullvadvpn.compose.cell

import androidx.compose.foundation.horizontalScroll
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.rememberScrollState
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
Expand All @@ -16,29 +14,22 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import net.mullvad.mullvadvpn.R
import net.mullvad.mullvadvpn.compose.component.MullvadFilterChip
import net.mullvad.mullvadvpn.compose.state.FilterChip
import net.mullvad.mullvadvpn.lib.model.Ownership
import net.mullvad.mullvadvpn.lib.theme.AppTheme
import net.mullvad.mullvadvpn.lib.theme.Dimens

@Preview
@Composable
private fun PreviewFilterCell() {
AppTheme {
FilterCell(
ownershipFilter = Ownership.MullvadOwned,
selectedProviderFilter = 3,
removeOwnershipFilter = {},
removeProviderFilter = {}
)
}
AppTheme { FilterRow(emptyList(), {}, {}) }
}

@Composable
fun FilterCell(
ownershipFilter: Ownership?,
selectedProviderFilter: Int?,
removeOwnershipFilter: () -> Unit,
removeProviderFilter: () -> Unit
fun FilterRow(
filters: List<FilterChip>,
onRemoveOwnershipFilter: () -> Unit,
onRemoveProviderFilter: () -> Unit
) {
val scrollState = rememberScrollState()
Row(
Expand All @@ -54,26 +45,29 @@ fun FilterCell(
modifier = Modifier.padding(end = Dimens.filterTittlePadding),
text = stringResource(id = R.string.filtered),
color = MaterialTheme.colorScheme.onPrimary,
style = MaterialTheme.typography.labelMedium
)

if (selectedProviderFilter != null) {
MullvadFilterChip(
text = stringResource(id = R.string.number_of_providers, selectedProviderFilter),
onRemoveClick = removeProviderFilter
)
Spacer(modifier = Modifier.size(Dimens.chipSpace))
}

if (ownershipFilter != null) {
MullvadFilterChip(
text = stringResource(ownershipFilter.stringResources()),
onRemoveClick = removeOwnershipFilter
)
style = MaterialTheme.typography.labelMedium)
filters.forEach {
when (it) {
is FilterChip.Ownership -> OwnershipFilterChip(it.ownership, onRemoveOwnershipFilter)
is FilterChip.Provider -> ProviderFilterChip(it.count, onRemoveProviderFilter)
}
}
}
}

@Composable
fun ProviderFilterChip(providers: Int, onRemoveClick: () -> Unit) {
MullvadFilterChip(
text = stringResource(id = R.string.number_of_providers, providers),
onRemoveClick = onRemoveClick)
}

@Composable
fun OwnershipFilterChip(ownership: Ownership, onRemoveClick: () -> Unit) {
MullvadFilterChip(
text = stringResource(ownership.stringResources()), onRemoveClick = onRemoveClick)
}

private fun Ownership.stringResources(): Int =
when (this) {
Ownership.MullvadOwned -> R.string.owned
Expand Down
Loading

0 comments on commit bd27b43

Please sign in to comment.