Compare commits

..

8 Commits

15 changed files with 72 additions and 127 deletions

View File

@@ -1,5 +1,14 @@
# Changelog # Changelog
## 0.24.9
* `Pagination`:
* Make alternative constructor parameter `size` of `PaginationResult` with default value
* Add `Pagination.previousPage` extension
* `Compose`:
* Rework of `InfinityPagedComponentContext`
* Rework of `PagedComponent`
## 0.24.8 ## 0.24.8
* `Versions`: * `Versions`:

View File

@@ -1,2 +0,0 @@
actual val AllowDeepInsertOnWorksTest: Boolean
get() = true

View File

@@ -1,2 +0,0 @@
actual val AllowDeepInsertOnWorksTest: Boolean
get() = false

View File

@@ -1,2 +0,0 @@
actual val AllowDeepInsertOnWorksTest: Boolean
get() = true

View File

@@ -1,25 +1,20 @@
package dev.inmo.micro_utils.coroutines package dev.inmo.micro_utils.coroutines
import kotlinx.coroutines.* import kotlinx.coroutines.*
import kotlinx.coroutines.test.runTest
import kotlin.test.Test import kotlin.test.Test
class HandleSafelyCoroutineContextTest { class HandleSafelyCoroutineContextTest {
@Test @Test
fun testHandleSafelyCoroutineContext() { fun testHandleSafelyCoroutineContext() = runTest {
val scope = CoroutineScope(Dispatchers.Default) val scope = this
var contextHandlerHappen = false var contextHandlerHappen = false
var localHandlerHappen = false var localHandlerHappen = false
var defaultHandlerHappen = false
defaultSafelyExceptionHandler = {
defaultHandlerHappen = true
throw it
}
val contextHandler: ExceptionHandler<Unit> = {
contextHandlerHappen = true
}
val checkJob = scope.launch { val checkJob = scope.launch {
safelyWithContextExceptionHandler(contextHandler) { runCatchingLogging ({
safely( contextHandlerHappen = true
}) {
runCatchingLogging (
{ {
localHandlerHappen = true localHandlerHappen = true
} }
@@ -29,10 +24,8 @@ class HandleSafelyCoroutineContextTest {
println(coroutineContext) println(coroutineContext)
error("That must happen too:)") error("That must happen too:)")
} }
} }.join()
launchSynchronously { checkJob.join() }
assert(contextHandlerHappen) assert(contextHandlerHappen)
assert(localHandlerHappen) assert(localHandlerHappen)
assert(defaultHandlerHappen)
} }
} }

View File

@@ -1,3 +1,5 @@
package dev.inmo.micro_utils.coroutines
import dev.inmo.micro_utils.coroutines.collections.SortedBinaryTreeNode import dev.inmo.micro_utils.coroutines.collections.SortedBinaryTreeNode
import dev.inmo.micro_utils.coroutines.collections.addSubNode import dev.inmo.micro_utils.coroutines.collections.addSubNode
import dev.inmo.micro_utils.coroutines.collections.findNode import dev.inmo.micro_utils.coroutines.collections.findNode
@@ -10,8 +12,6 @@ import kotlin.test.assertEquals
import kotlin.test.assertTrue import kotlin.test.assertTrue
import kotlin.time.Duration.Companion.seconds import kotlin.time.Duration.Companion.seconds
expect val AllowDeepInsertOnWorksTest: Boolean
class SortedBinaryTreeNodeTests { class SortedBinaryTreeNodeTests {
@Test @Test
fun insertOnZeroLevelWorks() = runTest { fun insertOnZeroLevelWorks() = runTest {
@@ -46,7 +46,6 @@ class SortedBinaryTreeNodeTests {
} }
@Test @Test
fun deepReInsertOnWorks() = runTest(timeout = 300.seconds) { fun deepReInsertOnWorks() = runTest(timeout = 300.seconds) {
if (AllowDeepInsertOnWorksTest == false) return@runTest
val zeroNode = SortedBinaryTreeNode(0) val zeroNode = SortedBinaryTreeNode(0)
val rangeRadius = 500 val rangeRadius = 500
val nodes = mutableMapOf<Int, SortedBinaryTreeNode<Int>>() val nodes = mutableMapOf<Int, SortedBinaryTreeNode<Int>>()
@@ -124,7 +123,6 @@ class SortedBinaryTreeNodeTests {
} }
@Test @Test
fun deepInsertOnWorks() = runTest(timeout = 240.seconds) { fun deepInsertOnWorks() = runTest(timeout = 240.seconds) {
if (AllowDeepInsertOnWorksTest == false) return@runTest
val zeroNode = SortedBinaryTreeNode(0) val zeroNode = SortedBinaryTreeNode(0)
val rangeRadius = 500 val rangeRadius = 500
val nodes = mutableMapOf<Int, SortedBinaryTreeNode<Int>>() val nodes = mutableMapOf<Int, SortedBinaryTreeNode<Int>>()

View File

@@ -1,2 +0,0 @@
actual val AllowDeepInsertOnWorksTest: Boolean
get() = true

View File

@@ -1,2 +0,0 @@
actual val AllowDeepInsertOnWorksTest: Boolean
get() = true

View File

@@ -1,2 +0,0 @@
actual val AllowDeepInsertOnWorksTest: Boolean
get() = true

View File

@@ -15,5 +15,5 @@ crypto_js_version=4.1.1
# Project data # Project data
group=dev.inmo group=dev.inmo
version=0.24.8 version=0.24.9
android_code_version=288 android_code_version=289

View File

@@ -32,7 +32,7 @@ data class PaginationResult<T>(
page: Int, page: Int,
results: List<T>, results: List<T>,
pagesNumber: Int, pagesNumber: Int,
size: Int size: Int = results.size
) : this( ) : this(
page, page,
size, size,

View File

@@ -26,6 +26,16 @@ inline fun Pagination.nextPage() =
size size
) )
/**
* This method DO NOT check [Pagination.page] of receiver. Returns pagination for previous page
*/
@Suppress("NOTHING_TO_INLINE")
inline fun Pagination.previousPage() =
SimplePagination(
page - 1,
size
)
/** /**
* @param page Current page number * @param page Current page number
* @param size Current page size * @param size Current page size

View File

@@ -1,6 +1,7 @@
package dev.inmo.micro_utils.pagination.compose package dev.inmo.micro_utils.pagination.compose
import androidx.compose.runtime.* import androidx.compose.runtime.*
import dev.inmo.micro_utils.coroutines.SpecialMutableStateFlow
import dev.inmo.micro_utils.pagination.* import dev.inmo.micro_utils.pagination.*
/** /**
@@ -18,32 +19,27 @@ class InfinityPagedComponentContext<T> internal constructor(
size: Int size: Int
) { ) {
internal val startPage = SimplePagination(page, size) internal val startPage = SimplePagination(page, size)
internal val iterationState: MutableState<Pair<Int, Pagination?>> = mutableStateOf(0 to null) internal val currentlyLoadingPage = SpecialMutableStateFlow<Pagination?>(startPage)
internal val dataState: MutableState<List<T>?> = mutableStateOf(null) internal val latestLoadedPage = SpecialMutableStateFlow<PaginationResult<T>?>(null)
internal var lastPageLoaded = false internal val dataState = SpecialMutableStateFlow<List<T>?>(null)
/** /**
* Loads the next page of data. If the current page is the last one, the function returns early. * Loads the next page of data. If the current page is the last one, the function returns early.
*/ */
fun loadNext() { fun loadNext() {
if (lastPageLoaded) return if (latestLoadedPage.value ?.isLastPage == true) return
if (iterationState.value.second is SimplePagination) return // Data loading has been inited but not loaded yet if (currentlyLoadingPage.value != null) return // Data loading has been inited but not loaded yet
iterationState.value = iterationState.value.let { currentlyLoadingPage.value = latestLoadedPage.value ?.nextPage() ?: startPage
if ((it.second as? PaginationResult<*>) ?.isLastPage == true) return
(it.first + 1) to (it.second ?: startPage).nextPage()
}
} }
/** /**
* Reloads the pagination from the first page, clearing previously loaded data. * Reloads the pagination from the first page, clearing previously loaded data.
*/ */
fun reload() { fun reload() {
dataState.value = null latestLoadedPage.value = null
lastPageLoaded = false currentlyLoadingPage.value = null
iterationState.value = iterationState.value.let { loadNext()
(it.first + 1) to null
}
} }
} }
@@ -66,16 +62,17 @@ internal fun <T> InfinityPagedComponent(
) { ) {
val context = remember { InfinityPagedComponentContext<T>(page, size) } val context = remember { InfinityPagedComponentContext<T>(page, size) }
LaunchedEffect(context.iterationState.value.first) { val currentlyLoadingState = context.currentlyLoadingPage.collectAsState()
val paginationResult = loader(context, context.iterationState.value.second ?: context.startPage) LaunchedEffect(currentlyLoadingState.value) {
if (paginationResult.isLastPage) { val paginationResult = loader(context, currentlyLoadingState.value ?: return@LaunchedEffect)
context.lastPageLoaded = true context.latestLoadedPage.value = paginationResult
} context.currentlyLoadingPage.value = null
context.iterationState.value = context.iterationState.value.copy(second = paginationResult)
context.dataState.value = (context.dataState.value ?: emptyList()) + paginationResult.results context.dataState.value = (context.dataState.value ?: emptyList()) + paginationResult.results
} }
context.block(context.dataState.value) val dataState = context.dataState.collectAsState()
context.block(dataState.value)
} }
/** /**

View File

@@ -1,9 +1,7 @@
package dev.inmo.micro_utils.pagination.compose package dev.inmo.micro_utils.pagination.compose
import androidx.compose.runtime.* import androidx.compose.runtime.*
import dev.inmo.micro_utils.common.Optional import dev.inmo.micro_utils.coroutines.SpecialMutableStateFlow
import dev.inmo.micro_utils.common.dataOrThrow
import dev.inmo.micro_utils.common.optional
import dev.inmo.micro_utils.pagination.* import dev.inmo.micro_utils.pagination.*
/** /**
@@ -19,23 +17,21 @@ import dev.inmo.micro_utils.pagination.*
* @param size Number of items per page. * @param size Number of items per page.
*/ */
class PagedComponentContext<T> internal constructor( class PagedComponentContext<T> internal constructor(
preset: PaginationResult<T>? = null,
initialPage: Int, initialPage: Int,
size: Int size: Int
) { ) {
internal val iterationState: MutableState<Pair<Int, Pagination>> = mutableStateOf(0 to SimplePagination(preset?.page ?: initialPage, preset?.size ?: size)) internal val startPage = SimplePagination(initialPage, size)
internal val currentlyLoadingPageState = SpecialMutableStateFlow<Pagination?>(startPage)
internal var dataOptional: PaginationResult<T>? = preset internal val latestLoadedPage = SpecialMutableStateFlow<PaginationResult<T>?>(null)
private set
internal val dataState: MutableState<PaginationResult<T>?> = mutableStateOf(dataOptional)
/** /**
* Loads the next page of data. If the last page is reached, this function returns early. * Loads the next page of data. If the last page is reached, this function returns early.
*/ */
fun loadNext() { fun loadNext() {
iterationState.value = iterationState.value.let { when {
if (dataState.value ?.isLastPage == true) return currentlyLoadingPageState.value != null -> return
(it.first + 1) to it.second.nextPage() latestLoadedPage.value ?.isLastPage == true -> return
else -> currentlyLoadingPageState.value = (latestLoadedPage.value ?.nextPage()) ?: startPage
} }
} }
@@ -43,12 +39,10 @@ class PagedComponentContext<T> internal constructor(
* Loads the previous page of data if available. * Loads the previous page of data if available.
*/ */
fun loadPrevious() { fun loadPrevious() {
iterationState.value = iterationState.value.let { when {
if (it.second.isFirstPage) return currentlyLoadingPageState.value != null -> return
(it.first - 1) to SimplePagination( latestLoadedPage.value ?.isFirstPage == true -> return
it.second.page - 1, else -> currentlyLoadingPageState.value = (latestLoadedPage.value ?.previousPage()) ?: startPage
it.second.size
)
} }
} }
@@ -56,9 +50,7 @@ class PagedComponentContext<T> internal constructor(
* Reloads the current page, refreshing the data. * Reloads the current page, refreshing the data.
*/ */
fun reload() { fun reload() {
iterationState.value = iterationState.value.let { currentlyLoadingPageState.value = latestLoadedPage.value
it.copy(it.first + 1)
}
} }
} }
@@ -74,46 +66,26 @@ class PagedComponentContext<T> internal constructor(
*/ */
@Composable @Composable
internal fun <T> PagedComponent( internal fun <T> PagedComponent(
preload: PaginationResult<T>?,
initialPage: Int, initialPage: Int,
size: Int, size: Int,
loader: suspend PagedComponentContext<T>.(Pagination) -> PaginationResult<T>, loader: suspend PagedComponentContext<T>.(Pagination) -> PaginationResult<T>,
block: @Composable PagedComponentContext<T>.(PaginationResult<T>) -> Unit block: @Composable PagedComponentContext<T>.(PaginationResult<T>) -> Unit
) { ) {
val context = remember { PagedComponentContext(preload, initialPage, size) } val context = remember { PagedComponentContext<T>(initialPage, size) }
LaunchedEffect(context.iterationState.value) { val currentlyLoadingState = context.currentlyLoadingPageState.collectAsState()
context.dataState.value = loader(context, context.iterationState.value.second) LaunchedEffect(currentlyLoadingState.value) {
val paginationResult = loader(context, currentlyLoadingState.value ?: return@LaunchedEffect)
context.latestLoadedPage.value = paginationResult
context.currentlyLoadingPageState.value = null
} }
context.dataState.value ?.let { val pageState = context.latestLoadedPage.collectAsState()
pageState.value ?.let {
context.block(it) context.block(it)
} }
} }
/**
* Overloaded composable function for paginated components with preloaded data.
*
* @param T The type of paginated data.
* @param preload Preloaded pagination result.
* @param loader Suspended function that loads paginated data.
* @param block Composable function that renders the UI with the loaded data.
*/
@Composable
fun <T> PagedComponent(
preload: PaginationResult<T>,
loader: suspend PagedComponentContext<T>.(Pagination) -> PaginationResult<T>,
block: @Composable PagedComponentContext<T>.(PaginationResult<T>) -> Unit
) {
PagedComponent(
preload,
preload.page,
preload.size,
loader,
block
)
}
/** /**
* Overloaded composable function for paginated components with pagination info. * Overloaded composable function for paginated components with pagination info.
* *
@@ -129,7 +101,6 @@ fun <T> PagedComponent(
block: @Composable PagedComponentContext<T>.(PaginationResult<T>) -> Unit block: @Composable PagedComponentContext<T>.(PaginationResult<T>) -> Unit
) { ) {
PagedComponent( PagedComponent(
null,
pageInfo.page, pageInfo.page,
pageInfo.size, pageInfo.size,
loader, loader,
@@ -137,25 +108,6 @@ fun <T> PagedComponent(
) )
} }
/**
* Overloaded composable function for paginated components with an initial page.
*
* @param T The type of paginated data.
* @param initialPage Initial page number.
* @param size Number of items per page.
* @param loader Suspended function that loads paginated data.
* @param block Composable function that renders the UI with the loaded data.
*/
@Composable
fun <T> PagedComponent(
initialPage: Int,
size: Int,
loader: suspend PagedComponentContext<T>.(Pagination) -> PaginationResult<T>,
block: @Composable PagedComponentContext<T>.(PaginationResult<T>) -> Unit
) {
PagedComponent(null, initialPage, size, loader, block)
}
/** /**
* Overloaded composable function for paginated components with only a size parameter. * Overloaded composable function for paginated components with only a size parameter.
* *

View File

@@ -30,9 +30,7 @@ class InfinityPagedComponentTests {
} }
) { ) {
if (it == null) { if (it == null) {
if (this.iterationState.value.second != null) { assertEquals(0, this.currentlyLoadingPage.value ?.page)
assertEquals(0, (this.iterationState.value.second as? SimplePagination) ?.page)
}
} else { } else {
assertEquals(expectedList, it) assertEquals(expectedList, it)
} }