Skip to content

Commit

Permalink
Fix widget coloring saving
Browse files Browse the repository at this point in the history
  • Loading branch information
w2sv committed Apr 17, 2024
1 parent 61d98db commit 9d5eae7
Show file tree
Hide file tree
Showing 23 changed files with 252 additions and 211 deletions.
15 changes: 1 addition & 14 deletions .idea/deploymentTargetDropDown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@ android {
"${versionName}.apk"
}
}

kotlinOptions {
freeCompilerArgs += listOf(
// Apply compose_compiler_config.conf
"-P",
"plugin:androidx.compose.compiler.plugins.kotlin:stabilityConfigurationPath=${project.rootDir.absolutePath}/compose_compiler_config.conf",
// Enable strong skipping
"-P",
"plugin:androidx.compose.compiler.plugins.kotlin:experimentalStrongSkipping=true"
)
}
}

// https://github.com/Triple-T/gradle-play-publisher
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ import com.w2sv.wifiwidget.ui.screens.home.components.widget.configurationdialog
import com.w2sv.wifiwidget.ui.screens.home.components.widget.configurationdialog.components.ColorPickerProperties
import com.w2sv.wifiwidget.ui.screens.home.components.widget.configurationdialog.components.PropertyInfoDialog
import com.w2sv.wifiwidget.ui.screens.home.components.widget.configurationdialog.model.InfoDialogData
import com.w2sv.wifiwidget.ui.screens.home.components.widget.configurationdialog.model.UnconfirmedWidgetConfiguration
import com.w2sv.wifiwidget.ui.screens.home.components.widget.configurationdialog.model.ReversibleWidgetConfiguration
import com.w2sv.wifiwidget.ui.utils.isLandscapeModeActivated
import com.w2sv.wifiwidget.ui.utils.thenIf
import kotlinx.coroutines.flow.update

