Skip to content

Commit

Permalink
Remove content model and replace with path changed callback
Browse files Browse the repository at this point in the history
For #8
  • Loading branch information
kirill-grouchnikov committed Jan 7, 2022
1 parent 00f4842 commit a61ecd6
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 75 deletions.
Expand Up @@ -48,6 +48,7 @@ import org.pushingpixels.aurora.theming.*
@Composable
fun <T> AuroraBreadcrumbBar(
contentProvider: BreadcrumbBarContentProvider<T>,
onShownPathChanged: ((List<BreadcrumbItem<T>>) -> Unit)? = null,
presentationModel: BreadcrumbBarPresentationModel = BreadcrumbBarPresentationModel(),
modifier: Modifier
) {
Expand All @@ -60,7 +61,6 @@ fun <T> AuroraBreadcrumbBar(
// for the root.
val shownPathChoices: MutableList<List<BreadcrumbItem<T>>> = remember { mutableStateListOf() }

val contentModel = remember { BreadcrumbBarContentModel(shownPath) }
if (!initialized.value) {
LaunchedEffect(null) {
coroutineScope {
Expand Down Expand Up @@ -240,6 +240,8 @@ fun <T> AuroraBreadcrumbBar(
action = {
shownPath.clear()
shownPath.add(rootChoice)
onShownPathChanged?.invoke(shownPath)

scope.launch {
while (shownPathChoices.size > 1) {
shownPathChoices.removeLast()
Expand All @@ -261,6 +263,7 @@ fun <T> AuroraBreadcrumbBar(
action = {
// Clear all entries in the shown path
shownPath.clear()
onShownPathChanged?.invoke(shownPath)
},
secondaryContentModel = rootSecondaryContentModel
)
Expand All @@ -285,6 +288,7 @@ fun <T> AuroraBreadcrumbBar(
shownPath.removeLast()
}
shownPath.add(entryChoice)
onShownPathChanged?.invoke(shownPath)

// And load choices for this entry
scope.launch {
Expand Down Expand Up @@ -312,6 +316,7 @@ fun <T> AuroraBreadcrumbBar(
while (shownPath.size > indexInShownPathChoices) {
shownPath.removeLast()
}
onShownPathChanged?.invoke(shownPath)
},
secondaryContentModel = shownPathEntryMenuContentModel
)
Expand Down
Expand Up @@ -15,13 +15,10 @@
*/
package org.pushingpixels.aurora.component.model

import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.remember
import androidx.compose.ui.graphics.painter.Painter
import org.pushingpixels.aurora.theming.BackgroundAppearanceStrategy
import org.pushingpixels.aurora.theming.IconFilterStrategy
import java.io.InputStream
import java.util.*

/**
* A single item in the breadcrumb bar model.
Expand Down Expand Up @@ -66,77 +63,6 @@ interface BreadcrumbBarContentProvider<T> {
}
}

/**
* Model for the breadcrumb bar component.
*/
class BreadcrumbBarContentModel<T>(val items: MutableList<BreadcrumbItem<T>>) {
/**
* Returns the index of the specified item.
*
* @param item Item.
* @return Index of the item if it is in the model or -1 if it is not.
*/
fun indexOf(item: BreadcrumbItem<T>): Int {
return items.indexOf(item)
}

/**
* Removes the last item in this model.
*/
fun removeLast() {
items.removeLast()
}

/**
* Resets this model, removing all the items.
*/
fun reset() {
items.clear()
}

/**
* Returns the number of items in this model.
*
* @return Number of items in this model.
*/
val itemCount: Int = items.size

/**
* Returns the model item at the specified index.
*
* @param index Item index.
* @return The model item at the specified index. Will return
* `null` if the index is negative or larger than the
* number of items.
*/
fun getItem(index: Int): BreadcrumbItem<T>? {
if (index < 0) return null
return if (index >= itemCount) null else items[index]
}

/**
* Replaces the current item list with the specified list.
*
* @param items New contents of the model.
*/
fun replace(items: List<BreadcrumbItem<T>>) {
this.items.clear()
for (i in items.indices) {
this.items.add(items[i])
}
}

/**
* Adds the specified item at the end of the path.
*
* @param item Item to add.
*/
fun add(item: BreadcrumbItem<T>) {
items.add(item)
}
}


data class BreadcrumbBarPresentationModel(
val presentationState: CommandButtonPresentationState = CommandButtonPresentationState.Medium,
val backgroundAppearanceStrategy: BackgroundAppearanceStrategy = BackgroundAppearanceStrategy.Flat,
Expand Down
Expand Up @@ -112,6 +112,9 @@ fun AuroraApplicationScope.BreadcrumbContent(auroraSkinDefinition: MutableState<
AuroraDecorationArea(decorationAreaType = DecorationAreaType.Header) {
AuroraBreadcrumbBar(
contentProvider = contentProvider,
onShownPathChanged = {
println(it.last().data)
},
presentationModel = BreadcrumbBarPresentationModel(
iconActiveFilterStrategy = IconFilterStrategy.ThemedFollowText,
iconEnabledFilterStrategy = IconFilterStrategy.ThemedFollowText,
Expand Down

0 comments on commit a61ecd6

Please sign in to comment.