Skip to content

Commit 9c9d85b

Browse files
authored
Merge pull request #5 from hrach/results
Add `results` module
2 parents c509b18 + 24eed58 commit 9c9d85b

File tree

12 files changed

+282
-12
lines changed

12 files changed

+282
-12
lines changed

demo/build.gradle.kts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,9 @@ kotlinter {
6060
dependencies {
6161
implementation(projects.bottomsheet)
6262
implementation(projects.modalsheet)
63+
implementation(projects.results)
6364

64-
implementation(libs.kotlin.serialization)
65+
implementation(libs.kotlin.serialization.core)
6566
implementation(libs.compose.material3)
6667
implementation(libs.navigation.compose)
6768
}

demo/src/main/kotlin/dev/hrach/navigation/demo/Destinations.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,10 @@ internal object Destinations {
1919
data object Modal2
2020

2121
@Serializable
22-
data object BottomSheet
22+
data object BottomSheet {
23+
@Serializable
24+
data class Result(
25+
val id: Int,
26+
)
27+
}
2328
}

demo/src/main/kotlin/dev/hrach/navigation/demo/screens/BottomSheet.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,23 @@ import androidx.compose.runtime.setValue
1414
import androidx.compose.ui.Modifier
1515
import androidx.compose.ui.unit.dp
1616
import androidx.navigation.NavController
17+
import dev.hrach.navigation.demo.Destinations
18+
import dev.hrach.navigation.results.setResult
19+
import kotlin.random.Random
1720

1821
@Composable
1922
internal fun BottomSheet(navController: NavController) {
2023
Column(Modifier.padding(horizontal = 16.dp)) {
2124
Text("This is a bottomsheet")
2225
var value by rememberSaveable { mutableStateOf("") }
2326
OutlinedTextField(value = value, onValueChange = { value = it })
24-
OutlinedButton(onClick = { navController.popBackStack() }, Modifier.fillMaxWidth()) {
27+
OutlinedButton(
28+
onClick = {
29+
navController.setResult(Destinations.BottomSheet.Result(Random.nextInt()))
30+
navController.popBackStack()
31+
},
32+
modifier = Modifier.fillMaxWidth(),
33+
) {
2534
Text("Close")
2635
}
2736
}
Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,53 @@
11
package dev.hrach.navigation.demo.screens
22

3+
import android.annotation.SuppressLint
34
import androidx.compose.foundation.layout.Column
45
import androidx.compose.material3.OutlinedButton
56
import androidx.compose.material3.Text
67
import androidx.compose.runtime.Composable
8+
import androidx.compose.runtime.getValue
9+
import androidx.compose.runtime.mutableIntStateOf
10+
import androidx.compose.runtime.remember
11+
import androidx.compose.runtime.saveable.rememberSaveable
12+
import androidx.compose.runtime.setValue
713
import androidx.navigation.NavController
814
import dev.hrach.navigation.demo.Destinations
15+
import dev.hrach.navigation.results.NavigationResultEffect
916

17+
@SuppressLint("UnrememberedGetBackStackEntry")
1018
@Composable
1119
internal fun Home(
1220
navController: NavController,
21+
) {
22+
var bottomSheetResult by rememberSaveable { mutableIntStateOf(-1) }
23+
NavigationResultEffect<Destinations.BottomSheet.Result>(
24+
backStackEntry = remember(navController) { navController.getBackStackEntry<Destinations.Home>() },
25+
navController = navController,
26+
) { result ->
27+
bottomSheetResult = result.id
28+
}
29+
Home(
30+
navigate = navController::navigate,
31+
bottomSheetResult = bottomSheetResult,
32+
)
33+
}
34+
35+
@Composable
36+
private fun Home(
37+
navigate: (Any) -> Unit,
38+
bottomSheetResult: Int,
1339
) {
1440
Column {
1541
Text("Home")
1642

17-
OutlinedButton(onClick = { navController.navigate(Destinations.Modal1) }) {
43+
OutlinedButton(onClick = { navigate(Destinations.Modal1) }) {
1844
Text("Modal 1")
1945
}
2046

21-
OutlinedButton(onClick = { navController.navigate(Destinations.BottomSheet) }) {
47+
OutlinedButton(onClick = { navigate(Destinations.BottomSheet) }) {
2248
Text("BottomSheet")
2349
}
50+
51+
Text("BottomSheetResult: $bottomSheetResult")
2452
}
2553
}

demo/src/main/kotlin/dev/hrach/navigation/demo/screens/Modal1.kt

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package dev.hrach.navigation.demo.screens
22

3+
import android.annotation.SuppressLint
34
import androidx.compose.foundation.background
45
import androidx.compose.foundation.layout.Column
56
import androidx.compose.foundation.layout.WindowInsets
@@ -10,24 +11,50 @@ import androidx.compose.material3.MaterialTheme
1011
import androidx.compose.material3.OutlinedButton
1112
import androidx.compose.material3.Text
1213
import androidx.compose.runtime.Composable
14+
import androidx.compose.runtime.getValue
15+
import androidx.compose.runtime.mutableIntStateOf
16+
import androidx.compose.runtime.remember
17+
import androidx.compose.runtime.saveable.rememberSaveable
18+
import androidx.compose.runtime.setValue
1319
import androidx.compose.ui.Modifier
1420
import androidx.navigation.NavController
1521
import dev.hrach.navigation.demo.Destinations
22+
import dev.hrach.navigation.results.NavigationResultEffect
1623

24+
@SuppressLint("UnrememberedGetBackStackEntry")
1725
@Composable
1826
internal fun Modal1(navController: NavController) {
27+
var bottomSheetResult by rememberSaveable { mutableIntStateOf(-1) }
28+
NavigationResultEffect<Destinations.BottomSheet.Result>(
29+
backStackEntry = remember(navController) { navController.getBackStackEntry<Destinations.Modal1>() },
30+
navController = navController,
31+
) { result ->
32+
bottomSheetResult = result.id
33+
}
34+
Modal1(
35+
navigate = navController::navigate,
36+
bottomSheetResult = bottomSheetResult,
37+
)
38+
}
39+
40+
@Composable
41+
private fun Modal1(
42+
navigate: (Any) -> Unit,
43+
bottomSheetResult: Int,
44+
) {
1945
Column(
2046
Modifier
2147
.fillMaxSize()
2248
.background(MaterialTheme.colorScheme.surface)
2349
.windowInsetsPadding(WindowInsets.systemBars),
2450
) {
2551
Text("Modal 1")
26-
OutlinedButton(onClick = { navController.navigate(Destinations.Modal2) }) {
52+
OutlinedButton(onClick = { navigate(Destinations.Modal2) }) {
2753
Text("Modal 2")
2854
}
29-
OutlinedButton(onClick = { navController.navigate(Destinations.BottomSheet) }) {
55+
OutlinedButton(onClick = { navigate(Destinations.BottomSheet) }) {
3056
Text("BottomSheet")
3157
}
58+
Text("BottomSheetResult: $bottomSheetResult")
3259
}
3360
}

gradle/libs.versions.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ minSdk = "21"
55

66
[libraries]
77

8-
kotlin-serialization = "org.jetbrains.kotlinx:kotlinx-serialization-core:1.6.3"
8+
kotlin-serialization-core = "org.jetbrains.kotlinx:kotlinx-serialization-core:1.6.3"
9+
kotlin-serialization-json = "org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.3"
910
appcompat = "androidx.appcompat:appcompat:1.6.1"
11+
androidx-lifecycle-runtime = "androidx.lifecycle:lifecycle-runtime:2.8.0"
1012
compose-material3 = "androidx.compose.material3:material3:1.3.0-beta01"
1113
navigation-compose = "androidx.navigation:navigation-compose:2.8.0-beta01"
1214
junit = { module = "junit:junit", version = "4.13.2" }

readme.md

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ Use Maven Central and these dependencies:
1212
dependencies {
1313
implementation("dev.hrach.navigation:bottomsheet:<version>")
1414
implementation("dev.hrach.navigation:modalsheet:<version>")
15+
implementation("dev.hrach.navigation:results:<version>")
1516
}
1617
```
1718

1819
Components:
1920

2021
- **BottomSheet** - Connects the official Material 3 BottomSheet with Jetpack Navigation.
2122
- **ModalSheet** - A custom destination type for Jetpack Navigation that brings fullscreen content with modal animation.
23+
- **Results** - Passing a result simply between destinations.
2224

2325
Quick setup:
2426

@@ -28,12 +30,44 @@ val bottomSheetNavigator = remember { BottomSheetNavigator() }
2830
val navController = rememberNavController(modalSheetNavigator, bottomSheetNavigator)
2931

3032
NavHost(
31-
navController = navController,
32-
startDestination = Destinations.Home,
33+
navController = navController,
34+
startDestination = Destinations.Home,
3335
) {
34-
modalSheet<Destinations.Modal> { Modal1(navController) }
35-
bottomSheet<Destinations.BottomSheet> { BottomSheet(navController) }
36+
composable<Destinations.Home> { Home(navController) }
37+
modalSheet<Destinations.Modal> { Modal(navController) }
38+
bottomSheet<Destinations.BottomSheet> { BottomSheet(navController) }
3639
}
3740
ModalSheetHost(modalSheetNavigator)
3841
BottomSheetHost(bottomSheetNavigator)
3942
```
43+
44+
Results sharing:
45+
46+
```kotlin
47+
object Destinations {
48+
@Serializable
49+
data object BottomSheet {
50+
@Serializable
51+
data class Result(
52+
val id: Int,
53+
)
54+
}
55+
}
56+
57+
@Composable
58+
fun Home(navController: NavController) {
59+
NavigationResultEffect<Destinations.BottomSheet.Result>(
60+
backStackEntry = remember(navController) { navController.getBackStackEntry<Destinations.Home>() },
61+
navController = navController,
62+
) { result ->
63+
// process result -
64+
}
65+
}
66+
67+
@Composable
68+
fun BottomSheet(navController: NavController) {
69+
OutlineButton(onClick = { navController.setResult(Destinations.BottomSheet.Result(42)) }) {
70+
Text("Close")
71+
}
72+
}
73+
```

results/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

results/build.gradle.kts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
@file:Suppress("UnstableApiUsage")
2+
3+
plugins {
4+
id("com.android.library")
5+
id("org.jetbrains.kotlin.android")
6+
id("org.jetbrains.kotlin.plugin.compose")
7+
id("org.jetbrains.kotlinx.binary-compatibility-validator")
8+
id("com.vanniktech.maven.publish")
9+
id("com.gradleup.nmcp")
10+
id("org.jmailen.kotlinter")
11+
}
12+
13+
version = property("VERSION_NAME") as String
14+
15+
android {
16+
namespace = "dev.hrach.navigation.results"
17+
18+
compileSdk = libs.versions.compileSdk.get().toInt()
19+
20+
defaultConfig {
21+
minSdk = libs.versions.minSdk.get().toInt()
22+
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
23+
}
24+
25+
buildFeatures {
26+
compose = true
27+
buildConfig = false
28+
}
29+
30+
compileOptions {
31+
sourceCompatibility = JavaVersion.VERSION_1_8
32+
targetCompatibility = JavaVersion.VERSION_1_8
33+
}
34+
35+
kotlinOptions {
36+
freeCompilerArgs = freeCompilerArgs.toMutableList().apply {
37+
add("-Xexplicit-api=strict")
38+
}.toList()
39+
}
40+
41+
lint {
42+
disable.add("GradleDependency")
43+
abortOnError = true
44+
warningsAsErrors = true
45+
}
46+
}
47+
48+
nmcp {
49+
publishAllPublications {}
50+
}
51+
52+
kotlinter {
53+
reporters = arrayOf("json")
54+
}
55+
56+
dependencies {
57+
implementation(libs.navigation.compose)
58+
implementation(libs.kotlin.serialization.json)
59+
implementation(libs.androidx.lifecycle.runtime)
60+
testImplementation(libs.junit)
61+
}

results/gradle.properties

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
POM_ARTIFACT_ID=results
2+
POM_NAME=Navigation Results sharing
3+
POM_DESCRIPTION=Results sharing between screens API for Jetpack Navigation Compose
4+
POM_PACKAGING=aar

0 commit comments

Comments
 (0)