Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate out of time view to compose #5175

Merged
merged 6 commits into from
Sep 26, 2023

Conversation

Pururun
Copy link
Contributor

@Pururun Pururun commented Sep 21, 2023

This change is Reviewable

@Pururun Pururun requested review from Rawa and sabercodic September 21, 2023 08:41
@linear
Copy link

linear bot commented Sep 21, 2023

@Pururun Pururun added the Android Issues related to Android label Sep 21, 2023
@Pururun Pururun force-pushed the migrate-out-of-time-view-to-compose-droid-55 branch 5 times, most recently from 06f3749 to c9f18a4 Compare September 21, 2023 12:06
Copy link
Contributor

@Rawa Rawa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 0 of 18 files reviewed, 4 unresolved discussions (waiting on @Pururun)


android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/button/ExternalActionButton.kt line 46 at r2 (raw file):

        isEnabled = isEnabled,
    ) {
        Box(modifier = Modifier.fillMaxSize()) {

If we use box layout here we need to account for the Image afterwards, I believe if there is a slim screen or a long button text it will overlap with the image. Since we know the size of the image I guess we could compensate for it in a way like this, another alternative would possibly be to use a constraintlayout:

    ActionButton(
        onClick = onClick,
        colors = colors,
        modifier = modifier,
        isEnabled = isEnabled,
    ) {
        Text(
            text = text,
            textAlign = TextAlign.Center,
            style = MaterialTheme.typography.bodyMedium,
            maxLines = 1,
            overflow = TextOverflow.Ellipsis,
            modifier = Modifier.padding(start = 24.dp).weight(1f)
        )
        Image(
            painter = painterResource(id = R.drawable.icon_extlink),
            contentDescription = null,
            modifier =
                Modifier.padding(horizontal = Dimens.smallPadding)
                    .alpha(if (isEnabled) AlphaVisible else AlphaDisabled)
        )
    }

android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/screen/OutOfTimeScreen.kt line 103 at r2 (raw file):

            when (viewAction) {
                is OutOfTimeViewModel.ViewAction.OpenAccountView ->
                    context.openAccountPageInBrowser(viewAction.token)

Is it possible to avoid using context and instead use something more Compose native? Saw this one:
https://stackoverflow.com/a/69103918/1724097


android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/fragment/OutOfTimeFragment.kt line 32 at r2 (raw file):

                    val state = vm.uiState.collectAsState().value
                    OutOfTimeScreen(
                        showSitePayment = BuildTypes.RELEASE != BuildConfig.BUILD_TYPE,

We should adapt this to the IS_PLAY flag @albin-mullvad is looking to add.


android/app/src/main/kotlin/net/mullvad/mullvadvpn/viewmodel/OutOfTimeViewModel.kt line 59 at r2 (raw file):

            accountRepository.accountExpiryState.collectLatest { accountExpiry ->
                accountExpiry.date()?.let { expiry ->
                    val tomorrow = DateTime.now().plusHours(20)

Any reason why we add 20h to the expiry? Can we avoid it? Shouldn't expiry.isAfter(DateTime.now()) be enough?

@Pururun Pururun force-pushed the migrate-out-of-time-view-to-compose-droid-55 branch 2 times, most recently from e6564cf to a77c85b Compare September 21, 2023 14:07
Copy link
Contributor Author

@Pururun Pururun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 0 of 18 files reviewed, 4 unresolved discussions (waiting on @albin-mullvad and @Rawa)


android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/button/ExternalActionButton.kt line 46 at r2 (raw file):

Previously, Rawa (David Göransson) wrote…

If we use box layout here we need to account for the Image afterwards, I believe if there is a slim screen or a long button text it will overlap with the image. Since we know the size of the image I guess we could compensate for it in a way like this, another alternative would possibly be to use a constraintlayout:

    ActionButton(
        onClick = onClick,
        colors = colors,
        modifier = modifier,
        isEnabled = isEnabled,
    ) {
        Text(
            text = text,
            textAlign = TextAlign.Center,
            style = MaterialTheme.typography.bodyMedium,
            maxLines = 1,
            overflow = TextOverflow.Ellipsis,
            modifier = Modifier.padding(start = 24.dp).weight(1f)
        )
        Image(
            painter = painterResource(id = R.drawable.icon_extlink),
            contentDescription = null,
            modifier =
                Modifier.padding(horizontal = Dimens.smallPadding)
                    .alpha(if (isEnabled) AlphaVisible else AlphaDisabled)
        )
    }

Done.


android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/screen/OutOfTimeScreen.kt line 103 at r2 (raw file):

Previously, Rawa (David Göransson) wrote…

Is it possible to avoid using context and instead use something more Compose native? Saw this one:
https://stackoverflow.com/a/69103918/1724097

I tried to use that, but since we we need to get the string from resources at least for me I needed to add some more code that made it kinda not as elegant.

Sort of like this:

val uriHandler \= LocalUriHandler.current  
val accountPath \= stringResource(id = R.string.account\_url) + "?token=%s"  
LaunchedEffect(key1 = Unit) {  
viewActions.collect { viewAction \->  
when (viewAction) {  
is OutOfTimeViewModel.ViewAction.OpenAccountView \->  
uriHandler.openUri(String.format(viewAction.token, accountPath))  
OutOfTimeViewModel.ViewAction.OpenConnectScreen \-> openConnectScreen()  
}  
}  
}

android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/fragment/OutOfTimeFragment.kt line 32 at r2 (raw file):

Previously, Rawa (David Göransson) wrote…

We should adapt this to the IS_PLAY flag @albin-mullvad is looking to add.

Yes, hopefully he will merge that soon. :)


android/app/src/main/kotlin/net/mullvad/mullvadvpn/viewmodel/OutOfTimeViewModel.kt line 59 at r2 (raw file):

Previously, Rawa (David Göransson) wrote…

Any reason why we add 20h to the expiry? Can we avoid it? Shouldn't expiry.isAfter(DateTime.now()) be enough?

The reason we have that is because if the user time is not set correctly, I guess if they are travelling or something, this function can fail pretty badly. It was set to 24 hours before but that was causing issues with one-day vouchers so I changed it to 20 instead.

The long term plan is to do something similar to what we have on desktop, where it needs to changed from the default value, but I would probably do that in another PR.

@Pururun Pururun force-pushed the migrate-out-of-time-view-to-compose-droid-55 branch 2 times, most recently from a9a1cb2 to 7d98235 Compare September 21, 2023 17:57
Copy link
Contributor Author

@Pururun Pururun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 0 of 18 files reviewed, 4 unresolved discussions (waiting on @albin-mullvad and @Rawa)


android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/fragment/OutOfTimeFragment.kt line 32 at r2 (raw file):

Previously, Pururun (Jonatan Rhodin) wrote…

Yes, hopefully he will merge that soon. :)

Done

Copy link
Contributor

@Rawa Rawa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! 👍

Reviewed 5 of 18 files at r1.
Reviewable status: 3 of 18 files reviewed, 2 unresolved discussions (waiting on @Pururun)


android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/screen/OutOfTimeScreen.kt line 103 at r2 (raw file):

Previously, Pururun (Jonatan Rhodin) wrote…

I tried to use that, but since we we need to get the string from resources at least for me I needed to add some more code that made it kinda not as elegant.

Sort of like this:

val uriHandler \= LocalUriHandler.current  
val accountPath \= stringResource(id = R.string.account\_url) + "?token=%s"  
LaunchedEffect(key1 = Unit) {  
viewActions.collect { viewAction \->  
when (viewAction) {  
is OutOfTimeViewModel.ViewAction.OpenAccountView \->  
uriHandler.openUri(String.format(viewAction.token, accountPath))  
OutOfTimeViewModel.ViewAction.OpenConnectScreen \-> openConnectScreen()  
}  
}  
}

Yeah I see what you mean, maybe we break some of it out as a lambda? I Believe this should work:

@Composable
fun createLaunchUrlHook(uri: String): () -> Unit {
    val uriHandler = LocalUriHandler.current
    return { uriHandler.openUri(uri) }
}

@Composable
private fun MyScreenWithLaunch(uri: String) {
    val launchUrlHook = createLaunchUrlHook(uri = uri)
    LaunchedEffect(Unit) {
        launchUrlHook()
    }
}

It is not the nicest but it would get rid of the context.


android/app/src/main/kotlin/net/mullvad/mullvadvpn/viewmodel/OutOfTimeViewModel.kt line 59 at r2 (raw file):

Previously, Pururun (Jonatan Rhodin) wrote…

The reason we have that is because if the user time is not set correctly, I guess if they are travelling or something, this function can fail pretty badly. It was set to 24 hours before but that was causing issues with one-day vouchers so I changed it to 20 instead.

The long term plan is to do something similar to what we have on desktop, where it needs to changed from the default value, but I would probably do that in another PR.

I see, yeah sounds like another PR.

Copy link
Contributor

@sabercodic sabercodic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 11 of 18 files at r1, 1 of 1 files at r2, 2 of 2 files at r3, 1 of 1 files at r4, all commit messages.
Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @Pururun)

@Pururun Pururun force-pushed the migrate-out-of-time-view-to-compose-droid-55 branch from 7d98235 to 5e2e066 Compare September 22, 2023 08:38
Copy link
Collaborator

@albin-mullvad albin-mullvad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @Pururun)


android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/fragment/OutOfTimeFragment.kt line 32 at r2 (raw file):

Previously, Pururun (Jonatan Rhodin) wrote…

Done

👍

@Pururun Pururun force-pushed the migrate-out-of-time-view-to-compose-droid-55 branch from 5e2e066 to 1ca9f4c Compare September 22, 2023 09:09
Copy link
Contributor Author

@Pururun Pururun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 17 of 19 files reviewed, 2 unresolved discussions (waiting on @Rawa and @sabercodic)


android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/screen/OutOfTimeScreen.kt line 103 at r2 (raw file):

Previously, Rawa (David Göransson) wrote…

Yeah I see what you mean, maybe we break some of it out as a lambda? I Believe this should work:

@Composable
fun createLaunchUrlHook(uri: String): () -> Unit {
    val uriHandler = LocalUriHandler.current
    return { uriHandler.openUri(uri) }
}

@Composable
private fun MyScreenWithLaunch(uri: String) {
    val launchUrlHook = createLaunchUrlHook(uri = uri)
    LaunchedEffect(Unit) {
        launchUrlHook()
    }
}

It is not the nicest but it would get rid of the context.

Thanks, I made a solution that at least did ont add a huge amount of code :)

Copy link
Contributor

@Rawa Rawa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

Reviewable status: 17 of 19 files reviewed, all discussions resolved (waiting on @sabercodic)

@Pururun Pururun force-pushed the migrate-out-of-time-view-to-compose-droid-55 branch from 1ca9f4c to f52aa17 Compare September 26, 2023 07:38
@Pururun Pururun merged commit 29bb3f7 into main Sep 26, 2023
14 checks passed
@Pururun Pururun deleted the migrate-out-of-time-view-to-compose-droid-55 branch September 26, 2023 09:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Android Issues related to Android
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants