Add MainSwitchPreference component

This commit is contained in:
MM20
2026-06-26 13:01:47 +02:00
parent 813e888fa8
commit b1b2daa67d
2 changed files with 77 additions and 25 deletions

View File

@@ -0,0 +1,53 @@
package de.mm20.launcher2.ui.component.preferences
import androidx.compose.animation.animateColorAsState
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import de.mm20.launcher2.ui.ktx.animateShapeAsState
@Composable
fun MainSwitchPreference(
title: String,
value: Boolean,
onValueChanged: (Boolean) -> Unit,
enabled: Boolean = true,
) {
val shape by animateShapeAsState(
if (value) MaterialTheme.shapes.small else MaterialTheme.shapes.large,
MaterialTheme.motionScheme.fastSpatialSpec(),
)
val backgroundColor by animateColorAsState(
if (value) MaterialTheme.colorScheme.primaryContainer else MaterialTheme.colorScheme.secondaryContainer,
MaterialTheme.motionScheme.fastEffectsSpec(),
)
val contentColor by animateColorAsState(
if (value) MaterialTheme.colorScheme.onPrimaryContainer else MaterialTheme.colorScheme.onSecondaryContainer,
MaterialTheme.motionScheme.fastEffectsSpec(),
)
Box(
modifier = Modifier
.clip(shape)
.background(backgroundColor, shape)
) {
CompositionLocalProvider(
LocalContentColor provides contentColor
) {
SwitchPreference(
title = title,
value = value,
onValueChanged = onValueChanged,
enabled = enabled,
containerColor = Color.Transparent,
)
}
}
}

View File

@@ -46,6 +46,7 @@ import de.mm20.launcher2.ktx.sendWithBackgroundPermission
import de.mm20.launcher2.plugin.PluginState
import de.mm20.launcher2.ui.R
import de.mm20.launcher2.ui.component.preferences.GuardedPreference
import de.mm20.launcher2.ui.component.preferences.MainSwitchPreference
import de.mm20.launcher2.ui.component.preferences.Preference
import de.mm20.launcher2.ui.component.preferences.PreferenceCategory
import de.mm20.launcher2.ui.component.preferences.PreferenceWithSwitch
@@ -56,7 +57,7 @@ import de.mm20.launcher2.ui.settings.weather.WeatherIntegrationSettingsRoute
import kotlinx.serialization.Serializable
@Serializable
data class PluginSettingsRoute(val pluginId: String): NavKey
data class PluginSettingsRoute(val pluginId: String) : NavKey
@Composable
fun PluginSettingsScreen(pluginId: String) {
@@ -262,27 +263,23 @@ fun PluginSettingsScreen(pluginId: String) {
modifier = Modifier.padding(horizontal = 12.dp),
verticalArrangement = Arrangement.spacedBy(12.dp),
) {
PreferenceCategory {
SwitchPreference(
enabled = pluginPackage != null && hasPermission != null,
iconPadding = false,
title = stringResource(R.string.preference_plugin_enable),
value = pluginPackage?.enabled == true && hasPermission == true,
onValueChanged = {
if (hasPermission == true) {
viewModel.setPluginEnabled(it)
} else {
requestPermissionStarter.launch(
Intent().apply {
`package` = pluginPackage?.packageName
action = "de.mm20.launcher2.plugin.REQUEST_PERMISSION"
}
)
}
},
containerColor = MaterialTheme.colorScheme.secondaryContainer
)
}
MainSwitchPreference(
enabled = pluginPackage != null && hasPermission != null,
title = stringResource(R.string.preference_plugin_enable),
value = pluginPackage?.enabled == true && hasPermission == true,
onValueChanged = {
if (hasPermission == true) {
viewModel.setPluginEnabled(it)
} else {
requestPermissionStarter.launch(
Intent().apply {
`package` = pluginPackage?.packageName
action = "de.mm20.launcher2.plugin.REQUEST_PERMISSION"
}
)
}
},
)
AnimatedVisibility(pluginPackage?.enabled == true && hasPermission == true) {
Column(
modifier = Modifier.verticalScroll(rememberScrollState())
@@ -526,9 +523,11 @@ fun PluginSettingsScreen(pluginId: String) {
},
iconPadding = false,
onClick = {
backStack.add(CalendarProviderSettingsRoute(
providerId = plugin.plugin.authority
))
backStack.add(
CalendarProviderSettingsRoute(
providerId = plugin.plugin.authority
)
)
}
)
}