Skip to content

Commit

Permalink
refactor: update theme switcher module
Browse files Browse the repository at this point in the history
  • Loading branch information
makeevrserg committed Oct 11, 2023
1 parent 1367a8e commit 3f6e5ca
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.makeevrserg.empireprojekt.mobile.features.status.root.DefaultRootStat
import com.makeevrserg.empireprojekt.mobile.features.status.root.RootStatusComponent
import com.makeevrserg.empireprojekt.mobile.features.theme.DefaultThemeSwitcherComponentComponent
import com.makeevrserg.empireprojekt.mobile.features.theme.ThemeSwitcherComponent
import com.makeevrserg.empireprojekt.mobile.features.theme.di.ThemeSwitcherModule
import ru.astrainteractive.klibs.kdi.Provider
import ru.astrainteractive.klibs.kdi.Single
import ru.astrainteractive.klibs.kdi.getValue
Expand Down Expand Up @@ -37,6 +38,7 @@ class RootModuleImpl : RootModule {
}

override val themeSwitcherComponent: Single<ThemeSwitcherComponent> = Single {
DefaultThemeSwitcherComponentComponent(servicesModule.settings.value)
val module = ThemeSwitcherModule.Default(servicesModule.settings.value)
DefaultThemeSwitcherComponentComponent(module)
}
}
Original file line number Diff line number Diff line change
@@ -1,45 +1,35 @@
package com.makeevrserg.empireprojekt.mobile.features.theme

import com.russhwolf.settings.Settings
import com.makeevrserg.empireprojekt.mobile.features.theme.data.model.Theme
import com.makeevrserg.empireprojekt.mobile.features.theme.di.ThemeSwitcherModule
import kotlinx.coroutines.flow.StateFlow
import ru.astrainteractive.klibs.kstorage.StateFlowMutableStorageValue
import ru.astrainteractive.klibs.kdi.Provider
import ru.astrainteractive.klibs.kdi.getValue
import ru.astrainteractive.klibs.mikro.core.util.next

