Skip to content

Commit

Permalink
Long click on back arrow to go to home
Browse files Browse the repository at this point in the history
  • Loading branch information
z-huang committed Jan 21, 2024
1 parent 6066b94 commit 6660e8b
Show file tree
Hide file tree
Showing 25 changed files with 264 additions and 46 deletions.
31 changes: 21 additions & 10 deletions app/src/main/java/com/zionhuang/music/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ import com.zionhuang.music.ui.screens.search.OnlineSearchScreen
import com.zionhuang.music.ui.screens.settings.*
import com.zionhuang.music.ui.theme.*
import com.zionhuang.music.ui.utils.appBarScrollBehavior
import com.zionhuang.music.ui.utils.backToMain
import com.zionhuang.music.ui.utils.canNavigateUp
import com.zionhuang.music.ui.utils.resetHeightOffset
import com.zionhuang.music.utils.dataStore
Expand Down Expand Up @@ -205,9 +206,7 @@ class MainActivity : ComponentActivity() {
val navController = rememberNavController()
val navBackStackEntry by navController.currentBackStackEntryAsState()

val navigationItems = remember {
listOf(Screens.Home, Screens.Songs, Screens.Artists, Screens.Albums, Screens.Playlists)
}
val navigationItems = remember { Screens.MainScreens }
val defaultOpenTab = remember {
dataStore[DefaultOpenTabKey].toEnum(defaultValue = NavigationTab.HOME)
}
Expand Down Expand Up @@ -586,16 +585,28 @@ class MainActivity : ComponentActivity() {
)
},
leadingIcon = {
IconButton(onClick = {
when {
active -> onActiveChange(false)
navController.canNavigateUp && !navigationItems.fastAny { it.route == navBackStackEntry?.destination?.route } -> {
navController.navigateUp()
IconButton(
onClick = {
when {
active -> onActiveChange(false)
navController.canNavigateUp && !navigationItems.fastAny { it.route == navBackStackEntry?.destination?.route } -> {
navController.navigateUp()
}

else -> onActiveChange(true)
}
},
onLongClick = {
when {
active -> {}
navController.canNavigateUp && !navigationItems.fastAny { it.route == navBackStackEntry?.destination?.route } -> {
navController.backToMain()
}

else -> onActiveChange(true)
else -> {}
}
}
}) {
) {
Icon(
painterResource(
if (active || (navController.canNavigateUp && !navigationItems.fastAny { it.route == navBackStackEntry?.destination?.route })) {
Expand Down
54 changes: 53 additions & 1 deletion app/src/main/java/com/zionhuang/music/ui/component/IconButton.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,36 @@
@file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")

package com.zionhuang.music.ui.component

import androidx.annotation.DrawableRes
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.Image
import androidx.compose.foundation.Indication
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.size
import androidx.compose.material.ripple.rememberRipple
import androidx.compose.material3.IconButtonColors
import androidx.compose.material3.IconButtonDefaults
import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.minimumInteractiveComponentSize
import androidx.compose.material3.toShape
import androidx.compose.material3.tokens.IconButtonTokens
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.semantics.Role

@Composable
fun ResizableIconButton(
Expand All @@ -38,4 +55,39 @@ fun ResizableIconButton(
.alpha(if (enabled) 1f else 0.5f)
.then(modifier)
)
}
}

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun IconButton(
onClick: () -> Unit,
onLongClick: () -> Unit,
modifier: Modifier = Modifier,
enabled: Boolean = true,
colors: IconButtonColors = IconButtonDefaults.iconButtonColors(),
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
content: @Composable () -> Unit,
) {
Box(
modifier = modifier
.minimumInteractiveComponentSize()
.size(IconButtonTokens.StateLayerSize)
.clip(IconButtonTokens.StateLayerShape.toShape())
.background(color = colors.containerColor(enabled).value)
.combinedClickable(
onClick = onClick,
onLongClick = onLongClick,
enabled = enabled,
role = Role.Button,
interactionSource = interactionSource,
indication = rememberRipple(
bounded = false,
radius = IconButtonTokens.StateLayerSize / 2
)
),
contentAlignment = Alignment.Center
) {
val contentColor = colors.contentColor(enabled).value
CompositionLocalProvider(LocalContentColor provides contentColor, content = content)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarScrollBehavior
Expand All @@ -25,11 +24,13 @@ import androidx.navigation.NavController
import com.zionhuang.music.LocalPlayerAwareWindowInsets
import com.zionhuang.music.R
import com.zionhuang.music.constants.GridThumbnailHeight
import com.zionhuang.music.ui.component.IconButton
import com.zionhuang.music.ui.component.LocalMenuState
import com.zionhuang.music.ui.component.YouTubeGridItem
import com.zionhuang.music.ui.component.shimmer.GridItemPlaceHolder
import com.zionhuang.music.ui.component.shimmer.ShimmerHost
import com.zionhuang.music.ui.menu.YouTubePlaylistMenu
import com.zionhuang.music.ui.utils.backToMain
import com.zionhuang.music.viewmodels.AccountViewModel

@OptIn(ExperimentalMaterial3Api::class, ExperimentalFoundationApi::class)
Expand Down Expand Up @@ -86,7 +87,10 @@ fun AccountScreen(
TopAppBar(
title = { Text(stringResource(R.string.account)) },
navigationIcon = {
IconButton(onClick = navController::navigateUp) {
IconButton(
onClick = navController::navigateUp,
onLongClick = navController::backToMain
) {
Icon(
painterResource(R.drawable.arrow_back),
contentDescription = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ import com.zionhuang.music.playback.ExoDownloadService
import com.zionhuang.music.playback.queues.ListQueue
import com.zionhuang.music.ui.component.AutoResizeText
import com.zionhuang.music.ui.component.FontSizeRange
import com.zionhuang.music.ui.component.IconButton
import com.zionhuang.music.ui.component.LocalMenuState
import com.zionhuang.music.ui.component.SongListItem
import com.zionhuang.music.ui.component.shimmer.ButtonPlaceholder
Expand All @@ -79,6 +80,7 @@ import com.zionhuang.music.ui.component.shimmer.ShimmerHost
import com.zionhuang.music.ui.component.shimmer.TextPlaceholder
import com.zionhuang.music.ui.menu.AlbumMenu
import com.zionhuang.music.ui.menu.SongMenu
import com.zionhuang.music.ui.utils.backToMain
import com.zionhuang.music.viewmodels.AlbumViewModel

@OptIn(ExperimentalFoundationApi::class, ExperimentalMaterial3Api::class)
Expand Down Expand Up @@ -425,7 +427,10 @@ fun AlbumScreen(
TopAppBar(
title = { },
navigationIcon = {
IconButton(onClick = navController::navigateUp) {
IconButton(
onClick = navController::navigateUp,
onLongClick = navController::backToMain
) {
Icon(
painterResource(R.drawable.arrow_back),
contentDescription = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ import com.zionhuang.music.R
import com.zionhuang.music.extensions.togglePlayPause
import com.zionhuang.music.models.toMediaMetadata
import com.zionhuang.music.playback.queues.YouTubeQueue
import com.zionhuang.music.ui.component.IconButton
import com.zionhuang.music.ui.component.LocalMenuState
import com.zionhuang.music.ui.component.NavigationTitle
import com.zionhuang.music.ui.component.SongListItem
import com.zionhuang.music.ui.menu.SongMenu
import com.zionhuang.music.ui.utils.backToMain
import com.zionhuang.music.viewmodels.DateAgo
import com.zionhuang.music.viewmodels.HistoryViewModel
import java.time.format.DateTimeFormatter
Expand Down Expand Up @@ -123,7 +125,10 @@ fun HistoryScreen(
TopAppBar(
title = { Text(stringResource(R.string.history)) },
navigationIcon = {
IconButton(onClick = navController::navigateUp) {
IconButton(
onClick = navController::navigateUp,
onLongClick = navController::backToMain
) {
Icon(
painterResource(R.drawable.arrow_back),
contentDescription = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.windowInsetsPadding
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
Expand All @@ -29,6 +28,8 @@ import com.zionhuang.music.constants.AccountEmailKey
import com.zionhuang.music.constants.AccountNameKey
import com.zionhuang.music.constants.InnerTubeCookieKey
import com.zionhuang.music.constants.VisitorDataKey
import com.zionhuang.music.ui.component.IconButton
import com.zionhuang.music.ui.utils.backToMain
import com.zionhuang.music.utils.rememberPreference
import com.zionhuang.music.utils.reportException
import kotlinx.coroutines.DelicateCoroutinesApi
Expand Down Expand Up @@ -97,7 +98,10 @@ fun LoginScreen(
TopAppBar(
title = { Text(stringResource(R.string.login)) },
navigationIcon = {
IconButton(onClick = navController::navigateUp) {
IconButton(
onClick = navController::navigateUp,
onLongClick = navController::backToMain
) {
Icon(
painterResource(R.drawable.arrow_back),
contentDescription = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
Expand All @@ -35,9 +34,11 @@ import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import com.zionhuang.music.LocalPlayerAwareWindowInsets
import com.zionhuang.music.R
import com.zionhuang.music.ui.component.IconButton
import com.zionhuang.music.ui.component.NavigationTitle
import com.zionhuang.music.ui.component.shimmer.ListItemPlaceHolder
import com.zionhuang.music.ui.component.shimmer.ShimmerHost
import com.zionhuang.music.ui.utils.backToMain
import com.zionhuang.music.viewmodels.MoodAndGenresViewModel

@OptIn(ExperimentalMaterial3Api::class)
Expand Down Expand Up @@ -101,7 +102,10 @@ fun MoodAndGenresScreen(
TopAppBar(
title = { Text(stringResource(R.string.mood_and_genres)) },
navigationIcon = {
IconButton(onClick = navController::navigateUp) {
IconButton(
onClick = navController::navigateUp,
onLongClick = navController::backToMain
) {
Icon(
painterResource(R.drawable.arrow_back),
contentDescription = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarScrollBehavior
Expand All @@ -26,11 +25,13 @@ import com.zionhuang.music.LocalPlayerAwareWindowInsets
import com.zionhuang.music.LocalPlayerConnection
import com.zionhuang.music.R
import com.zionhuang.music.constants.GridThumbnailHeight
import com.zionhuang.music.ui.component.IconButton
import com.zionhuang.music.ui.component.LocalMenuState
import com.zionhuang.music.ui.component.YouTubeGridItem
import com.zionhuang.music.ui.component.shimmer.GridItemPlaceHolder
import com.zionhuang.music.ui.component.shimmer.ShimmerHost
import com.zionhuang.music.ui.menu.YouTubeAlbumMenu
import com.zionhuang.music.ui.utils.backToMain
import com.zionhuang.music.viewmodels.NewReleaseViewModel

@OptIn(ExperimentalFoundationApi::class, ExperimentalMaterial3Api::class)
Expand Down Expand Up @@ -93,7 +94,10 @@ fun NewReleaseScreen(
TopAppBar(
title = { Text(stringResource(R.string.new_release_albums)) },
navigationIcon = {
IconButton(onClick = navController::navigateUp) {
IconButton(
onClick = navController::navigateUp,
onLongClick = navController::backToMain
) {
Icon(
painterResource(R.drawable.arrow_back),
contentDescription = null
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/com/zionhuang/music/ui/screens/Screens.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ sealed class Screens(
object Artists : Screens(R.string.artists, R.drawable.artist, "artists")
object Albums : Screens(R.string.albums, R.drawable.album, "albums")
object Playlists : Screens(R.string.playlists, R.drawable.queue_music, "playlists")

companion object {
val MainScreens = listOf(Home, Songs, Artists, Albums, Playlists)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ import com.zionhuang.music.playback.queues.YouTubeQueue
import com.zionhuang.music.ui.component.AlbumGridItem
import com.zionhuang.music.ui.component.ArtistGridItem
import com.zionhuang.music.ui.component.ChipsRow
import com.zionhuang.music.ui.component.IconButton
import com.zionhuang.music.ui.component.LocalMenuState
import com.zionhuang.music.ui.component.NavigationTitle
import com.zionhuang.music.ui.component.SongListItem
import com.zionhuang.music.ui.menu.AlbumMenu
import com.zionhuang.music.ui.menu.ArtistMenu
import com.zionhuang.music.ui.menu.SongMenu
import com.zionhuang.music.ui.utils.backToMain
import com.zionhuang.music.viewmodels.StatsViewModel

@OptIn(ExperimentalMaterial3Api::class, ExperimentalFoundationApi::class)
Expand Down Expand Up @@ -215,7 +217,10 @@ fun StatsScreen(
TopAppBar(
title = { Text(stringResource(R.string.stats)) },
navigationIcon = {
IconButton(onClick = navController::navigateUp) {
IconButton(
onClick = navController::navigateUp,
onLongClick = navController::backToMain
) {
Icon(
painterResource(R.drawable.arrow_back),
contentDescription = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import com.zionhuang.music.R
import com.zionhuang.music.extensions.togglePlayPause
import com.zionhuang.music.models.toMediaMetadata
import com.zionhuang.music.playback.queues.YouTubeQueue
import com.zionhuang.music.ui.component.IconButton
import com.zionhuang.music.ui.component.LocalMenuState
import com.zionhuang.music.ui.component.NavigationTitle
import com.zionhuang.music.ui.component.YouTubeListItem
Expand All @@ -39,6 +40,7 @@ import com.zionhuang.music.ui.menu.YouTubeAlbumMenu
import com.zionhuang.music.ui.menu.YouTubeArtistMenu
import com.zionhuang.music.ui.menu.YouTubePlaylistMenu
import com.zionhuang.music.ui.menu.YouTubeSongMenu
import com.zionhuang.music.ui.utils.backToMain
import com.zionhuang.music.viewmodels.YouTubeBrowseViewModel

@OptIn(ExperimentalMaterial3Api::class, ExperimentalFoundationApi::class)
Expand Down Expand Up @@ -148,7 +150,10 @@ fun YouTubeBrowseScreen(
TopAppBar(
title = { Text(browseResult?.title.orEmpty()) },
navigationIcon = {
IconButton(onClick = navController::navigateUp) {
IconButton(
onClick = navController::navigateUp,
onLongClick = navController::backToMain
) {
Icon(
painterResource(R.drawable.arrow_back),
contentDescription = null
Expand Down
Loading

0 comments on commit 6660e8b

Please sign in to comment.