OXYGENHUB
◆ v0.1.7 oxygenhub-android / app / src / main / java / io / oxygenhub / android / ui / theme / Appearance.kt
1.3 KiB · last changed Panels you can see: the hub's palette by default, clear surface steps and a 1 dp edge on every panel (closes #24) 7 days ago source blame history raw
1
package io.oxygenhub.android.ui.theme
2
3
import android.content.Context
4
import androidx.compose.runtime.Composable
5
import androidx.compose.runtime.collectAsState
6
import androidx.compose.runtime.getValue
7
import androidx.compose.ui.platform.LocalContext
8
import kotlinx.coroutines.flow.MutableStateFlow
9
import kotlinx.coroutines.flow.StateFlow
10
import kotlinx.coroutines.flow.asStateFlow
11
12
/**
13
 * Whether the app takes its colours from the wallpaper (#24). Off by
14
 * default: the hub's own palette, as oxygenhub.io and its backdrops have.
15
 */
16
object Appearance {
17
    private const val PREFS = "appearance"
18
    private const val WALLPAPER = "wallpaper_colours"
19
    private var state: MutableStateFlow<Boolean>? = null
20
21
    fun wallpaperColours(context: Context): StateFlow<Boolean> =
22
        (state ?: MutableStateFlow(prefs(context).getBoolean(WALLPAPER, false)).also { state = it }).asStateFlow()
23
24
    fun setWallpaperColours(context: Context, on: Boolean) {
25
        prefs(context).edit().putBoolean(WALLPAPER, on).apply()
26
        wallpaperColours(context)
27
        state?.value = on
28
    }
29
30
    private fun prefs(context: Context) = context.applicationContext.getSharedPreferences(PREFS, Context.MODE_PRIVATE)
31
}
32
33
@Composable
34
fun rememberWallpaperColours(): Boolean {
35
    val on by Appearance.wallpaperColours(LocalContext.current).collectAsState()
36
    return on
37
}