Skip to content

Commit

Permalink
Use inner function to reduce boilerplate in LookAndFeelActivity
Browse files Browse the repository at this point in the history
  • Loading branch information
twizmwazin committed Jun 5, 2023
1 parent 466195d commit d8ce3bd
Showing 1 changed file with 18 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import com.alorma.compose.settings.storage.base.SettingValueState
import com.alorma.compose.settings.storage.base.rememberFloatSettingState
import com.alorma.compose.settings.storage.base.rememberIntSettingState
import com.alorma.compose.settings.ui.SettingsList
Expand Down Expand Up @@ -48,6 +47,20 @@ fun LookAndFeelActivity(

val snackbarHostState = remember { SnackbarHostState() }

fun updateAppSettings() {
appSettingsViewModel.update(
AppSettings(
id = 1,
viewedChangelog = appSettingsViewModel.appSettings.value?.viewedChangelog ?: 0,
theme = themeState.value,
themeColor = themeColorState.value,
fontSize = fontSizeState.value.toInt(),
postViewMode = postViewModeState.value,
)
)

}

Scaffold(
snackbarHost = { SnackbarHost(snackbarHostState) },
topBar = {
Expand All @@ -68,15 +81,7 @@ fun LookAndFeelActivity(
title = {
Text(text = "Font size: ${fontSizeState.value.toInt()}")
},
onValueChangeFinished = {
updateAppSettings(
appSettingsViewModel,
fontSizeState,
themeState,
themeColorState,
postViewModeState,
)
},
onValueChangeFinished = { updateAppSettings() },
)
SettingsList(
state = themeState,
Expand All @@ -92,13 +97,7 @@ fun LookAndFeelActivity(
},
onItemSelected = { i, _ ->
themeState.value = i
updateAppSettings(
appSettingsViewModel,
fontSizeState,
themeState,
themeColorState,
postViewModeState,
)
updateAppSettings()
},
)
SettingsList(
Expand All @@ -115,13 +114,7 @@ fun LookAndFeelActivity(
},
onItemSelected = { i, _ ->
themeColorState.value = i
updateAppSettings(
appSettingsViewModel,
fontSizeState,
themeState,
themeColorState,
postViewModeState,
)
updateAppSettings()
},
)
SettingsList(
Expand All @@ -138,35 +131,10 @@ fun LookAndFeelActivity(
},
onItemSelected = { i, _ ->
postViewModeState.value = i
updateAppSettings(
appSettingsViewModel,
fontSizeState,
themeState,
themeColorState,
postViewModeState,
)
updateAppSettings()
},
)
}
},
)
}

private fun updateAppSettings(
appSettingsViewModel: AppSettingsViewModel,
fontSizeState: SettingValueState<Float>,
themeState: SettingValueState<Int>,
themeColorState: SettingValueState<Int>,
postViewModeState: SettingValueState<Int>,
) {
appSettingsViewModel.update(
AppSettings(
id = 1,
fontSize = fontSizeState.value.toInt(),
theme = themeState.value,
themeColor = themeColorState.value,
viewedChangelog = appSettingsViewModel.appSettings.value?.viewedChangelog ?: 0,
postViewMode = postViewModeState.value,
),
)
}

0 comments on commit d8ce3bd

Please sign in to comment.