backup: route through MainNavigation for predictive back
- Commit
3fa55934221e1387d5cb85dced93325903bd49df- Author
- vayun-mathur <[email protected]>
- When
- 2026-08-29 01:18 UTC (3 months ago)
- Parents
647d5a1
Diff
backup/src/main/java/com/vayunmathur/backup/Navigation.kt
diff --git a/backup/src/main/java/com/vayunmathur/backup/Navigation.kt b/backup/src/main/java/com/vayunmathur/backup/Navigation.ktindex 1ff8a421b8..8f7fe1935c 100644--- a/backup/src/main/java/com/vayunmathur/backup/Navigation.kt+++ b/backup/src/main/java/com/vayunmathur/backup/Navigation.kt@@ -1,10 +1,83 @@ package com.vayunmathur.backup +import android.Manifest+import android.content.Intent+import android.os.Build+import androidx.activity.compose.rememberLauncherForActivityResult+import androidx.activity.result.contract.ActivityResultContracts import androidx.compose.runtime.Composable+import androidx.compose.runtime.LaunchedEffect+import androidx.compose.ui.platform.LocalContext import com.vayunmathur.backup.platform.BackupViewModel-import com.vayunmathur.backup.ui.BackupApp+import com.vayunmathur.backup.ui.DashboardScreen+import com.vayunmathur.backup.ui.OnboardingScreen+import com.vayunmathur.library.util.MainNavigation+import com.vayunmathur.library.util.rememberNavBackStack @Composable fun Navigation(viewModel: BackupViewModel) {- BackupApp(viewModel)+ val context = LocalContext.current+ val state = viewModel.state+ val start = if (state.onboarded) Route.Dashboard else Route.Onboarding+ val backStack = rememberNavBackStack<Route>(start)++ // Onboarding ends because the ViewModel now has a key and a destination, not because anything+ // navigated - so the stack follows the flag rather than the other way round. reset() rather than+ // add(): there is no going back to onboarding once a recovery code and a backend exist, and+ // leaving it on the stack would let Back return to it.+ LaunchedEffect(state.onboarded) {+ if (backStack.last() != start) backStack.reset(start)+ }++ // Both screens can choose a backup destination, so the picker is owned here rather than by+ // either one of them.+ val folderPicker = rememberLauncherForActivityResult(+ ActivityResultContracts.OpenDocumentTree(),+ ) { uri ->+ if (uri != null) {+ context.contentResolver.takePersistableUriPermission(+ uri,+ Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION,+ )+ viewModel.setSafBackend(uri.toString())+ }+ }+ val mediaPermissions = rememberLauncherForActivityResult(+ ActivityResultContracts.RequestMultiplePermissions(),+ ) { viewModel.backupFilesNow() }+ val mediaPermissionList = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {+ arrayOf(+ Manifest.permission.READ_MEDIA_IMAGES,+ Manifest.permission.READ_MEDIA_VIDEO,+ Manifest.permission.READ_MEDIA_AUDIO,+ )+ } else {+ arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE)+ }++ MainNavigation(backStack) {+ entry<Route.Onboarding> {+ OnboardingScreen(+ state = state,+ onPickFolder = { folderPicker.launch(null) },+ onSetWebDav = viewModel::setWebDavBackend,+ onGenerate = viewModel::generateRecoveryCode,+ onConfirmNew = viewModel::confirmNewCode,+ onRestoreWithCode = viewModel::restoreWithCode,+ onDismissMessages = viewModel::dismissMessages,+ )+ }+ entry<Route.Dashboard> {+ DashboardScreen(+ state = state,+ onPickFolder = { folderPicker.launch(null) },+ onSetWebDav = viewModel::setWebDavBackend,+ onAppBackupToggle = viewModel::setAppBackupEnabled,+ onFileBackupToggle = viewModel::setFileBackupEnabled,+ onBackupNow = { mediaPermissions.launch(mediaPermissionList) },+ onRestoreNow = viewModel::restoreFilesNow,+ onDismissMessages = viewModel::dismissMessages,+ )+ }+ } }
backup/src/main/java/com/vayunmathur/backup/ui/BackupApp.kt
diff --git a/backup/src/main/java/com/vayunmathur/backup/ui/BackupApp.kt b/backup/src/main/java/com/vayunmathur/backup/ui/BackupApp.ktdeleted file mode 100644index 97c99fb9f1..0000000000--- a/backup/src/main/java/com/vayunmathur/backup/ui/BackupApp.kt+++ /dev/null@@ -1,71 +0,0 @@-package com.vayunmathur.backup.ui--import android.content.Intent-import android.os.Build-import androidx.activity.compose.rememberLauncherForActivityResult-import androidx.activity.result.contract.ActivityResultContracts-import androidx.compose.runtime.Composable-import androidx.compose.ui.platform.LocalContext-import com.vayunmathur.backup.platform.BackupUiState-import com.vayunmathur.backup.platform.BackupViewModel--/**- * Root of the Backup UI: routes to onboarding until a recovery code and a destination- * are set, then to the dashboard. Owns the SAF folder picker so both flows can select- * a backup destination.- */-@Composable-fun BackupApp(viewModel: BackupViewModel) {- val context = LocalContext.current- val state = viewModel.state-- val folderPicker = rememberLauncherForActivityResult(- ActivityResultContracts.OpenDocumentTree(),- ) { uri ->- if (uri != null) {- context.contentResolver.takePersistableUriPermission(- uri,- Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION,- )- viewModel.setSafBackend(uri.toString())- }- }-- val mediaPermissions = rememberLauncherForActivityResult(- ActivityResultContracts.RequestMultiplePermissions(),- ) { viewModel.backupFilesNow() }-- if (state.onboarded) {- DashboardScreen(- state = state,- onPickFolder = { folderPicker.launch(null) },- onSetWebDav = viewModel::setWebDavBackend,- onAppBackupToggle = viewModel::setAppBackupEnabled,- onFileBackupToggle = viewModel::setFileBackupEnabled,- onBackupNow = { mediaPermissions.launch(mediaPermissionList()) },- onRestoreNow = viewModel::restoreFilesNow,- onDismissMessages = viewModel::dismissMessages,- )- } else {- OnboardingScreen(- state = state,- onPickFolder = { folderPicker.launch(null) },- onSetWebDav = viewModel::setWebDavBackend,- onGenerate = viewModel::generateRecoveryCode,- onConfirmNew = viewModel::confirmNewCode,- onRestoreWithCode = viewModel::restoreWithCode,- onDismissMessages = viewModel::dismissMessages,- )- }-}--private fun mediaPermissionList(): Array<String> =- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {- arrayOf(- android.Manifest.permission.READ_MEDIA_IMAGES,- android.Manifest.permission.READ_MEDIA_VIDEO,- android.Manifest.permission.READ_MEDIA_AUDIO,- )- } else {- arrayOf(android.Manifest.permission.READ_EXTERNAL_STORAGE)- }