18
18
19
19
package com.example.jetcaster.ui.home
20
20
21
+ import android.util.Log
21
22
import androidx.compose.foundation.ExperimentalFoundationApi
22
23
import androidx.compose.foundation.Image
23
24
import androidx.compose.foundation.background
24
25
import androidx.compose.foundation.clickable
25
26
import androidx.compose.foundation.layout.Box
27
+ import androidx.compose.foundation.layout.BoxWithConstraints
26
28
import androidx.compose.foundation.layout.Column
27
29
import androidx.compose.foundation.layout.PaddingValues
28
30
import androidx.compose.foundation.layout.Row
@@ -64,18 +66,13 @@ import androidx.compose.material3.TopAppBar
64
66
import androidx.compose.runtime.Composable
65
67
import androidx.compose.runtime.LaunchedEffect
66
68
import androidx.compose.runtime.getValue
67
- import androidx.compose.runtime.mutableStateOf
68
- import androidx.compose.runtime.remember
69
69
import androidx.compose.runtime.rememberCoroutineScope
70
- import androidx.compose.runtime.setValue
70
+ import androidx.compose.runtime.snapshotFlow
71
71
import androidx.compose.ui.Alignment
72
72
import androidx.compose.ui.Modifier
73
73
import androidx.compose.ui.draw.clip
74
74
import androidx.compose.ui.graphics.Color
75
75
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
79
76
import androidx.compose.ui.res.painterResource
80
77
import androidx.compose.ui.res.stringResource
81
78
import androidx.compose.ui.text.style.TextOverflow
@@ -97,11 +94,11 @@ import com.example.jetcaster.ui.theme.JetcasterTheme
97
94
import com.example.jetcaster.util.ToggleFollowPodcastIconButton
98
95
import com.example.jetcaster.util.quantityStringResource
99
96
import com.example.jetcaster.util.verticalGradientScrim
97
+ import kotlinx.collections.immutable.PersistentList
98
+ import kotlinx.coroutines.launch
100
99
import java.time.Duration
101
100
import java.time.LocalDateTime
102
101
import java.time.OffsetDateTime
103
- import kotlinx.collections.immutable.PersistentList
104
- import kotlinx.coroutines.launch
105
102
106
103
@Composable
107
104
fun Home (
@@ -412,42 +409,38 @@ fun FollowedPodcasts(
412
409
onPodcastUnfollowed : (String ) -> Unit ,
413
410
) {
414
411
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
+ }
448
441
}
449
- }
450
- )
442
+ )
443
+ }
451
444
}
452
445
}
453
446
0 commit comments