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

Fix NPE in result processing #92

Merged
merged 2 commits into from
Oct 27, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ package com.kiwi.navigationcompose.typed

import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.navigation.NavController
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.first
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.KSerializer
import kotlinx.serialization.json.Json
Expand All @@ -31,9 +36,23 @@ public inline fun <reified T : ResultDestination<R>, reified R : Any> Composable
navController: NavController,
noinline block: (R) -> Unit,
) {
// The async waiting workarounds situations when navController.currentBackStackEntry is null
// ~ not yet populated.
val currentDestinationState = remember(navController) {
mutableStateOf(navController.currentDestination)
}
val currentDestination = currentDestinationState.value
if (currentDestination == null) {
LaunchedEffect(navController) {
val currentBackStackEntry = navController.currentBackStackEntryFlow.filterNotNull().first()
currentDestinationState.value = currentBackStackEntry.destination
}
return
}

ResultEffectImpl(
navController = navController,
currentRoute = navController.currentDestination!!.route!!,
currentRoute = currentDestination.route!!, // routes are always not null in Nav Compose
destinationSerializer = serializer<T>(),
resultSerializer = serializer<R>(),
block = block,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ internal fun NameEditDialog(navController: NavController) {
NameEdit(
onNameSave = { name ->
navController.setResult(ProfileDestinations.NameEditDialog.Result(name))
navController.navigateUp()
navController.popBackStack()
},
onDismiss = navController::navigateUp,
onDismiss = navController::popBackStack,
)
}

Expand Down
16 changes: 15 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,9 @@ Another set of functionality is provided to support the result sharing. First, d

```kotlin
import com.kiwi.navigationcompose.typed.Destination
import com.kiwi.navigationcompose.typed.DialogResultEffect
import com.kiwi.navigationcompose.typed.ResultDestination
import com.kiwi.navigationcompose.typed.setResult

sealed interface Destinations : Destination {
@Serializable
Expand All @@ -164,7 +166,7 @@ sealed interface Destinations : Destination {

@Composable
fun Host(navController: NavController) {
ComposableResultEffect(navController) { result: Destinations.Dialog.Result ->
DialogResultEffect(navController) { result: Destinations.Dialog.Result ->
println(result)
// process the result
}
Expand All @@ -175,4 +177,16 @@ fun Host(navController: NavController) {
Text("Open")
}
}

@Composable
fun Dialog(navController: NavController) {
Button(
onClick = {
navController.setResult(Destinations.Dialog.Result(something = 42))
navController.popBackStack()
}
) {
Text("Set and close")
}
}
```