Skip to content

Commit

Permalink
Remove usage of activty to show toast
Browse files Browse the repository at this point in the history
  • Loading branch information
Rawa committed Nov 10, 2023
1 parent 6858a66 commit 4dda5b4
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,25 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment.Companion.CenterVertically
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import net.mullvad.mullvadvpn.R
import net.mullvad.mullvadvpn.compose.button.AnimatedIconButton
import net.mullvad.mullvadvpn.lib.common.util.SdkUtils
import net.mullvad.mullvadvpn.lib.theme.AppTheme
import net.mullvad.mullvadvpn.ui.extension.copyToClipboard

@Preview
@Composable
private fun PreviewCopyableObfuscationView() {
AppTheme { CopyableObfuscationView("1111222233334444", modifier = Modifier.fillMaxWidth()) }
AppTheme { CopyableObfuscationView("1111222233334444", {}, modifier = Modifier.fillMaxWidth()) }
}

@Composable
fun CopyableObfuscationView(content: String, modifier: Modifier = Modifier) {
fun CopyableObfuscationView(
content: String,
onCopyClicked: (String) -> Unit,
modifier: Modifier = Modifier
) {
var obfuscationEnabled by remember { mutableStateOf(true) }

Row(verticalAlignment = CenterVertically, modifier = modifier) {
Expand All @@ -45,19 +46,7 @@ fun CopyableObfuscationView(content: String, modifier: Modifier = Modifier) {
onClick = { obfuscationEnabled = !obfuscationEnabled }
)

val context = LocalContext.current
val copy = {
context.copyToClipboard(
content = content,
clipboardLabel = context.getString(R.string.mullvad_account_number)
)
SdkUtils.showCopyToastIfNeeded(
context,
context.getString(R.string.copied_mullvad_account_number)
)
}

CopyAnimatedIconButton(onClick = copy)
CopyAnimatedIconButton(onClick = { onCopyClicked(content) })
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

package net.mullvad.mullvadvpn.compose.screen

import android.os.Build
import androidx.compose.animation.animateContentSize
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
Expand All @@ -14,6 +15,7 @@ import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
Expand All @@ -24,16 +26,19 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalClipboardManager
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.tooling.preview.Preview
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.navigation.DestinationsNavigator
import com.ramcosta.composedestinations.navigation.popUpTo
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.launch
import net.mullvad.mullvadvpn.R
import net.mullvad.mullvadvpn.compose.NavGraphs
import net.mullvad.mullvadvpn.compose.button.ExternalButton
Expand Down Expand Up @@ -94,6 +99,7 @@ fun Account(navigator: DestinationsNavigator) {
popUpTo(NavGraphs.root) { inclusive = true }
}
},
onCopyAccountNumber = vm::onCopyAccountNumber,
onBackClick = { navigator.navigateUp() }
)
}
Expand All @@ -103,6 +109,7 @@ fun Account(navigator: DestinationsNavigator) {
fun AccountScreen(
uiState: AccountUiState,
uiSideEffect: SharedFlow<AccountViewModel.UiSideEffect>,
onCopyAccountNumber: (String) -> Unit = {},
onRedeemVoucherClick: () -> Unit = {},
onManageAccountClick: () -> Unit = {},
onLogoutClick: () -> Unit = {},
Expand All @@ -118,14 +125,25 @@ fun AccountScreen(
}

val context = LocalContext.current
val clipboardManager = LocalClipboardManager.current
val snackbarHostState = remember { SnackbarHostState() }
val copyTextString = stringResource(id = R.string.copied_mullvad_account_number)
LaunchedEffect(Unit) {
uiSideEffect.collect { uiSideEffect ->
when (uiSideEffect) {
AccountViewModel.UiSideEffect.NavigateToLogin -> navigateToLogin()
is AccountViewModel.UiSideEffect.OpenAccountManagementPageInBrowser ->
context.openAccountPageInBrowser(uiSideEffect.token)
is AccountViewModel.UiSideEffect.CopyAccountNumber ->
launch {
clipboardManager.setText(AnnotatedString(uiSideEffect.accountNumber))

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
snackbarHostState.currentSnackbarData?.dismiss()
snackbarHostState.showSnackbar(message = copyTextString)
}
}
}
if (uiSideEffect is AccountViewModel.UiSideEffect.OpenAccountManagementPageInBrowser) {}
}
}

Expand All @@ -140,7 +158,7 @@ fun AccountScreen(
) {
DeviceNameRow(deviceName = uiState.deviceName ?: "") { showDeviceNameInfoDialog = true }

AccountNumberRow(accountNumber = uiState.accountNumber ?: "")
AccountNumberRow(accountNumber = uiState.accountNumber ?: "", onCopyAccountNumber)

PaidUntilRow(accountExpiry = uiState.accountExpiry)

Expand Down Expand Up @@ -192,14 +210,15 @@ private fun DeviceNameRow(deviceName: String, onInfoClick: () -> Unit) {
}

@Composable
private fun AccountNumberRow(accountNumber: String) {
private fun AccountNumberRow(accountNumber: String, onCopyAccountNumber: (String) -> Unit) {
Column(modifier = Modifier.fillMaxWidth()) {
Text(
style = MaterialTheme.typography.labelMedium,
text = stringResource(id = R.string.account_number),
)
CopyableObfuscationView(
content = accountNumber,
onCopyClicked = { onCopyAccountNumber(accountNumber) },
modifier = Modifier.heightIn(min = Dimens.accountRowMinHeight).fillMaxWidth()
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package net.mullvad.mullvadvpn.compose.screen

import android.util.Log
import androidx.compose.animation.animateContentSize
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
Expand All @@ -20,7 +19,6 @@ import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
Expand Down Expand Up @@ -232,17 +230,14 @@ fun VpnSettingsScreen(
val savedCustomPort = rememberSaveable { mutableStateOf<Constraint<Port>>(Constraint.Any()) }

val snackbarHostState = remember { SnackbarHostState() }
val scope = rememberCoroutineScope()
LaunchedEffect(Unit) {
sideEffect.collect {
when (it) {
is VpnSettingsSideEffect.ShowToast -> {
Log.d("VPNSettings", "Show Snackbar!")
scope.launch {
is VpnSettingsSideEffect.ShowToast ->
launch {
snackbarHostState.currentSnackbarData?.dismiss()
snackbarHostState.showSnackbar(message = it.message)
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,16 @@ class AccountViewModel(
viewModelScope.launch { _uiSideEffect.emit(UiSideEffect.NavigateToLogin) }
}

fun onCopyAccountNumber(accountNumber: String) {
viewModelScope.launch { _uiSideEffect.emit(UiSideEffect.CopyAccountNumber(accountNumber)) }
}

sealed class UiSideEffect {
data object NavigateToLogin : UiSideEffect()

data class OpenAccountManagementPageInBrowser(val token: String) : UiSideEffect()

data class CopyAccountNumber(val accountNumber: String) : UiSideEffect()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,4 @@ object SdkUtils {
} else {
@Suppress("DEPRECATION") getInstalledPackages(flags)
}

fun showCopyToastIfNeeded(context: Context, message: String) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
Toast.makeText(context, message, Toast.LENGTH_SHORT).show()
}
}
}

0 comments on commit 4dda5b4

Please sign in to comment.