Move PageCurlConfig to PageCurl argument

This commit is contained in:
Oleksandr Balan
2023-09-06 21:45:03 +02:00
parent c08c65ec96
commit 9addca8f27
12 changed files with 137 additions and 104 deletions

View File

@@ -45,8 +45,6 @@
<package name="kotlinx.android.synthetic" alias="false" withSubpackages="true" /> <package name="kotlinx.android.synthetic" alias="false" withSubpackages="true" />
</value> </value>
</option> </option>
<option name="NAME_COUNT_TO_USE_STAR_IMPORT" value="2147483647" />
<option name="NAME_COUNT_TO_USE_STAR_IMPORT_FOR_MEMBERS" value="2147483647" />
<option name="WRAP_ELVIS_EXPRESSIONS" value="0" /> <option name="WRAP_ELVIS_EXPRESSIONS" value="0" />
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" /> <option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
</JetCodeStyleSettings> </JetCodeStyleSettings>

2
.idea/compiler.xml generated
View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="CompilerConfiguration"> <component name="CompilerConfiguration">
<bytecodeTargetLevel target="11" /> <bytecodeTargetLevel target="17" />
</component> </component>
</project> </project>

4
.idea/gradle.xml generated
View File

@@ -4,10 +4,9 @@
<component name="GradleSettings"> <component name="GradleSettings">
<option name="linkedExternalProjectsSettings"> <option name="linkedExternalProjectsSettings">
<GradleProjectSettings> <GradleProjectSettings>
<option name="testRunner" value="GRADLE" />
<option name="distributionType" value="DEFAULT_WRAPPED" />
<option name="externalProjectPath" value="$PROJECT_DIR$" /> <option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="gradleHome" value="/usr/local/Cellar/gradle/6.0.1/libexec" /> <option name="gradleHome" value="/usr/local/Cellar/gradle/6.0.1/libexec" />
<option name="gradleJvm" value="#GRADLE_LOCAL_JAVA_HOME" />
<option name="modules"> <option name="modules">
<set> <set>
<option value="$PROJECT_DIR$" /> <option value="$PROJECT_DIR$" />
@@ -15,6 +14,7 @@
<option value="$PROJECT_DIR$/pagecurl" /> <option value="$PROJECT_DIR$/pagecurl" />
</set> </set>
</option> </option>
<option name="resolveExternalAnnotations" value="false" />
</GradleProjectSettings> </GradleProjectSettings>
</option> </option>
</component> </component>

10
.idea/migrations.xml generated Normal file
View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectMigrations">
<option name="MigrateToGradleLocalJavaHome">
<set>
<option value="$PROJECT_DIR$" />
</set>
</option>
</component>
</project>

3
.idea/misc.xml generated
View File

@@ -1,4 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="DesignSurface"> <component name="DesignSurface">
<option name="filePathToZoomLevelMap"> <option name="filePathToZoomLevelMap">
@@ -8,7 +7,7 @@
</map> </map>
</option> </option>
</component> </component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="11" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="jbr-17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" /> <output url="file://$PROJECT_DIR$/build/classes" />
</component> </component>
<component name="ProjectType"> <component name="ProjectType">

View File

