Merge pull request #178 from InsanusMokrassar/0.11.14

0.11.14
This commit is contained in:
InsanusMokrassar 2022-08-04 13:48:18 +06:00 committed by GitHub
commit a7a88b29b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 79 additions and 69 deletions

View File

@ -1,5 +1,10 @@
# Changelog
## 0.11.14
* `Pagination`:
* `PaginationResult` got new field `objectsNumber` which by default is a times between `pagesNumber` and `size`
## 0.11.13
* `Versions`:

View File

@ -14,5 +14,5 @@ crypto_js_version=4.1.1
# Project data
group=dev.inmo
version=0.11.13
android_code_version=137
version=0.11.14
android_code_version=138

View File

@ -1,23 +1,60 @@
package dev.inmo.micro_utils.pagination
import kotlinx.serialization.Serializable
import kotlinx.serialization.*
import kotlin.math.ceil
/**
* @param page Current page number
* @param size Current page size. It can be greater than size of [results]
* @param results Result objects
* @param objectsNumber Count of all objects across all pages
*/
@Serializable
data class PaginationResult<T>(
override val page: Int,
val pagesNumber: Int,
override val size: Int,
val results: List<T>,
override val size: Int
) : Pagination
val objectsNumber: Long
) : Pagination {
/**
* Amount of pages for current pagination
*/
@EncodeDefault
val pagesNumber: Int = ceil(objectsNumber / size.toFloat()).toInt()
fun <T> emptyPaginationResult() = PaginationResult<T>(0, 0, emptyList(), 0)
constructor(
page: Int,
results: List<T>,
pagesNumber: Int,
size: Int
) : this(
page,
size,
results,
(pagesNumber * size).toLong()
)
@Deprecated("Replace with The other order of incoming parameters or objectsCount parameter")
constructor(
page: Int,
pagesNumber: Int,
results: List<T>,
size: Int
) : this(
page,
results,
pagesNumber,
size
)
}
fun <T> emptyPaginationResult() = PaginationResult<T>(0, 0, emptyList(), 0L)
/**
* @return New [PaginationResult] with [data] without checking of data sizes equality
*/
fun <I, O> PaginationResult<I>.changeResultsUnchecked(
data: List<O>
): PaginationResult<O> = PaginationResult(page, pagesNumber, data, size)
): PaginationResult<O> = PaginationResult(page, size, data, objectsNumber)
/**
* @return New [PaginationResult] with [data] <b>with</b> checking of data sizes equality
*/
@ -33,12 +70,9 @@ fun <T> List<T>.createPaginationResult(
commonObjectsNumber: Long
) = PaginationResult(
pagination.page,
calculatePagesNumber(
commonObjectsNumber,
pagination.size
),
pagination.size,
this,
pagination.size
commonObjectsNumber
)
fun <T> List<T>.createPaginationResult(
@ -46,12 +80,9 @@ fun <T> List<T>.createPaginationResult(
commonObjectsNumber: Long
) = PaginationResult(
calculatePage(firstIndex, size),
calculatePagesNumber(
commonObjectsNumber,
size
),
size,
this,
size
commonObjectsNumber
)
fun <T> Pair<Long, List<T>>.createPaginationResult(

View File

@ -26,6 +26,10 @@ inline fun Pagination.nextPage() =
size
)
/**
* @param page Current page number
* @param size Current page size
*/
@Serializable
data class SimplePagination(
override val page: Int,

View File

@ -1,7 +1,6 @@
package dev.inmo.micro_utils.repos.mappers
import dev.inmo.micro_utils.pagination.Pagination
import dev.inmo.micro_utils.pagination.PaginationResult
import dev.inmo.micro_utils.pagination.*
import dev.inmo.micro_utils.repos.*
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
@ -23,11 +22,8 @@ open class MapperReadKeyValueRepo<FromKey, FromValue, ToKey, ToValue>(
pagination,
reversed
).let {
PaginationResult(
it.page,
it.pagesNumber,
it.results.map { it.toInnerValue() },
it.size
it.changeResultsUnchecked(
it.results.map { it.toInnerValue() }
)
}
@ -38,11 +34,8 @@ open class MapperReadKeyValueRepo<FromKey, FromValue, ToKey, ToValue>(
pagination,
reversed
).let {
PaginationResult(
it.page,
it.pagesNumber,
it.results.map { it.toInnerKey() },
it.size
it.changeResultsUnchecked(
it.results.map { it.toInnerKey() }
)
}
@ -55,11 +48,8 @@ open class MapperReadKeyValueRepo<FromKey, FromValue, ToKey, ToValue>(
pagination,
reversed
).let {
PaginationResult(
it.page,
it.pagesNumber,
it.results.map { it.toInnerKey() },
it.size
it.changeResultsUnchecked(
it.results.map { it.toInnerKey() }
)
}

View File

@ -1,7 +1,6 @@
package dev.inmo.micro_utils.repos.mappers
import dev.inmo.micro_utils.pagination.Pagination
import dev.inmo.micro_utils.pagination.PaginationResult
import dev.inmo.micro_utils.pagination.*
import dev.inmo.micro_utils.repos.*
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
@ -21,11 +20,8 @@ open class MapperReadKeyValuesRepo<FromKey, FromValue, ToKey, ToValue>(
pagination,
reversed
).let {
PaginationResult(
it.page,
it.pagesNumber,
it.results.map { it.toInnerValue() },
it.size
it.changeResultsUnchecked(
it.results.map { it.toInnerValue() }
)
}
@ -36,11 +32,8 @@ open class MapperReadKeyValuesRepo<FromKey, FromValue, ToKey, ToValue>(
pagination,
reversed
).let {
PaginationResult(
it.page,
it.pagesNumber,
it.results.map { it.toInnerKey() },
it.size
it.changeResultsUnchecked(
it.results.map { it.toInnerKey() }
)
}
@ -53,11 +46,8 @@ open class MapperReadKeyValuesRepo<FromKey, FromValue, ToKey, ToValue>(
pagination,
reversed
).let {
PaginationResult(
it.page,
it.pagesNumber,
it.results.map { it.toInnerKey() },
it.size
it.changeResultsUnchecked(
it.results.map { it.toInnerKey() }
)
}

View File

@ -3,8 +3,7 @@ package dev.inmo.micro_utils.repos.keyvalue
import android.content.Context
import android.content.SharedPreferences
import androidx.core.content.edit
import dev.inmo.micro_utils.pagination.Pagination
import dev.inmo.micro_utils.pagination.PaginationResult
import dev.inmo.micro_utils.pagination.*
import dev.inmo.micro_utils.pagination.utils.paginate
import dev.inmo.micro_utils.pagination.utils.reverse
import dev.inmo.micro_utils.repos.KeyValueRepo
@ -72,14 +71,11 @@ class KeyValueStore<T : Any> internal constructor (
return sharedPreferences.all.values.paginate(
resultPagination
).let {
PaginationResult(
it.page,
it.pagesNumber,
it.changeResultsUnchecked(
it.results.map {
@Suppress("UNCHECKED_CAST")
it as T
}.let { if (reversed) it.reversed() else it },
it.size
}.let { if (reversed) it.reversed() else it }
)
}
}
@ -89,11 +85,8 @@ class KeyValueStore<T : Any> internal constructor (
return sharedPreferences.all.keys.paginate(
resultPagination
).let {
PaginationResult(
it.page,
it.pagesNumber,
it.results.let { if (reversed) it.reversed() else it },
it.size
it.changeResultsUnchecked(
it.results.let { if (reversed) it.reversed() else it }
)
}
}
@ -103,11 +96,8 @@ class KeyValueStore<T : Any> internal constructor (
return sharedPreferences.all.mapNotNull { (k, value) -> if (value == v) k else null }.paginate(
resultPagination
).let {
PaginationResult(
it.page,
it.pagesNumber,
it.results.let { if (reversed) it.reversed() else it },
it.size
it.changeResultsUnchecked(
it.results.let { if (reversed) it.reversed() else it }
)
}
}