How to restrict access to the outer Lambda method from the inner Lambda when using the Lambda of the Kotlin extension function?

Issue

I am imitating LazyDsl in Jetpack Compose to implement a custom Preference layout.

This is my code:

Preference.kt

@Composable
fun PreferenceContainer(
    /* ... */,
    content: PreferenceScope.() -> Unit
) {
    LazyColumn(
        /* ... */,
        content  PreferenceScope(context).apply(content).getLazyListScope()
    )
}

class PreferenceScope(private val context: Context) {
    private val itemList  mutableListOf<@Composable () -> Unit>()

    fun getLazyListScope(): LazyListScope.() -> Unit  {
        items(itemList) { it() }
    }

    fun category(title: String, items: PreferenceCategoryScope.() -> Unit) {
        /* ... */
    }
}

class PreferenceCategoryScope(private val itemList: MutableList<@Composable () -> Unit>, context: Context) {
    private val sharedPreferences  PreferenceManager.getDefaultSharedPreferences(context)

    fun editPreference( /* ... */ ) {
        /* ... */
    }
}

It can be called like this:

SettingsActivity.kt

PreferenceContainer( /* ... */ ) {
    category( /* ... */ ) {
        editPreference( /* ... */ )
    }
}

But it can also be used like this:

PreferenceContainer( /* ... */ ) {
    category( /* ... */ ) {
        category( /* ... */ ) { }  //This is not what I want: category() is called in category()
    }
}

Therefore, the problem can be embodied as: how to make category() unable to be called by the Lambda parameter in category(). If anyone knows, I would be very grateful, thank you!

Solution

After reminding me in the comments, I found the answer.

The code can be modified like this:

@DslMarker
annotation class PreferenceDsl

@Composable
fun PreferenceContainer(
    /* ... */,
    content: PreferenceScope.() -> Unit
) {
    LazyColumn(
        /* ... */,
        content  PreferenceScope(context).apply(content).getLazyListScope()
    )
}

@PreferenceDsl
class PreferenceScope(private val context: Context) {
    private val itemList  mutableListOf<@Composable () -> Unit>()

    fun getLazyListScope(): LazyListScope.() -> Unit  {
        items(itemList) { it() }
    }

    fun category(title: String, items: PreferenceCategoryScope.() -> Unit) {
        /* ... */
    }
}

@PreferenceDsl
class PreferenceCategoryScope(private val itemList: MutableList<@Composable () -> Unit>, context: Context) {
    private val sharedPreferences  PreferenceManager.getDefaultSharedPreferences(context)

    fun editPreference( /* ... */ ) {
        /* ... */
    }
}

Answered By – LTFan

Leave a Comment