-
Notifications
You must be signed in to change notification settings - Fork 349
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement support for daita with multihop
- Loading branch information
Showing
34 changed files
with
1,186 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,4 +9,5 @@ object ContentType { | |
const val SPACER = 5 | ||
const val PROGRESS = 6 | ||
const val EMPTY_TEXT = 7 | ||
const val BUTTON = 8 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
204 changes: 204 additions & 0 deletions
204
android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/screen/DaitaScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,204 @@ | ||
package net.mullvad.mullvadvpn.compose.screen | ||
|
||
import androidx.compose.foundation.Image | ||
import androidx.compose.foundation.background | ||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.layout.size | ||
import androidx.compose.foundation.layout.widthIn | ||
import androidx.compose.foundation.layout.wrapContentHeight | ||
import androidx.compose.foundation.pager.HorizontalPager | ||
import androidx.compose.foundation.pager.PagerState | ||
import androidx.compose.foundation.pager.rememberPagerState | ||
import androidx.compose.foundation.shape.CircleShape | ||
import androidx.compose.material3.HorizontalDivider | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.draw.clip | ||
import androidx.compose.ui.layout.ContentScale | ||
import androidx.compose.ui.res.painterResource | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import androidx.lifecycle.compose.collectAsStateWithLifecycle | ||
import androidx.lifecycle.compose.dropUnlessResumed | ||
import com.ramcosta.composedestinations.annotation.Destination | ||
import com.ramcosta.composedestinations.annotation.RootGraph | ||
import com.ramcosta.composedestinations.generated.destinations.DaitaDirectOnlyConfirmationDestination | ||
import com.ramcosta.composedestinations.generated.destinations.DaitaDirectOnlyInfoDestination | ||
import com.ramcosta.composedestinations.navigation.DestinationsNavigator | ||
import com.ramcosta.composedestinations.result.ResultRecipient | ||
import net.mullvad.mullvadvpn.R | ||
import net.mullvad.mullvadvpn.compose.cell.HeaderSwitchComposeCell | ||
import net.mullvad.mullvadvpn.compose.cell.SwitchComposeSubtitleCell | ||
import net.mullvad.mullvadvpn.compose.component.NavigateBackIconButton | ||
import net.mullvad.mullvadvpn.compose.component.ScaffoldWithMediumTopBar | ||
import net.mullvad.mullvadvpn.compose.state.DaitaUiState | ||
import net.mullvad.mullvadvpn.compose.transitions.SlideInFromRightTransition | ||
import net.mullvad.mullvadvpn.compose.util.OnNavResultValue | ||
import net.mullvad.mullvadvpn.lib.theme.AppTheme | ||
import net.mullvad.mullvadvpn.lib.theme.Dimens | ||
import net.mullvad.mullvadvpn.viewmodel.DaitaViewModel | ||
import org.koin.androidx.compose.koinViewModel | ||
|
||
@Preview | ||
@Composable | ||
private fun PreviewDaitaScreen() { | ||
AppTheme { DaitaScreen(state = DaitaUiState(daitaEnabled = false, directOnly = false)) } | ||
} | ||
|
||
@Destination<RootGraph>(style = SlideInFromRightTransition::class) | ||
@Composable | ||
fun Daita( | ||
navigator: DestinationsNavigator, | ||
daitaConfirmationDialogResult: ResultRecipient<DaitaDirectOnlyConfirmationDestination, Boolean>, | ||
) { | ||
val viewModel = koinViewModel<DaitaViewModel>() | ||
val state by viewModel.uiState.collectAsStateWithLifecycle() | ||
|
||
daitaConfirmationDialogResult.OnNavResultValue { | ||
if (it) { | ||
viewModel.setDirectOnly(true) | ||
} | ||
} | ||
|
||
DaitaScreen( | ||
state = state, | ||
onDaitaEnabled = viewModel::setDaita, | ||
onDirectOnly = { enable -> | ||
if (enable) { | ||
navigator.navigate(DaitaDirectOnlyConfirmationDestination) | ||
} else { | ||
viewModel.setDirectOnly(false) | ||
} | ||
}, | ||
onDirectOnlyInfoClick = | ||
dropUnlessResumed { navigator.navigate(DaitaDirectOnlyInfoDestination) }, | ||
onBackClick = dropUnlessResumed { navigator.navigateUp() }, | ||
) | ||
} | ||
|
||
@Composable | ||
fun DaitaScreen( | ||
state: DaitaUiState, | ||
onDaitaEnabled: (enable: Boolean) -> Unit = {}, | ||
onDirectOnly: (enable: Boolean) -> Unit = {}, | ||
onDirectOnlyInfoClick: () -> Unit = {}, | ||
onBackClick: () -> Unit = {}, | ||
) { | ||
ScaffoldWithMediumTopBar( | ||
appBarTitle = stringResource(id = R.string.daita), | ||
navigationIcon = { NavigateBackIconButton { onBackClick() } }, | ||
) { modifier -> | ||
Column(modifier = modifier) { | ||
val pagerState = rememberPagerState(pageCount = { DAITA_PAGES.entries.size }) | ||
DescriptionPager(pagerState = pagerState) | ||
PageIndicator(pagerState = pagerState) | ||
HeaderSwitchComposeCell( | ||
title = stringResource(R.string.enable), | ||
isToggled = state.daitaEnabled, | ||
onCellClicked = onDaitaEnabled, | ||
) | ||
HorizontalDivider() | ||
HeaderSwitchComposeCell( | ||
title = stringResource(R.string.direct_only), | ||
isToggled = state.directOnly, | ||
isEnabled = state.daitaEnabled, | ||
onCellClicked = onDirectOnly, | ||
onInfoClicked = onDirectOnlyInfoClick, | ||
) | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
private fun DescriptionPager(pagerState: PagerState) { | ||
HorizontalPager( | ||
state = pagerState, | ||
verticalAlignment = Alignment.Top, | ||
beyondViewportPageCount = DAITA_PAGES.entries.size, | ||
) { page -> | ||
Column(modifier = Modifier.fillMaxWidth()) { | ||
val page = DAITA_PAGES.entries[page] | ||
// Scale image to fit width up to certain width | ||
Image( | ||
contentScale = ContentScale.FillWidth, | ||
modifier = | ||
Modifier.widthIn(max = Dimens.settingsDetailsImageMaxWidth) | ||
.fillMaxWidth() | ||
.padding(horizontal = Dimens.mediumPadding) | ||
.align(Alignment.CenterHorizontally), | ||
painter = painterResource(id = page.image), | ||
contentDescription = stringResource(R.string.daita), | ||
) | ||
DescriptionText( | ||
firstParagraph = page.textFirstParagraph, | ||
secondParagraph = page.textSecondParagraph, | ||
thirdParagraph = page.textThirdParagraph, | ||
) | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
private fun DescriptionText(firstParagraph: Int, secondParagraph: Int, thirdParagraph: Int) { | ||
SwitchComposeSubtitleCell( | ||
modifier = Modifier.padding(vertical = Dimens.smallPadding), | ||
text = | ||
buildString { | ||
appendLine(stringResource(firstParagraph)) | ||
appendLine() | ||
appendLine(stringResource(secondParagraph)) | ||
appendLine() | ||
append(stringResource(thirdParagraph)) | ||
}, | ||
) | ||
} | ||
|
||
@Composable | ||
private fun PageIndicator(pagerState: PagerState) { | ||
Row( | ||
Modifier.wrapContentHeight().fillMaxWidth().padding(bottom = Dimens.mediumPadding), | ||
horizontalArrangement = Arrangement.Center, | ||
verticalAlignment = Alignment.Bottom, | ||
) { | ||
repeat(pagerState.pageCount) { iteration -> | ||
val color = | ||
if (pagerState.currentPage == iteration) MaterialTheme.colorScheme.onPrimary | ||
else MaterialTheme.colorScheme.primary | ||
Box( | ||
modifier = | ||
Modifier.padding(Dimens.indicatorPadding) | ||
.clip(CircleShape) | ||
.background(color) | ||
.size(Dimens.indicatorSize) | ||
) | ||
} | ||
} | ||
} | ||
|
||
private enum class DAITA_PAGES( | ||
val image: Int, | ||
val textFirstParagraph: Int, | ||
val textSecondParagraph: Int, | ||
val textThirdParagraph: Int, | ||
) { | ||
FIRST( | ||
image = R.drawable.daita_illustration_1, | ||
textFirstParagraph = R.string.daita_description_slide_1_first_paragraph, | ||
textSecondParagraph = R.string.daita_description_slide_1_second_paragraph, | ||
textThirdParagraph = R.string.daita_description_slide_1_third_paragraph, | ||
), | ||
SECOND( | ||
image = R.drawable.daita_illustration_2, | ||
textFirstParagraph = R.string.daita_description_slide_2_first_paragraph, | ||
textSecondParagraph = R.string.daita_description_slide_2_second_paragraph, | ||
textThirdParagraph = R.string.daita_description_slide_2_third_paragraph, | ||
), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.