From 258f76276905068ae91aed41f67077b42d203015 Mon Sep 17 00:00:00 2001 From: Isaac Udy Date: Tue, 30 Apr 2024 17:46:16 +1200 Subject: [PATCH] Add example for HorizontalPager to the test application --- .../application/compose/HorizontalPager.kt | 107 ++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 tests/application/src/main/java/dev/enro/tests/application/compose/HorizontalPager.kt diff --git a/tests/application/src/main/java/dev/enro/tests/application/compose/HorizontalPager.kt b/tests/application/src/main/java/dev/enro/tests/application/compose/HorizontalPager.kt new file mode 100644 index 00000000..5d34b08a --- /dev/null +++ b/tests/application/src/main/java/dev/enro/tests/application/compose/HorizontalPager.kt @@ -0,0 +1,107 @@ +package dev.enro.tests.application.compose + +import androidx.compose.foundation.ExperimentalFoundationApi +import androidx.compose.foundation.pager.HorizontalPager +import androidx.compose.foundation.pager.rememberPagerState +import androidx.compose.material.Button +import androidx.compose.material.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import dev.enro.annotations.NavigationDestination +import dev.enro.core.NavigationKey +import dev.enro.core.compose.container.rememberNavigationContainerGroup +import dev.enro.core.compose.navigationHandle +import dev.enro.core.compose.rememberNavigationContainer +import dev.enro.core.container.EmptyBehavior +import dev.enro.core.container.accept +import dev.enro.core.push +import dev.enro.tests.application.compose.common.TitledColumn +import kotlinx.parcelize.Parcelize + +@Parcelize +object HorizontalPager : NavigationKey.SupportsPush { + @Parcelize + internal class PageOne(val name: String) : NavigationKey.SupportsPush + + @Parcelize + internal class PageTwo(val name: String) : NavigationKey.SupportsPush +} + +@OptIn(ExperimentalFoundationApi::class) +@NavigationDestination(HorizontalPager::class) +@Composable +fun HorizontalPagerCrossFadeScreen() { + val pageOne = rememberNavigationContainer( + root = HorizontalPager.PageOne("Root"), + emptyBehavior = EmptyBehavior.CloseParent, + filter = accept { key() } + ) + val pageTwo = rememberNavigationContainer( + root = HorizontalPager.PageTwo("Root"), + emptyBehavior = EmptyBehavior.CloseParent, + filter = accept { key() } + ) + val containerGroup = rememberNavigationContainerGroup(pageOne, pageTwo) + val state = rememberPagerState { containerGroup.containers.size } + LaunchedEffect(containerGroup.activeContainer) { + val target = when (containerGroup.activeContainer) { + pageOne -> 0 + else -> 1 + } + if (target != state.currentPage) { + state.animateScrollToPage(target) + } + } + HorizontalPager(state = state) { page -> + containerGroup.containers[page].Render() + } +} + +@NavigationDestination(HorizontalPager.PageOne::class) +@Composable +fun HorizontalPagerCrossFadePageOneScreen() { + val navigation = navigationHandle() + TitledColumn(title = "Page One") { + Text(text = "Id: ${navigation.key.name}") + Button( + onClick = { + navigation.push(HorizontalPager.PageOne((navigation.key.name.toIntOrNull() ?: 0).plus(1).toString())) + } + ) { + Text(text = "Next Page (one)") + } + Button( + onClick = { + navigation.push(HorizontalPager.PageTwo((navigation.key.name.toIntOrNull() ?: 0).plus(1).toString())) + } + ) { + Text(text = "Next Page (two)") + } + } +} + + +@NavigationDestination(HorizontalPager.PageTwo::class) +@Composable +fun HorizontalPagerCrossFadePageTwoScreen() { + val navigation = navigationHandle() + TitledColumn(title = "Page Two") { + Text(text = "Id: ${navigation.key.name}") + Button( + onClick = { + navigation.push(HorizontalPager.PageOne((navigation.key.name.toIntOrNull() ?: 0).plus(1).toString())) + } + ) { + Text(text = "Next Page (one)") + } + Button( + onClick = { + navigation.push(HorizontalPager.PageTwo((navigation.key.name.toIntOrNull() ?: 0).plus(1).toString())) + } + ) { + Text(text = "Next Page (two)") + } + } +} + +