@@ -52,32 +52,32 @@ fun BackPagePageCurlScreen() {
Box(Modifier.fillMaxSize()) { Box(Modifier.fillMaxSize()) {
val pages = remember { HowToPageData.backPageHowToPages } val pages = remember { HowToPageData.backPageHowToPages }
var zoomOut by remember { mutableStateOf(false) } var zoomOut by remember { mutableStateOf(false) }
val state = rememberPageCurlState( val state = rememberPageCurlState()
config = rememberPageCurlConfig( val config = rememberPageCurlConfig(
onCustomTap = { size, position -> onCustomTap = { size, position ->
// When PageCurl is zoomed out then zoom back in // When PageCurl is zoomed out then zoom back in
// Else detect tap somewhere in the center with 64 radius and zoom out a PageCurl // Else detect tap somewhere in the center with 64 radius and zoom out a PageCurl
if (zoomOut) { if (zoomOut) {
zoomOut = false zoomOut = false
true true
} else if ((position - size.center.toOffset()).getDistance() < 64.dp.toPx()) { } else if ((position - size.center.toOffset()).getDistance() < 64.dp.toPx()) {
zoomOut = true zoomOut = true
true true
} else { } else {
false false
}
} }
) }
) )
ZoomOutLayout( ZoomOutLayout(
zoomOut = zoomOut, zoomOut = zoomOut,
config = state.config, config = config,
bottom = { SettingsRow(state.config) }, bottom = { SettingsRow(config) },
) { ) {
PageCurl( PageCurl(
count = pages.size, count = pages.size,
state = state, state = state,
config = config,
) { index -> ) { index ->
HowToPage(index, pages[index]) HowToPage(index, pages[index])
} }

View File

@@ -47,32 +47,32 @@ fun InteractionConfigInPageCurlScreen() {
Box(Modifier.fillMaxSize()) { Box(Modifier.fillMaxSize()) {
val pages = remember { HowToPageData.interactionSettingsHowToPages } val pages = remember { HowToPageData.interactionSettingsHowToPages }
var zoomOut by remember { mutableStateOf(false) } var zoomOut by remember { mutableStateOf(false) }
val state = rememberPageCurlState( val state = rememberPageCurlState()
config = rememberPageCurlConfig( val config = rememberPageCurlConfig(
onCustomTap = { size, position -> onCustomTap = { size, position ->
// When PageCurl is zoomed out then zoom back in // When PageCurl is zoomed out then zoom back in
// Else detect tap somewhere in the center with 64 radius and zoom out a PageCurl // Else detect tap somewhere in the center with 64 radius and zoom out a PageCurl
if (zoomOut) { if (zoomOut) {
zoomOut = false zoomOut = false
true true
} else if ((position - size.center.toOffset()).getDistance() < 64.dp.toPx()) { } else if ((position - size.center.toOffset()).getDistance() < 64.dp.toPx()) {
zoomOut = true zoomOut = true
true true
} else { } else {
false false
}
} }
) }
) )
ZoomOutLayout( ZoomOutLayout(
zoomOut = zoomOut, zoomOut = zoomOut,
config = state.config, config = config,
bottom = { SettingsRow(state.config) }, bottom = { SettingsRow(config) },
) { ) {
PageCurl( PageCurl(
count = pages.size, count = pages.size,
state = state, state = state,
config = config,
) { index -> ) { index ->
HowToPage(index, pages[index]) HowToPage(index, pages[index])
} }

View File

@@ -33,30 +33,30 @@ fun SettingsPageCurlScreen() {
var showPopup by rememberSaveable { mutableStateOf(false) } var showPopup by rememberSaveable { mutableStateOf(false) }
val state = rememberPageCurlState( val state = rememberPageCurlState()
config = rememberPageCurlConfig( val config = rememberPageCurlConfig(
onCustomTap = { size, position -> onCustomTap = { size, position ->
// Detect tap somewhere in the center with 64 radius and show popup // Detect tap somewhere in the center with 64 radius and show popup
if ((position - size.center.toOffset()).getDistance() < 64.dp.toPx()) { if ((position - size.center.toOffset()).getDistance() < 64.dp.toPx()) {
showPopup = true showPopup = true
true true
} else { } else {
false false
}
} }
) }
) )
PageCurl( PageCurl(
count = pages.size, count = pages.size,
state = state, state = state,
config = config,
) { index -> ) { index ->
HowToPage(index, pages[index]) HowToPage(index, pages[index])
} }
if (showPopup) { if (showPopup) {
SettingsPopup( SettingsPopup(
config = state.config, config = config,
onDismiss = { onDismiss = {
showPopup = false showPopup = false
} }

View File

@@ -39,32 +39,32 @@ fun ShadowInPageCurlScreen() {
Box(Modifier.fillMaxSize()) { Box(Modifier.fillMaxSize()) {
val pages = remember { HowToPageData.shadowHowToPages } val pages = remember { HowToPageData.shadowHowToPages }
var zoomOut by remember { mutableStateOf(false) } var zoomOut by remember { mutableStateOf(false) }
val state = rememberPageCurlState( val state = rememberPageCurlState()
config = rememberPageCurlConfig( val config = rememberPageCurlConfig(
onCustomTap = { size, position -> onCustomTap = { size, position ->
// When PageCurl is zoomed out then zoom back in // When PageCurl is zoomed out then zoom back in
// Else detect tap somewhere in the center with 64 radius and zoom out a PageCurl // Else detect tap somewhere in the center with 64 radius and zoom out a PageCurl
if (zoomOut) { if (zoomOut) {
zoomOut = false zoomOut = false
true true
} else if ((position - size.center.toOffset()).getDistance() < 64.dp.toPx()) { } else if ((position - size.center.toOffset()).getDistance() < 64.dp.toPx()) {
zoomOut = true zoomOut = true
true true
} else { } else {
false false
}
} }
) }
) )
ZoomOutLayout( ZoomOutLayout(
zoomOut = zoomOut, zoomOut = zoomOut,
config = state.config, config = config,
bottom = { SettingsRow(state.config) }, bottom = { SettingsRow(config) },
) { ) {
PageCurl( PageCurl(
count = pages.size, count = pages.size,
state = state, state = state,
config = config,
) { index -> ) { index ->
HowToPage(index, pages[index]) HowToPage(index, pages[index])
} }

View File

@@ -44,32 +44,32 @@ fun StateInPageCurlScreen() {
Box(Modifier.fillMaxSize()) { Box(Modifier.fillMaxSize()) {
val pages = remember { HowToPageData.stateHowToPages } val pages = remember { HowToPageData.stateHowToPages }
var zoomOut by remember { mutableStateOf(false) } var zoomOut by remember { mutableStateOf(false) }
val state = rememberPageCurlState( val state = rememberPageCurlState()
config = rememberPageCurlConfig( val config = rememberPageCurlConfig(
onCustomTap = { size, position -> onCustomTap = { size, position ->
// When PageCurl is zoomed out then zoom back in // When PageCurl is zoomed out then zoom back in
// Else detect tap somewhere in the center with 64 radius and zoom out a PageCurl // Else detect tap somewhere in the center with 64 radius and zoom out a PageCurl
if (zoomOut) { if (zoomOut) {
zoomOut = false zoomOut = false
true true
} else if ((position - size.center.toOffset()).getDistance() < 64.dp.toPx()) { } else if ((position - size.center.toOffset()).getDistance() < 64.dp.toPx()) {
zoomOut = true zoomOut = true
true true
} else { } else {
false false
}
} }
) }
) )
ZoomOutLayout( ZoomOutLayout(
zoomOut = zoomOut, zoomOut = zoomOut,
config = state.config, config = config,
bottom = { SettingsRow(pages.size, state) }, bottom = { SettingsRow(pages.size, state) },
) { ) {
PageCurl( PageCurl(
count = pages.size, count = pages.size,
state = state, state = state,
config = config,
) { index -> ) { index ->
HowToPage(index, pages[index]) HowToPage(index, pages[index])
} }

View File

@@ -12,13 +12,16 @@ import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.runtime.setValue import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import eu.wewox.pagecurl.ExperimentalPageCurlApi import eu.wewox.pagecurl.ExperimentalPageCurlApi
import eu.wewox.pagecurl.config.PageCurlConfig
import eu.wewox.pagecurl.config.rememberPageCurlConfig
/** /**
* Shows the pages which may be turned by drag or tap gestures. * Shows the pages which may be turned by drag or tap gestures.
* *
* @param count The count of pages. * @param count The count of pages.
* @param state The state of the PageCurl. Use this to programmatically change the current page or observe changes.
* @param modifier The modifier for this composable. * @param modifier The modifier for this composable.
* @param state The state of the PageCurl. Use this to programmatically change the current page or observe changes.
* @param config The configuration for PageCurl.
* @param content The content lambda to provide the page composable. Receives the page number. * @param content The content lambda to provide the page composable. Receives the page number.
*/ */
@ExperimentalPageCurlApi @ExperimentalPageCurlApi
@@ -27,6 +30,7 @@ public fun PageCurl(
count: Int, count: Int,
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
state: PageCurlState = rememberPageCurlState(), state: PageCurlState = rememberPageCurlState(),
config: PageCurlConfig = rememberPageCurlConfig(),
content: @Composable (Int) -> Unit content: @Composable (Int) -> Unit
) { ) {
val scope = rememberCoroutineScope() val scope = rememberCoroutineScope()
@@ -37,13 +41,13 @@ public fun PageCurl(
val updatedCurrent by rememberUpdatedState(state.current) val updatedCurrent by rememberUpdatedState(state.current)
val internalState by rememberUpdatedState(state.internalState ?: return@BoxWithConstraints) val internalState by rememberUpdatedState(state.internalState ?: return@BoxWithConstraints)
val config by rememberUpdatedState(state.config) val config by rememberUpdatedState(config)
Box( Box(
Modifier Modifier
.curlGesture( .curlGesture(
state = internalState, state = internalState,
enabled = state.config.dragForwardEnabled && updatedCurrent < state.max - 1, enabled = config.dragForwardEnabled && updatedCurrent < state.max - 1,
scope = scope, scope = scope,
targetStart = config.dragForwardInteraction.start, targetStart = config.dragForwardInteraction.start,
targetEnd = config.dragForwardInteraction.end, targetEnd = config.dragForwardInteraction.end,
@@ -54,7 +58,7 @@ public fun PageCurl(
) )
.curlGesture( .curlGesture(
state = internalState, state = internalState,
enabled = state.config.dragBackwardEnabled && updatedCurrent > 0, enabled = config.dragBackwardEnabled && updatedCurrent > 0,
scope = scope, scope = scope,
targetStart = config.dragBackwardInteraction.start, targetStart = config.dragBackwardInteraction.start,
targetEnd = config.dragBackwardInteraction.end, targetEnd = config.dragBackwardInteraction.end,

View File

@@ -28,30 +28,52 @@ import kotlinx.coroutines.withContext
* Remembers the [PageCurlState]. * Remembers the [PageCurlState].
* *
* @param initialCurrent The initial current page. * @param initialCurrent The initial current page.
* @param config The configuration for PageCurl.
* @return The remembered [PageCurlState]. * @return The remembered [PageCurlState].
*/ */
@ExperimentalPageCurlApi @ExperimentalPageCurlApi
@Composable @Composable
public fun rememberPageCurlState( public fun rememberPageCurlState(
initialCurrent: Int = 0, initialCurrent: Int = 0,
config: PageCurlConfig = rememberPageCurlConfig()
): PageCurlState = ): PageCurlState =
rememberSaveable( rememberSaveable(
initialCurrent, initialCurrent,
saver = Saver( saver = Saver(
save = { it.current }, save = { it.current },
restore = { restore = { PageCurlState(initialCurrent = it) }
PageCurlState( )
initialCurrent = it, ) {
config = config, PageCurlState(
) initialCurrent = initialCurrent,
} )
}
/**
* Remembers the [PageCurlState].
*
* @param initialCurrent The initial current page.
* @param config The configuration for PageCurl.
* @return The remembered [PageCurlState].
*/
@ExperimentalPageCurlApi
@Composable
@Deprecated(
message = "Specify 'config' as 'config' in PageCurl composable.",
level = DeprecationLevel.ERROR,
)
@Suppress("UnusedPrivateMember")
public fun rememberPageCurlState(
initialCurrent: Int = 0,
config: PageCurlConfig,
): PageCurlState =
rememberSaveable(
initialCurrent,
saver = Saver(
save = { it.current },
restore = { PageCurlState(initialCurrent = it) }
) )
) { ) {
PageCurlState( PageCurlState(
initialCurrent = initialCurrent, initialCurrent = initialCurrent,
config = config,
) )
} }
@@ -65,7 +87,11 @@ public fun rememberPageCurlState(
*/ */
@ExperimentalPageCurlApi @ExperimentalPageCurlApi
@Composable @Composable
@Deprecated("Specify 'max' as 'count' in PageCurl composable.") @Deprecated(
message = "Specify 'max' as 'count' in PageCurl composable and 'config' as 'config' in PageCurl composable.",
level = DeprecationLevel.ERROR,
)
@Suppress("UnusedPrivateMember")
public fun rememberPageCurlState( public fun rememberPageCurlState(
max: Int, max: Int,
initialCurrent: Int = 0, initialCurrent: Int = 0,
@@ -79,7 +105,6 @@ public fun rememberPageCurlState(
PageCurlState( PageCurlState(
initialCurrent = it, initialCurrent = it,
initialMax = max, initialMax = max,
config = config,
) )
} }
) )
@@ -87,20 +112,17 @@ public fun rememberPageCurlState(
PageCurlState( PageCurlState(
initialCurrent = initialCurrent, initialCurrent = initialCurrent,
initialMax = max, initialMax = max,
config = config,
) )
} }
/** /**
* The state of the PageCurl. * The state of the PageCurl.
* *
* @property config The configuration for PageCurl.
* @param initialMax The initial max number of pages. * @param initialMax The initial max number of pages.
* @param initialCurrent The initial current page. * @param initialCurrent The initial current page.
*/ */
@ExperimentalPageCurlApi @ExperimentalPageCurlApi
public class PageCurlState( public class PageCurlState(
public val config: PageCurlConfig,
initialMax: Int = 0, initialMax: Int = 0,
initialCurrent: Int = 0, initialCurrent: Int = 0,
) { ) {