-
Notifications
You must be signed in to change notification settings - Fork 4
Prepare dynamic theme choosing #67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package com.redmadrobot.debug.core.data.storage.model | ||
|
|
||
| import com.redmadrobot.debug.uikit.theme.model.ThemeMode | ||
| import kotlinx.serialization.Serializable | ||
|
|
||
| @Serializable | ||
| internal data class ThemeData( | ||
| val themeMode: ThemeMode = ThemeMode.System, | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package com.redmadrobot.debug.core.data.storage.theme | ||
|
|
||
| import androidx.datastore.core.Serializer | ||
| import com.redmadrobot.debug.core.data.storage.model.ThemeData | ||
| import kotlinx.serialization.ExperimentalSerializationApi | ||
| import kotlinx.serialization.json.Json | ||
| import kotlinx.serialization.json.decodeFromStream | ||
| import kotlinx.serialization.json.encodeToStream | ||
| import java.io.InputStream | ||
| import java.io.OutputStream | ||
|
|
||
| @OptIn(ExperimentalSerializationApi::class) | ||
| internal object ThemeDataSerializer : Serializer<ThemeData> { | ||
| override val defaultValue: ThemeData = ThemeData() | ||
|
|
||
| override suspend fun readFrom(input: InputStream): ThemeData { | ||
| return Json.decodeFromStream(input) | ||
| } | ||
|
|
||
| override suspend fun writeTo(themeData: ThemeData, output: OutputStream) { | ||
| Json.encodeToStream(themeData, output) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package com.redmadrobot.debug.core.data.storage.theme | ||
|
|
||
| import android.content.Context | ||
| import com.redmadrobot.debug.uikit.theme.model.ThemeMode | ||
| import kotlinx.coroutines.flow.Flow | ||
| import kotlinx.coroutines.flow.first | ||
| import kotlinx.coroutines.flow.map | ||
| import kotlinx.coroutines.runBlocking | ||
|
|
||
| internal class ThemeDataStore(private val context: Context) { | ||
| private val dataStore by lazy { context.themeStorage } | ||
|
|
||
| fun getSelectedTheme(): ThemeMode { | ||
| return runBlocking { dataStore.data.first().themeMode } | ||
| } | ||
|
|
||
| fun observeThemeMode(): Flow<ThemeMode> { | ||
| return dataStore.data.map { it.themeMode } | ||
| } | ||
|
|
||
| suspend fun saveThemeMode(mode: ThemeMode) { | ||
| dataStore.updateData { data -> data.copy(themeMode = mode) } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package com.redmadrobot.debug.core.data.storage.theme | ||
|
|
||
| import android.content.Context | ||
| import androidx.datastore.dataStore | ||
|
|
||
| internal val Context.themeStorage by dataStore( | ||
| fileName = "debug_panel_theme.json", | ||
| serializer = ThemeDataSerializer, | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| package com.redmadrobot.debug.uikit.components | ||
|
|
||
| import androidx.compose.foundation.layout.size | ||
| import androidx.compose.material3.DropdownMenu | ||
| import androidx.compose.material3.DropdownMenuItem | ||
| import androidx.compose.material3.Icon | ||
| import androidx.compose.material3.IconButton | ||
| import androidx.compose.material3.Text | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.runtime.getValue | ||
| import androidx.compose.runtime.key | ||
| import androidx.compose.runtime.mutableStateOf | ||
| import androidx.compose.runtime.remember | ||
| import androidx.compose.runtime.setValue | ||
| import androidx.compose.ui.Modifier | ||
| import androidx.compose.ui.res.painterResource | ||
| import androidx.compose.ui.res.stringResource | ||
| import androidx.compose.ui.unit.dp | ||
| import com.redmadrobot.debug.uikit.theme.DebugPanelTheme | ||
| import com.redmadrobot.debug.uikit.theme.model.ThemeMode | ||
|
|
||
| @Composable | ||
| public fun ThemeSwitcher( | ||
| currentMode: ThemeMode, | ||
| onModeSelect: (ThemeMode) -> Unit, | ||
| modifier: Modifier = Modifier, | ||
| ) { | ||
| var expanded by remember { mutableStateOf(false) } | ||
|
|
||
| IconButton(onClick = { expanded = true }, modifier = modifier) { | ||
| Icon( | ||
| painter = painterResource(id = ThemeMode.getIconRes(mode = currentMode)), | ||
| contentDescription = null, | ||
| tint = DebugPanelTheme.colors.content.secondary, | ||
| modifier = Modifier.size(size = 20.dp), | ||
| ) | ||
| DropdownMenu( | ||
| expanded = expanded, | ||
| onDismissRequest = { expanded = false }, | ||
| ) { | ||
| ThemeMode.entries.forEach { mode -> | ||
| key(mode.name) { | ||
| DropdownMenuItem( | ||
| title = stringResource(id = ThemeMode.getTitleRes(mode = mode)), | ||
| onModeSelect = { | ||
| onModeSelect.invoke(mode) | ||
| expanded = false | ||
| } | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Composable | ||
| private fun DropdownMenuItem( | ||
| title: String, | ||
| onModeSelect: () -> Unit | ||
| ) { | ||
| DropdownMenuItem( | ||
| text = { Text(text = title, style = DebugPanelTheme.typography.bodyMedium) }, | ||
| onClick = { onModeSelect() }, | ||
| ) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package com.redmadrobot.debug.uikit.theme | ||
|
|
||
| import androidx.compose.ui.unit.Dp | ||
| import androidx.compose.ui.unit.dp | ||
|
|
||
| public object DebugPanelDimensions { | ||
| // Common | ||
| public val topBarHeight: Dp = 56.dp | ||
| public val tabRowHeight: Dp = 48.dp | ||
| public val bottomBarHeight: Dp = 56.dp | ||
| public val rowMinHeight: Dp = 48.dp | ||
|
|
||
| // Toggles | ||
| public val toggleWidth: Dp = 44.dp | ||
| public val toggleHeight: Dp = 24.dp | ||
| public val dotSize: Dp = 10.dp | ||
|
|
||
| // Icons | ||
| public val iconSizeSmall: Dp = 20.dp | ||
| public val iconSizeMedium: Dp = 24.dp | ||
| public val iconSizeLarge: Dp = 32.dp | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package com.redmadrobot.debug.uikit.theme | ||
|
|
||
| import androidx.compose.foundation.shape.RoundedCornerShape | ||
| import androidx.compose.ui.unit.dp | ||
|
|
||
| public object DebugPanelShapes { | ||
| public val small: RoundedCornerShape = RoundedCornerShape(4.dp) | ||
| public val medium: RoundedCornerShape = RoundedCornerShape(8.dp) | ||
| public val large: RoundedCornerShape = RoundedCornerShape(20.dp) | ||
| public val dialog: RoundedCornerShape = RoundedCornerShape(28.dp) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package com.redmadrobot.debug.uikit.theme | ||
|
|
||
| import androidx.compose.ui.unit.Dp | ||
| import androidx.compose.ui.unit.dp | ||
|
|
||
| public object DebugPanelSpacing { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Кажется излишне
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Согласен, выглядит спорно, но я бы пока что оставил, общую картину посмотреть, потом если что можно дропнуть
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Потом можно и забыть) |
||
| public val xxs: Dp = 2.dp | ||
| public val xs: Dp = 4.dp | ||
| public val sm: Dp = 8.dp | ||
| public val md: Dp = 12.dp | ||
| public val lg: Dp = 16.dp | ||
| public val xl: Dp = 20.dp | ||
| public val xxl: Dp = 24.dp | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.