Skip to content

Instantly share code, notes, and snippets.

sealed class RootRouting {
object LoggedOut: RootRouting()
data class LoggedIn(val user: User): RootRouting()
}
sealed class LoggedInRouting {
object News : LoggedInRouting()
object Gallery : LoggedInRouting()
object Profile : LoggedInRouting()
}
interface Gallery {
sealed class Routing {
object AlbumList : Routing()
data class PhotosOfAlbum(val album: Album) : Routing()
data class FullScreenPhoto(val photo: Photo) : Routing()
}
companion object {
@Composable
routing = Routing.PhotosOfAlbum(someAlbum)
@Model
class BackStack<T>(
defaultElement: T
) {
private var elements: List<T> = listOf(defaultElement)
fun last(): T =
elements.last()
fun push(element: T) {
@Composable
fun Content() {
- var routing by +state<Routing> { Routing.AlbumList }
+ var backStack by +state<BackStack<Routing>> { BackStack(Routing.AlbumList) }
- when (routing) {
+ when (currentRouting = backStack.last()) {
// all Content() on the right hand side are @Composable
is Routing.AlbumList -> AlbumList.Content()
is Routing.PhotosOfAlbum -> PhotosOfAlbum.Content(currentRouting.album)
is Routing.FullScreenPhoto -> FullScreenPhoto.Content(currentRouting..photo)
}
// “going forward”
backStack.push(Routing.PhotosOfAlbum(album))
// “going backwards” -- we’ll want to trigger this from actual back press
backStack.pop()
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MaterialTheme {
Root.Content()
}
}
}
@Model
object BackPress {
var triggered = false
}
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MaterialTheme {
Root.Content(Backpress)
}
}
}