PeachAppNavHost.kt

12.3 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) {149            // C1: non-null only when the list was pushed on top of an entry150            // (slide-up-to-open from EntryDetailSearchStrip) — slide back151            // down pops back to that exact entry instance.152            val hasOrigin = navController.previousBackStackEntry != null153            VaultListScreen(154                onNavigate = nav,155                onBack = if (hasOrigin) ({ navController.popBackStack() }) else null,156            )157        }158159        composable(160            route = PeachNav.EntryDetail.ROUTE,161            arguments = listOf(navArgument("id") { type = NavType.StringType }),162        ) { backStack ->163            // C1: when this entry is the post-unlock landing surface, its164            // unlock navigation popped Lock off the stack (see the Lock165            // composable below), so there's no previous entry — it's the166            // deck's root, not a screen pushed from the vault list, and167            // shouldn't offer a "back to list" affordance that implies168            // otherwise. Only entries reached by tapping a vault-list row169            // (a real push) get a real back target.170            val isRoot = navController.previousBackStackEntry == null171            EntryDetailScreen(172                id = backStack.arguments?.getString("id").orEmpty(),173                onNavigate = nav,174                onBack = if (isRoot) null else ({ navController.navigate(PeachNav.VaultList.route) }),175            )176        }177        composable(178            route = PeachNav.EntryEdit.ROUTE,179            arguments = listOf(navArgument("id") { type = NavType.StringType; nullable = true }),180        ) { backStack ->181            val editId = backStack.arguments?.getString("id")?.takeIf { it.isNotBlank() }182            if (editId == null) {183                EntryEditScreen(184                    id = null,185                    onBack = back,186                    onOpenNote = { noteId ->187                        navController.navigate(PeachNav.NotesMode.of(noteId)) {188                            popUpTo(PeachNav.EntryEdit.ROUTE) { inclusive = true }189                        }190                    },191                )192            } else {193                EntryDetailScreen(194                    id = editId,195                    onNavigate = nav,196                    onBack = back,197                )198            }199        }200201        composable(202            route = PeachNav.NotesMode.ROUTE,203            arguments = listOf(navArgument("id") { type = NavType.StringType }),204        ) {205            NotesModeScreen(onBack = back)206        }207        composable(PeachNav.Settings.route) { SettingsScreen(onBack = back, onNavigate = nav) }208        composable(PeachNav.CodexGenerate.route) {209            CodexGenerateScreen(210                onBack = back,211                onSetupRecovery = { nav(PeachNav.RecoverySetup) },212                onUpgradePro = { nav(PeachNav.Settings) },213            )214        }215        composable(PeachNav.CodexRestore.route) { CodexRestoreScreen(onBack = back, onRestored = back) }216        composable(PeachNav.ChirpPair.route) { ChirpPairScreen(onBack = back) }217        composable(PeachNav.Health.route) {218            VaultHealthScreen(onBack = back, onEditEntry = { nav(PeachNav.EntryDetail(it)) })219        }220        composable(PeachNav.Trash.route) { TrashScreen(onBack = back) }221        composable(PeachNav.Import.route) {222            ImportScreen(223                onBack = back,224                onVaultCreated = {225                    navController.navigate(PeachNav.RecoverySetup.route) {226                        popUpTo(PeachNav.Lock.route) { inclusive = true }227                    }228                },229            )230        }231        composable(PeachNav.Export.route) { ExportScreen(onBack = back) }232        composable(PeachNav.Panic.route) {233            PanicScreen(234                onBack = back,235                onDestroyed = {236                    navController.navigate(PeachNav.Lock.route) {237                        popUpTo(0) { inclusive = true }238                    }239                },240            )241        }242        composable(PeachNav.RecoverySetup.route) {243            RecoveryWordsScreen(244                requireConfirm = true,245                onDone = {246                    navController.navigate(PeachNav.VaultList.route) {247                        popUpTo(PeachNav.Lock.route) { inclusive = true }248                    }249                },250            )251        }252        composable(PeachNav.RecoveryView.route) {253            RecoveryWordsScreen(requireConfirm = false, onDone = back)254        }255        composable(PeachNav.RecoveryEntry.route) {256            RecoveryWordEntryScreen(257                onSuccess = {258                    navController.navigate(PeachNav.VaultList.route) {259                        popUpTo(PeachNav.Lock.route) { inclusive = true }260                    }261                },262                onBack = back,263                onRestore = { nav(PeachNav.Import) },264            )265        }266        }267        CopyFeedbackOverlay()268    }269}270271internal fun isVaultSheetTransition(fromRoute: String?, toRoute: String?, reverse: Boolean = false): Boolean {272    val fromEntry = isEntrySurfaceRoute(fromRoute)273    val toEntry = isEntrySurfaceRoute(toRoute)274    return if (reverse) {275        fromRoute == PeachNav.VaultList.route && toEntry276    } else {277        fromEntry && toRoute == PeachNav.VaultList.route278    }279}280281internal fun isEntrySurfaceRoute(route: String?): Boolean =282    route?.startsWith("entry/") == true || route?.startsWith("notes/") == true