-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #529 from nimblehq/chore/improve-the-base-navigation
[#552] Improve the base navigation with a "MainNavGraph"
- Loading branch information
Showing
33 changed files
with
332 additions
and
220 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
sample-compose/app/src/androidTest/java/co/nimblehq/sample/compose/test/MockUtil.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package co.nimblehq.sample.compose.test | ||
|
||
import co.nimblehq.sample.compose.domain.model.Model | ||
|
||
object MockUtil { | ||
|
||
val models = listOf( | ||
Model( | ||
id = 1, | ||
username = "name1", | ||
), | ||
Model( | ||
id = 2, | ||
username = "name2", | ||
), | ||
Model( | ||
id = 3, | ||
username = "name3", | ||
), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 4 additions & 33 deletions
37
sample-compose/app/src/main/java/co/nimblehq/sample/compose/ui/AppDestination.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,10 @@ | ||
package co.nimblehq.sample.compose.ui | ||
|
||
import androidx.navigation.NamedNavArgument | ||
import androidx.navigation.NavType | ||
import androidx.navigation.navArgument | ||
import co.nimblehq.sample.compose.ui.models.UiModel | ||
import co.nimblehq.sample.compose.ui.base.BaseDestination | ||
|
||
const val KeyId = "id" | ||
const val KeyModel = "model" | ||
sealed class AppDestination { | ||
|
||
sealed class AppDestination(val route: String = "") { | ||
object RootNavGraph : BaseDestination("rootNavGraph") | ||
|
||
open val arguments: List<NamedNavArgument> = emptyList() | ||
|
||
open var destination: String = route | ||
|
||
open var parcelableArgument: Pair<String, Any?> = "" to null | ||
|
||
object Up : AppDestination() | ||
|
||
object Home : AppDestination("home") | ||
|
||
object Second : AppDestination("second/{$KeyId}") { | ||
|
||
override val arguments = listOf( | ||
navArgument(KeyId) { type = NavType.StringType } | ||
) | ||
|
||
fun createRoute(id: String) = apply { | ||
destination = "second/$id" | ||
} | ||
} | ||
|
||
object Third : AppDestination("third") { | ||
fun addParcel(value: UiModel) = apply { | ||
parcelableArgument = KeyModel to value | ||
} | ||
} | ||
object MainNavGraph : BaseDestination("mainNavGraph") | ||
} |
55 changes: 55 additions & 0 deletions
55
sample-compose/app/src/main/java/co/nimblehq/sample/compose/ui/AppNavGraph.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package co.nimblehq.sample.compose.ui | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.navigation.NavBackStackEntry | ||
import androidx.navigation.NavDeepLink | ||
import androidx.navigation.NavGraphBuilder | ||
import androidx.navigation.NavHostController | ||
import androidx.navigation.compose.NavHost | ||
import androidx.navigation.compose.composable | ||
import co.nimblehq.sample.compose.ui.base.BaseDestination | ||
import co.nimblehq.sample.compose.ui.screens.main.mainNavGraph | ||
|
||
@Composable | ||
fun AppNavigation( | ||
navController: NavHostController, | ||
) { | ||
NavHost( | ||
navController = navController, | ||
route = AppDestination.RootNavGraph.route, | ||
startDestination = AppDestination.MainNavGraph.destination | ||
) { | ||
mainNavGraph(navController = navController) | ||
} | ||
} | ||
|
||
fun NavGraphBuilder.composable( | ||
destination: BaseDestination, | ||
deepLinks: List<NavDeepLink> = emptyList(), | ||
content: @Composable (NavBackStackEntry) -> Unit, | ||
) { | ||
composable( | ||
route = destination.route, | ||
arguments = destination.arguments, | ||
deepLinks = deepLinks, | ||
content = content | ||
) | ||
} | ||
|
||
/** | ||
* Navigate to provided [BaseDestination] with a Pair of key value String and Data [parcel] | ||
* Caution to use this method. This method use savedStateHandle to store the Parcelable data. | ||
* When previousBackstackEntry is popped out from navigation stack, savedStateHandle will return null and cannot retrieve data. | ||
* eg.Login -> Home, the Login screen will be popped from the back-stack on logging in successfully. | ||
*/ | ||
fun NavHostController.navigate(destination: BaseDestination, parcel: Pair<String, Any?>? = null) { | ||
when (destination) { | ||
is BaseDestination.Up -> navigateUp() | ||
else -> { | ||
parcel?.let { (key, value) -> | ||
currentBackStackEntry?.savedStateHandle?.set(key, value) | ||
} | ||
navigate(route = destination.destination) | ||
} | ||
} | ||
} |
78 changes: 0 additions & 78 deletions
78
sample-compose/app/src/main/java/co/nimblehq/sample/compose/ui/AppNavigation.kt
This file was deleted.
Oops, something went wrong.
14 changes: 14 additions & 0 deletions
14
sample-compose/app/src/main/java/co/nimblehq/sample/compose/ui/base/BaseDestination.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package co.nimblehq.sample.compose.ui.base | ||
|
||
import androidx.navigation.NamedNavArgument | ||
|
||
abstract class BaseDestination(val route: String = "") { | ||
|
||
open val arguments: List<NamedNavArgument> = emptyList() | ||
|
||
open var destination: String = route | ||
|
||
open var parcelableArgument: Pair<String, Any?> = "" to null | ||
|
||
object Up : BaseDestination() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...blehq/sample/compose/ui/screens/AppBar.kt → ...mblehq/sample/compose/ui/common/AppBar.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...e-compose/app/src/main/java/co/nimblehq/sample/compose/ui/screens/main/MainDestination.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package co.nimblehq.sample.compose.ui.screens.main | ||
|
||
import androidx.navigation.NavType | ||
import androidx.navigation.navArgument | ||
import co.nimblehq.sample.compose.ui.base.BaseDestination | ||
import co.nimblehq.sample.compose.ui.models.UiModel | ||
|
||
const val KeyId = "id" | ||
const val KeyModel = "model" | ||
|
||
sealed class MainDestination { | ||
|
||
object Home : BaseDestination("home") | ||
|
||
object Second : BaseDestination("second/{$KeyId}") { | ||
|
||
override val arguments = listOf( | ||
navArgument(KeyId) { type = NavType.StringType } | ||
) | ||
|
||
fun createRoute(id: String) = apply { | ||
destination = "second/$id" | ||
} | ||
} | ||
|
||
object Third : BaseDestination("third") { | ||
fun addParcel(value: UiModel) = apply { | ||
parcelableArgument = KeyModel to value | ||
} | ||
} | ||
} |
Oops, something went wrong.