From 0ac0d8a75f7bbb25b478c3fe7436657fc4f72937 Mon Sep 17 00:00:00 2001 From: yongsuk44 Date: Tue, 8 Oct 2024 21:00:11 +0900 Subject: [PATCH] [Jetcaster] Remove duplicated code in Home content layout --- .../com/example/jetcaster/ui/home/Home.kt | 150 +++--------------- 1 file changed, 18 insertions(+), 132 deletions(-) diff --git a/Jetcaster/mobile/src/main/java/com/example/jetcaster/ui/home/Home.kt b/Jetcaster/mobile/src/main/java/com/example/jetcaster/ui/home/Home.kt index 5eb20979b9..b0538c64c3 100644 --- a/Jetcaster/mobile/src/main/java/com/example/jetcaster/ui/home/Home.kt +++ b/Jetcaster/mobile/src/main/java/com/example/jetcaster/ui/home/Home.kt @@ -39,7 +39,6 @@ import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.width import androidx.compose.foundation.layout.windowInsetsPadding -import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.grid.GridCells import androidx.compose.foundation.lazy.grid.LazyVerticalGrid import androidx.compose.foundation.pager.HorizontalPager @@ -77,7 +76,6 @@ import androidx.compose.material3.adaptive.layout.PaneAdaptedValue import androidx.compose.material3.adaptive.layout.PaneScaffoldDirective import androidx.compose.material3.adaptive.layout.SupportingPaneScaffold import androidx.compose.material3.adaptive.layout.SupportingPaneScaffoldRole -import androidx.compose.material3.adaptive.layout.ThreePaneScaffoldValue import androidx.compose.material3.adaptive.navigation.ThreePaneScaffoldNavigator import androidx.compose.material3.adaptive.navigation.rememberSupportingPaneScaffoldNavigator import androidx.compose.material3.adaptive.occludingVerticalHingeBounds @@ -154,15 +152,6 @@ data class HomeState( private val HomeState.showHomeCategoryTabs: Boolean get() = featuredPodcasts.isNotEmpty() && homeCategories.isNotEmpty() -@OptIn(ExperimentalMaterial3AdaptiveApi::class) -private fun HomeState.showGrid( - scaffoldValue: ThreePaneScaffoldValue -): Boolean = windowSizeClass.windowWidthSizeClass == WindowWidthSizeClass.EXPANDED || - ( - windowSizeClass.windowWidthSizeClass == WindowWidthSizeClass.MEDIUM && - scaffoldValue[SupportingPaneScaffoldRole.Supporting] == PaneAdaptedValue.Hidden - ) - @OptIn(ExperimentalMaterial3AdaptiveApi::class) private fun ThreePaneScaffoldNavigator.isMainPaneHidden(): Boolean { return scaffoldValue[SupportingPaneScaffoldRole.Main] == PaneAdaptedValue.Hidden @@ -331,11 +320,9 @@ private fun HomeScreenReady( Surface { val podcastUri = navigator.currentDestination?.content - val showGrid = homeState.showGrid(navigator.scaffoldValue) if (podcastUri.isNullOrEmpty()) { HomeScreen( homeState = homeState, - showGrid = showGrid, modifier = Modifier.fillMaxSize() ) } else { @@ -363,7 +350,6 @@ private fun HomeScreenReady( mainPane = { HomeScreen( homeState = homeState, - showGrid = showGrid, modifier = Modifier.fillMaxSize() ) }, @@ -441,7 +427,6 @@ private fun HomeScreenBackground( @Composable private fun HomeScreen( homeState: HomeState, - showGrid: Boolean, modifier: Modifier = Modifier ) { // Effect that changes the home category selection when there are no subscribed podcasts @@ -471,7 +456,6 @@ private fun HomeScreen( // Main Content val snackBarText = stringResource(id = R.string.episode_added_to_your_queue) HomeContent( - showGrid = showGrid, showHomeCategoryTabs = homeState.showHomeCategoryTabs, featuredPodcasts = homeState.featuredPodcasts, selectedHomeCategory = homeState.selectedHomeCategory, @@ -500,7 +484,6 @@ private fun HomeScreen( @Composable private fun HomeContent( - showGrid: Boolean, showHomeCategoryTabs: Boolean, featuredPodcasts: PersistentList, selectedHomeCategory: HomeCategory, @@ -527,120 +510,24 @@ private fun HomeContent( } } - // Note: ideally, `HomeContentColumn` and `HomeContentGrid` would be the same implementation - // (i.e. a grid). However, LazyVerticalGrid does not have the concept of a sticky header. - // So we are using two different composables here depending on the provided window size class. - // See: https://issuetracker.google.com/issues/231557184 - if (showGrid) { - HomeContentGrid( - pagerState = pagerState, - showHomeCategoryTabs = showHomeCategoryTabs, - featuredPodcasts = featuredPodcasts, - selectedHomeCategory = selectedHomeCategory, - homeCategories = homeCategories, - filterableCategoriesModel = filterableCategoriesModel, - podcastCategoryFilterResult = podcastCategoryFilterResult, - library = library, - modifier = modifier, - onPodcastUnfollowed = onPodcastUnfollowed, - onHomeCategorySelected = onHomeCategorySelected, - onCategorySelected = onCategorySelected, - navigateToPodcastDetails = navigateToPodcastDetails, - navigateToPlayer = navigateToPlayer, - onTogglePodcastFollowed = onTogglePodcastFollowed, - onQueueEpisode = onQueueEpisode, - ) - } else { - HomeContentColumn( - pagerState = pagerState, - showHomeCategoryTabs = showHomeCategoryTabs, - featuredPodcasts = featuredPodcasts, - selectedHomeCategory = selectedHomeCategory, - homeCategories = homeCategories, - filterableCategoriesModel = filterableCategoriesModel, - podcastCategoryFilterResult = podcastCategoryFilterResult, - library = library, - modifier = modifier, - onPodcastUnfollowed = onPodcastUnfollowed, - onHomeCategorySelected = onHomeCategorySelected, - onCategorySelected = onCategorySelected, - navigateToPodcastDetails = navigateToPodcastDetails, - navigateToPlayer = navigateToPlayer, - onTogglePodcastFollowed = onTogglePodcastFollowed, - onQueueEpisode = onQueueEpisode, - ) - } -} - -@OptIn(ExperimentalFoundationApi::class) -@Composable -private fun HomeContentColumn( - showHomeCategoryTabs: Boolean, - pagerState: PagerState, - featuredPodcasts: PersistentList, - selectedHomeCategory: HomeCategory, - homeCategories: List, - filterableCategoriesModel: FilterableCategoriesModel, - podcastCategoryFilterResult: PodcastCategoryFilterResult, - library: LibraryInfo, - modifier: Modifier = Modifier, - onPodcastUnfollowed: (PodcastInfo) -> Unit, - onHomeCategorySelected: (HomeCategory) -> Unit, - onCategorySelected: (CategoryInfo) -> Unit, - navigateToPodcastDetails: (PodcastInfo) -> Unit, - navigateToPlayer: (EpisodeInfo) -> Unit, - onTogglePodcastFollowed: (PodcastInfo) -> Unit, - onQueueEpisode: (PlayerEpisode) -> Unit, -) { - LazyColumn( - modifier = modifier.fillMaxSize() - ) { - if (featuredPodcasts.isNotEmpty()) { - item { - FollowedPodcastItem( - pagerState = pagerState, - items = featuredPodcasts, - onPodcastUnfollowed = onPodcastUnfollowed, - navigateToPodcastDetails = navigateToPodcastDetails, - modifier = Modifier - .fillMaxWidth() - ) - } - } - - if (showHomeCategoryTabs) { - item { - HomeCategoryTabs( - categories = homeCategories, - selectedCategory = selectedHomeCategory, - showHorizontalLine = true, - onCategorySelected = onHomeCategorySelected - ) - } - } - - when (selectedHomeCategory) { - HomeCategory.Library -> { - libraryItems( - library = library, - navigateToPlayer = navigateToPlayer, - onQueueEpisode = onQueueEpisode - ) - } - - HomeCategory.Discover -> { - discoverItems( - filterableCategoriesModel = filterableCategoriesModel, - podcastCategoryFilterResult = podcastCategoryFilterResult, - navigateToPodcastDetails = navigateToPodcastDetails, - navigateToPlayer = navigateToPlayer, - onCategorySelected = onCategorySelected, - onTogglePodcastFollowed = onTogglePodcastFollowed, - onQueueEpisode = onQueueEpisode - ) - } - } - } + HomeContentGrid( + pagerState = pagerState, + showHomeCategoryTabs = showHomeCategoryTabs, + featuredPodcasts = featuredPodcasts, + selectedHomeCategory = selectedHomeCategory, + homeCategories = homeCategories, + filterableCategoriesModel = filterableCategoriesModel, + podcastCategoryFilterResult = podcastCategoryFilterResult, + library = library, + modifier = modifier, + onPodcastUnfollowed = onPodcastUnfollowed, + onHomeCategorySelected = onHomeCategorySelected, + onCategorySelected = onCategorySelected, + navigateToPodcastDetails = navigateToPodcastDetails, + navigateToPlayer = navigateToPlayer, + onTogglePodcastFollowed = onTogglePodcastFollowed, + onQueueEpisode = onQueueEpisode, + ) } @Composable @@ -947,7 +834,6 @@ private fun PreviewHome() { ) HomeScreen( homeState = homeState, - showGrid = false ) } }