Skip to content

Commit

Permalink
Update androidx.navigation and raamcosta.compose-destinations
Browse files Browse the repository at this point in the history
  • Loading branch information
romansavka-mq committed Oct 2, 2024
1 parent 2a9a244 commit ad2c5c6
Show file tree
Hide file tree
Showing 19 changed files with 163 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class LintConventionPlugin : Plugin<Project> {
"JavaPluginLanguageLevel",
"AndroidGradlePluginVersion",
"EnsureInitializerMetadata",
"ObsoleteLintCustomCheck",
"VectorPath"
)
}
Expand Down
7 changes: 5 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ agp = "8.7.0"
androidTools = "31.7.0"
kotlin = "2.0.20"
kotlinx-coroutines = "1.9.0"
kotlinx-serialization-json = "1.7.1"
kotlin-kover = "0.8.3"
androidx-compose = "1.7.2"
androidx-core = "1.13.1"
androidx-lifecycle = "2.8.6"
androidx-material = "1.7.2"
androidx-navigation = "2.7.7"
androidx-navigation = "2.8.1"
androidx-startup = "1.2.0"
androidx-test = "1.6.1"
androidx-test-ext = "1.2.1"
Expand All @@ -23,7 +24,7 @@ androidx-espresso = "3.6.1"
androidx-espresso-device = "1.0.1"
androidx-espresso-intents = "3.6.1"
androidx-uiautomator = "2.3.0"
compose-destinations = "1.10.2"
compose-destinations = "1.11.6"
hilt = "2.52"
koin = "3.5.6"
detekt = "1.23.7"
Expand All @@ -38,6 +39,7 @@ deps-catalog-updater = "0.8.4"
[libraries]
kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "kotlinx-coroutines" }
kotlinx-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "kotlinx-coroutines" }
kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinx-serialization-json" }
androidx-compose-foundation = { module = "androidx.compose.foundation:foundation", version.ref = "androidx-compose" }
androidx-compose-material = { module = "androidx.compose.material:material", version.ref = "androidx-material" }
androidx-compose-runtime = { module = "androidx.compose.runtime:runtime", version.ref = "androidx-compose" }
Expand Down Expand Up @@ -97,6 +99,7 @@ android-library = { id = "com.android.library", version.ref = "agp" }
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
kotlin-kapt = { id = "org.jetbrains.kotlin.kapt", version.ref = "kotlin" }
kotlin-compose = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" }
hilt = { id = "com.google.dagger.hilt.android", version.ref = "hilt" }
# Plugins defined by this project
android-application-convention = { id = "android-application.convention" }
Expand Down
1 change: 0 additions & 1 deletion instrumented-tests/.gitignore

This file was deleted.

1 change: 0 additions & 1 deletion navigation-destinations/.gitignore

This file was deleted.

2 changes: 0 additions & 2 deletions navigation-destinations/src/main/AndroidManifest.xml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,22 @@ import com.ramcosta.composedestinations.spec.Direction
import com.ramcosta.composedestinations.spec.DirectionDestinationSpec
import com.ramcosta.composedestinations.spec.Route

operator fun NavAction.To.Companion.invoke(
fun NavAction.NavigateTo.Companion.direction(
direction: Direction,
options: NavOptions = EMPTY_OPTIONS
): NavAction.To = NavAction.To(direction.route, options)
): NavAction.NavigateTo = NavAction.NavigateTo(direction.route, options)

operator fun NavAction.Pop.Companion.invoke(
fun NavAction.PopBackTo.Companion.route(
route: Route,
inclusive: Boolean,
saveState: Boolean = false
): NavAction.Pop = NavAction.Pop(route.route, inclusive, saveState)
): NavAction.PopBackTo = NavAction.PopBackTo(route.route, inclusive, saveState)

fun NavAction.To.direction(): Direction = Direction(direction)
fun NavAction.NavigateTo.direction(): Direction =
Direction(requireNotNull(directionString) { "ramcosta.composedestinations.Direction requires a directions defined by string." })

fun NavAction.Pop.route(): Route = RouteIml(route)
fun NavAction.PopBackTo.route(): Route =
RouteIml(requireNotNull(routeString) { "ramcosta.composedestinations.Route requires route defined by string." })

