Add dynamic max support

This commit is contained in:
Oleksandr Balan
2023-03-21 17:35:16 +01:00
parent 2c1d7b3ef9
commit 27a3ae2e79
14 changed files with 237 additions and 26 deletions

View File

@@ -12,6 +12,7 @@ import eu.wewox.pagecurl.ExperimentalPageCurlApi
/**
* 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 content The content lambda to provide the page composable. Receives the page number.
@@ -19,14 +20,16 @@ import eu.wewox.pagecurl.ExperimentalPageCurlApi
@ExperimentalPageCurlApi
@Composable
public fun PageCurl(
state: PageCurlState,
count: Int,
modifier: Modifier = Modifier,
state: PageCurlState = rememberPageCurlState(),
content: @Composable (Int) -> Unit
) {
val scope = rememberCoroutineScope()
val updatedCurrent by rememberUpdatedState(state.current)
BoxWithConstraints(modifier) {
state.max = count
state.setup(constraints)
val internalState by rememberUpdatedState(state.internalState ?: return@BoxWithConstraints)
@@ -82,3 +85,26 @@ public fun PageCurl(
}
}
}
/**
* Shows the pages which may be turned by drag or tap gestures.
*
* @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 content The content lambda to provide the page composable. Receives the page number.
*/
@ExperimentalPageCurlApi
@Composable
@Deprecated("Specify 'max' as 'count' in PageCurl composable.")
public fun PageCurl(
state: PageCurlState,
modifier: Modifier = Modifier,
content: @Composable (Int) -> Unit
) {
PageCurl(
count = state.max,
state = state,
modifier = modifier,
content = content,
)
}

View File

@@ -24,6 +24,37 @@ import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
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,
)
}
)
) {
PageCurlState(
initialCurrent = initialCurrent,
config = config,
)
}
/**
* Remembers the [PageCurlState].
*
@@ -34,6 +65,7 @@ import kotlinx.coroutines.withContext
*/
@ExperimentalPageCurlApi
@Composable
@Deprecated("Specify 'max' as 'count' in PageCurl composable.")
public fun rememberPageCurlState(
max: Int,
initialCurrent: Int = 0,
@@ -45,16 +77,16 @@ public fun rememberPageCurlState(
save = { it.current },
restore = {
PageCurlState(
max = max,
initialCurrent = it,
initialMax = max,
config = config,
)
}
)
) {
PageCurlState(
max = max,
initialCurrent = initialCurrent,
initialMax = max,
config = config,
)
}
@@ -62,14 +94,14 @@ public fun rememberPageCurlState(
/**
* The state of the PageCurl.
*
* @property max The max number of pages.
* @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 max: Int,
public val config: PageCurlConfig,
initialMax: Int = 0,
initialCurrent: Int = 0,
) {
/**
@@ -84,6 +116,8 @@ public class PageCurlState(
*/
public val progress: Float get() = internalState?.progress ?: 0f
internal var max: Int = initialMax
internal var internalState: InternalState? by mutableStateOf(null)
internal fun setup(constraints: Constraints) {