Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add results module #5

Merged
merged 1 commit into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion demo/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ kotlinter {
dependencies {
implementation(projects.bottomsheet)
implementation(projects.modalsheet)
implementation(projects.results)

implementation(libs.kotlin.serialization)
implementation(libs.kotlin.serialization.core)
implementation(libs.compose.material3)
implementation(libs.navigation.compose)
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,10 @@ internal object Destinations {
data object Modal2

@Serializable
data object BottomSheet
data object BottomSheet {
@Serializable
data class Result(
val id: Int,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,23 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import dev.hrach.navigation.demo.Destinations
import dev.hrach.navigation.results.setResult
import kotlin.random.Random

@Composable
internal fun BottomSheet(navController: NavController) {
Column(Modifier.padding(horizontal = 16.dp)) {
Text("This is a bottomsheet")
var value by rememberSaveable { mutableStateOf("") }
OutlinedTextField(value = value, onValueChange = { value = it })
OutlinedButton(onClick = { navController.popBackStack() }, Modifier.fillMaxWidth()) {
OutlinedButton(
onClick = {
navController.setResult(Destinations.BottomSheet.Result(Random.nextInt()))
navController.popBackStack()
},
modifier = Modifier.fillMaxWidth(),
) {
Text("Close")
}
}
Expand Down
32 changes: 30 additions & 2 deletions demo/src/main/kotlin/dev/hrach/navigation/demo/screens/Home.kt
Original file line number Diff line number Diff line change
@@ -1,25 +1,53 @@
package dev.hrach.navigation.demo.screens

import android.annotation.SuppressLint
import androidx.compose.foundation.layout.Column
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.navigation.NavController
import dev.hrach.navigation.demo.Destinations
import dev.hrach.navigation.results.NavigationResultEffect

@SuppressLint("UnrememberedGetBackStackEntry")
@Composable
internal fun Home(
navController: NavController,
) {
var bottomSheetResult by rememberSaveable { mutableIntStateOf(-1) }
NavigationResultEffect<Destinations.BottomSheet.Result>(
backStackEntry = remember(navController) { navController.getBackStackEntry<Destinations.Home>() },
navController = navController,
) { result ->
bottomSheetResult = result.id
}
Home(
navigate = navController::navigate,
bottomSheetResult = bottomSheetResult,
)
}

@Composable
private fun Home(
navigate: (Any) -> Unit,
bottomSheetResult: Int,
) {
Column {
Text("Home")

OutlinedButton(onClick = { navController.navigate(Destinations.Modal1) }) {
OutlinedButton(onClick = { navigate(Destinations.Modal1) }) {
Text("Modal 1")
}

OutlinedButton(onClick = { navController.navigate(Destinations.BottomSheet) }) {
OutlinedButton(onClick = { navigate(Destinations.BottomSheet) }) {
Text("BottomSheet")
}

Text("BottomSheetResult: $bottomSheetResult")
}
}
31 changes: 29 additions & 2 deletions demo/src/main/kotlin/dev/hrach/navigation/demo/screens/Modal1.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.hrach.navigation.demo.screens

import android.annotation.SuppressLint
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.WindowInsets
Expand All @@ -10,24 +11,50 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.navigation.NavController
import dev.hrach.navigation.demo.Destinations
import dev.hrach.navigation.results.NavigationResultEffect

@SuppressLint("UnrememberedGetBackStackEntry")
@Composable
internal fun Modal1(navController: NavController) {
var bottomSheetResult by rememberSaveable { mutableIntStateOf(-1) }
NavigationResultEffect<Destinations.BottomSheet.Result>(
backStackEntry = remember(navController) { navController.getBackStackEntry<Destinations.Modal1>() },
navController = navController,
) { result ->
bottomSheetResult = result.id
}
Modal1(
navigate = navController::navigate,
bottomSheetResult = bottomSheetResult,
)
}

@Composable
private fun Modal1(
navigate: (Any) -> Unit,
bottomSheetResult: Int,
) {
Column(
Modifier
.fillMaxSize()
.background(MaterialTheme.colorScheme.surface)
.windowInsetsPadding(WindowInsets.systemBars),
) {
Text("Modal 1")
OutlinedButton(onClick = { navController.navigate(Destinations.Modal2) }) {
OutlinedButton(onClick = { navigate(Destinations.Modal2) }) {
Text("Modal 2")
}
OutlinedButton(onClick = { navController.navigate(Destinations.BottomSheet) }) {
OutlinedButton(onClick = { navigate(Destinations.BottomSheet) }) {
Text("BottomSheet")
}
Text("BottomSheetResult: $bottomSheetResult")
}
}
4 changes: 3 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ minSdk = "21"

[libraries]

kotlin-serialization = "org.jetbrains.kotlinx:kotlinx-serialization-core:1.6.3"
kotlin-serialization-core = "org.jetbrains.kotlinx:kotlinx-serialization-core:1.6.3"
kotlin-serialization-json = "org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.3"
appcompat = "androidx.appcompat:appcompat:1.6.1"
androidx-lifecycle-runtime = "androidx.lifecycle:lifecycle-runtime:2.8.0"
compose-material3 = "androidx.compose.material3:material3:1.3.0-beta01"
navigation-compose = "androidx.navigation:navigation-compose:2.8.0-beta01"
junit = { module = "junit:junit", version = "4.13.2" }
42 changes: 38 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ Use Maven Central and these dependencies:
dependencies {
implementation("dev.hrach.navigation:bottomsheet:<version>")
implementation("dev.hrach.navigation:modalsheet:<version>")
implementation("dev.hrach.navigation:results:<version>")
}
```

Components:

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

Quick setup:

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

NavHost(
navController = navController,
startDestination = Destinations.Home,
navController = navController,
startDestination = Destinations.Home,
) {
modalSheet<Destinations.Modal> { Modal1(navController) }
bottomSheet<Destinations.BottomSheet> { BottomSheet(navController) }
composable<Destinations.Home> { Home(navController) }
modalSheet<Destinations.Modal> { Modal(navController) }
bottomSheet<Destinations.BottomSheet> { BottomSheet(navController) }
}
ModalSheetHost(modalSheetNavigator)
BottomSheetHost(bottomSheetNavigator)
```

Results sharing:

```kotlin
object Destinations {
@Serializable
data object BottomSheet {
@Serializable
data class Result(
val id: Int,
)
}
}

@Composable
fun Home(navController: NavController) {
NavigationResultEffect<Destinations.BottomSheet.Result>(
backStackEntry = remember(navController) { navController.getBackStackEntry<Destinations.Home>() },
navController = navController,
) { result ->
// process result -
}
}

@Composable
fun BottomSheet(navController: NavController) {
OutlineButton(onClick = { navController.setResult(Destinations.BottomSheet.Result(42)) }) {
Text("Close")
}
}
```
1 change: 1 addition & 0 deletions results/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
61 changes: 61 additions & 0 deletions results/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
@file:Suppress("UnstableApiUsage")

plugins {
id("com.android.library")
id("org.jetbrains.kotlin.android")
id("org.jetbrains.kotlin.plugin.compose")
id("org.jetbrains.kotlinx.binary-compatibility-validator")
id("com.vanniktech.maven.publish")
id("com.gradleup.nmcp")
id("org.jmailen.kotlinter")
}

version = property("VERSION_NAME") as String

android {
namespace = "dev.hrach.navigation.results"

compileSdk = libs.versions.compileSdk.get().toInt()

defaultConfig {
minSdk = libs.versions.minSdk.get().toInt()
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}

buildFeatures {
compose = true
buildConfig = false
}

compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}

kotlinOptions {
freeCompilerArgs = freeCompilerArgs.toMutableList().apply {
add("-Xexplicit-api=strict")
}.toList()
}

lint {
disable.add("GradleDependency")
abortOnError = true
warningsAsErrors = true
}
}

nmcp {
publishAllPublications {}
}

kotlinter {
reporters = arrayOf("json")
}

dependencies {
implementation(libs.navigation.compose)
implementation(libs.kotlin.serialization.json)
implementation(libs.androidx.lifecycle.runtime)
testImplementation(libs.junit)
}
4 changes: 4 additions & 0 deletions results/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
POM_ARTIFACT_ID=results
POM_NAME=Navigation Results sharing
POM_DESCRIPTION=Results sharing between screens API for Jetpack Navigation Compose
POM_PACKAGING=aar
Loading