Skip to content

Commit

Permalink
Merge pull request #26 from Kotlin/typed-navigation
Browse files Browse the repository at this point in the history
Use Compose typed navigation
  • Loading branch information
zsmb13 authored Sep 30, 2024
2 parents 013fba6 + 8b1aa9a commit a212018
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 22 deletions.
29 changes: 20 additions & 9 deletions composeApp/src/commonMain/kotlin/com/jetbrains/kmpapp/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,16 @@ import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import androidx.navigation.toRoute
import com.jetbrains.kmpapp.screens.detail.DetailScreen
import com.jetbrains.kmpapp.screens.list.ListScreen
import kotlinx.serialization.Serializable

@Serializable
object ListDestination

@Serializable
data class DetailDestination(val objectId: Int)

@Composable
fun App() {
Expand All @@ -20,16 +28,19 @@ fun App() {
) {
Surface {
val navController: NavHostController = rememberNavController()
NavHost(
navController,
startDestination = "list"
) {
composable("list") {
ListScreen(navController)
NavHost(navController = navController, startDestination = ListDestination) {
composable<ListDestination> {
ListScreen(navigateToDetails = { objectId ->
navController.navigate(DetailDestination(objectId))
})
}
composable("detail/{objectId}") { backStackEntry ->
val objectId = backStackEntry.arguments?.getString("objectId")?.toInt()
DetailScreen(navController, objectId!!)
composable<DetailDestination> { backStackEntry ->
DetailScreen(
objectId = backStackEntry.toRoute<DetailDestination>().objectId,
navigateBack = {
navController.popBackStack()
}
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
import io.ktor.http.ContentType
import io.ktor.serialization.kotlinx.json.json
import kotlinx.serialization.json.Json
import org.koin.compose.viewmodel.dsl.viewModel
import org.koin.core.context.startKoin
import org.koin.core.module.dsl.factoryOf
import org.koin.dsl.module
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import com.jetbrains.kmpapp.data.MuseumObject
import com.jetbrains.kmpapp.screens.EmptyScreenContent
import io.kamel.image.KamelImage
Expand All @@ -53,15 +52,15 @@ import org.koin.compose.viewmodel.koinViewModel

@Composable
fun DetailScreen(
navController: NavController,
objectId: Int,
navigateBack: () -> Unit,
) {
val viewModel = koinViewModel<DetailViewModel>()

val obj by viewModel.getObject(objectId).collectAsState(initial = null)
AnimatedContent(obj != null) { objectAvailable ->
if (objectAvailable) {
ObjectDetails(obj!!, onBackClick = { navController.navigateUp() })
ObjectDetails(obj!!, onBackClick = navigateBack)
} else {
EmptyScreenContent(Modifier.fillMaxSize())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import com.jetbrains.kmpapp.data.MuseumObject
import com.jetbrains.kmpapp.screens.EmptyScreenContent
import io.kamel.image.KamelImage
Expand All @@ -37,7 +36,7 @@ import org.koin.compose.viewmodel.koinViewModel

@Composable
fun ListScreen(
navController: NavController,
navigateToDetails: (objectId: Int) -> Unit
) {
val viewModel = koinViewModel<ListViewModel>()
val objects by viewModel.objects.collectAsState()
Expand All @@ -46,9 +45,7 @@ fun ListScreen(
if (objectsAvailable) {
ObjectGrid(
objects = objects,
onObjectClick = { objectId ->
navController.navigate("detail/$objectId")
}
onObjectClick = navigateToDetails,
)
} else {
EmptyScreenContent(Modifier.fillMaxSize())
Expand All @@ -68,7 +65,8 @@ private fun ObjectGrid(
modifier = modifier
.fillMaxSize()
.padding(WindowInsets.safeDrawing.only(WindowInsetsSides.Horizontal).asPaddingValues()),
contentPadding = WindowInsets.safeDrawing.only(WindowInsetsSides.Vertical).asPaddingValues(),
contentPadding = WindowInsets.safeDrawing.only(WindowInsetsSides.Vertical)
.asPaddingValues(),
) {
items(objects, key = { it.objectID }) { obj ->
ObjectFrame(
Expand Down Expand Up @@ -102,7 +100,10 @@ private fun ObjectFrame(

Spacer(Modifier.height(2.dp))

Text(obj.title, style = MaterialTheme.typography.subtitle1.copy(fontWeight = FontWeight.Bold))
Text(
obj.title,
style = MaterialTheme.typography.subtitle1.copy(fontWeight = FontWeight.Bold)
)
Text(obj.artistDisplayName, style = MaterialTheme.typography.body2)
Text(obj.objectDate, style = MaterialTheme.typography.caption)
}
Expand Down
4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[versions]
agp = "8.6.0"
androidx-activityCompose = "1.9.2"
androidx-ui-tooling = "1.7.0"
androidx-ui-tooling = "1.7.2"
compose-multiplatform = "1.6.11"
kamel = "0.9.5"
koin = "4.0.0"
kotlin = "2.0.20"
ktor = "2.3.12"
navigationCompose = "2.7.0-alpha07"
navigationCompose = "2.8.0-alpha10"

[libraries]
androidx-activity-compose = { module = "androidx.activity:activity-compose", version.ref = "androidx-activityCompose" }
Expand Down

0 comments on commit a212018

Please sign in to comment.