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

@@ -12,13 +12,16 @@ import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
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.
*
* @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 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.
*/
@ExperimentalPageCurlApi
@@ -27,6 +30,7 @@ public fun PageCurl(
count: Int,
modifier: Modifier = Modifier,
state: PageCurlState = rememberPageCurlState(),
config: PageCurlConfig = rememberPageCurlConfig(),
content: @Composable (Int) -> Unit
) {
val scope = rememberCoroutineScope()
@@ -37,13 +41,13 @@ public fun PageCurl(
val updatedCurrent by rememberUpdatedState(state.current)
val internalState by rememberUpdatedState(state.internalState ?: return@BoxWithConstraints)
val config by rememberUpdatedState(state.config)
val config by rememberUpdatedState(config)
Box(
Modifier
.curlGesture(
state = internalState,
enabled = state.config.dragForwardEnabled && updatedCurrent < state.max - 1,
enabled = config.dragForwardEnabled && updatedCurrent < state.max - 1,
scope = scope,
targetStart = config.dragForwardInteraction.start,
targetEnd = config.dragForwardInteraction.end,
@@ -54,7 +58,7 @@ public fun PageCurl(
)
.curlGesture(
state = internalState,
enabled = state.config.dragBackwardEnabled && updatedCurrent > 0,
enabled = config.dragBackwardEnabled && updatedCurrent > 0,
scope = scope,
targetStart = config.dragBackwardInteraction.start,
targetEnd = config.dragBackwardInteraction.end,

View File

@@ -28,30 +28,52 @@ import kotlinx.coroutines.withContext
* Remembers the [PageCurlState].
*
* @param initialCurrent The initial current page.
* @param config The configuration for PageCurl.
* @return The remembered [PageCurlState].
*/
@ExperimentalPageCurlApi
@Composable
public fun rememberPageCurlState(
initialCurrent: Int = 0,
config: PageCurlConfig = rememberPageCurlConfig()
): PageCurlState =
rememberSaveable(
initialCurrent,
saver = Saver(
save = { it.current },
restore = {
PageCurlState(
initialCurrent = it,
config = config,
)
}
restore = { PageCurlState(initialCurrent = it) }
)
) {
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(
initialCurrent = initialCurrent,
config = config,
)
}
@@ -65,7 +87,11 @@ public fun rememberPageCurlState(
*/
@ExperimentalPageCurlApi
@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(
max: Int,
initialCurrent: Int = 0,
@@ -79,7 +105,6 @@ public fun rememberPageCurlState(
PageCurlState(
initialCurrent = it,
initialMax = max,
config = config,
)
}
)
@@ -87,20 +112,17 @@ public fun rememberPageCurlState(
PageCurlState(
initialCurrent = initialCurrent,
initialMax = max,
config = config,
)
}
/**
* The state of the PageCurl.
*
* @property config The configuration for PageCurl.
* @param initialMax The initial max number of pages.
* @param initialCurrent The initial current page.
*/
@ExperimentalPageCurlApi
public class PageCurlState(
public val config: PageCurlConfig,
initialMax: Int = 0,
initialCurrent: Int = 0,
) {