class DefaultThemeSwitcherComponentComponent(
private val settings: Settings
) : ThemeSwitcherComponent {

private val key = "THEME"

private val default = ThemeSwitcherComponent.Theme.DARK

private val themeFlowStorageValue = StateFlowMutableStorageValue(
default = default,
loadSettingsValue = {
val ordinal = settings.getInt(key, ThemeSwitcherComponent.Theme.LIGHT.ordinal)
ThemeSwitcherComponent.Theme.entries.getOrNull(ordinal) ?: default
},
saveSettingsValue = {
settings.putInt(key, it.ordinal)
}
)
themeSwitcherModule: ThemeSwitcherModule
) : ThemeSwitcherComponent, ThemeSwitcherModule by themeSwitcherModule {
private val themeFlowStorageValue by Provider {
themeSwitcherRepository.themeFlowStorageValue
}

override val theme: StateFlow<ThemeSwitcherComponent.Theme> = themeFlowStorageValue.stateFlow
override val theme: StateFlow<Theme> = themeFlowStorageValue.stateFlow

override fun selectDarkTheme() {
themeFlowStorageValue.save(ThemeSwitcherComponent.Theme.DARK)
themeFlowStorageValue.save(Theme.DARK)
}

override fun selectLightTheme() {
themeFlowStorageValue.save(ThemeSwitcherComponent.Theme.LIGHT)
themeFlowStorageValue.save(Theme.LIGHT)
}

override fun selectTheme(theme: ThemeSwitcherComponent.Theme) {
override fun selectTheme(theme: Theme) {
themeFlowStorageValue.save(theme)
}

override fun next() {
val entries = ThemeSwitcherComponent.Theme.entries.toTypedArray()
val entries = Theme.entries.toTypedArray()
val nextTheme = theme.value.next(entries)
selectTheme(nextTheme)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
package com.makeevrserg.empireprojekt.mobile.features.theme

import com.makeevrserg.empireprojekt.mobile.features.theme.data.model.Theme
import kotlinx.coroutines.flow.MutableStateFlow
import ru.astrainteractive.klibs.mikro.core.util.next

class PreviewThemeSwitcherComponent : ThemeSwitcherComponent {
override val theme: MutableStateFlow<ThemeSwitcherComponent.Theme> =
MutableStateFlow(ThemeSwitcherComponent.Theme.LIGHT)
override val theme: MutableStateFlow<Theme> =
MutableStateFlow(Theme.LIGHT)

override fun selectDarkTheme() {
selectTheme(ThemeSwitcherComponent.Theme.DARK)
selectTheme(Theme.DARK)
}

override fun selectLightTheme() {
selectTheme(ThemeSwitcherComponent.Theme.LIGHT)
selectTheme(Theme.LIGHT)
}

override fun selectTheme(theme: ThemeSwitcherComponent.Theme) {
override fun selectTheme(theme: Theme) {
this.theme.value = theme
}

override fun next() {
theme.value.next(ThemeSwitcherComponent.Theme.values()).run(::selectTheme)
theme.value.next(Theme.values()).run(::selectTheme)
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package com.makeevrserg.empireprojekt.mobile.features.theme

import com.makeevrserg.empireprojekt.mobile.features.theme.data.model.Theme
import kotlinx.coroutines.flow.StateFlow

interface ThemeSwitcherComponent {
val theme: StateFlow<Theme>

enum class Theme {
DARK, LIGHT
}

fun selectDarkTheme()
fun selectLightTheme()
fun selectTheme(theme: Theme)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.makeevrserg.empireprojekt.mobile.features.theme.data

import com.makeevrserg.empireprojekt.mobile.features.theme.data.model.Theme
import ru.astrainteractive.klibs.kstorage.api.StateFlowMutableStorageValue

interface ThemeSwitcherRepository {

val themeFlowStorageValue: StateFlowMutableStorageValue<Theme>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.makeevrserg.empireprojekt.mobile.features.theme.data

import com.makeevrserg.empireprojekt.mobile.features.theme.data.model.Theme
import com.russhwolf.settings.Settings
import ru.astrainteractive.klibs.kstorage.StateFlowMutableStorageValue

class ThemeSwitcherRepositoryImpl(
private val settings: Settings
) : ThemeSwitcherRepository {

private val key = "THEME"

private val default = Theme.DARK

override val themeFlowStorageValue = StateFlowMutableStorageValue(
default = default,
loadSettingsValue = {
val ordinal = settings.getInt(key, Theme.LIGHT.ordinal)
Theme.entries.getOrNull(ordinal) ?: default
},
saveSettingsValue = {
settings.putInt(key, it.ordinal)
}
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.makeevrserg.empireprojekt.mobile.features.theme.data.model

enum class Theme {
DARK, LIGHT
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.makeevrserg.empireprojekt.mobile.features.theme.di

import com.makeevrserg.empireprojekt.mobile.features.theme.data.ThemeSwitcherRepository
import com.makeevrserg.empireprojekt.mobile.features.theme.data.ThemeSwitcherRepositoryImpl
import com.russhwolf.settings.Settings
import ru.astrainteractive.klibs.kdi.Single
import ru.astrainteractive.klibs.kdi.getValue

interface ThemeSwitcherModule {

val themeSwitcherRepository: ThemeSwitcherRepository

class Default(settings: Settings) : ThemeSwitcherModule {
override val themeSwitcherRepository: ThemeSwitcherRepository by Single {
ThemeSwitcherRepositoryImpl(settings)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import com.makeevrserg.empireprojekt.mobile.core.ui.theme.AppTheme
import com.makeevrserg.empireprojekt.mobile.core.ui.theme.LocalAppTheme
import com.makeevrserg.empireprojekt.mobile.features.theme.PreviewThemeSwitcherComponent
import com.makeevrserg.empireprojekt.mobile.features.theme.ThemeSwitcherComponent
import com.makeevrserg.empireprojekt.mobile.features.theme.data.model.Theme

fun ThemeSwitcherComponent.Theme.toComposeTheme() = when (this) {
ThemeSwitcherComponent.Theme.DARK -> AppTheme.DefaultDarkTheme
ThemeSwitcherComponent.Theme.LIGHT -> AppTheme.DefaultLightTheme
fun Theme.toComposeTheme() = when (this) {
Theme.DARK -> AppTheme.DefaultDarkTheme
Theme.LIGHT -> AppTheme.DefaultLightTheme
}

@Composable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.makeevrserg.empireprojekt.mobile.features.root.RootComponent
import com.makeevrserg.empireprojekt.mobile.features.root.modal.RootBottomSheetComponent
import com.makeevrserg.empireprojekt.mobile.features.status.root.RootStatusComponent
import com.makeevrserg.empireprojekt.mobile.features.theme.ThemeSwitcherComponent
import com.makeevrserg.empireprojekt.mobile.features.theme.data.model.Theme
import com.makeevrserg.empireprojekt.mobile.features.ui.status.widget.StatusWidget
import com.makeevrserg.empireprojekt.mobile.resources.MR
import ru.astrainteractive.klibs.mikro.core.util.next
Expand Down Expand Up @@ -65,7 +66,7 @@ fun StatusScreen(
.clip(CircleShape)
.clickable {
val nextTheme = themeSwitcherComponent.theme.value.next(
ThemeSwitcherComponent.Theme.values()
Theme.values()
)
themeSwitcherComponent.selectTheme(nextTheme)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,20 @@ import androidx.wear.compose.material.Icon
import androidx.wear.compose.material.Text
import com.makeevrserg.empireprojekt.mobile.core.ui.theme.AppTheme
import com.makeevrserg.empireprojekt.mobile.features.theme.ThemeSwitcherComponent
import com.makeevrserg.empireprojekt.mobile.features.theme.data.model.Theme
import com.makeevrserg.empireprojekt.mobile.wear.features.components.AstraChip

@Composable
fun ThemeChip(themeSwitcherComponent: ThemeSwitcherComponent) {
val theme by themeSwitcherComponent.theme.collectAsState()
val icon = when (theme) {
ThemeSwitcherComponent.Theme.DARK -> Icons.Filled.Bedtime
ThemeSwitcherComponent.Theme.LIGHT -> Icons.Filled.WbSunny
Theme.DARK -> Icons.Filled.Bedtime
Theme.LIGHT -> Icons.Filled.WbSunny
}
val color by animateColorAsState(
targetValue = when (theme) {
ThemeSwitcherComponent.Theme.DARK -> AppTheme.materialColor.onPrimary
ThemeSwitcherComponent.Theme.LIGHT -> AppTheme.materialColor.onPrimary
Theme.DARK -> AppTheme.materialColor.onPrimary
Theme.LIGHT -> AppTheme.materialColor.onPrimary
},
label = "LABEL"
)
Expand Down

0 comments on commit 3f6e5ca

Please sign in to comment.