PeachAppNavHost.kt

11.7 KB · raw

1package com.peachpasswords.ui23import androidx.compose.animation.EnterTransition4import androidx.compose.animation.ExitTransition5import androidx.compose.animation.core.tween6import androidx.compose.animation.fadeIn7import androidx.compose.animation.fadeOut8import androidx.compose.animation.slideInVertically9import androidx.compose.animation.slideOutVertically10import androidx.compose.foundation.layout.Box11import androidx.compose.foundation.layout.fillMaxSize12import androidx.compose.runtime.Composable13import androidx.compose.runtime.LaunchedEffect14import androidx.compose.runtime.getValue15import androidx.compose.ui.Modifier16import androidx.lifecycle.compose.collectAsStateWithLifecycle17import androidx.navigation.NavHostController18import androidx.navigation.NavType19import androidx.navigation.compose.NavHost20import androidx.navigation.compose.composable21import androidx.navigation.compose.rememberNavController22import androidx.navigation.navArgument23import com.peachpasswords.ui.codex.CodexGenerateScreen24import com.peachpasswords.ui.codex.CodexRestoreScreen25import com.peachpasswords.ui.components.CopyFeedbackOverlay26import com.peachpasswords.ui.lock.LockScreen27import com.peachpasswords.ui.onboarding.OnboardingScreen28import com.peachpasswords.ui.pairing.ChirpPairScreen29import com.peachpasswords.ui.recovery.RecoveryWordEntryScreen30import com.peachpasswords.ui.settings.PanicScreen31import com.peachpasswords.ui.settings.SettingsScreen32import com.peachpasswords.ui.vault.VaultHealthScreen33import com.peachpasswords.ui.theme.PeachMotion34import com.peachpasswords.ui.vault.EntryDetailScreen35import com.peachpasswords.ui.vault.EntryEditScreen36import com.peachpasswords.ui.vault.ExportScreen37import com.peachpasswords.ui.vault.ImportScreen38import com.peachpasswords.ui.vault.NotesModeScreen39import com.peachpasswords.ui.vault.RecoveryWordsScreen40import com.peachpasswords.ui.vault.TrashScreen41import com.peachpasswords.ui.vault.VaultListScreen4243@Composable44fun PeachAppNavHost(45    navController: NavHostController = rememberNavController(),46    startDestination: PeachNav = PeachNav.Lock,47    isUnlocked: kotlinx.coroutines.flow.StateFlow<Boolean>? = null,48) {49    val nav: (PeachNav) -> Unit = { navController.navigate(it.route) }50    val back: () -> Unit = { navController.popBackStack() }5152    // When the vault locks for any reason — manual lock button, idle auto-lock, or53    // background lock (CF-1/CF-2) — drop to the lock screen and clear history so no54    // post-unlock screen lingers behind it. Guarded so it never fires on the lock55    // or onboarding screens (where the vault is legitimately locked).56    if (isUnlocked != null) {57        val unlocked by isUnlocked.collectAsStateWithLifecycle()58        LaunchedEffect(unlocked) {59            if (!unlocked) {60                val current = navController.currentDestination?.route61                if (current != null &&62                    current != PeachNav.Lock.route &&63                    current != PeachNav.Onboarding.route64                ) {65                    navController.navigate(PeachNav.Lock.route) {66                        popUpTo(0) { inclusive = true }67                        launchSingleTop = true68                    }69                }70            }71        }72    }7374    Box(Modifier.fillMaxSize()) {75        NavHost(76            navController = navController,77            startDestination = startDestination.route,78            enterTransition = {79                if (isVaultSheetTransition(initialState.destination.route, targetState.destination.route)) {80                    slideInVertically(81                        initialOffsetY = { it },82                        animationSpec = tween(PeachMotion.Enter, easing = PeachMotion.Spring),83                    ) + fadeIn(tween(PeachMotion.Enter, easing = PeachMotion.Spring))84                } else if (isVaultSheetTransition(initialState.destination.route, targetState.destination.route, reverse = true)) {85                    EnterTransition.None86                } else {87                    slideInVertically(88                        initialOffsetY = { (it * 0.05f).toInt() },89                        animationSpec = tween(PeachMotion.Enter, easing = PeachMotion.Spring),90                    ) + fadeIn(tween(PeachMotion.Enter, easing = PeachMotion.Spring))91                }92            },93            exitTransition = {94                if (isVaultSheetTransition(initialState.destination.route, targetState.destination.route)) {95                    ExitTransition.None96                } else if (isVaultSheetTransition(initialState.destination.route, targetState.destination.route, reverse = true)) {97                    slideOutVertically(98                        targetOffsetY = { it },99                        animationSpec = tween(PeachMotion.Exit, easing = PeachMotion.Retreat),100                    ) + fadeOut(tween(PeachMotion.Exit, easing = PeachMotion.Retreat))101                } else {102                    slideOutVertically(103                        targetOffsetY = { -(it * 0.03f).toInt() },104                        animationSpec = tween(PeachMotion.Exit, easing = PeachMotion.Retreat),105                    ) + fadeOut(tween(PeachMotion.Exit, easing = PeachMotion.Retreat))106                }107            },108            popEnterTransition = {109                if (isVaultSheetTransition(initialState.destination.route, targetState.destination.route, reverse = true)) {110                    EnterTransition.None111                } else {112                    fadeIn(tween(PeachMotion.Enter, easing = PeachMotion.Spring))113                }114            },115            popExitTransition = {116                if (isVaultSheetTransition(initialState.destination.route, targetState.destination.route, reverse = true)) {117                    slideOutVertically(118                        targetOffsetY = { it },119                        animationSpec = tween(PeachMotion.Exit, easing = PeachMotion.Retreat),120                    ) + fadeOut(tween(PeachMotion.Exit, easing = PeachMotion.Retreat))121                } else {122                    slideOutVertically(123                        targetOffsetY = { (it * 0.05f).toInt() },124                        animationSpec = tween(PeachMotion.Exit, easing = PeachMotion.Retreat),125                    ) + fadeOut(tween(PeachMotion.Exit, easing = PeachMotion.Retreat))126                }127            },128        ) {129        composable(PeachNav.Onboarding.route) {130            OnboardingScreen(131                onFinished = {132                    navController.navigate(PeachNav.Lock.route) {133                        popUpTo(PeachNav.Onboarding.route) { inclusive = true }134                    }135                },136            )137        }138        composable(PeachNav.Lock.route) {139            LockScreen(140                onUnlocked = { dest ->141                    navController.navigate(dest.route) {142                        popUpTo(PeachNav.Lock.route) { inclusive = true }143                    }144                },145                onNavigate = nav,146            )147        }148        composable(PeachNav.VaultList.route) { VaultListScreen(onNavigate = nav) }149150        composable(151            route = PeachNav.EntryDetail.ROUTE,152            arguments = listOf(navArgument("id") { type = NavType.StringType }),153        ) { backStack ->154            // C1: when this entry is the post-unlock landing surface, its155            // unlock navigation popped Lock off the stack (see the Lock156            // composable below), so there's no previous entry — it's the157            // deck's root, not a screen pushed from the vault list, and158            // shouldn't offer a "back to list" affordance that implies159            // otherwise. Only entries reached by tapping a vault-list row160            // (a real push) get a real back target.161            val isRoot = navController.previousBackStackEntry == null162            EntryDetailScreen(163                id = backStack.arguments?.getString("id").orEmpty(),164                onNavigate = nav,165                onBack = if (isRoot) null else ({ navController.navigate(PeachNav.VaultList.route) }),166            )167        }168        composable(169            route = PeachNav.EntryEdit.ROUTE,170            arguments = listOf(navArgument("id") { type = NavType.StringType; nullable = true }),171        ) { backStack ->172            val editId = backStack.arguments?.getString("id")?.takeIf { it.isNotBlank() }173            if (editId == null) {174                EntryEditScreen(175                    id = null,176                    onBack = back,177                    onOpenNote = { noteId ->178                        navController.navigate(PeachNav.NotesMode.of(noteId)) {179                            popUpTo(PeachNav.EntryEdit.ROUTE) { inclusive = true }180                        }181                    },182                )183            } else {184                EntryDetailScreen(185                    id = editId,186                    onNavigate = nav,187                    onBack = back,188                )189            }190        }191192        composable(193            route = PeachNav.NotesMode.ROUTE,194            arguments = listOf(navArgument("id") { type = NavType.StringType }),195        ) {196            NotesModeScreen(onBack = back)197        }198        composable(PeachNav.Settings.route) { SettingsScreen(onBack = back, onNavigate = nav) }199        composable(PeachNav.CodexGenerate.route) {200            CodexGenerateScreen(201                onBack = back,202                onSetupRecovery = { nav(PeachNav.RecoverySetup) },203                onUpgradePro = { nav(PeachNav.Settings) },204            )205        }206        composable(PeachNav.CodexRestore.route) { CodexRestoreScreen(onBack = back, onRestored = back) }207        composable(PeachNav.ChirpPair.route) { ChirpPairScreen(onBack = back) }208        composable(PeachNav.Health.route) {209            VaultHealthScreen(onBack = back, onEditEntry = { nav(PeachNav.EntryDetail(it)) })210        }211        composable(PeachNav.Trash.route) { TrashScreen(onBack = back) }212        composable(PeachNav.Import.route) { ImportScreen(onBack = back) }213        composable(PeachNav.Export.route) { ExportScreen(onBack = back) }214        composable(PeachNav.Panic.route) {215            PanicScreen(216                onBack = back,217                onDestroyed = {218                    navController.navigate(PeachNav.Lock.route) {219                        popUpTo(0) { inclusive = true }220                    }221                },222            )223        }224        composable(PeachNav.RecoverySetup.route) {225            RecoveryWordsScreen(226                requireConfirm = true,227                onDone = {228                    navController.navigate(PeachNav.VaultList.route) {229                        popUpTo(PeachNav.Lock.route) { inclusive = true }230                    }231                },232            )233        }234        composable(PeachNav.RecoveryView.route) {235            RecoveryWordsScreen(requireConfirm = false, onDone = back)236        }237        composable(PeachNav.RecoveryEntry.route) {238            RecoveryWordEntryScreen(239                onSuccess = {240                    navController.navigate(PeachNav.VaultList.route) {241                        popUpTo(PeachNav.Lock.route) { inclusive = true }242                    }243                },244                onBack = back,245                onRestore = { nav(PeachNav.Import) },246            )247        }248        }249        CopyFeedbackOverlay()250    }251}252253internal fun isVaultSheetTransition(fromRoute: String?, toRoute: String?, reverse: Boolean = false): Boolean {254    val fromEntry = isEntrySurfaceRoute(fromRoute)255    val toEntry = isEntrySurfaceRoute(toRoute)256    return if (reverse) {257        fromRoute == PeachNav.VaultList.route && toEntry258    } else {259        fromEntry && toRoute == PeachNav.VaultList.route260    }261}262263internal fun isEntrySurfaceRoute(route: String?): Boolean =264    route?.startsWith("entry/") == true || route?.startsWith("notes/") == true