使用jetpack compose和数据存储实现设置提供程序

问题描述 投票:0回答:1

我想将我的应用程序设置存储在数据存储中,并使其可以从任何地方轻松访问。

例如,拥有一个 ui 设置类别,并能够从任何地方轻松访问和设置它,同时在配置更改时重新组合 UI 的不同部分。

我目前正在使用“compose-remember-preference”库来实现此功能,但每次需要时我都必须复制粘贴 RememberXPreference,并且每次使用它的新可组合项组合/重新组合时,它都会检索首选项-组成。

有没有更好的方法来处理这个问题?

https://github.com/burnoo/compose-remember-preference

这是我如何在两个地方使用它的示例:


@Composable
fun R2DroidTheme(
    content: @Composable () -> Unit
) {

    val isSystemDarkMode = isSystemInDarkTheme()

    var defaultColorTuple = rememberStringPreference(
        keyName = "ui.color_tuple", initialValue = colorTupleToJson(ColorTuples["Latte"]!!), defaultValue = colorTupleToJson(ColorTuples["Latte"]!!)
    )

    val (defaultContrast, setdefaultContrast) = rememberFloatPreference(keyName = "color.contrast_level", initialValue = 0.5f, defaultValue = 0.5f)
    var (darkMode, setDarkMode) = rememberIntPreference(keyName = "ui.dark", initialValue = DarkMode.SYSTEM.value, defaultValue = DarkMode.SYSTEM.value )
    var (amoled, setAmoled) = rememberBooleanPreference(keyName = "ui.amoled", initialValue = false, defaultValue = false)
    var (materialYou, setMaterialYou) = rememberBooleanPreference(keyName = "ui.material_you", initialValue = false, defaultValue = false)
    var (invertColor, setInvertColor) = rememberBooleanPreference(keyName = "color.inverted", initialValue = false, defaultValue = false)



    val state = rememberDynamicThemeState(initialColorTuple = jsonToColorTuple(defaultColorTuple.value))

    var dark by remember { mutableStateOf(isSystemDarkMode) }

    when (darkMode) {
        DarkMode.SYSTEM.value -> dark = isSystemDarkMode
        DarkMode.LIGHT.value -> dark = false
        DarkMode.DARK.value -> dark = true
    }
    MyTheme(
        state = state,
        defaultColorTuple = jsonToColorTuple(defaultColorTuple.value),
        dynamicColor = materialYou,
        amoledMode = amoled,
        isDarkTheme = dark,
        contrastLevel = defaultContrast.toDouble(),
        style = PaletteStyle.TonalSpot,
        isInvertColors = invertColor,
        content = content,
    )
}
@Composable
fun ColorTuplePreference(modifier: Modifier = Modifier, colorTuples: MutableMap<String, ColorTuple> = ColorTuples) {

    val value = rememberStringPreference(
        keyName = "ui.color_tuple", initialValue = colorTupleToJson(
            ColorTuples["Latte"]!!
        ), defaultValue = colorTupleToJson(ColorTuples["Latte"]!!)
    )
    Box(
        contentAlignment = Alignment.Center
    ) {
        
        PreferenceCard(title = "foo", enabled = false, darkenOnDisable = false, modifier = modifier) {
            LazyRow(
                modifier = Modifier,
            ) {
                items(colorTuples.keys.toList()) { key ->
                    val colorTuple = ColorTuples[key]
                    colorTuple?.let {
                        ColorTuplePreview(
                            modifier = Modifier.padding(horizontal = 4.dp),
                            onClick = { value.value = colorTupleToJson(colorTuple) },
                            colorTuple = colorTuple,
                            selected = colorTuple == jsonToColorTuple(value.value)
                        )
                    }
                }
            }
        }
    }
}

android kotlin android-jetpack-compose android-jetpack
1个回答
0
投票

您找到解决问题的方法了吗?我正在使用

CompositionLocal
https://developer.android.com/develop/ui/compose/compositionlocal#creating

像这样:

data class Settings(val some: Int? = null)
internal val LocalSettings = compositionLocalOf { Settings() }

fun Fragment.composeView(content: @Composable () -> Unit): ComposeView {
    val settingsViewModel = get<SettingsViewModel>() // here injected by koin
    settingsViewModel.init() // <- launch and extract values from datastore/flow

    return ComposeView(requireContext()).apply {
        setContent {
            val settings = remember(settingsViewModel.some) { Settings(settingsViewModel.some) }

            CompositionLocalProvider(LocalSettings provides settings) {
                MaterialTheme {
                    content()
                }
            }
        }
    }
}

然后可以在层次结构中使用值:

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ) = composeView {
        val some = LocalSettings.current.some

        Text(some)
    }
© www.soinside.com 2019 - 2024. All rights reserved.