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