Skip to content

Commit

Permalink
feat: click bottom nav item to scroll to top, #134
Browse files Browse the repository at this point in the history
  • Loading branch information
z-huang authored and Malopieds committed Aug 23, 2024
1 parent 0028baa commit 28027f7
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 9 deletions.
17 changes: 12 additions & 5 deletions app/src/main/java/com/malopieds/innertune/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -762,12 +762,19 @@ class MainActivity : ComponentActivity() {
)
},
onClick = {
navController.navigate(screen.route) {
popUpTo(navController.graph.startDestinationId) {
saveState = true
if (navBackStackEntry?.destination?.hierarchy?.any { it.route == screen.route } == true) {
navController.currentBackStackEntry?.savedStateHandle?.set("scrollToTop", true)
coroutineScope.launch {
searchBarScrollBehavior.state.resetHeightOffset()
}
} else {
navController.navigate(screen.route) {
popUpTo(navController.graph.startDestinationId) {
saveState = true
}
launchSingleTop = true
restoreState = true
}
launchSingleTop = true
restoreState = true
}
},
)
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/java/com/malopieds/innertune/ui/screens/HomeScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
Expand All @@ -45,6 +46,7 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import androidx.navigation.compose.currentBackStackEntryAsState
import com.google.accompanist.swiperefresh.SwipeRefresh
import com.google.accompanist.swiperefresh.rememberSwipeRefreshState
import com.malopieds.innertube.models.AlbumItem
Expand Down Expand Up @@ -140,6 +142,16 @@ fun HomeScreen(
val coroutineScope = rememberCoroutineScope()
val scrollState = rememberScrollState()

val backStackEntry by navController.currentBackStackEntryAsState()
val scrollToTop = backStackEntry?.savedStateHandle?.getStateFlow("scrollToTop", false)?.collectAsState()

LaunchedEffect(scrollToTop?.value) {
if (scrollToTop?.value == true) {
scrollState.animateScrollTo(value = 0)
backStackEntry?.savedStateHandle?.set("scrollToTop", false)
}
}

SwipeRefresh(
state = rememberSwipeRefreshState(isRefreshing),
onRefresh = viewModel::refresh,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@ import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.GridItemSpan
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.foundation.lazy.grid.rememberLazyGridState
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material3.FilterChip
import androidx.compose.material3.FilterChipDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberCoroutineScope
Expand All @@ -37,6 +40,7 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import androidx.navigation.compose.currentBackStackEntryAsState
import com.malopieds.innertune.LocalPlayerAwareWindowInsets
import com.malopieds.innertune.LocalPlayerConnection
import com.malopieds.innertune.R
Expand Down Expand Up @@ -85,6 +89,21 @@ fun LibraryAlbumsScreen(

val coroutineScope = rememberCoroutineScope()

val lazyListState = rememberLazyListState()
val lazyGridState = rememberLazyGridState()
val backStackEntry by navController.currentBackStackEntryAsState()
val scrollToTop = backStackEntry?.savedStateHandle?.getStateFlow("scrollToTop", false)?.collectAsState()

LaunchedEffect(scrollToTop?.value) {
if (scrollToTop?.value == true) {
when (viewType) {
LibraryViewType.LIST -> lazyListState.animateScrollToItem(0)
LibraryViewType.GRID -> lazyGridState.animateScrollToItem(0)
}
backStackEntry?.savedStateHandle?.set("scrollToTop", false)
}
}

val filterContent = @Composable {
Row {
Spacer(Modifier.width(12.dp))
Expand Down Expand Up @@ -169,6 +188,7 @@ fun LibraryAlbumsScreen(
when (viewType) {
LibraryViewType.LIST ->
LazyColumn(
state = lazyListState,
contentPadding = LocalPlayerAwareWindowInsets.current.asPaddingValues(),
) {
item(
Expand Down Expand Up @@ -236,10 +256,8 @@ fun LibraryAlbumsScreen(

LibraryViewType.GRID ->
LazyVerticalGrid(
columns =
GridCells.Adaptive(
minSize = GridThumbnailHeight + if (gridItemSize == GridItemSize.BIG) 24.dp else (-24).dp,
),
state = lazyGridState,
columns = GridCells.Adaptive(minSize = GridThumbnailHeight + 24.dp),
contentPadding = LocalPlayerAwareWindowInsets.current.asPaddingValues(),
) {
item(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@ import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.GridItemSpan
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.foundation.lazy.grid.rememberLazyGridState
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material3.FilterChip
import androidx.compose.material3.FilterChipDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberCoroutineScope
Expand All @@ -37,6 +40,7 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import androidx.navigation.compose.currentBackStackEntryAsState
import com.malopieds.innertune.LocalPlayerAwareWindowInsets
import com.malopieds.innertune.R
import com.malopieds.innertune.constants.ArtistFilter
Expand Down Expand Up @@ -80,6 +84,21 @@ fun LibraryArtistsScreen(
val artists by viewModel.allArtists.collectAsState()
val coroutineScope = rememberCoroutineScope()

val lazyListState = rememberLazyListState()
val lazyGridState = rememberLazyGridState()
val backStackEntry by navController.currentBackStackEntryAsState()
val scrollToTop = backStackEntry?.savedStateHandle?.getStateFlow("scrollToTop", false)?.collectAsState()

LaunchedEffect(scrollToTop?.value) {
if (scrollToTop?.value == true) {
when (viewType) {
LibraryViewType.LIST -> lazyListState.animateScrollToItem(0)
LibraryViewType.GRID -> lazyGridState.animateScrollToItem(0)
}
backStackEntry?.savedStateHandle?.set("scrollToTop", false)
}
}

val filterContent = @Composable {
Row {
Spacer(Modifier.width(12.dp))
Expand Down Expand Up @@ -161,6 +180,7 @@ fun LibraryArtistsScreen(
when (viewType) {
LibraryViewType.LIST ->
LazyColumn(
state = lazyListState,
contentPadding = LocalPlayerAwareWindowInsets.current.asPaddingValues(),
) {
item(
Expand Down Expand Up @@ -226,6 +246,7 @@ fun LibraryArtistsScreen(

LibraryViewType.GRID ->
LazyVerticalGrid(
state = lazyGridState,
columns =
GridCells.Adaptive(
minSize = GridThumbnailHeight + if (gridItemSize == GridItemSize.BIG) 24.dp else (-24).dp,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.foundation.lazy.grid.rememberLazyGridState
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberCoroutineScope
Expand All @@ -33,6 +35,7 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import androidx.navigation.compose.currentBackStackEntryAsState
import com.malopieds.innertune.LocalPlayerAwareWindowInsets
import com.malopieds.innertune.LocalPlayerConnection
import com.malopieds.innertune.R
Expand Down Expand Up @@ -152,7 +155,20 @@ fun LibraryMixScreen(

val coroutineScope = rememberCoroutineScope()

val lazyListState = rememberLazyListState()
val lazyGridState = rememberLazyGridState()
val backStackEntry by navController.currentBackStackEntryAsState()
val scrollToTop = backStackEntry?.savedStateHandle?.getStateFlow("scrollToTop", false)?.collectAsState()

LaunchedEffect(scrollToTop?.value) {
if (scrollToTop?.value == true) {
when (viewType) {
LibraryViewType.LIST -> lazyListState.animateScrollToItem(0)
LibraryViewType.GRID -> lazyGridState.animateScrollToItem(0)
}
backStackEntry?.savedStateHandle?.set("scrollToTop", false)
}
}
val headerContent = @Composable {
Row(
verticalAlignment = Alignment.CenterVertically,
Expand Down Expand Up @@ -200,6 +216,7 @@ fun LibraryMixScreen(
when (viewType) {
LibraryViewType.LIST ->
LazyColumn(
state = lazyListState,
contentPadding = LocalPlayerAwareWindowInsets.current.asPaddingValues(),
) {
item(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
Expand All @@ -39,6 +40,7 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import androidx.navigation.compose.currentBackStackEntryAsState
import com.malopieds.innertune.LocalDatabase
import com.malopieds.innertune.LocalPlayerAwareWindowInsets
import com.malopieds.innertune.R
Expand Down Expand Up @@ -111,6 +113,19 @@ fun LibraryPlaylistsScreen(
val lazyListState = rememberLazyListState()
val lazyGridState = rememberLazyGridState()

val backStackEntry by navController.currentBackStackEntryAsState()
val scrollToTop = backStackEntry?.savedStateHandle?.getStateFlow("scrollToTop", false)?.collectAsState()

LaunchedEffect(scrollToTop?.value) {
if (scrollToTop?.value == true) {
when (viewType) {
LibraryViewType.LIST -> lazyListState.animateScrollToItem(0)
LibraryViewType.GRID -> lazyGridState.animateScrollToItem(0)
}
backStackEntry?.savedStateHandle?.set("scrollToTop", false)
}
}

var showAddPlaylistDialog by rememberSaveable {
mutableStateOf(false)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
Expand All @@ -36,6 +37,7 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import androidx.navigation.compose.currentBackStackEntryAsState
import com.malopieds.innertune.LocalPlayerAwareWindowInsets
import com.malopieds.innertune.LocalPlayerConnection
import com.malopieds.innertune.R
Expand Down Expand Up @@ -89,6 +91,16 @@ fun LibrarySongsScreen(

val lazyListState = rememberLazyListState()

val backStackEntry by navController.currentBackStackEntryAsState()
val scrollToTop = backStackEntry?.savedStateHandle?.getStateFlow("scrollToTop", false)?.collectAsState()

LaunchedEffect(scrollToTop?.value) {
if (scrollToTop?.value == true) {
lazyListState.animateScrollToItem(0)
backStackEntry?.savedStateHandle?.set("scrollToTop", false)
}
}

Box(
modifier = Modifier.fillMaxSize(),
) {
Expand Down

0 comments on commit 28027f7

Please sign in to comment.