Bharat Kumar
08/01/2023, 6:15 AMxoangon
08/01/2023, 6:17 AMBharat Kumar
08/01/2023, 6:19 AMBharat Kumar
08/01/2023, 6:19 AM@Stable
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun TopCarouselPager(
uiModule: UIModule?,
modifier: Modifier = Modifier,
contentModule: ContentModule,
onTrayClick: (contentData: ContentData) -> Unit,
watchList: ArrayList<String?>,
onLoginClick: () -> Unit,
) {
val items: List<ContentData> = contentModule.contentData ?: emptyList()
val carouselImageSize = uiModule?.settings?.carouselImageSize ?: "3x4"
val selectedItem = remember { mutableStateOf<ContentData?>(null) }
var itemSpacing = 0.dp
var contentPadding = 0.dp
var paddingStart = 0.dp
(carouselImageSize == "16x9" || carouselImageSize == "32x9").ifTrue {
itemSpacing = 10.dp
contentPadding = 50.dp
paddingStart = 14.dp
}
items.isNotEmpty().ifTrue {
val pagerState = rememberPagerState(
initialPage = 0,
initialPageOffsetFraction = 0f,
) {
// provide pageCount
Int.MAX_VALUE
}
HorizontalPager(
state = pagerState,
key = { page -> items[page.floorMod(items.size)].contentId },
modifier = modifier.padding(start = paddingStart),
pageSpacing = itemSpacing,
contentPadding = PaddingValues(end = contentPadding),
beyondBoundsPageCount = 1,
) { page ->
val item = items[page.floorMod(items.size)]
val isAddedToWatchList = watchList.contains(item.contentId)
TopCarouselItem(
item,
isAddedToWatchList,
carouselImageSize,
onTrayClick,
onLoginClick,
onWatchListItemClick = {
if (it) {
watchList.add(item.contentId)
} else {
watchList.remove(item.contentId)
}
},
contentPadding + paddingStart,
)
// selectedPage = pagerState.currentPage % items.size
selectedItem.value = item
}
AutoScrollPager(pagerState)
}
}
xoangon
08/01/2023, 6:22 AMAutoScrollPager
is the one managing the swipe logic. Can you paste the code in that function?Bharat Kumar
08/01/2023, 6:24 AM@OptIn(ExperimentalFoundationApi::class)
@Stable
@Composable
fun AutoScrollPager(pagerState: PagerState) {
val isDraggedState = pagerState.interactionSource.collectIsDraggedAsState()
LaunchedEffect(isDraggedState) {
snapshotFlow { isDraggedState.value }.collectLatest { isDragged ->
if (!isDragged) {
while (true) {
delay(10000L)
try {
pagerState.animateScrollToPage(pagerState.currentPage + 1)
} catch (ignore: IllegalStateException) {
ignore.printStackTrace()
}
}
}
}
no auto AutoScrollPager is to automatically scroll the pageBharat Kumar
08/01/2023, 6:27 AMfun Int.floorMod(other: Int): Int = when (other) {
0 -> this
else -> this - floorDiv(other) * other
}
Bharat Kumar
08/01/2023, 6:27 AMfloorMod
xoangon
08/01/2023, 6:33 AMAutoScrollPager
function. This line seems suspicious to me:
try {
pagerState.animateScrollToPage(pagerState.currentPage + 1)
}
xoangon
08/01/2023, 6:35 AMsnapshotFlow
in that AutoScrollPager
functionBharat Kumar
08/01/2023, 7:08 AMBharat Kumar
08/01/2023, 7:15 AMBharat Kumar
08/01/2023, 7:56 AMval pagerState = rememberPagerState(
// the initial value is set high to manage both side infinite scrolling
initialPage = (Int.MAX_VALUE/2)-(Int.MAX_VALUE/2%items.size),
initialPageOffsetFraction = 0f,
) {
// provide pageCount
Int.MAX_VALUE
}
to manage that i did this so that the initial item will be much far and look like infiniteczuckie
08/01/2023, 9:05 AMBharat Kumar
08/01/2023, 9:09 AM