compose: navigation: Migrate from navigation2 to navigation3
Signed-off-by: Aayush Gupta <aayushgupta219@gmail.com>
This commit is contained in:
@@ -11,10 +11,9 @@ import androidx.activity.compose.setContent
|
||||
import androidx.activity.enableEdgeToEdge
|
||||
import androidx.compose.runtime.CompositionLocalProvider
|
||||
import androidx.core.content.IntentCompat
|
||||
import androidx.navigation.compose.rememberNavController
|
||||
import com.aurora.store.compose.compositions.UI
|
||||
import com.aurora.store.compose.compositions.LocalUI
|
||||
import com.aurora.store.compose.navigation.NavGraph
|
||||
import com.aurora.store.compose.navigation.NavDisplay
|
||||
import com.aurora.store.compose.navigation.Screen
|
||||
import com.aurora.store.compose.theme.AuroraTheme
|
||||
import com.aurora.store.util.PackageUtil
|
||||
@@ -42,8 +41,7 @@ class ComposeActivity : ComponentActivity() {
|
||||
setContent {
|
||||
CompositionLocalProvider(LocalUI provides localUI) {
|
||||
AuroraTheme {
|
||||
val navController = rememberNavController()
|
||||
NavGraph(navHostController = navController, startDestination = startDestination)
|
||||
NavDisplay(startDestination = startDestination)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2025 The Calyx Institute
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*/
|
||||
|
||||
package com.aurora.store.compose.navigation
|
||||
|
||||
import androidx.activity.compose.LocalActivity
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.lifecycle.viewmodel.navigation3.rememberViewModelStoreNavEntryDecorator
|
||||
import androidx.navigation3.runtime.NavKey
|
||||
import androidx.navigation3.runtime.entry
|
||||
import androidx.navigation3.runtime.entryProvider
|
||||
import androidx.navigation3.runtime.rememberNavBackStack
|
||||
import androidx.navigation3.runtime.rememberSavedStateNavEntryDecorator
|
||||
import androidx.navigation3.ui.NavDisplay
|
||||
import com.aurora.store.compose.ui.commons.BlacklistScreen
|
||||
import com.aurora.store.compose.ui.details.AppDetailsScreen
|
||||
import com.aurora.store.compose.ui.dev.DevProfileScreen
|
||||
import com.aurora.store.compose.ui.search.SearchScreen
|
||||
|
||||
/**
|
||||
* Navigation display for compose screens
|
||||
* @param startDestination Starting destination for the activity/app
|
||||
*/
|
||||
@Composable
|
||||
fun NavDisplay(startDestination: NavKey) {
|
||||
val backstack = rememberNavBackStack(startDestination)
|
||||
|
||||
// TODO: Drop this logic once everything is in compose
|
||||
val activity = LocalActivity.current
|
||||
fun onNavigateUp() {
|
||||
if (backstack.size == 1) activity?.finish() else backstack.removeLastOrNull()
|
||||
}
|
||||
|
||||
NavDisplay(
|
||||
backStack = backstack,
|
||||
entryDecorators = listOf(
|
||||
rememberSavedStateNavEntryDecorator(),
|
||||
rememberViewModelStoreNavEntryDecorator()
|
||||
),
|
||||
entryProvider = entryProvider {
|
||||
entry<Screen.Blacklist> {
|
||||
BlacklistScreen(onNavigateUp = { onNavigateUp() })
|
||||
}
|
||||
|
||||
entry<Screen.Search> {
|
||||
SearchScreen(onNavigateUp = { onNavigateUp() })
|
||||
}
|
||||
|
||||
entry<Screen.AppDetails> { screen ->
|
||||
AppDetailsScreen(
|
||||
packageName = screen.packageName,
|
||||
onNavigateUp = { onNavigateUp() },
|
||||
onNavigateToAppDetails = { packageName ->
|
||||
backstack.add(Screen.AppDetails(packageName))
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
entry<Screen.DevProfile> { screen ->
|
||||
DevProfileScreen(
|
||||
developerId = screen.developerId,
|
||||
onNavigateUp = { onNavigateUp() },
|
||||
onNavigateToAppDetails = { packageName ->
|
||||
backstack.add(Screen.AppDetails(packageName))
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2025 The Calyx Institute
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*/
|
||||
|
||||
package com.aurora.store.compose.navigation
|
||||
|
||||
import androidx.activity.compose.LocalActivity
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.navigation.NavHostController
|
||||
import androidx.navigation.compose.NavHost
|
||||
import androidx.navigation.compose.composable
|
||||
import androidx.navigation.toRoute
|
||||
import com.aurora.store.compose.ui.commons.BlacklistScreen
|
||||
import com.aurora.store.compose.ui.details.AppDetailsScreen
|
||||
import com.aurora.store.compose.ui.dev.DevProfileScreen
|
||||
import com.aurora.store.compose.ui.search.SearchScreen
|
||||
|
||||
/**
|
||||
* Navigation graph for compose screens
|
||||
* @param navHostController [NavHostController] to navigate with compose
|
||||
* @param startDestination Starting destination for the activity/app
|
||||
*/
|
||||
@Composable
|
||||
fun NavGraph(navHostController: NavHostController, startDestination: Screen) {
|
||||
// TODO: Drop this logic once everything is in compose
|
||||
val activity = LocalActivity.current
|
||||
fun onNavigateUp() {
|
||||
if (navHostController.previousBackStackEntry != null) {
|
||||
navHostController.navigateUp()
|
||||
} else {
|
||||
activity?.finish()
|
||||
}
|
||||
}
|
||||
|
||||
NavHost(navController = navHostController, startDestination = startDestination) {
|
||||
composable<Screen.Blacklist> {
|
||||
BlacklistScreen(onNavigateUp = { onNavigateUp() })
|
||||
}
|
||||
|
||||
composable<Screen.Search> {
|
||||
SearchScreen(onNavigateUp = { onNavigateUp() })
|
||||
}
|
||||
|
||||
composable<Screen.AppDetails> { backstackEntry ->
|
||||
val appDetails = backstackEntry.toRoute<Screen.AppDetails>()
|
||||
AppDetailsScreen(
|
||||
packageName = appDetails.packageName,
|
||||
onNavigateUp = { onNavigateUp() },
|
||||
onNavigateToAppDetails = { packageName ->
|
||||
navHostController.navigate(Screen.AppDetails(packageName))
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
composable<Screen.DevProfile> { backstackEntry ->
|
||||
val devProfile = backstackEntry.toRoute<Screen.DevProfile>()
|
||||
DevProfileScreen(
|
||||
developerId = devProfile.developerId,
|
||||
onNavigateUp = { onNavigateUp() },
|
||||
onNavigateToAppDetails = { packageName ->
|
||||
navHostController.navigate(Screen.AppDetails(packageName))
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ package com.aurora.store.compose.navigation
|
||||
import android.os.Parcelable
|
||||
import androidx.annotation.DrawableRes
|
||||
import androidx.annotation.StringRes
|
||||
import androidx.navigation3.runtime.NavKey
|
||||
import com.aurora.store.R
|
||||
import kotlinx.parcelize.Parcelize
|
||||
import kotlinx.serialization.Serializable
|
||||
@@ -22,7 +23,7 @@ import kotlinx.serialization.Serializable
|
||||
sealed class Screen(
|
||||
@StringRes val label: Int? = null,
|
||||
@DrawableRes val icon: Int? = null
|
||||
) : Parcelable {
|
||||
) : NavKey, Parcelable {
|
||||
|
||||
companion object {
|
||||
const val PARCEL_KEY = "SCREEN"
|
||||
|
||||
Reference in New Issue
Block a user