-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example for HorizontalPager to the test application
- Loading branch information
Showing
1 changed file
with
107 additions
and
0 deletions.
There are no files selected for viewing
107 changes: 107 additions & 0 deletions
107
tests/application/src/main/java/dev/enro/tests/application/compose/HorizontalPager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<HorizontalPager.PageOne>() } | ||
) | ||
val pageTwo = rememberNavigationContainer( | ||
root = HorizontalPager.PageTwo("Root"), | ||
emptyBehavior = EmptyBehavior.CloseParent, | ||
filter = accept { key<HorizontalPager.PageTwo>() } | ||
) | ||
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<HorizontalPager.PageOne>() | ||
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<HorizontalPager.PageTwo>() | ||
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)") | ||
} | ||
} | ||
} | ||
|
||
|