Add MainSwitchPreference component
This commit is contained in:
@@ -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,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
)
|
||||
)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user