mirror of
https://github.com/fankes/pagecurl-multiplatform.git
synced 2025-09-06 10:45:43 +08:00
Refactor page curl config
This commit is contained in:
@@ -20,13 +20,11 @@ import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.window.Popup
|
||||
import androidx.compose.ui.window.PopupProperties
|
||||
import eu.wewox.pagecurl.ExperimentalPageCurlApi
|
||||
import eu.wewox.pagecurl.config.InteractionConfig
|
||||
import eu.wewox.pagecurl.config.copy
|
||||
import eu.wewox.pagecurl.config.PageCurlConfig
|
||||
|
||||
@Composable
|
||||
internal fun SettingsPopup(
|
||||
interaction: InteractionConfig,
|
||||
onConfigChange: (InteractionConfig) -> Unit,
|
||||
config: PageCurlConfig,
|
||||
onDismiss: () -> Unit,
|
||||
) {
|
||||
Popup(
|
||||
@@ -49,26 +47,26 @@ internal fun SettingsPopup(
|
||||
.padding(horizontal = 10.dp)
|
||||
SwitchRow(
|
||||
text = "Forward drag enabled",
|
||||
enabled = interaction.drag.forward.enabled,
|
||||
onChanged = { onConfigChange(interaction.copy(dragForwardEnabled = it)) },
|
||||
enabled = config.dragForwardEnabled,
|
||||
onChanged = { config.dragForwardEnabled = it },
|
||||
modifier = switchRowModifier
|
||||
)
|
||||
SwitchRow(
|
||||
text = "Backward drag enabled",
|
||||
enabled = interaction.drag.backward.enabled,
|
||||
onChanged = { onConfigChange(interaction.copy(dragBackwardEnabled = it)) },
|
||||
enabled = config.dragBackwardEnabled,
|
||||
onChanged = { config.dragBackwardEnabled = it },
|
||||
modifier = switchRowModifier
|
||||
)
|
||||
SwitchRow(
|
||||
text = "Forward tap enabled",
|
||||
enabled = interaction.tap.forward.enabled,
|
||||
onChanged = { onConfigChange(interaction.copy(tapForwardEnabled = it)) },
|
||||
enabled = config.tapForwardEnabled,
|
||||
onChanged = { config.tapForwardEnabled = it },
|
||||
modifier = switchRowModifier
|
||||
)
|
||||
SwitchRow(
|
||||
text = "Backward tap enabled",
|
||||
enabled = interaction.tap.backward.enabled,
|
||||
onChanged = { onConfigChange(interaction.copy(tapBackwardEnabled = it)) },
|
||||
enabled = config.tapBackwardEnabled,
|
||||
onChanged = { config.tapBackwardEnabled = it },
|
||||
modifier = switchRowModifier
|
||||
)
|
||||
}
|
||||
|
@@ -8,6 +8,7 @@ import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.saveable.rememberSaveable
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.unit.center
|
||||
@@ -17,8 +18,7 @@ import eu.wewox.pagecurl.ExperimentalPageCurlApi
|
||||
import eu.wewox.pagecurl.HowToPageData
|
||||
import eu.wewox.pagecurl.components.HowToPage
|
||||
import eu.wewox.pagecurl.components.SettingsPopup
|
||||
import eu.wewox.pagecurl.config.InteractionConfig
|
||||
import eu.wewox.pagecurl.config.PageCurlConfig
|
||||
import eu.wewox.pagecurl.config.rememberPageCurlConfig
|
||||
import eu.wewox.pagecurl.page.PageCurl
|
||||
import eu.wewox.pagecurl.page.rememberPageCurlState
|
||||
|
||||
@@ -26,45 +26,31 @@ import eu.wewox.pagecurl.page.rememberPageCurlState
|
||||
fun SettingsPageCurlScreen() {
|
||||
Box(Modifier.fillMaxSize()) {
|
||||
val pages = remember { HowToPageData.interactionHowToPages }
|
||||
val state = rememberPageCurlState(max = pages.size)
|
||||
|
||||
var showPopup by remember { mutableStateOf(false) }
|
||||
var showPopup by rememberSaveable { mutableStateOf(false) }
|
||||
|
||||
// Create a mutable interaction config with custom tap interaction
|
||||
// In SettingsPopup config is mutated
|
||||
var interaction by remember {
|
||||
mutableStateOf(
|
||||
InteractionConfig(
|
||||
tap = InteractionConfig.Tap(
|
||||
custom = InteractionConfig.Tap.CustomInteraction(true) { size, position ->
|
||||
// Detect tap somewhere in the center with 64 radius and show popup
|
||||
if ((position - size.center.toOffset()).getDistance() < 64.dp.toPx()) {
|
||||
showPopup = true
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
)
|
||||
)
|
||||
val state = rememberPageCurlState(
|
||||
max = pages.size,
|
||||
config = rememberPageCurlConfig(
|
||||
onCustomTap = { size, position ->
|
||||
// Detect tap somewhere in the center with 64 radius and show popup
|
||||
if ((position - size.center.toOffset()).getDistance() < 64.dp.toPx()) {
|
||||
showPopup = true
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
PageCurl(
|
||||
state = state,
|
||||
config = PageCurlConfig(
|
||||
interaction = interaction
|
||||
)
|
||||
) { index ->
|
||||
PageCurl(state = state) { index ->
|
||||
HowToPage(index, pages[index])
|
||||
}
|
||||
|
||||
if (showPopup) {
|
||||
SettingsPopup(
|
||||
interaction = interaction,
|
||||
onConfigChange = {
|
||||
interaction = it
|
||||
},
|
||||
config = state.config,
|
||||
onDismiss = {
|
||||
showPopup = false
|
||||
}
|
||||
|
@@ -16,6 +16,7 @@ import androidx.compose.material.Button
|
||||
import androidx.compose.material.Card
|
||||
import androidx.compose.material.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
@@ -30,9 +31,7 @@ import eu.wewox.pagecurl.ExperimentalPageCurlApi
|
||||
import eu.wewox.pagecurl.HowToPageData
|
||||
import eu.wewox.pagecurl.components.HowToPage
|
||||
import eu.wewox.pagecurl.components.ZoomOutLayout
|
||||
import eu.wewox.pagecurl.config.InteractionConfig
|
||||
import eu.wewox.pagecurl.config.PageCurlConfig
|
||||
import eu.wewox.pagecurl.config.copy
|
||||
import eu.wewox.pagecurl.config.rememberPageCurlConfig
|
||||
import eu.wewox.pagecurl.page.PageCurl
|
||||
import eu.wewox.pagecurl.page.PageCurlState
|
||||
import eu.wewox.pagecurl.page.rememberPageCurlState
|
||||
@@ -44,28 +43,34 @@ import kotlinx.coroutines.launch
|
||||
fun StatePageCurlScreen() {
|
||||
Box(Modifier.fillMaxSize()) {
|
||||
val pages = remember { HowToPageData.interactionHowToPages }
|
||||
val state = rememberPageCurlState(max = pages.size)
|
||||
|
||||
var zoomOut by remember { mutableStateOf(false) }
|
||||
|
||||
val interactionConfig = remember {
|
||||
InteractionConfig(
|
||||
tap = InteractionConfig.Tap(
|
||||
custom = InteractionConfig.Tap.CustomInteraction(true) { size, position ->
|
||||
// When PageCurl is zoomed out then zoom back in
|
||||
// Else detect tap somewhere in the center with 64 radius and zoom out a PageCurl
|
||||
if (zoomOut) {
|
||||
zoomOut = false
|
||||
true
|
||||
} else if ((position - size.center.toOffset()).getDistance() < 64.dp.toPx()) {
|
||||
zoomOut = true
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
val state = rememberPageCurlState(
|
||||
max = pages.size,
|
||||
config = rememberPageCurlConfig(
|
||||
onCustomTap = { size, position ->
|
||||
// When PageCurl is zoomed out then zoom back in
|
||||
// Else detect tap somewhere in the center with 64 radius and zoom out a PageCurl
|
||||
if (zoomOut) {
|
||||
zoomOut = false
|
||||
true
|
||||
} else if ((position - size.center.toOffset()).getDistance() < 64.dp.toPx()) {
|
||||
zoomOut = true
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
)
|
||||
}
|
||||
)
|
||||
)
|
||||
|
||||
// Disable all state interactions when PageCurl is zoomed out
|
||||
LaunchedEffect(zoomOut) {
|
||||
with(state.config) {
|
||||
dragForwardEnabled = !zoomOut
|
||||
dragBackwardEnabled = !zoomOut
|
||||
tapForwardEnabled = !zoomOut
|
||||
tapBackwardEnabled = !zoomOut
|
||||
}
|
||||
}
|
||||
|
||||
ZoomOutLayout(
|
||||
@@ -79,17 +84,7 @@ fun StatePageCurlScreen() {
|
||||
shape = RoundedCornerShape(cornersAndElevation),
|
||||
elevation = cornersAndElevation,
|
||||
) {
|
||||
PageCurl(
|
||||
state = state,
|
||||
config = PageCurlConfig(
|
||||
interaction = interactionConfig.copy(
|
||||
dragForwardEnabled = !zoomOut,
|
||||
dragBackwardEnabled = !zoomOut,
|
||||
tapForwardEnabled = !zoomOut,
|
||||
tapBackwardEnabled = !zoomOut,
|
||||
)
|
||||
)
|
||||
) { index ->
|
||||
PageCurl(state = state) { index ->
|
||||
HowToPage(index, pages[index])
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user