79159334

Date: 2024-11-05 13:44:21
Score: 2.5
Natty:
Report link

Did you find a solution to your problem? I'm using CompositionLocal (https://developer.android.com/develop/ui/compose/compositionlocal#creating)

Something like so:

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()
                }
            }
        }
    }
}

Then value can be use in the hierarchy:

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

        Text(some)
    }
Reasons:
  • RegEx Blacklisted phrase (3): Did you find a solution to your problem
  • Probably link only (1):
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Starts with a question (0.5): Did you find a solution to you
  • High reputation (-1):
Posted by: Friesgaard