Share repeated UI pieces between screens

This commit is contained in:
2026-10-31 11:34:07 +02:00
parent 6c26cb99a6
commit b53aa34516
7 changed files with 70 additions and 76 deletions
@@ -33,7 +33,6 @@ import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Switch
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.TriStateCheckbox
@@ -338,7 +337,7 @@ private fun AppRow(modifier: Modifier, app: AppItem, enabled: Boolean, last: Boo
)
}
Spacer(Modifier.width(12.dp))
Switch(checked = enabled, onCheckedChange = null, colors = appSwitchColors())
AppSwitch(checked = enabled, onCheckedChange = null)
}
}
}
@@ -100,8 +100,7 @@ fun BlocklistScreen(bottomPadding: Dp, onBack: () -> Unit, modifier: Modifier =
// Downloads live in Blocklist, so they keep running and stay visible when the page is closed
val busy by Blocklist.busy.collectAsStateWithLifecycle()
val error by Blocklist.error.collectAsStateWithLifecycle()
val lastError = remember { arrayOf("") } // kept while the card animates out
error?.let { lastError[0] = it }
val shownError = rememberLast(error).orEmpty()
var search by rememberSaveable { mutableStateOf("") }
var domainsOpen by rememberSaveable { mutableStateOf(false) }
@@ -167,7 +166,7 @@ fun BlocklistScreen(bottomPadding: Dp, onBack: () -> Unit, modifier: Modifier =
AnimatedVisibility(visible = error != null, enter = Motion.expandIn, exit = Motion.collapseOut) {
Box(Modifier.padding(top = 12.dp)) {
HintCard(
stringResource(R.string.bl_download_failed, lastError[0]),
stringResource(R.string.bl_download_failed, shownError),
icon = R.drawable.ic_alert,
tint = Palette.Blocked,
)
@@ -434,16 +433,7 @@ private fun BlocklistCard(
overflow = TextOverflow.Ellipsis,
modifier = Modifier.weight(1f),
)
if (onDelete != null) {
IconButton(onClick = onDelete) {
Icon(
painterResource(R.drawable.ic_delete),
contentDescription = stringResource(R.string.delete),
tint = Palette.Fg3,
modifier = Modifier.size(20.dp),
)
}
}
if (onDelete != null) DeleteButton(onDelete)
AppSwitch(checked = checked, onCheckedChange = onCheckedChange, enabled = switchEnabled)
}
Text(
@@ -551,7 +541,7 @@ private fun DomainRow(domain: String, shape: Shape, onDelete: () -> Unit, modifi
.clip(shape)
.background(Palette.Card)
) {
HorizontalDivider(modifier = Modifier.padding(horizontal = 16.dp), color = Palette.Line)
ListDivider()
Row(
modifier = Modifier.padding(start = 16.dp, end = 4.dp),
verticalAlignment = Alignment.CenterVertically,
@@ -564,7 +554,14 @@ private fun DomainRow(domain: String, shape: Shape, onDelete: () -> Unit, modifi
overflow = TextOverflow.Ellipsis,
modifier = Modifier.weight(1f),
)
IconButton(onClick = onDelete) {
DeleteButton(onDelete)
}
}
}
@Composable
private fun DeleteButton(onClick: () -> Unit) {
IconButton(onClick = onClick) {
Icon(
painterResource(R.drawable.ic_delete),
contentDescription = stringResource(R.string.delete),
@@ -572,8 +569,6 @@ private fun DomainRow(domain: String, shape: Shape, onDelete: () -> Unit, modifi
modifier = Modifier.size(20.dp),
)
}
}
}
}
@Composable
@@ -83,7 +83,6 @@ import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import kotlinx.coroutines.delay
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
import java.text.SimpleDateFormat
import java.util.Calendar
@@ -137,16 +136,7 @@ fun HomeScreen(
// Strict Private DNS makes Android ignore the VPN's DNS server; followed while Home is visible
val privateDnsHost by remember { privateDnsHosts(context) }.collectAsStateWithLifecycle(initialValue = null)
// Set by a tap until the VPN state changes or the attempt gives up, so a double tap neither
// starts two connections nor switches the VPN back on right after turning it off
var busy by remember { mutableStateOf(false) }
LaunchedEffect(active) { busy = false }
LaunchedEffect(busy) {
if (busy) {
delay(BUSY_TIMEOUT_MS)
busy = false
}
}
var busy by rememberVpnBusy(active)
val vpnPermission = rememberLauncherForActivityResult(
ActivityResultContracts.StartActivityForResult()
@@ -430,8 +420,7 @@ private fun PowerToggle(on: Boolean, enabled: Boolean, onToggle: () -> Unit) {
@Composable
private fun PrivateDnsWarning(privateDnsHost: String?, prefs: Prefs) {
val context = LocalContext.current
val lastHost = remember { arrayOf("") }
privateDnsHost?.let { lastHost[0] = it }
val shownHost = rememberLast(privateDnsHost).orEmpty()
var dismissedHost by remember { mutableStateOf(prefs.dismissedPrivateDns) }
AnimatedVisibility(
visible = privateDnsHost != null && privateDnsHost != dismissedHost,
@@ -439,15 +428,15 @@ private fun PrivateDnsWarning(privateDnsHost: String?, prefs: Prefs) {
exit = Motion.collapseOut,
) {
HintCard(
text = stringResource(R.string.hint, lastHost[0]),
text = stringResource(R.string.hint, shownHost),
icon = R.drawable.ic_alert,
tint = WarningColor,
action = {
TextButton(onClick = { openNetworkSettings(context) }) { Text(stringResource(R.string.open_settings)) }
},
onDismiss = {
dismissedHost = lastHost[0]
prefs.dismissedPrivateDns = lastHost[0]
dismissedHost = shownHost
prefs.dismissedPrivateDns = shownHost
},
)
}
@@ -29,6 +29,10 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.Saver
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
@@ -38,6 +42,7 @@ import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.distinctUntilChanged
@@ -56,7 +61,22 @@ class MainActivity : ComponentActivity() {
internal class TestResult(val ok: Boolean, val text: String)
// How long a tap on Connect, Disconnect or Switch blocks further taps if the VPN state never changes
internal const val BUSY_TIMEOUT_MS = 5000L
private const val BUSY_TIMEOUT_MS = 5000L
// Set by a tap until the VPN state changes or the attempt gives up, so a double tap neither
// starts two connections nor switches the VPN back on right after turning it off
@Composable
internal fun rememberVpnBusy(active: DohProvider?): MutableState<Boolean> {
val busy = remember { mutableStateOf(false) }
LaunchedEffect(active) { busy.value = false }
LaunchedEffect(busy.value) {
if (busy.value) {
delay(BUSY_TIMEOUT_MS)
busy.value = false
}
}
return busy
}
// Keeps test results across rotation and process death
internal val TestResultSaver = Saver<TestResult?, String>(
@@ -28,7 +28,6 @@ import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
@@ -51,7 +50,6 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
@@ -85,14 +83,7 @@ fun SettingsScreen(
// Picking a server never touches a live connection; switching is an explicit action
val pendingSwitch = active?.let { it != selectedProvider && canConnect } == true
var busy by remember { mutableStateOf(false) }
LaunchedEffect(active) { busy = false }
LaunchedEffect(busy) {
if (busy) {
delay(BUSY_TIMEOUT_MS)
busy = false
}
}
var busy by rememberVpnBusy(active)
fun select(id: String) {
selected = id
@@ -203,9 +194,8 @@ fun SettingsScreen(
Text(stringResource(if (testing) R.string.testing else R.string.test), maxLines = 1)
}
// Remembers the last name so the button keeps its label while it animates out
val lastSwitch = remember { arrayOf("") }
if (pendingSwitch) lastSwitch[0] = selectedProvider.displayName(context)
// The button keeps its label while it animates out
val switchName = rememberLast(if (pendingSwitch) selectedProvider.displayName(context) else null).orEmpty()
AnimatedVisibility(visible = pendingSwitch, modifier = Modifier.weight(1f)) {
Button(
onClick = {
@@ -223,7 +213,7 @@ fun SettingsScreen(
Icon(painterResource(R.drawable.ic_swap), contentDescription = null, modifier = Modifier.size(18.dp))
Spacer(Modifier.width(8.dp))
Text(
stringResource(R.string.switch_to, lastSwitch[0]),
stringResource(R.string.switch_to, switchName),
maxLines = 1,
overflow = TextOverflow.Ellipsis,
)
@@ -296,26 +286,8 @@ private fun ServerRow(
)
Text(subtitle, color = Palette.Fg3, fontSize = 12.sp, maxLines = 1, overflow = TextOverflow.Ellipsis)
}
if (onDonate != null) {
IconButton(onClick = onDonate, modifier = Modifier.size(40.dp)) {
Icon(
painterResource(R.drawable.ic_donate),
contentDescription = stringResource(R.string.donate),
tint = Palette.Fg3,
modifier = Modifier.size(18.dp),
)
}
}
if (onPolicy != null) {
IconButton(onClick = onPolicy, modifier = Modifier.size(40.dp)) {
Icon(
painterResource(R.drawable.ic_policy),
contentDescription = stringResource(R.string.about_privacy_policy),
tint = Palette.Fg3,
modifier = Modifier.size(18.dp),
)
}
}
if (onDonate != null) LinkButton(R.drawable.ic_donate, stringResource(R.string.donate), onDonate)
if (onPolicy != null) LinkButton(R.drawable.ic_policy, stringResource(R.string.about_privacy_policy), onPolicy)
Spacer(Modifier.width(4.dp))
if (selected) {
Icon(
@@ -331,6 +303,14 @@ private fun ServerRow(
}
}
// Small button next to a server that opens one of its pages
@Composable
private fun LinkButton(@DrawableRes icon: Int, description: String, onClick: () -> Unit) {
IconButton(onClick = onClick, modifier = Modifier.size(40.dp)) {
Icon(painterResource(icon), contentDescription = description, tint = Palette.Fg3, modifier = Modifier.size(18.dp))
}
}
@Composable
private fun NavRow(@DrawableRes icon: Int, title: String, subtitle: String, onClick: () -> Unit) {
Row(
@@ -126,7 +126,7 @@ fun AppSwitch(
}
@Composable
fun appSwitchColors() = SwitchDefaults.colors(
private fun appSwitchColors() = SwitchDefaults.colors(
checkedThumbColor = Color.White,
checkedTrackColor = Palette.Accent,
checkedBorderColor = Palette.Accent,
@@ -26,6 +26,7 @@ import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
@@ -109,6 +110,16 @@ fun HintCard(
}
}
private class Last<T>(var value: T?)
// The last non-null value, so a card can keep showing its text while it animates out
@Composable
fun <T : Any> rememberLast(value: T?): T? {
val last = remember { Last<T>(null) }
if (value != null) last.value = value
return last.value
}
// Runs [onChange] when the query log changes, at most once a second and only while the screen
// is started: nothing runs while the app sits in the background
@Composable