Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a "single-top" MenuHost #40

Merged
merged 2 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ dependencies {
```kotlin
@Composable
fun MyComposable() {
ProvideMenuHost {
ProvideCumulativeMenuHost {
Scaffold(
topBar = {
TopAppBar(
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.boswelja.menuprovider

import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.remember

internal class CumulativeMenuHost : MenuHost {
private val _menuItems = mutableStateListOf<MenuItem>()

override val menuItems: List<MenuItem> = _menuItems

override fun addItems(vararg newItems: MenuItem) {
_menuItems.addAll(newItems)
}

override fun removeItems(vararg items: MenuItem) {
_menuItems.removeAll(items.toSet())
}
}

internal class SingleTopMenuHost : MenuHost {
private val _menuItems = mutableStateListOf<List<MenuItem>>()

override val menuItems: List<MenuItem> = _menuItems.lastOrNull().orEmpty()

override fun addItems(vararg newItems: MenuItem) {
_menuItems.add(newItems.toList())
}

override fun removeItems(vararg items: MenuItem) {
_menuItems.remove(items.toList())
}
}

/**
* Creates a new [MenuHost] for use in Composition. This should be provided via [LocalMenuHost].
*/
@Composable
@Deprecated(
"There are MenuHosts with different behaviors out-of-the-box.\n" +
"For a MenuHost that behaves the same, please switch to rememberCumulativeMenuHost",
replaceWith = ReplaceWith(
expression = "rememberCumulativeMenuHost()"
)
)
public fun rememberMenuHost(): MenuHost {
return rememberCumulativeMenuHost()
}

/**
* Remembers a new [MenuHost] that "accumulates" all menu items provided from direct descendants. In
* other words, every MenuItem provided to this MenuHost will be exposed to [MenuHost.menuItems] at
* once.
*/
@Composable
public fun rememberCumulativeMenuHost(): MenuHost {
return remember {
CumulativeMenuHost()
}
}

/**
* Remembers a new [MenuHost] that only exposes the last set of MenuItems to [MenuHost.menuItems].
* Inx other words, only the last group of MenuItems that were provided will be returned by
* [MenuHost.menuItems].
*/
@Composable
public fun rememberSingleTopMenuHost(): MenuHost {
return remember {
SingleTopMenuHost()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,21 @@ public fun ProvideMenuHost(
content = content,
)
}

/**
* A convenience function for providing a cumulative MenuHost. See [rememberCumulativeMenuHost] for
* details. To provide your own MenuHost, use [ProvideMenuHost].
*/
@Composable
public fun ProvideCumulativeMenuHost(content: @Composable () -> Unit) {
ProvideMenuHost(menuHost = rememberCumulativeMenuHost(), content = content)
}

/**
* A convenience function for providing a single-top MenuHost. See [rememberCumulativeMenuHost] for
* details. To provide your own MenuHost, use [ProvideMenuHost].
*/
@Composable
public fun ProvideSingleTopMenuHost(content: @Composable () -> Unit) {
ProvideMenuHost(menuHost = rememberSingleTopMenuHost(), content = content)
}