private data class RouteIml(
override val route: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import kotlinx.coroutines.flow.Flow
import kotlin.reflect.KClass

/**
* Navigate to a route in the current NavGraph. If an invalid route is given, an IllegalArgumentException will be thrown.
* Navigate to a destination in the current NavGraph. If an invalid route is given, an IllegalArgumentException will be thrown.
*
* @param direction route for the destination
* @param builder DSL for constructing a new [androidx.navigation.NavOptions]
*
* @see [androidx.navigation.NavController.navigate]
*/
suspend fun Navigator.navigate(direction: Direction, builder: NavOptionsBuilder.() -> Unit = {}) =
suspend fun Navigator.navigateToDirection(direction: Direction, builder: NavOptionsBuilder.() -> Unit = {}) =
navigate(direction.route, builder)

/**
Expand All @@ -27,7 +27,7 @@ suspend fun Navigator.navigate(direction: Direction, builder: NavOptionsBuilder.
*
* @see [androidx.navigation.NavController.popBackStack]
*/
suspend fun Navigator.popBackStack(route: Route, inclusive: Boolean, saveState: Boolean = false) =
suspend fun Navigator.popBackToRoute(route: Route, inclusive: Boolean, saveState: Boolean = false) =
popBackStack(route.route, inclusive, saveState)

/**
Expand Down
1 change: 0 additions & 1 deletion navigation-hilt/.gitignore

This file was deleted.

1 change: 0 additions & 1 deletion navigation-koin/.gitignore

This file was deleted.

1 change: 0 additions & 1 deletion navigation-runtime/.gitignore

This file was deleted.

2 changes: 2 additions & 0 deletions navigation-runtime/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
alias(libs.plugins.android.library.convention)
alias(libs.plugins.android.library.compose.convention)
alias(libs.plugins.kotlin.serialization)
alias(libs.plugins.publishing)
}

Expand All @@ -13,6 +14,7 @@ dependencies {
implementation(libs.androidx.navigation.compose)

implementation(libs.kotlinx.coroutines.core)
implementation(libs.kotlinx.serialization.json)

testImplementation(libs.bundles.test)
}
2 changes: 0 additions & 2 deletions navigation-runtime/src/main/AndroidManifest.xml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,23 @@ import androidx.navigation.navOptions
val EMPTY_OPTIONS = navOptions {}

sealed class NavAction {
data class To(
val direction: String,
@ConsistentCopyVisibility
data class NavigateTo private constructor(
val directionString: String? = null,
val directionAny: Any? = null,
val options: NavOptions = EMPTY_OPTIONS
) : NavAction() {

constructor(direction: String, options: NavOptions = EMPTY_OPTIONS) : this(
directionString = direction,
options = options
)

constructor(direction: Any, options: NavOptions = EMPTY_OPTIONS) : this(
directionAny = direction,
options = options
)

companion object
}

Expand All @@ -19,13 +32,28 @@ sealed class NavAction {
val options: NavOptions = EMPTY_OPTIONS
) : NavAction()

data class Pop(
val route: String,
val inclusive: Boolean,
@ConsistentCopyVisibility
data class PopBackTo private constructor(
val routeString: String? = null,
val routeAny: Any? = null,
val inclusive: Boolean = false,
val saveState: Boolean = false
) : NavAction() {

constructor(route: String, inclusive: Boolean, saveState: Boolean = false) : this(
routeString = route,
inclusive = inclusive,
saveState = saveState
)

constructor(route: Any, inclusive: Boolean, saveState: Boolean = false) : this(
routeAny = route,
inclusive = inclusive,
saveState = saveState
)

companion object
}

object Up : NavAction()
object NavigateUp : NavAction()
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ interface Navigator {
val previousNavEntry: NavEntryInfo?

/**
* Navigate to a route in the current NavGraph.
* Navigate to a destination in the current NavGraph.
* If an invalid route is given, an IllegalArgumentException will be thrown.
*
* @param direction route for the destination
Expand All @@ -32,6 +32,17 @@ interface Navigator {
*/
suspend fun navigate(direction: String, builder: NavOptionsBuilder.() -> Unit = {})

/**
* Navigate to a destination in the current NavGraph.
* If an invalid route is given, an IllegalArgumentException will be thrown.
*
* @param direction route for the destination
* @param builder DSL for constructing a new [androidx.navigation.NavOptions]
*
* @see [androidx.navigation.NavController.navigate]
*/
suspend fun navigate(direction: Any, builder: NavOptionsBuilder.() -> Unit = {})

/**
* Navigate to a destination via the given deep link Uri.
* If an invalid deep link is given, an IllegalArgumentException will be thrown.
Expand All @@ -53,6 +64,18 @@ interface Navigator {
*/
suspend fun popBackStack(route: String, inclusive: Boolean, saveState: Boolean = false)

/**
* Attempts to pop back stack.
*
* @param route the topmost destination to retain
* @param inclusive - whether the given destination should also be popped.
* @param saveState - whether the back stack and the state of all destinations between the current destination
* and the route should be saved for later restoration via [androidx.navigation.NavOptions]
*
* @see [androidx.navigation.NavController.popBackStack]
*/
suspend fun popBackStack(route: Any, inclusive: Boolean, saveState: Boolean = false)

/**
* Attempts to navigate up in the navigation hierarchy.
* @see [androidx.navigation.NavController.navigateUp]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import android.net.Uri
import androidx.activity.result.contract.ActivityResultContract
import androidx.navigation.NavOptionsBuilder
import androidx.navigation.navOptions
import com.miquido.android.navigation.NavAction.Deeplink
import com.miquido.android.navigation.NavAction.NavigateTo
import com.miquido.android.navigation.NavAction.PopBackTo
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow
Expand All @@ -18,19 +21,27 @@ internal class NavigatorImpl(
get() = navigation.previousNavEntry

override suspend fun navigate(direction: String, builder: NavOptionsBuilder.() -> Unit) {
navigation.dispatchAction(navEntryId, NavAction.To(direction, navOptions(builder)))
navigation.dispatchAction(navEntryId, NavigateTo(direction, navOptions(builder)))
}

override suspend fun navigate(direction: Any, builder: NavOptionsBuilder.() -> Unit) {
navigation.dispatchAction(navEntryId, NavigateTo(direction, navOptions(builder)))
}

override suspend fun navigate(deeplink: Uri, builder: NavOptionsBuilder.() -> Unit) {
navigation.dispatchAction(navEntryId, NavAction.Deeplink(deeplink, navOptions(builder)))
navigation.dispatchAction(navEntryId, Deeplink(deeplink, navOptions(builder)))
}

override suspend fun popBackStack(route: String, inclusive: Boolean, saveState: Boolean) {
navigation.dispatchAction(navEntryId, NavAction.Pop(route, inclusive, saveState))
navigation.dispatchAction(navEntryId, PopBackTo(route, inclusive, saveState))
}

override suspend fun popBackStack(route: Any, inclusive: Boolean, saveState: Boolean) {
navigation.dispatchAction(navEntryId, PopBackTo(route, inclusive, saveState))
}

override suspend fun navigateUp() {
navigation.dispatchAction(navEntryId, NavAction.Up)
navigation.dispatchAction(navEntryId, NavAction.NavigateUp)
}

override suspend fun <R : Any> setNavResult(result: R) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import androidx.lifecycle.Lifecycle.State.RESUMED
import androidx.lifecycle.flowWithLifecycle
import androidx.navigation.NavBackStackEntry
import androidx.navigation.NavController
import com.miquido.android.navigation.NavAction
import com.miquido.android.navigation.NavAction.Deeplink
import com.miquido.android.navigation.NavAction.NavigateTo
import com.miquido.android.navigation.NavAction.NavigateUp
import com.miquido.android.navigation.NavAction.PopBackTo
import com.miquido.android.navigation.viewmodel.AbstractNavigationViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
Expand All @@ -22,11 +25,23 @@ internal fun NavActionsHandler(
.collect { action ->
withContext(Dispatchers.Main) {
when (action) {
is NavAction.To -> navController.navigate(action.direction, action.options)
is NavAction.Deeplink -> navController.navigate(action.uri, action.options)
is NavAction.Pop -> navController.popBackStack(action.route, action.inclusive, action.saveState)
is NavAction.Up -> navController.navigateUp()
is NavigateTo -> navController.navigateTo(action)
is Deeplink -> navController.navigate(action.uri, action.options)
is PopBackTo -> navController.popBackTo(action)
is NavigateUp -> navController.navigateUp()
}
}
}
}

private fun NavController.navigateTo(direction: NavigateTo) = when {
direction.directionString != null -> navigate(direction.directionString)
direction.directionAny != null -> navigate(direction.directionAny)
else -> throw IllegalArgumentException("Invalid direction provided!")
}

private fun NavController.popBackTo(direction: PopBackTo) = when {
direction.routeString != null -> popBackStack(direction.routeString, direction.inclusive, direction.saveState)
direction.routeAny != null -> popBackStack(direction.routeAny, direction.inclusive, direction.saveState)
else -> throw IllegalArgumentException("Invalid route provided!")
}
Loading

0 comments on commit ad2c5c6

Please sign in to comment.