6

I'm making an implementation of PreferencesScreen in Compose and I've made all the components like PreferencesSwitch, CheckBox, etc.
Now I'm wondering if there is any way to make it so all the components can only be used inside the scope of the PreferencesScreen function and cannot be used outside of it.
Like for example, in LazyColumn, items can only be used inside LazyColumnScope. I looked at the implementation of it but it used the annotation @LazyScopeMarker so I'm assuming there's different markers for different scopes?

Expected Behaviour:

PreferencesScreen{
    PreferencesCheckBox(...){ ... }
}

is possible but,

PreferencesCheckBox(...){ ... }

alone is not possible.

2 Answers 2

11

You can declare some scope same like LazyColumn does:

interface PreferencesScreenScope {
    @Composable
    fun PreferencesCheckBox()
}

private class PreferencesScreenScopeImpl: PreferencesScreenScope {
    @Composable
    override fun PreferencesCheckBox() {

    }
}

interface/class ...Impl is used here to make sure that no other screen can reuse PreferencesScreenScopeImpl, also it adds testing possibility.

Use it in PreferencesScreen:

@Composable
fun PreferencesScreen(content: @Composable PreferencesScreenScope.() -> Unit ) {
    PreferencesScreenScopeImpl().content()
}

Use PreferencesScreen like this:

PreferencesScreen {
    PreferencesCheckBox()
}
3
  • This implementation doesn't restrict which composables are called inside your content lambda.
    – AouledIssa
    Jul 27 at 10:13
  • @AouledIssa I'm not sure it's possible restrict, do you know any system view examples that does it? Jul 27 at 10:37
  • One of the possible ways I see is to use either wrapper classes around your composables or to use custom scopes with functions that call your composables.
    – AouledIssa
    Jul 27 at 21:47
0

I wanted to add functions to LazyListScope, but only make them available under Preferences. The simplest way I found is to use an interface as a flag and delegate the implementation of LazyListScope.

@Composable
fun Preferences(
    modifier: Modifier = Modifier,
    dataStore: DataStore<Preferences> = LocalContext.current.settingsDataStore,
    content: PreferencesScope.() -> Unit
) {
    PreferencesProvider(dataStore = dataStore) {
        LazyColumn(modifier = modifier) {
            content(PreferencesScopeImpl(this))
        }
    }
}

interface PreferencesScope : LazyListScope
class PreferencesScopeImpl(lazyListScope: LazyListScope): PreferencesScope, LazyListScope by lazyListScope

Now I can write extension functions using PreferencesScope.

fun PreferencesScope.preferenceItem(
    modifier: Modifier = Modifier,
    title: String,
    description: String? = null,
    onClick: () -> Unit = {}
) {
    item {
        PreferenceItem(
            modifier = modifier,
            title = title,
            description = description,
            onClick = onClick
        )
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.