mirror of
https://github.com/fankes/pagecurl-multiplatform.git
synced 2025-09-08 03:24:03 +08:00
Add examples to demo app
This commit is contained in:
@@ -1,133 +1,65 @@
|
||||
@file:OptIn(ExperimentalPageCurlApi::class)
|
||||
|
||||
package eu.wewox.pagecurl
|
||||
|
||||
import android.os.Bundle
|
||||
import androidx.activity.ComponentActivity
|
||||
import androidx.activity.compose.BackHandler
|
||||
import androidx.activity.compose.setContent
|
||||
import androidx.compose.foundation.Image
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.animation.Crossfade
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.foundation.layout.safeDrawingPadding
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.items
|
||||
import androidx.compose.material.Icon
|
||||
import androidx.compose.material.MaterialTheme
|
||||
import androidx.compose.material.Scaffold
|
||||
import androidx.compose.material.Text
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.KeyboardArrowRight
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.derivedStateOf
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.rememberCoroutineScope
|
||||
import androidx.compose.runtime.saveable.rememberSaveable
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.layout.ContentScale
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.unit.center
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import androidx.compose.ui.unit.toOffset
|
||||
import eu.wewox.pagecurl.components.SettingsAction
|
||||
import eu.wewox.pagecurl.components.SettingsPopup
|
||||
import eu.wewox.pagecurl.config.CurlConfig
|
||||
import eu.wewox.pagecurl.config.InteractionConfig
|
||||
import eu.wewox.pagecurl.config.PageCurlConfig
|
||||
import eu.wewox.pagecurl.config.ShadowConfig
|
||||
import eu.wewox.pagecurl.config.copy
|
||||
import eu.wewox.pagecurl.page.PageCurl
|
||||
import eu.wewox.pagecurl.page.PageCurlState
|
||||
import eu.wewox.pagecurl.page.rememberPageCurlState
|
||||
import androidx.core.view.WindowCompat
|
||||
import eu.wewox.pagecurl.components.TopBar
|
||||
import eu.wewox.pagecurl.screens.AnimatePageCurlScreen
|
||||
import eu.wewox.pagecurl.screens.SettingsPageCurlScreen
|
||||
import eu.wewox.pagecurl.screens.SimplePageCurlScreen
|
||||
import eu.wewox.pagecurl.screens.StatePageCurlScreen
|
||||
import eu.wewox.pagecurl.ui.SpacingMedium
|
||||
import eu.wewox.pagecurl.ui.theme.PageCurlTheme
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
/**
|
||||
* Main activity for demo application.
|
||||
* Contains simple "Crossfade" based navigation to various examples.
|
||||
*/
|
||||
class MainActivity : ComponentActivity() {
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
WindowCompat.setDecorFitsSystemWindows(window, false)
|
||||
|
||||
setContent {
|
||||
PageCurlTheme {
|
||||
Box {
|
||||
val state = rememberPageCurlState(max = 6)
|
||||
var showPopup by remember { mutableStateOf(false) }
|
||||
var interaction by remember {
|
||||
mutableStateOf(
|
||||
InteractionConfig(
|
||||
tap = InteractionConfig.Tap(
|
||||
custom = InteractionConfig.Tap.CustomInteraction(true) { size, position ->
|
||||
if ((position - size.center.toOffset()).getDistance() < 64.dp.toPx()) {
|
||||
showPopup = true
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
var example by rememberSaveable { mutableStateOf<Example?>(null) }
|
||||
|
||||
val curlConfig by derivedStateOf {
|
||||
val offset = if (state.progress > 0f) 0f else 1f
|
||||
val shadowAlpha = (offset + state.progress) * 0.5f
|
||||
BackHandler(enabled = example != null) {
|
||||
example = null
|
||||
}
|
||||
|
||||
CurlConfig(
|
||||
shadow = ShadowConfig(
|
||||
alpha = shadowAlpha
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
PageCurl(
|
||||
state = state,
|
||||
config = PageCurlConfig(
|
||||
curl = curlConfig,
|
||||
interaction = interaction
|
||||
)
|
||||
) { index ->
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.background(Color.White)
|
||||
) {
|
||||
when (index) {
|
||||
0 -> {
|
||||
Image(
|
||||
painter = painterResource(R.drawable.img_sleep),
|
||||
contentDescription = null,
|
||||
contentScale = ContentScale.Crop,
|
||||
)
|
||||
}
|
||||
else -> {
|
||||
Text(
|
||||
text = if (index % 2 == 1) Data.Lorem1 else Data.Lorem2,
|
||||
fontSize = 22.sp,
|
||||
modifier = Modifier.padding(16.dp)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Text(
|
||||
text = index.toString(),
|
||||
color = Color.White,
|
||||
modifier = Modifier
|
||||
.align(Alignment.BottomEnd)
|
||||
.background(Color.Black, RoundedCornerShape(topStartPercent = 100))
|
||||
.padding(16.dp)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if (showPopup) {
|
||||
SettingsPopup(
|
||||
state = state,
|
||||
interaction = interaction,
|
||||
onConfigChange = {
|
||||
interaction = it
|
||||
},
|
||||
onDismiss = {
|
||||
showPopup = false
|
||||
}
|
||||
)
|
||||
Crossfade(targetState = example, Modifier.safeDrawingPadding()) { selected ->
|
||||
when (selected) {
|
||||
null -> RootScreen(onExampleClick = { example = it })
|
||||
Example.SimplePageCurl -> SimplePageCurlScreen()
|
||||
Example.SettingsPageCurl -> SettingsPageCurlScreen()
|
||||
Example.CustomPageCurl -> StatePageCurlScreen()
|
||||
Example.AnimatePageCurl -> AnimatePageCurlScreen()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -136,48 +68,35 @@ class MainActivity : ComponentActivity() {
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun SettingsPopup(
|
||||
state: PageCurlState,
|
||||
interaction: InteractionConfig,
|
||||
onConfigChange: (InteractionConfig) -> Unit,
|
||||
onDismiss: () -> Unit,
|
||||
) {
|
||||
val scope = rememberCoroutineScope()
|
||||
SettingsPopup(
|
||||
interactionConfig = interaction,
|
||||
onAction = { action ->
|
||||
when (action) {
|
||||
SettingsAction.GoToFirst -> {
|
||||
scope.launch {
|
||||
state.snapTo(0)
|
||||
private fun RootScreen(onExampleClick: (Example) -> Unit) {
|
||||
Scaffold(
|
||||
topBar = { TopBar("Page Curl Demo") }
|
||||
) { padding ->
|
||||
LazyColumn(Modifier.padding(padding)) {
|
||||
items(Example.values()) {
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.clickable { onExampleClick(it) }
|
||||
.padding(SpacingMedium)
|
||||
) {
|
||||
Column(modifier = Modifier.weight(1f)) {
|
||||
Text(
|
||||
text = it.label,
|
||||
style = MaterialTheme.typography.h6
|
||||
)
|
||||
Text(
|
||||
text = it.description,
|
||||
style = MaterialTheme.typography.body2
|
||||
)
|
||||
}
|
||||
}
|
||||
SettingsAction.GoToLast -> {
|
||||
scope.launch {
|
||||
state.snapTo(state.max - 1)
|
||||
}
|
||||
}
|
||||
is SettingsAction.ForwardDragEnabled -> {
|
||||
onConfigChange(interaction.copy(dragForwardEnabled = action.value))
|
||||
}
|
||||
is SettingsAction.BackwardDragEnabled -> {
|
||||
onConfigChange(interaction.copy(dragBackwardEnabled = action.value))
|
||||
}
|
||||
is SettingsAction.ForwardTapEnabled -> {
|
||||
onConfigChange(interaction.copy(tapForwardEnabled = action.value))
|
||||
}
|
||||
is SettingsAction.BackwardTapEnabled -> {
|
||||
onConfigChange(interaction.copy(tapBackwardEnabled = action.value))
|
||||
Icon(
|
||||
imageVector = Icons.Default.KeyboardArrowRight,
|
||||
contentDescription = null
|
||||
)
|
||||
}
|
||||
}
|
||||
},
|
||||
onDismiss = onDismiss
|
||||
)
|
||||
}
|
||||
|
||||
private object Data {
|
||||
val Lorem1 =
|
||||
"Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aliquam erat volutpat. Phasellus enim erat, vestibulum vel, aliquam a, posuere eu, velit. Et harum quidem rerum facilis est et expedita distinctio. In sem justo, commodo ut, suscipit at, pharetra vitae, orci. Vestibulum fermentum tortor id mi. Sed elit dui, pellentesque a, faucibus vel, interdum nec, diam. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat. Nunc tincidunt ante vitae massa. Maecenas fermentum, sem in pharetra pellentesque, velit turpis volutpat ante, in pharetra metus odio a lectus. Proin in tellus sit amet nibh dignissim sagittis. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos. Etiam posuere lacus quis dolor. Class aptent taciti sociosqu ad litora."
|
||||
val Lorem2 =
|
||||
"Phasellus enim erat, vestibulum vel, aliquam a, posuere eu, velit. Duis ante orci, molestie vitae vehicula venenatis, tincidunt ac pede. Aliquam in lorem sit amet leo accumsan lacinia. Morbi imperdiet, mauris ac auctor dictum, nisl ligula egestas nulla, et sollicitudin sem purus in lacus. Ut tempus purus at lorem. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Integer malesuada. Sed vel lectus. Donec odio tempus molestie, porttitor ut, iaculis quis, sem. Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Etiam egestas wisi a erat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Maecenas lorem. Mauris dolor felis, sagittis at, luctus sed, aliquam non, tellus. Aenean id metus id velit ullamcorper pulvinar. Integer malesuada."
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user