-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5587060
commit 50e6763
Showing
10 changed files
with
4,386 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
sample/src/main/java/cafe/adriel/bonsai/sample/HomeScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package cafe.adriel.bonsai.sample | ||
|
||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.material.Button | ||
import androidx.compose.material.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import cafe.adriel.bonsai.sample.tree.DslTreeScreen | ||
import cafe.adriel.bonsai.sample.tree.FileSystemTreeScreen | ||
import cafe.adriel.bonsai.sample.tree.JsonTreeScreen | ||
import cafe.adriel.voyager.core.screen.Screen | ||
import cafe.adriel.voyager.navigator.LocalNavigator | ||
import cafe.adriel.voyager.navigator.currentOrThrow | ||
|
||
object HomeScreen : Screen { | ||
|
||
@Composable | ||
override fun Content() { | ||
Column( | ||
verticalArrangement = Arrangement.Center, | ||
horizontalAlignment = Alignment.CenterHorizontally, | ||
modifier = Modifier.fillMaxSize() | ||
) { | ||
SampleButton( | ||
text = "DSL Tree", | ||
screen = DslTreeScreen | ||
) | ||
SampleButton( | ||
text = "File System Tree", | ||
screen = FileSystemTreeScreen | ||
) | ||
SampleButton( | ||
text = "JSON Tree", | ||
screen = JsonTreeScreen | ||
) | ||
} | ||
} | ||
|
||
@Composable | ||
private fun SampleButton( | ||
text: String, | ||
screen: Screen | ||
) { | ||
val navigator = LocalNavigator.currentOrThrow | ||
|
||
Button( | ||
onClick = { navigator push screen } | ||
) { | ||
Text(text) | ||
} | ||
} | ||
} |
177 changes: 2 additions & 175 deletions
177
sample/src/main/java/cafe/adriel/bonsai/sample/SampleActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,192 +1,19 @@ | ||
package cafe.adriel.bonsai.sample | ||
|
||
import android.os.Build | ||
import android.os.Bundle | ||
import androidx.activity.ComponentActivity | ||
import androidx.activity.compose.setContent | ||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.lazy.LazyRow | ||
import androidx.compose.material.Button | ||
import androidx.compose.material.MaterialTheme | ||
import androidx.compose.material.Text | ||
import androidx.compose.material.icons.Icons | ||
import androidx.compose.material.icons.outlined.Adb | ||
import androidx.compose.material.icons.outlined.Android | ||
import androidx.compose.material.icons.outlined.Description | ||
import androidx.compose.material.icons.outlined.Folder | ||
import androidx.compose.material.icons.outlined.FolderOpen | ||
import androidx.compose.material.icons.outlined.Image | ||
import androidx.compose.material.icons.outlined.InsertDriveFile | ||
import androidx.compose.material.icons.outlined.LocalCafe | ||
import androidx.compose.material.icons.outlined.Memory | ||
import androidx.compose.material.icons.outlined.Mic | ||
import androidx.compose.material.icons.outlined.Videocam | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.vector.ImageVector | ||
import androidx.compose.ui.graphics.vector.rememberVectorPainter | ||
import androidx.compose.ui.text.font.FontWeight | ||
import androidx.compose.ui.unit.dp | ||
import cafe.adriel.bonsai.core.Bonsai | ||
import cafe.adriel.bonsai.core.node.BranchNode | ||
import cafe.adriel.bonsai.core.node.Node | ||
import cafe.adriel.bonsai.core.tree.Tree | ||
import cafe.adriel.bonsai.core.tree.rememberTree | ||
import cafe.adriel.bonsai.filesystem.FileSystemBonsaiStyle | ||
import cafe.adriel.bonsai.filesystem.fileSystemNodes | ||
import okio.Path | ||
import cafe.adriel.voyager.navigator.Navigator | ||
|
||
class SampleActivity : ComponentActivity() { | ||
|
||
private val rootDirectory by lazy { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) dataDir | ||
else codeCacheDir | ||
} | ||
|
||
private val Tree<Path>.firstBranchNodeAtLevel1: Node<Path> | ||
get() = nodes | ||
.filterIsInstance<BranchNode<Path>>() | ||
.first { it.children.isNotEmpty() } | ||
.children | ||
.filterIsInstance<BranchNode<Path>>() | ||
.first { it.children.isNotEmpty() } | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContent { | ||
MaterialTheme { | ||
val tree = rememberTree<Path>( | ||
nodes = fileSystemNodes( | ||
rootPath = rootDirectory, | ||
selfInclude = true | ||
) | ||
) | ||
|
||
Column( | ||
verticalArrangement = Arrangement.Bottom, | ||
modifier = Modifier.fillMaxSize() | ||
) { | ||
Bonsai( | ||
tree = tree, | ||
style = FileSystemBonsaiStyle().copy( | ||
nodeCollapsedIcon = { node -> | ||
getIcon( | ||
path = node.content, | ||
default = if (node is BranchNode) Icons.Outlined.Folder | ||
else Icons.Outlined.InsertDriveFile | ||
) | ||
}, | ||
nodeExpandedIcon = { node -> | ||
getIcon(path = node.content, default = Icons.Outlined.FolderOpen) | ||
} | ||
), | ||
modifier = Modifier | ||
.weight(1f) | ||
.padding(bottom = 4.dp) | ||
) | ||
TreeController( | ||
tree = tree | ||
) | ||
} | ||
Navigator(HomeScreen) | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
private fun TreeController( | ||
tree: Tree<Path> | ||
) { | ||
Column { | ||
TreeManagerTitle("Expand") | ||
LazyRow { | ||
item { | ||
TreeManagerButton( | ||
text = "Root", | ||
onClick = { tree.expandRoot() } | ||
) | ||
TreeManagerButton( | ||
text = "Node", | ||
onClick = { tree.expandNode(tree.firstBranchNodeAtLevel1) } | ||
) | ||
TreeManagerButton( | ||
text = "Level 2", | ||
onClick = { tree.expandUntil(2) } | ||
) | ||
} | ||
} | ||
TreeManagerTitle("Collapse") | ||
LazyRow { | ||
item { | ||
TreeManagerButton( | ||
text = "Root", | ||
onClick = { tree.collapseRoot() } | ||
) | ||
TreeManagerButton( | ||
text = "Node", | ||
onClick = { tree.collapseNode(tree.firstBranchNodeAtLevel1) } | ||
) | ||
TreeManagerButton( | ||
text = "Level 2", | ||
onClick = { tree.collapseFrom(2) } | ||
) | ||
} | ||
} | ||
TreeManagerTitle("Select") | ||
LazyRow { | ||
item { | ||
TreeManagerButton( | ||
text = "Toggle", | ||
onClick = { tree.toggleSelection(tree.nodes.first()) } | ||
) | ||
TreeManagerButton( | ||
text = "Clear", | ||
onClick = { tree.clearSelection() } | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
private fun TreeManagerTitle( | ||
text: String | ||
) { | ||
Text( | ||
text = text, | ||
fontWeight = FontWeight.SemiBold, | ||
modifier = Modifier.padding(horizontal = 4.dp) | ||
) | ||
} | ||
|
||
@Composable | ||
private fun TreeManagerButton( | ||
text: String, | ||
onClick: () -> Unit | ||
) { | ||
Button( | ||
onClick = onClick, | ||
modifier = Modifier.padding(horizontal = 4.dp) | ||
) { | ||
Text(text = text) | ||
} | ||
} | ||
|
||
@Composable | ||
private fun getIcon(path: Path, default: ImageVector) = | ||
rememberVectorPainter( | ||
when (path.toFile().extension) { | ||
"apk" -> Icons.Outlined.Android | ||
"jar" -> Icons.Outlined.LocalCafe | ||
"studio" -> Icons.Outlined.Adb | ||
"so" -> Icons.Outlined.Memory | ||
"xml" -> Icons.Outlined.Description | ||
"png", "webp", "jpg" -> Icons.Outlined.Image | ||
"mp4", "webm", "gif" -> Icons.Outlined.Videocam | ||
"wav", "mp3", "ogg" -> Icons.Outlined.Mic | ||
else -> default | ||
} | ||
) | ||
} |
75 changes: 75 additions & 0 deletions
75
sample/src/main/java/cafe/adriel/bonsai/sample/tree/DslTreeScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package cafe.adriel.bonsai.sample.tree | ||
|
||
import androidx.compose.material.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.unit.dp | ||
import cafe.adriel.bonsai.core.Bonsai | ||
import cafe.adriel.bonsai.core.BonsaiStyle | ||
import cafe.adriel.bonsai.core.node.Branch | ||
import cafe.adriel.bonsai.core.node.Leaf | ||
import cafe.adriel.bonsai.core.tree.Tree | ||
|
||
object DslTreeScreen : TreeScreen<String> { | ||
|
||
override val title = "DSL Tree" | ||
|
||
@Composable | ||
override fun buildTree(): Tree<String> = | ||
Tree { | ||
Branch("Animalia") { | ||
Branch("Chordata") { | ||
Branch("Mammalia") { | ||
Branch("Carnivora") { | ||
Branch("Canidae") { | ||
Branch("Canis") { | ||
Leaf("Wolf", customIcon = { LeafIcon("\uD83D\uDC3A") }) | ||
Leaf("Dog", customIcon = { LeafIcon("\uD83D\uDC36") }) | ||
} | ||
} | ||
Branch("Felidae") { | ||
Branch("Felis") { | ||
Leaf("Cat", customIcon = { LeafIcon("\uD83D\uDC31") }) | ||
} | ||
Branch("Panthera") { | ||
Leaf("Lion", customIcon = { LeafIcon("\uD83E\uDD81") }) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
Branch("Plantae") { | ||
Branch("Solanales") { | ||
Branch("Convolvulaceae") { | ||
Branch("Ipomoea") { | ||
Leaf("Sweet Potato", customIcon = { LeafIcon("\uD83C\uDF60") }) | ||
} | ||
} | ||
Branch("Solanaceae") { | ||
Leaf("Potato", customIcon = { LeafIcon("\uD83E\uDD54") }) | ||
Leaf("Tomato", customIcon = { LeafIcon("\uD83C\uDF45") }) | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
override fun BonsaiContent( | ||
tree: Tree<String>, | ||
modifier: Modifier | ||
) { | ||
Bonsai( | ||
tree = tree, | ||
style = BonsaiStyle( | ||
nodeNameStartPadding = 4.dp | ||
), | ||
modifier = modifier | ||
) | ||
} | ||
|
||
@Composable | ||
private fun LeafIcon(emoji: String) { | ||
Text(emoji) | ||
} | ||
} |
Oops, something went wrong.