Skip to content

Commit

Permalink
feat: carousel for tv recommend gallery.
Browse files Browse the repository at this point in the history
  • Loading branch information
oxyroid committed Jan 15, 2024
1 parent 9d6211a commit 67489fc
Showing 1 changed file with 61 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.m3u.features.foryou.components.recommend

import androidx.compose.animation.animateContentSize
import androidx.compose.animation.core.tween
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.togetherWith
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.padding
Expand All @@ -9,7 +13,10 @@ import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.tv.material3.Carousel
import androidx.tv.material3.rememberCarouselState
import com.m3u.material.components.HorizontalPagerIndicator
import com.m3u.material.ktx.isTelevision
import com.m3u.material.model.LocalSpacing
import com.m3u.ui.LocalHelper
import kotlin.math.absoluteValue
Expand All @@ -23,20 +30,61 @@ internal fun RecommendGallery(
val spacing = LocalSpacing.current
val helper = LocalHelper.current

val state = rememberPagerState { recommend.size }

Column(modifier) {
HorizontalPager(
state = state,
contentPadding = PaddingValues(spacing.medium),
modifier = Modifier.animateContentSize()
) { page ->
val spec = recommend[page]
val pageOffset =
((state.currentPage - page) + state.currentPageOffsetFraction).absoluteValue
val tv = isTelevision()

if (!tv) {
val state = rememberPagerState { recommend.size }

Column(modifier) {
HorizontalPager(
state = state,
contentPadding = PaddingValues(spacing.medium),
modifier = Modifier.animateContentSize()
) { page ->
val spec = recommend[page]
val pageOffset =
((state.currentPage - page) + state.currentPageOffsetFraction).absoluteValue
RecommendItem(
spec = spec,
pageOffset = pageOffset,
onClick = {
when (spec) {
is Recommend.UnseenSpec -> {
helper.play(spec.stream.url)
navigateToStream()
}

is Recommend.DiscoverSpec -> {

}
}
}
)
}
HorizontalPagerIndicator(
pagerState = state,
modifier = Modifier
.align(Alignment.End)
.padding(horizontal = spacing.medium),
)
}
} else {
val state = rememberCarouselState()
Carousel(
carouselState = state,
itemCount = recommend.size,
contentTransformEndToStart =
fadeIn(tween(1000)).togetherWith(fadeOut(tween(1000))),
contentTransformStartToEnd =
fadeIn(tween(1000)).togetherWith(fadeOut(tween(1000))),
modifier = Modifier
.padding(spacing.medium)
.then(modifier)
) { index ->
val spec = recommend[index]
RecommendItem(
spec = spec,
pageOffset = pageOffset,
pageOffset = 0f,
onClick = {
when (spec) {
is Recommend.UnseenSpec -> {
Expand All @@ -51,11 +99,6 @@ internal fun RecommendGallery(
}
)
}
HorizontalPagerIndicator(
pagerState = state,
modifier = Modifier
.align(Alignment.End)
.padding(horizontal = spacing.medium),
)
}

}

0 comments on commit 67489fc

Please sign in to comment.