@Composable
fun WidgetConfigurationDialog(
locationAccessState: LocationAccessState,
widgetConfiguration: UnconfirmedWidgetConfiguration,
widgetConfiguration: ReversibleWidgetConfiguration,
closeDialog: () -> Unit,
modifier: Modifier = Modifier,
) {
Expand Down Expand Up @@ -82,8 +83,14 @@ fun WidgetConfigurationDialog(
properties = properties,
applyColor = remember {
{
widgetConfiguration.customColoringData.value =
properties.createCustomColoringData(widgetConfiguration.customColoringData.value)
widgetConfiguration.coloringConfig.update {
it.copy(
custom = properties.createCustomColoringData(
it.custom
)
)
}

}
},
onDismissRequest = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import com.w2sv.wifiwidget.ui.screens.home.components.widget.configurationdialog
import com.w2sv.wifiwidget.ui.screens.home.components.widget.configurationdialog.components.PropertyCheckRowColumn
import com.w2sv.wifiwidget.ui.screens.home.components.widget.configurationdialog.model.InfoDialogData
import com.w2sv.wifiwidget.ui.screens.home.components.widget.configurationdialog.model.PropertyCheckRowData
import com.w2sv.wifiwidget.ui.screens.home.components.widget.configurationdialog.model.UnconfirmedWidgetConfiguration
import com.w2sv.wifiwidget.ui.screens.home.components.widget.configurationdialog.model.ReversibleWidgetConfiguration
import com.w2sv.wifiwidget.ui.screens.home.components.widget.configurationdialog.model.getInfoDialogData
import com.w2sv.wifiwidget.ui.utils.ShakeConfig
import com.w2sv.wifiwidget.ui.utils.ShakeController
Expand All @@ -51,7 +51,7 @@ private data class Section(

@Composable
fun WidgetConfigurationDialogContent(
widgetConfiguration: UnconfirmedWidgetConfiguration,
widgetConfiguration: ReversibleWidgetConfiguration,
locationAccessState: LocationAccessState,
showPropertyInfoDialog: (InfoDialogData) -> Unit,
showCustomColorConfigurationDialog: (ColorPickerProperties) -> Unit,
Expand All @@ -74,19 +74,22 @@ fun WidgetConfigurationDialogContent(
),
) {
AppearanceConfiguration(
presetColoringData = widgetConfiguration.presetColoringData.collectAsStateWithLifecycle().value,
setPresetColoringData = {
widgetConfiguration.presetColoringData.value = it
coloringConfig = widgetConfiguration.coloringConfig.collectAsStateWithLifecycle().value,
setColoringConfig = remember {
{
widgetConfiguration.coloringConfig.value = it
}
},
customColoringData = widgetConfiguration.customColoringData.collectAsStateWithLifecycle().value,
coloring = widgetConfiguration.coloring.collectAsStateWithLifecycle().value,
setColoring = { widgetConfiguration.coloring.value = it },
opacity = widgetConfiguration.opacity.collectAsStateWithLifecycle().value,
setOpacity = {
widgetConfiguration.opacity.value = it
setOpacity = remember {
{
widgetConfiguration.opacity.value = it
}
},
fontSize = widgetConfiguration.fontSize.collectAsStateWithLifecycle().value,
setFontSize = { widgetConfiguration.fontSize.value = it },
setFontSize = remember {
{ widgetConfiguration.fontSize.value = it }
},
showCustomColorConfigurationDialog = showCustomColorConfigurationDialog,
modifier = Modifier.padding(horizontal = 16.dp)
)
Expand Down Expand Up @@ -177,7 +180,7 @@ fun WidgetConfigurationDialogContent(

@Composable
private fun rememberWidgetWifiPropertyCheckRowData(
widgetConfiguration: UnconfirmedWidgetConfiguration,
widgetConfiguration: ReversibleWidgetConfiguration,
locationAccessState: LocationAccessState,
): ImmutableList<PropertyCheckRowData<WidgetWifiProperty>> {
val context = LocalContext.current
Expand Down Expand Up @@ -283,7 +286,7 @@ private fun rememberWidgetWifiPropertyCheckRowData(
}
}

private fun UnconfirmedWidgetConfiguration.moreThanOnePropertyChecked(): Boolean =
private fun ReversibleWidgetConfiguration.moreThanOnePropertyChecked(): Boolean =
wifiProperties.values.count { it } > 1

private val shakeConfig = ShakeConfig(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,8 @@ private val verticalPadding = 12.dp
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun AppearanceConfiguration(
presetColoringData: WidgetColoring.Data.Preset,
setPresetColoringData: (WidgetColoring.Data.Preset) -> Unit,
customColoringData: WidgetColoring.Data.Custom,
coloring: WidgetColoring,
setColoring: (WidgetColoring) -> Unit,
coloringConfig: WidgetColoring.Config,
setColoringConfig: (WidgetColoring.Config) -> Unit,
opacity: Float,
setOpacity: (Float) -> Unit,
fontSize: FontSize,
Expand All @@ -64,32 +61,37 @@ fun AppearanceConfiguration(
.padding(bottom = verticalPadding)
.align(Alignment.CenterHorizontally)
) {
WidgetColoring.entries.forEach {
coloringConfig.styles.forEachIndexed { i, style ->
SegmentedButton(
selected = it == coloring,
onClick = { setColoring(it) },
selected = style.javaClass == coloringConfig.appliedStyle.javaClass,
onClick = remember(i) {
{ setColoringConfig(coloringConfig.copy(isCustomSelected = style is WidgetColoring.Style.Custom)) }
},
shape = SegmentedButtonDefaults.itemShape(
index = it.ordinal,
count = WidgetColoring.entries.size
index = i,
count = 2
)
) {
Text(text = stringResource(id = it.labelRes))
Text(text = stringResource(id = style.labelRes))
}
}
}

AnimatedContent(targetState = coloring, label = "") {
when (it) {
WidgetColoring.Preset -> {
AnimatedContent(
targetState = coloringConfig.isCustomSelected,
label = ""
) { isCustomStyleSelected ->
when (isCustomStyleSelected) {
false -> {
PresetColoringConfiguration(
data = presetColoringData,
setData = setPresetColoringData
data = coloringConfig.preset,
setData = remember { { setColoringConfig(coloringConfig.copy(preset = it)) } }
)
}

WidgetColoring.Custom -> {
true -> {
CustomColorConfiguration(
data = customColoringData,
data = coloringConfig.custom,
showCustomColorConfigurationDialog = showCustomColorConfigurationDialog
)
}
Expand Down Expand Up @@ -133,8 +135,8 @@ fun AppearanceConfiguration(

@Composable
private fun PresetColoringConfiguration(
data: WidgetColoring.Data.Preset,
setData: (WidgetColoring.Data.Preset) -> Unit,
data: WidgetColoring.Style.Preset,
setData: (WidgetColoring.Style.Preset) -> Unit,
modifier: Modifier = Modifier
) {
Column(modifier = modifier) {
Expand All @@ -159,12 +161,12 @@ private fun PresetColoringConfiguration(
}
}

enum class WidgetColorType(@StringRes val labelRes: Int) {
enum class CustomWidgetColor(@StringRes val labelRes: Int) {
Background(com.w2sv.core.domain.R.string.background),
Primary(com.w2sv.core.domain.R.string.primary),
Secondary(com.w2sv.core.domain.R.string.secondary);

fun getColor(data: WidgetColoring.Data.Custom): Color =
fun getColor(data: WidgetColoring.Style.Custom): Color =
Color(
when (this) {
Background -> data.background
Expand All @@ -176,12 +178,12 @@ enum class WidgetColorType(@StringRes val labelRes: Int) {

@Composable
private fun CustomColorConfiguration(
data: WidgetColoring.Data.Custom,
data: WidgetColoring.Style.Custom,
showCustomColorConfigurationDialog: (ColorPickerProperties) -> Unit,
modifier: Modifier = Modifier,
) {
Column(modifier = modifier, verticalArrangement = Arrangement.spacedBy(8.dp)) {
WidgetColorType.entries
CustomWidgetColor.entries
.forEach { widgetColorType ->
val color = remember(widgetColorType, data) {
widgetColorType.getColor(data)
Expand All @@ -192,7 +194,7 @@ private fun CustomColorConfiguration(
onClick = {
showCustomColorConfigurationDialog(
ColorPickerProperties(
widgetColorType = widgetColorType,
customWidgetColor = widgetColorType,
appliedColor = color
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ import com.w2sv.wifiwidget.ui.utils.nullableListSaver

@Stable
class ColorPickerProperties(
val widgetColorType: WidgetColorType,
val customWidgetColor: CustomWidgetColor,
private val appliedColor: Color,
initialColor: Color
) {
constructor(widgetColorType: WidgetColorType, appliedColor: Color) : this(
widgetColorType = widgetColorType,
constructor(customWidgetColor: CustomWidgetColor, appliedColor: Color) : this(
customWidgetColor = customWidgetColor,
appliedColor = appliedColor,
initialColor = appliedColor
)
Expand All @@ -41,21 +41,21 @@ class ColorPickerProperties(
color != appliedColor
}

fun createCustomColoringData(data: WidgetColoring.Data.Custom): WidgetColoring.Data.Custom =
when (widgetColorType) {
WidgetColorType.Background -> data.copy(background = color.toArgb())
WidgetColorType.Primary -> data.copy(primary = color.toArgb())
WidgetColorType.Secondary -> data.copy(secondary = color.toArgb())
fun createCustomColoringData(data: WidgetColoring.Style.Custom): WidgetColoring.Style.Custom =
when (customWidgetColor) {
CustomWidgetColor.Background -> data.copy(background = color.toArgb())
CustomWidgetColor.Primary -> data.copy(primary = color.toArgb())
CustomWidgetColor.Secondary -> data.copy(secondary = color.toArgb())
}

companion object {
val nullableStateSaver = nullableListSaver<ColorPickerProperties, Any>(
saveNonNull = {
listOf(it.widgetColorType, it.appliedColor.toArgb(), it.color.toArgb())
listOf(it.customWidgetColor, it.appliedColor.toArgb(), it.color.toArgb())
},
restoreNonNull = {
ColorPickerProperties(
widgetColorType = it[0] as WidgetColorType,
customWidgetColor = it[0] as CustomWidgetColor,
appliedColor = Color(it[1] as Int),
initialColor = Color(it[2] as Int)
)
Expand All @@ -82,7 +82,7 @@ fun ColorPickerDialog(
modifier = modifier,
columnModifier = Modifier
.verticalScroll(rememberScrollState()),
title = stringResource(id = properties.widgetColorType.labelRes),
title = stringResource(id = properties.customWidgetColor.labelRes),
applyButtonEnabled = properties.colorsDissimilar
) {
HSVColorPickerCircularWithSliders(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.launch

@Stable
class UnconfirmedWidgetConfiguration(
val coloring: UnconfirmedStateFlow<WidgetColoring>,
val presetColoringData: UnconfirmedStateFlow<WidgetColoring.Data.Preset>,
val customColoringData: UnconfirmedStateFlow<WidgetColoring.Data.Custom>,
class ReversibleWidgetConfiguration(
val coloringConfig: UnconfirmedStateFlow<WidgetColoring.Config>,
val opacity: UnconfirmedStateFlow<Float>,
val fontSize: UnconfirmedStateFlow<FontSize>,
val wifiProperties: UnconfirmedStateMap<WidgetWifiProperty, Boolean>,
Expand All @@ -36,9 +34,7 @@ class UnconfirmedWidgetConfiguration(
onStateSynced: suspend () -> Unit
) : UnconfirmedStatesComposition(
unconfirmedStates = listOf(
coloring,
presetColoringData,
customColoringData,
coloringConfig,
opacity,
fontSize,
wifiProperties,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import com.w2sv.wifiwidget.di.SnackbarVisualsFlow
import com.w2sv.wifiwidget.di.WidgetPinSuccessFlow
import com.w2sv.wifiwidget.ui.designsystem.AppSnackbarVisuals
import com.w2sv.wifiwidget.ui.designsystem.SnackbarKind
import com.w2sv.wifiwidget.ui.screens.home.components.widget.configurationdialog.model.UnconfirmedWidgetConfiguration
import com.w2sv.wifiwidget.ui.screens.home.components.widget.configurationdialog.model.ReversibleWidgetConfiguration
import com.w2sv.wifiwidget.ui.utils.fromStateFlowMap
import dagger.hilt.android.AndroidEntryPoint
import dagger.hilt.android.lifecycle.HiltViewModel
Expand Down Expand Up @@ -83,20 +83,11 @@ class WidgetViewModel @Inject constructor(
val showConfigurationDialogInitially =
savedStateHandle.get<Boolean>(Extra.SHOW_WIDGET_CONFIGURATION_DIALOG) == true

val configuration = UnconfirmedWidgetConfiguration(
coloring = UnconfirmedStateFlow(
coroutineScope = viewModelScope,
dataStoreStateFlow = repository.coloring
),
presetColoringData = UnconfirmedStateFlow(
scope = viewModelScope,
appliedStateFlow = repository.presetColoringData,
syncState = { repository.savePresetColoringData(it) }
),
customColoringData = UnconfirmedStateFlow(
val configuration = ReversibleWidgetConfiguration(
coloringConfig = UnconfirmedStateFlow(
scope = viewModelScope,
appliedStateFlow = repository.customColoringData,
syncState = { repository.saveCustomColoringData(it) }
appliedStateFlow = repository.coloringConfig,
syncState = { repository.saveColoringConfig(it) }
),
opacity = UnconfirmedStateFlow(
coroutineScope = viewModelScope,
Expand Down
23 changes: 23 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,27 @@ plugins {
alias(libs.plugins.play) apply false
alias(libs.plugins.androidTest) apply false
alias(libs.plugins.baselineprofile) apply false
}

val Task.absoluteBuildDirectoryPath: String
get() = project.layout.buildDirectory.get().asFile.absolutePath

subprojects {
// Enable compose compiler reports as per https://developer.android.com/develop/ui/compose/performance/stability/diagnose#setup
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>().configureEach {
kotlinOptions {
if (project.findProperty("composeCompilerReports") == "true") {
freeCompilerArgs += listOf(
"-P",
"plugin:androidx.compose.compiler.plugins.kotlin:reportsDestination=$absoluteBuildDirectoryPath/compose_compiler"
)
}
if (project.findProperty("composeCompilerMetrics") == "true") {
freeCompilerArgs += listOf(
"-P",
"plugin:androidx.compose.compiler.plugins.kotlin:metricsDestination=$absoluteBuildDirectoryPath/compose_compiler"
)
}
}
}
}
Loading

0 comments on commit 9d5eae7

Please sign in to comment.