Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/nightly-notes/2026-11-16-feat-profile-top-bar.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
## New
- The profile button shows on every main tab, and Settings can move into its menu
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package io.github.aedev.flow.ui.components.layout.topbar

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.size
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithContentDescription
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.unit.dp
import androidx.test.platform.app.InstrumentationRegistry
import io.github.aedev.flow.R
import kotlinx.coroutines.flow.MutableStateFlow
Expand All @@ -24,14 +30,19 @@ class FlowTopBarTest {

private fun setBar(
unread: Int = 0,
account: TopBarAccountSettings = TopBarAccountSettings(showProfile = true, settingsInProfileMenu = false),
content: @Composable () -> Unit,
) {
composeRule.setContent {
MaterialTheme {
ProvideFlowGlobalActions(
unreadNotifications = MutableStateFlow(unread),
account = MutableStateFlow(account),
onOpenNotifications = {},
onOpenSettings = {},
profileAction = { withSettings ->
Box(Modifier.size(48.dp).semantics { contentDescription = if (withSettings) PROFILE_WITH_SETTINGS else PROFILE })
},
content = content,
)
}
Expand All @@ -50,6 +61,36 @@ class FlowTopBarTest {
.assertIsDisplayed()
}

@Test
fun rootDestinationShowsTheProfileAfterSettings() {
setBar { FlowTopBar(title = "Library") }

composeRule.onNodeWithContentDescription(PROFILE).assertIsDisplayed()
}

@Test
fun crowdedBarFoldsSettingsIntoTheProfile() {
setBar { FlowTopBar(title = "Subscriptions", crowded = true) }

composeRule.onNodeWithContentDescription(PROFILE_WITH_SETTINGS).assertIsDisplayed()
composeRule
.onNodeWithContentDescription(context.getString(R.string.settings))
.assertDoesNotExist()
}

@Test
fun hiddenProfileKeepsSettingsEvenOnACrowdedBar() {
setBar(account = TopBarAccountSettings(showProfile = false, settingsInProfileMenu = true)) {
FlowTopBar(title = "Subscriptions", crowded = true)
}

composeRule
.onNodeWithContentDescription(context.getString(R.string.settings))
.assertIsDisplayed()
composeRule.onNodeWithContentDescription(PROFILE).assertDoesNotExist()
composeRule.onNodeWithContentDescription(PROFILE_WITH_SETTINGS).assertDoesNotExist()
}

@Test
fun detailDestinationShowsNeitherShellAction() {
setBar { FlowTopBar(title = "Buffer", onBack = {}) }
Expand Down Expand Up @@ -97,4 +138,9 @@ class FlowTopBarTest {
.onNodeWithContentDescription(context.getString(R.string.settings))
.assertDoesNotExist()
}

private companion object {
const val PROFILE = "profile"
const val PROFILE_WITH_SETTINGS = "profile with settings"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,8 @@ class PlayerPreferences(

// Show app logo icon in home screen top bar
val SHOW_APP_LOGO_ICON = booleanPreferencesKey("show_app_logo_icon")
val SHOW_PROFILE_IN_TOP_BAR = booleanPreferencesKey("show_profile_in_top_bar")
val SETTINGS_IN_PROFILE_MENU = booleanPreferencesKey("settings_in_profile_menu")

// Player comments preview
val COMMENTS_ENABLED = booleanPreferencesKey("comments_enabled")
Expand Down Expand Up @@ -4286,6 +4288,26 @@ class PlayerPreferences(
}
}

val showProfileInTopBar: Flow<Boolean> =
context.playerPreferencesDataStore.data
.map { preferences -> preferences[Keys.SHOW_PROFILE_IN_TOP_BAR] ?: true }

suspend fun setShowProfileInTopBar(enabled: Boolean) {
context.playerPreferencesDataStore.edit { preferences ->
preferences[Keys.SHOW_PROFILE_IN_TOP_BAR] = enabled
}
}

val settingsInProfileMenu: Flow<Boolean> =
context.playerPreferencesDataStore.data
.map { preferences -> preferences[Keys.SETTINGS_IN_PROFILE_MENU] ?: false }

suspend fun setSettingsInProfileMenu(enabled: Boolean) {
context.playerPreferencesDataStore.edit { preferences ->
preferences[Keys.SETTINGS_IN_PROFILE_MENU] = enabled
}
}

// Screen-level view mode toggles
val searchIsGridMode: Flow<Boolean> =
context.playerPreferencesDataStore.data
Expand Down
15 changes: 15 additions & 0 deletions app/src/main/java/io/github/aedev/flow/ui/FlowApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ import io.github.aedev.flow.ui.components.music.common.ProvideMusicPlaybackState
import io.github.aedev.flow.ui.components.music.sheet.LocalMusicMenus
import io.github.aedev.flow.ui.components.music.sheet.rememberMusicMenus
import io.github.aedev.flow.ui.components.musicplayer.sheet.rememberMusicPlayerSheetState
import io.github.aedev.flow.ui.components.profiles.ProfileSwitchViewModel
import io.github.aedev.flow.ui.components.profiles.ProfileTopBarAction
import io.github.aedev.flow.ui.components.profiles.SwitchRefusals
import io.github.aedev.flow.ui.components.settings.SettingsDestination
import io.github.aedev.flow.ui.components.settings.SettingsTarget
import io.github.aedev.flow.ui.components.shared.FlowLoadingIndicator
Expand Down Expand Up @@ -105,6 +108,8 @@ fun FlowApp(
// Activity-scoped so the unread badge has exactly one collector for the whole shell.
val notificationViewModel: NotificationViewModel = hiltViewModel(activity)
val equalizerViewModel: EqualizerViewModel = hiltViewModel(activity)
// One for every root top bar's profile avatar, rather than one per back stack entry.
val profileSwitchViewModel: ProfileSwitchViewModel = hiltViewModel(activity)
val playerUiStateResult = playerViewModel.uiState.collectAsStateWithLifecycle()
val playerUiState by playerUiStateResult

Expand Down Expand Up @@ -442,8 +447,17 @@ fun FlowApp(

ProvideFlowGlobalActions(
unreadNotifications = notificationViewModel.unreadCount,
account = profileSwitchViewModel.topBarAccount,
onOpenNotifications = { navController.navigate("notifications") },
onOpenSettings = { navController.navigate("settings") },
profileAction = { withSettings ->
ProfileTopBarAction(
withSettings = withSettings,
onOpenSettings = { navController.navigate("settings") },
onOpenSettingsPage = mediaNavigator::openSettings,
viewModel = profileSwitchViewModel,
)
},
) {
CompositionLocalProvider(
*mediaNavigationLocals(mediaNavigator),
Expand All @@ -456,6 +470,7 @@ fun FlowApp(
) {
if (needsOnboarding == false) SubscriptionBellAlerts(snackbarHostState)
LinkedSyncAlerts(snackbarHostState)
SwitchRefusals(profileSwitchViewModel)
PlayOnHost(onManageDevices = { mediaNavigator.openSettings(SettingsTarget(SettingsDestination.SYNC)) })
NavHost(
navController = navController,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ fun NavGraphBuilder.flowAppGraph(
// With a Search tab in the bar, the icon opens that tab, so Search keeps its state like any other tab.
if (searchIsTab()) navController.navigateToTab(FlowTab.Search, defaultStartRoute) else navController.navigate("search")
},
onOpenSettings = { target -> navController.navigate("settings?target=${target.encode()}") },
onNavigateToHistory = {
navController.navigate("history")
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,21 @@ import kotlinx.coroutines.flow.StateFlow
* Before this existed, `notifications` had a single entry point — the Home top bar — so turning
* Home off in navigation settings orphaned the screen entirely. Root tabs 3 (Subscriptions) and 4
* (Library) can never be hidden, so rendering these actions on every backless destination
* guarantees reachability for any navigation configuration.
* guarantees reachability for any navigation configuration. The profile avatar is here for the
* same reason.
*
* [unreadNotifications] is the flow rather than the current value so that a new notification
* invalidates only the badge that collects it, instead of the whole shell that provides it.
* [unreadNotifications] and [account] are flows rather than current values so that a change
* invalidates only the actions that collect them, instead of the whole shell that provides them.
* [account] is null until it is read, and [profileAction] draws the avatar, given whether its menu
* holds Settings; the shell supplies it so app chrome needs no knowledge of profiles.
*/
@Immutable
data class FlowGlobalActions(
val unreadNotifications: StateFlow<Int>,
val account: StateFlow<TopBarAccountSettings?>,
val onOpenNotifications: () -> Unit,
val onOpenSettings: () -> Unit,
val profileAction: @Composable (withSettings: Boolean) -> Unit,
)

/**
Expand All @@ -38,16 +43,20 @@ val LocalFlowGlobalActions = staticCompositionLocalOf<FlowGlobalActions?> { null
@Composable
fun ProvideFlowGlobalActions(
unreadNotifications: StateFlow<Int>,
account: StateFlow<TopBarAccountSettings?>,
onOpenNotifications: () -> Unit,
onOpenSettings: () -> Unit,
profileAction: @Composable (withSettings: Boolean) -> Unit,
content: @Composable () -> Unit,
) {
val actions =
remember(unreadNotifications, onOpenNotifications, onOpenSettings) {
remember(unreadNotifications, account, onOpenNotifications, onOpenSettings, profileAction) {
FlowGlobalActions(
unreadNotifications = unreadNotifications,
account = account,
onOpenNotifications = onOpenNotifications,
onOpenSettings = onOpenSettings,
profileAction = profileAction,
)
}
CompositionLocalProvider(LocalFlowGlobalActions provides actions, content = content)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ enum class FlowGlobalActionsMode {
* with a [leading] slot is still treated as a root destination.
* @param actions screen-specific actions. Keep this to two on a root destination; put the rest in
* [FlowTopBarOverflow] so the title keeps its width.
* @param trailingAction drawn after the global actions, last in the bar: Home's profile avatar.
* @param crowded set by a root destination with two [actions]: its profile avatar holds Settings by
* default, so the bar ends with three global actions rather than four.
*/
@OptIn(ExperimentalMaterial3Api::class)
@Composable
Expand All @@ -53,7 +54,7 @@ fun FlowTopBar(
actions: @Composable RowScope.() -> Unit = {},
globalActions: FlowGlobalActionsMode = FlowGlobalActionsMode.Auto,
windowInsets: WindowInsets = FlowTopBarDefaults.WindowInsets,
trailingAction: (@Composable () -> Unit)? = null,
crowded: Boolean = false,
) {
FlowTopBar(
modifier = modifier,
Expand All @@ -65,7 +66,7 @@ fun FlowTopBar(
actions = actions,
globalActions = globalActions,
windowInsets = windowInsets,
trailingAction = trailingAction,
crowded = crowded,
)
}

Expand All @@ -84,7 +85,7 @@ fun FlowTopBar(
actions: @Composable RowScope.() -> Unit = {},
globalActions: FlowGlobalActionsMode = FlowGlobalActionsMode.Auto,
windowInsets: WindowInsets = FlowTopBarDefaults.WindowInsets,
trailingAction: (@Composable () -> Unit)? = null,
crowded: Boolean = false,
) {
val showGlobalActions =
when (globalActions) {
Expand Down Expand Up @@ -115,9 +116,8 @@ fun FlowTopBar(
actions = {
actions()
if (showGlobalActions) {
FlowGlobalActionsRow()
FlowGlobalActionsRow(crowded)
}
trailingAction?.invoke()
},
colors = FlowTopBarDefaults.colors(),
windowInsets = windowInsets,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,34 @@ import androidx.lifecycle.compose.collectAsStateWithLifecycle
import io.github.aedev.flow.R

/**
* The Notifications and Settings actions every root destination gets.
* The Notifications, Settings and profile actions every root destination gets, Settings and profile
* as [topBarAccount] decides for a [crowded] or roomy bar. Until the account switches are read only
* Notifications shows, so a bar whose profile holds Settings never flashes the settings icon.
*
* Renders nothing when the shell has not provided [LocalFlowGlobalActions] — the case in previews
* and in isolated Compose tests.
*/
@Composable
internal fun FlowGlobalActionsRow() {
internal fun FlowGlobalActionsRow(crowded: Boolean) {
val actions = LocalFlowGlobalActions.current ?: return
val unreadCount by actions.unreadNotifications.collectAsStateWithLifecycle()
val settings by actions.account.collectAsStateWithLifecycle()

FlowNotificationsAction(
unreadCount = unreadCount,
onClick = actions.onOpenNotifications,
)
IconButton(onClick = actions.onOpenSettings) {
Icon(
imageVector = Icons.Outlined.Settings,
contentDescription = stringResource(R.string.settings),
)
val account = settings?.let { topBarAccount(it, crowded) } ?: return
if (account != TopBarAccount.PROFILE_WITH_SETTINGS) {
IconButton(onClick = actions.onOpenSettings) {
Icon(
imageVector = Icons.Outlined.Settings,
contentDescription = stringResource(R.string.settings),
)
}
}
if (account != TopBarAccount.SETTINGS_ONLY) {
actions.profileAction(account == TopBarAccount.PROFILE_WITH_SETTINGS)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.github.aedev.flow.ui.components.layout.topbar

import androidx.compose.runtime.Immutable

/** The two account switches as stored. */
@Immutable
data class TopBarAccountSettings(
val showProfile: Boolean,
val settingsInProfileMenu: Boolean,
)

/** Which account buttons a root top bar ends with. */
enum class TopBarAccount {
/** The settings icon, then the profile avatar. */
PROFILE_AND_SETTINGS,

/** The profile avatar alone; its menu opens Settings. */
PROFILE_WITH_SETTINGS,

/** The settings icon alone. */
SETTINGS_ONLY,
}

/**
* A hidden profile always leaves the settings icon, so no combination loses Settings. A [crowded] bar, one with two
* screen actions of its own, folds Settings into the profile so its title keeps its width.
*/
fun topBarAccount(
settings: TopBarAccountSettings,
crowded: Boolean,
): TopBarAccount =
when {
!settings.showProfile -> TopBarAccount.SETTINGS_ONLY
settings.settingsInProfileMenu || crowded -> TopBarAccount.PROFILE_WITH_SETTINGS
else -> TopBarAccount.PROFILE_AND_SETTINGS
}
Loading
Loading