diff --git a/library/src/main/kotlin/com/github/terrakok/cicerone/graph/GraphRouter.kt b/library/src/main/kotlin/com/github/terrakok/cicerone/graph/GraphRouter.kt index 411299a..62e8345 100644 --- a/library/src/main/kotlin/com/github/terrakok/cicerone/graph/GraphRouter.kt +++ b/library/src/main/kotlin/com/github/terrakok/cicerone/graph/GraphRouter.kt @@ -4,7 +4,7 @@ import com.github.terrakok.cicerone.* class GraphRouter( - private val root: Vertex + private val root: Root ) : BaseRouter() { private val vertexes: Map private val currentPath: MutableList @@ -14,7 +14,7 @@ class GraphRouter( val allVertexes = mutableMapOf() val links = mutableSetOf() val jumps = mutableListOf>() - validateGraph(root, allVertexes, links, jumps) + validateGraph(root.vertex, allVertexes, links, jumps) val linkAndJumpIds = links.map { it.id } + jumps.flatten() linkAndJumpIds.forEach { id -> @@ -22,11 +22,16 @@ class GraphRouter( } vertexes = allVertexes - currentPath = mutableListOf(root.id) + currentPath = mutableListOf(root.vertex.id) jumps.forEach { require(validateJump(it)) { "Invalid jump path=$it" } } + + if (root.defaultDestination != null) { + require(root.vertex.edges.any { it.id == root.defaultDestination }) { "Not found default destination" } + navigateTo(root.defaultDestination) + } } private fun validateGraph( @@ -61,7 +66,7 @@ class GraphRouter( val destination = currentVertex.edges.first { it.id == vertexId } val screen = createScreen(destination.id, screenFactory) val command = - if (currentVertex.id == root.id) Replace(screen) + if (currentVertex.id == Root.ID) Replace(screen) else Forward(screen, destination.destroyPreviousView) currentPath.add(destination.id) executeCommands(command) @@ -73,7 +78,7 @@ class GraphRouter( ) { val jump = currentVertex.jumps.first { it.id == jumpId } - if (jump.backTo == root.id && jump.chain.isEmpty()) { + if (jump.backTo == Root.ID && jump.chain.isEmpty()) { finish() return } @@ -84,13 +89,13 @@ class GraphRouter( val index = currentPath.indexOfFirst { it == id } if (index == -1) error("Current path doesn't contain vertex $id") currentPath.subList(index + 1, currentPath.size).clear() - if (jump.backTo == root.id) add(BackTo(null)) + if (jump.backTo == Root.ID) add(BackTo(null)) else add(BackTo(Key(id))) } jump.chain.forEachIndexed { index, vertexId -> val screen = createScreen(vertexId, screenFactory) currentPath.add(vertexId) - if (index == 0 && jump.backTo == root.id) { + if (index == 0 && jump.backTo == Root.ID) { add(Replace(screen)) } else { add(Forward(screen, vertexes.getValue(vertexId).destroyPreviousView)) @@ -108,7 +113,7 @@ class GraphRouter( private fun finish() { currentPath.clear() - currentPath.add(root.id) + currentPath.add(Root.ID) executeCommands(BackTo(null), Back()) } diff --git a/library/src/main/kotlin/com/github/terrakok/cicerone/graph/Vertex.kt b/library/src/main/kotlin/com/github/terrakok/cicerone/graph/Vertex.kt index c01a311..fa41bc8 100644 --- a/library/src/main/kotlin/com/github/terrakok/cicerone/graph/Vertex.kt +++ b/library/src/main/kotlin/com/github/terrakok/cicerone/graph/Vertex.kt @@ -58,17 +58,26 @@ class GraphInfo( var jumps: MutableSet.() -> Unit = {} ) -const val ROOT_ID = "graph-root" +class Root( + val vertex: Vertex, + val defaultDestination: String? +) { + companion object { + const val ID = "graph-root" + } +} fun graph( + defaultDestination: String? = null, setup: GraphInfo.() -> Unit -): Vertex { +): Root { val info = GraphInfo().apply(setup) - return Vertex( - ROOT_ID, + val v = Vertex( + Root.ID, mutableSetOf().apply(info.edges), mutableSetOf().apply(info.jumps) ) + return Root(v, defaultDestination) } class VertexInfo( @@ -121,5 +130,5 @@ fun MutableSet.jump( } fun MutableSet.finish(id: String) { - add(Jump(id, ROOT_ID, emptyList())) + add(Jump(id, Root.ID, emptyList())) } \ No newline at end of file diff --git a/sample/src/main/java/com/github/terrakok/cicerone/sample/NavGraph.kt b/sample/src/main/java/com/github/terrakok/cicerone/sample/NavGraph.kt index 73aee5c..791651c 100644 --- a/sample/src/main/java/com/github/terrakok/cicerone/sample/NavGraph.kt +++ b/sample/src/main/java/com/github/terrakok/cicerone/sample/NavGraph.kt @@ -8,7 +8,7 @@ import com.github.terrakok.cicerone.sample.ui.graph.RoadFragment private val RoadScreen = { id: String -> FragmentScreen(id) { RoadFragment.getNewInstance(id) } } private val ForkScreen = { id: String -> FragmentScreen(id) { ForkFragment.getNewInstance(id) } } -fun Graph() = graph { +fun Graph() = graph("8") { edges = { dest("1") { screen = RoadScreen @@ -55,7 +55,7 @@ fun Graph() = graph { } jumps = { jump("1") { - backTo = ROOT_ID + backTo = Root.ID chain("1", "2", "3", "4", "5", "7") } } diff --git a/sample/src/main/java/com/github/terrakok/cicerone/sample/ui/graph/GraphActivity.kt b/sample/src/main/java/com/github/terrakok/cicerone/sample/ui/graph/GraphActivity.kt index 461339d..2ae89ff 100644 --- a/sample/src/main/java/com/github/terrakok/cicerone/sample/ui/graph/GraphActivity.kt +++ b/sample/src/main/java/com/github/terrakok/cicerone/sample/ui/graph/GraphActivity.kt @@ -39,9 +39,6 @@ class GraphActivity : AppCompatActivity() { super.onCreate(savedInstanceState) setContentView(R.layout.activity_container) - if (savedInstanceState == null) { - graphRouter.navigateTo("8") - } } override fun onResumeFragments() {