Skip to content

Commit b899958

Browse files
committed
PR Feedback.
1 parent fd4e7a0 commit b899958

File tree

2 files changed

+43
-45
lines changed

2 files changed

+43
-45
lines changed

Jetcaster/app/src/main/java/com/example/jetcaster/ui/home/Home.kt

Lines changed: 36 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818

1919
package com.example.jetcaster.ui.home
2020

21+
import android.util.Log
2122
import androidx.compose.foundation.ExperimentalFoundationApi
2223
import androidx.compose.foundation.Image
2324
import androidx.compose.foundation.background
2425
import androidx.compose.foundation.clickable
2526
import androidx.compose.foundation.layout.Box
27+
import androidx.compose.foundation.layout.BoxWithConstraints
2628
import androidx.compose.foundation.layout.Column
2729
import androidx.compose.foundation.layout.PaddingValues
2830
import androidx.compose.foundation.layout.Row
@@ -64,18 +66,13 @@ import androidx.compose.material3.TopAppBar
6466
import androidx.compose.runtime.Composable
6567
import androidx.compose.runtime.LaunchedEffect
6668
import androidx.compose.runtime.getValue
67-
import androidx.compose.runtime.mutableStateOf
68-
import androidx.compose.runtime.remember
6969
import androidx.compose.runtime.rememberCoroutineScope
70-
import androidx.compose.runtime.setValue
70+
import androidx.compose.runtime.snapshotFlow
7171
import androidx.compose.ui.Alignment
7272
import androidx.compose.ui.Modifier
7373
import androidx.compose.ui.draw.clip
7474
import androidx.compose.ui.graphics.Color
7575
import androidx.compose.ui.layout.ContentScale
76-
import androidx.compose.ui.layout.onSizeChanged
77-
import androidx.compose.ui.platform.LocalConfiguration
78-
import androidx.compose.ui.platform.LocalDensity
7976
import androidx.compose.ui.res.painterResource
8077
import androidx.compose.ui.res.stringResource
8178
import androidx.compose.ui.text.style.TextOverflow
@@ -97,11 +94,11 @@ import com.example.jetcaster.ui.theme.JetcasterTheme
9794
import com.example.jetcaster.util.ToggleFollowPodcastIconButton
9895
import com.example.jetcaster.util.quantityStringResource
9996
import com.example.jetcaster.util.verticalGradientScrim
97+
import kotlinx.collections.immutable.PersistentList
98+
import kotlinx.coroutines.launch
10099
import java.time.Duration
101100
import java.time.LocalDateTime
102101
import java.time.OffsetDateTime
103-
import kotlinx.collections.immutable.PersistentList
104-
import kotlinx.coroutines.launch
105102

106103
@Composable
107104
fun Home(
@@ -412,42 +409,38 @@ fun FollowedPodcasts(
412409
onPodcastUnfollowed: (String) -> Unit,
413410
) {
414411
val coroutineScope = rememberCoroutineScope()
415-
416-
var horizontalPadding by remember { mutableStateOf(0.dp) }
417-
val density = LocalDensity.current
418-
val screenWidth = LocalConfiguration.current.screenWidthDp.dp
419-
HorizontalPager(
420-
state = pagerState,
421-
modifier = modifier.onSizeChanged { size ->
422-
// TODO: this is not quite performant since it requires 2 passes to compute the content
423-
// padding. This should be revisited once a carousel component is available.
424-
// Alternatively, version 1.7.0-alpha05 of Compose Foundation supports `snapPosition`
425-
// which solves this problem and avoids this calculation altogether. Once 1.7.0 is
426-
// stable, this implementation can be updated.
427-
horizontalPadding = with(density) {
428-
(size.width.toDp() - FEATURED_PODCAST_IMAGE_WIDTH_DP) / 2
429-
}
430-
},
431-
contentPadding = PaddingValues(
432-
horizontal = horizontalPadding,
433-
vertical = 16.dp,
434-
),
435-
pageSize = PageSize.Fixed(180.dp)
436-
) { page ->
437-
val (podcast, lastEpisodeDate) = items[page]
438-
FollowedPodcastCarouselItem(
439-
podcastImageUrl = podcast.imageUrl,
440-
podcastTitle = podcast.title,
441-
onUnfollowedClick = { onPodcastUnfollowed(podcast.uri) },
442-
lastEpisodeDateText = lastEpisodeDate?.let { lastUpdated(it) },
443-
modifier = Modifier
444-
.fillMaxSize()
445-
.clickable {
446-
coroutineScope.launch {
447-
pagerState.animateScrollToPage(page)
412+
// TODO: Using BoxWithConstraints is not quite performant since it requires 2 passes to compute
413+
// the content padding. This should be revisited once a carousel component is available.
414+
// Alternatively, version 1.7.0-alpha05 of Compose Foundation supports `snapPosition`
415+
// which solves this problem and avoids this calculation altogether. Once 1.7.0 is
416+
// stable, this implementation can be updated.
417+
BoxWithConstraints(modifier) {
418+
val horizontalPadding = (this.maxWidth - FEATURED_PODCAST_IMAGE_WIDTH_DP) / 2
419+
Log.d("JetcasterPadding.", "MaxWidth: ${this.maxWidth}, horiziontalPadding: $horizontalPadding")
420+
HorizontalPager(
421+
state = pagerState,
422+
contentPadding = PaddingValues(
423+
horizontal = horizontalPadding,
424+
vertical = 16.dp,
425+
),
426+
pageSpacing = 24.dp,
427+
pageSize = PageSize.Fixed(FEATURED_PODCAST_IMAGE_WIDTH_DP)
428+
) { page ->
429+
val (podcast, lastEpisodeDate) = items[page]
430+
FollowedPodcastCarouselItem(
431+
podcastImageUrl = podcast.imageUrl,
432+
podcastTitle = podcast.title,
433+
onUnfollowedClick = { onPodcastUnfollowed(podcast.uri) },
434+
lastEpisodeDateText = lastEpisodeDate?.let { lastUpdated(it) },
435+
modifier = Modifier
436+
.fillMaxSize()
437+
.clickable {
438+
coroutineScope.launch {
439+
pagerState.animateScrollToPage(page)
440+
}
448441
}
449-
}
450-
)
442+
)
443+
}
451444
}
452445
}
453446

Jetcaster/app/src/main/java/com/example/jetcaster/ui/home/HomeViewModel.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class HomeViewModel(
9292
)
9393
}
9494
) { homeCategories,
95-
selectedHomeCategory,
95+
homeCategory,
9696
podcasts,
9797
refreshing,
9898
filterableCategories,
@@ -101,9 +101,14 @@ class HomeViewModel(
101101

102102
_selectedCategory.value = filterableCategories.selectedCategory
103103

104+
// Override selected home category to show 'DISCOVER' if there are no
105+
// featured podcasts
106+
selectedHomeCategory.value =
107+
if (podcasts.isEmpty()) HomeCategory.Discover else homeCategory
108+
104109
HomeViewState(
105110
homeCategories = homeCategories,
106-
selectedHomeCategory = selectedHomeCategory,
111+
selectedHomeCategory = homeCategory,
107112
featuredPodcasts = podcasts.toPersistentList(),
108113
refreshing = refreshing,
109114
filterableCategoriesModel = filterableCategories,

0 commit comments

Comments
 (0)