youpipe: add setting to choose default startup page
- Commit
ba6f3bfe9a6fefafd138b0064e5e479e4dbf3911- Author
- Vayun Mathur <[email protected]>
- When
- 2026-07-30 15:39 UTC (4 months ago)
- Parents
0c52b5a
Diff
youpipe/src/main/java/com/vayunmathur/youpipe/MainActivity.kt
diff --git a/youpipe/src/main/java/com/vayunmathur/youpipe/MainActivity.kt b/youpipe/src/main/java/com/vayunmathur/youpipe/MainActivity.ktindex b22ad46b28..fedc756cd7 100644--- a/youpipe/src/main/java/com/vayunmathur/youpipe/MainActivity.kt+++ b/youpipe/src/main/java/com/vayunmathur/youpipe/MainActivity.kt@@ -27,6 +27,7 @@ import com.vayunmathur.library.ui.IconList import com.vayunmathur.library.ui.IconSubscriptions import com.vayunmathur.library.ui.IconSettings import com.vayunmathur.library.util.BottomBarItem+import com.vayunmathur.library.util.DataStoreUtils import com.vayunmathur.library.util.DialogPage import com.vayunmathur.library.util.MainNavigation import com.vayunmathur.library.room.buildDatabase@@ -101,10 +102,23 @@ class MainActivity : ComponentActivity() { youPipeViewModel setContent { DynamicTheme {- Navigation(getRoute(intent.data), youPipeViewModel)+ Navigation(resolveInitialBackStack(intent.data), youPipeViewModel) } } }++ /**+ * Decide the initial backstack at launch. A `watch` deep-link always wins;+ * otherwise fall back to the user's chosen default page (see+ * [DEFAULT_PAGE_KEY]), defaulting to Home. Read synchronously here because+ * this runs in [onCreate] before `setContent`.+ */+ private fun resolveInitialBackStack(uri: Uri?): List<Route> {+ if (uri != null && "watch" in uri.pathSegments && "v" in uri.queryParameterNames) {+ return listOf(Route.VideoPage(videoURLtoID(uri.toString())))+ }+ return defaultBackStack(DataStoreUtils.getInstance(this).getString(DEFAULT_PAGE_KEY))+ } override fun onPictureInPictureModeChanged( isInPictureInPictureMode: Boolean, newConfig: Configuration@@ -125,10 +139,44 @@ class MainActivity : ComponentActivity() { } } -fun getRoute(uri: Uri?): Route =- if (uri != null && "watch" in uri.pathSegments && "v" in uri.queryParameterNames)- Route.VideoPage(videoURLtoID(uri.toString()))- else Route.SearchPage+// --- Default startup page ("Open to" setting) ---++/** DataStore key holding the persisted default-page choice. */+const val DEFAULT_PAGE_KEY = "default_page"++const val DEFAULT_PAGE_HOME = "home"+const val DEFAULT_PAGE_SUBSCRIPTIONS = "subscriptions"+const val DEFAULT_PAGE_ALL_SUBSCRIPTIONS = "all_subscriptions"+const val DEFAULT_PAGE_HISTORY = "history"+const val DEFAULT_PAGE_DOWNLOADS = "downloads"+const val DEFAULT_PAGE_SETTINGS = "settings"++/**+ * Ordered options for the "Open to" picker: (persisted key, label string res).+ * Stable string keys are used instead of [Route] objects so the choice+ * serializes cleanly and survives refactors.+ */+val DEFAULT_PAGE_OPTIONS: List<Pair<String, Int>> = listOf(+ DEFAULT_PAGE_HOME to R.string.page_home,+ DEFAULT_PAGE_SUBSCRIPTIONS to R.string.title_subscriptions,+ DEFAULT_PAGE_ALL_SUBSCRIPTIONS to R.string.label_all_subscriptions,+ DEFAULT_PAGE_HISTORY to R.string.title_history,+ DEFAULT_PAGE_DOWNLOADS to R.string.page_downloads,+ DEFAULT_PAGE_SETTINGS to R.string.title_settings,+)++/** Map a persisted default-page key to the initial backstack to launch with. */+fun defaultBackStack(key: String?): List<Route> = when (key) {+ DEFAULT_PAGE_SUBSCRIPTIONS -> listOf(Route.SubscriptionsPage)+ // Seed the Subscriptions root beneath the all-subscriptions feed so Back+ // returns to the Subscriptions list instead of exiting the app.+ DEFAULT_PAGE_ALL_SUBSCRIPTIONS ->+ listOf(Route.SubscriptionsPage, Route.SubscriptionVideosPage(null))+ DEFAULT_PAGE_HISTORY -> listOf(Route.History)+ DEFAULT_PAGE_DOWNLOADS -> listOf(Route.Downloads)+ DEFAULT_PAGE_SETTINGS -> listOf(Route.Settings)+ else -> listOf(Route.SearchPage) // home / unset / unknown+} @Serializable sealed interface Route: NavKey {@@ -164,8 +212,8 @@ sealed interface Route: NavKey { } @Composable-fun Navigation(initialRoute: Route, ypvm: YouPipeViewModel) {- val backStack = rememberNavBackStack(initialRoute)+fun Navigation(initialBackStack: List<Route>, ypvm: YouPipeViewModel) {+ val backStack = rememberNavBackStack(initialBackStack) MainNavigation(backStack) { entry<Route.SearchPage> { SearchPage(backStack, ypvm)
youpipe/src/main/java/com/vayunmathur/youpipe/ui/SettingsPage.kt
diff --git a/youpipe/src/main/java/com/vayunmathur/youpipe/ui/SettingsPage.kt b/youpipe/src/main/java/com/vayunmathur/youpipe/ui/SettingsPage.ktindex 52def9c0a6..df7351fb66 100644--- a/youpipe/src/main/java/com/vayunmathur/youpipe/ui/SettingsPage.kt+++ b/youpipe/src/main/java/com/vayunmathur/youpipe/ui/SettingsPage.kt@@ -41,6 +41,7 @@ import androidx.compose.foundation.layout.width import androidx.compose.ui.unit.dp import com.vayunmathur.library.util.BottomNavBar import com.vayunmathur.library.util.NavBackStack+import com.vayunmathur.youpipe.DEFAULT_PAGE_OPTIONS import com.vayunmathur.youpipe.MAIN_BOTTOM_BAR_ITEMS import com.vayunmathur.youpipe.R import com.vayunmathur.youpipe.Route@@ -79,6 +80,33 @@ private fun LanguageSelector(selectedCode: String, onSelect: (String) -> Unit) { } } +@OptIn(ExperimentalMaterial3Api::class)+@Composable+private fun DefaultPageSelector(selectedKey: String, onSelect: (String) -> Unit) {+ var expanded by remember { mutableStateOf(false) }+ val currentLabelRes = (DEFAULT_PAGE_OPTIONS.firstOrNull { it.first == selectedKey }+ ?: DEFAULT_PAGE_OPTIONS.first()).second++ Box {+ ListItem(+ content = { Text(stringResource(R.string.label_default_page)) },+ supportingContent = { Text(stringResource(currentLabelRes)) },+ trailingContent = {+ IconArrowDropDown()+ },+ modifier = Modifier.padding(start = 16.dp).clickable { expanded = true },+ )+ DropdownMenu(expanded = expanded, onDismissRequest = { expanded = false }) {+ DEFAULT_PAGE_OPTIONS.forEach { (key, labelRes) ->+ DropdownMenuItem(+ text = { Text(stringResource(labelRes)) },+ onClick = { onSelect(key); expanded = false },+ )+ }+ }+ }+}+ private val SOURCE_TOGGLES = listOf( RecSource.RELATED to R.string.label_source_related, RecSource.TRENDING to R.string.label_source_trending,@@ -96,6 +124,7 @@ fun SettingsPage( val sponsorBlockCategories by ypvm.sponsorBlockCategories.collectAsState() val deArrowEnabled by ypvm.deArrowEnabled.collectAsState() val youtubeLanguage by ypvm.youtubeLanguage.collectAsState()+ val defaultPage by ypvm.defaultPage.collectAsState() val recPrefs by ypvm.recommendationPreferences.collectAsState() val isLoading by ypvm.isImporting.collectAsState() val progress by ypvm.importProgress.collectAsState()@@ -122,6 +151,18 @@ fun SettingsPage( ) { paddingValues -> if (!isLoading) { LazyColumn(Modifier.padding(paddingValues)) {+ item {+ ListItem(+ content = { Text(stringResource(R.string.label_general)) }+ )+ }+ item {+ DefaultPageSelector(+ selectedKey = defaultPage,+ onSelect = { ypvm.setDefaultPage(it) },+ )+ }+ item { HorizontalDivider() } item { ListItem( content = { Text(stringResource(R.string.label_sponsorblock)) }
youpipe/src/main/java/com/vayunmathur/youpipe/util/YouPipeViewModel.kt
diff --git a/youpipe/src/main/java/com/vayunmathur/youpipe/util/YouPipeViewModel.kt b/youpipe/src/main/java/com/vayunmathur/youpipe/util/YouPipeViewModel.ktindex 62566260eb..acf50c7e83 100644--- a/youpipe/src/main/java/com/vayunmathur/youpipe/util/YouPipeViewModel.kt+++ b/youpipe/src/main/java/com/vayunmathur/youpipe/util/YouPipeViewModel.kt@@ -10,6 +10,8 @@ import androidx.lifecycle.viewModelScope import androidx.work.WorkInfo import androidx.work.WorkManager import com.vayunmathur.library.util.DataStoreUtils+import com.vayunmathur.youpipe.DEFAULT_PAGE_HOME+import com.vayunmathur.youpipe.DEFAULT_PAGE_KEY import com.vayunmathur.youpipe.data.CachedRelatedVideo import com.vayunmathur.youpipe.data.CachedRelatedVideoDao import com.vayunmathur.youpipe.data.ChannelPreference@@ -937,6 +939,18 @@ class YouPipeViewModel( } } + /** Which screen the app opens to at launch. See [DEFAULT_PAGE_KEY]; default Home. */+ val defaultPage: StateFlow<String> = DataStoreUtils+ .getInstance(application)+ .stringFlow(DEFAULT_PAGE_KEY)+ .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), DEFAULT_PAGE_HOME)++ fun setDefaultPage(key: String) {+ viewModelScope.launch {+ DataStoreUtils.getInstance(getApplication()).setString(DEFAULT_PAGE_KEY, key)+ }+ }+ private val _sponsorBlockCategories: StateFlow<Set<String>> = DataStoreUtils .getInstance(application) .stringSetFlow("sponsorblock_categories")
youpipe/src/main/res/values/strings.xml
diff --git a/youpipe/src/main/res/values/strings.xml b/youpipe/src/main/res/values/strings.xmlindex ed8a9eaee5..0089756353 100644--- a/youpipe/src/main/res/values/strings.xml+++ b/youpipe/src/main/res/values/strings.xml@@ -95,6 +95,15 @@ <!-- Button: update category. Verb --> <string name="action_update">Update</string> + <!-- Settings section header: general app options. Noun, Title Case -->+ <string name="label_general">General</string>+ <!-- Settings row: choose which screen the app opens to at launch. Verb phrase -->+ <string name="label_default_page">Open to</string>+ <!-- Default-page option: the home/search feed. Noun -->+ <string name="page_home">Home</string>+ <!-- Default-page option: the downloads list. Noun plural -->+ <string name="page_downloads">Downloads</string>+ <!-- TopAppBar title for watch history. Noun --> <string name="title_history">History</string> <!-- Selection mode badge: number selected (lowercase s in this variant). %1$s = count e.g. 3 -->