-
Notifications
You must be signed in to change notification settings - Fork 7
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 #727 from Orange-OpenSource/19-ods-module-about
19 - ODS About module
- Loading branch information
Showing
115 changed files
with
2,700 additions
and
654 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
100 changes: 100 additions & 0 deletions
100
app/src/main/java/com/orange/ods/app/ui/AppNavigation.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,100 @@ | ||
/* | ||
* | ||
* Copyright 2021 Orange | ||
* | ||
* Use of this source code is governed by an MIT-style | ||
* license that can be found in the LICENSE file or at | ||
* https://opensource.org/licenses/MIT. | ||
* / | ||
*/ | ||
|
||
package com.orange.ods.app.ui | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.remember | ||
import androidx.lifecycle.Lifecycle | ||
import androidx.navigation.NavBackStackEntry | ||
import androidx.navigation.NavController | ||
import androidx.navigation.NavDestination | ||
import androidx.navigation.NavGraph | ||
import androidx.navigation.NavHostController | ||
import androidx.navigation.compose.currentBackStackEntryAsState | ||
import androidx.navigation.compose.rememberNavController | ||
|
||
@Composable | ||
fun rememberAppNavigationState(navController: NavHostController = rememberNavController()) = remember(navController) { AppNavigationState(navController) } | ||
|
||
class AppNavigationState(val navController: NavHostController) { | ||
val previousRoute: String? | ||
get() = navController.previousBackStackEntry?.destination?.route | ||
|
||
val currentRoute: String? | ||
get() = navController.currentDestination?.route | ||
|
||
val currentScreen: Screen? | ||
@Composable get() { | ||
val routeArgs = navController.currentBackStackEntryAsState().value?.arguments | ||
return currentRoute?.let { getScreen(it, routeArgs) } | ||
} | ||
|
||
fun navigateToBottomBarRoute(route: String) { | ||
if (route != currentRoute) { | ||
navController.navigateToBottomBarRoute(route) | ||
} | ||
} | ||
|
||
fun navigateToElement(route: String, elementId: Long?, from: NavBackStackEntry) { | ||
// In order to discard duplicated navigation events, we check the Lifecycle | ||
if (from.lifecycleIsResumed()) { | ||
val fullRoute = if (elementId != null) "$route/$elementId" else route | ||
navController.navigate(fullRoute) | ||
} | ||
} | ||
|
||
fun upPress() { | ||
navController.navigateUp() | ||
} | ||
} | ||
|
||
fun NavController.navigateToElement(route: String, elementId: Long?, from: NavBackStackEntry) { | ||
// In order to discard duplicated navigation events, we check the Lifecycle | ||
if (from.lifecycleIsResumed()) { | ||
val fullRoute = if (elementId != null) "$route/$elementId" else route | ||
navigate(fullRoute) | ||
} | ||
} | ||
|
||
fun NavController.navigateToBottomBarRoute(route: String) { | ||
navigate(route) { | ||
// Avoid multiple copies of the same destination when | ||
// reselecting the same item | ||
launchSingleTop = true | ||
// Restore state when reselecting a previously selected item | ||
restoreState = true | ||
// Pop up backstack to the first destination and save state. This makes going back | ||
// to the start destination when pressing back in any other bottom tab. | ||
popUpTo(findStartDestination(graph).id) { | ||
saveState = true | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* If the lifecycle is not resumed it means this NavBackStackEntry already processed a nav event. | ||
* | ||
* This is used to de-duplicate navigation events. | ||
*/ | ||
private fun NavBackStackEntry.lifecycleIsResumed() = | ||
this.getLifecycle().currentState == Lifecycle.State.RESUMED | ||
|
||
private val NavGraph.startDestination: NavDestination? | ||
get() = findNode(startDestinationId) | ||
|
||
/** | ||
* Copied from similar function in NavigationUI.kt | ||
* | ||
* https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:navigation/navigation-ui/src/main/java/androidx/navigation/ui/NavigationUI.kt | ||
*/ | ||
private tailrec fun findStartDestination(graph: NavDestination): NavDestination { | ||
return if (graph is NavGraph) findStartDestination(graph.startDestination!!) else graph | ||
} |
Oops, something went wrong.