Compare commits

..

No commits in common. "5ce8ebe82ca85eeae84a7be8bdc4c6ff4e5a9ed4" and "cd7b982385c58627960f16e926de2898c24b4fa5" have entirely different histories.

8 changed files with 69 additions and 85 deletions
CHANGELOG.mdgradle.properties
gradle/wrapper
pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination
repos/common/src
commonMain/kotlin/dev/inmo/micro_utils/repos/mappers
main/kotlin/dev/inmo/micro_utils/repos/keyvalue

View File

@ -18,17 +18,6 @@
* `Ktor`:
* All previously standard functions related to work with binary data by default have been deprecated
## 0.11.14
* `Pagination`:
* `PaginationResult` got new field `objectsNumber` which by default is a times between `pagesNumber` and `size`
## 0.11.13
* `Versions`:
* `Coroutines`: `1.6.3` -> `1.6.4`
* `Compose`: `1.2.0-alpha01-dev629` -> `1.2.0-alpha01-dev731`
## 0.11.12
* `Repos`:

View File

@ -15,4 +15,4 @@ crypto_js_version=4.1.1
group=dev.inmo
version=0.12.0
android_code_version=139
android_code_version=137

View File

@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

View File

@ -1,60 +1,23 @@
package dev.inmo.micro_utils.pagination
import kotlinx.serialization.*
import kotlin.math.ceil
import kotlinx.serialization.Serializable
/**
* @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,
override val size: Int,
val pagesNumber: Int,
val results: List<T>,
val objectsNumber: Long
) : Pagination {
/**
* Amount of pages for current pagination
*/
@EncodeDefault
val pagesNumber: Int = ceil(objectsNumber / size.toFloat()).toInt()
override val size: Int
) : Pagination
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)
fun <T> emptyPaginationResult() = PaginationResult<T>(0, 0, emptyList(), 0)
/**
* @return New [PaginationResult] with [data] without checking of data sizes equality
*/
fun <I, O> PaginationResult<I>.changeResultsUnchecked(
data: List<O>
): PaginationResult<O> = PaginationResult(page, size, data, objectsNumber)
): PaginationResult<O> = PaginationResult(page, pagesNumber, data, size)
/**
* @return New [PaginationResult] with [data] <b>with</b> checking of data sizes equality
*/
@ -70,9 +33,12 @@ fun <T> List<T>.createPaginationResult(
commonObjectsNumber: Long
) = PaginationResult(
pagination.page,
pagination.size,
calculatePagesNumber(
commonObjectsNumber,
pagination.size
),
this,
commonObjectsNumber
pagination.size
)
fun <T> List<T>.createPaginationResult(
@ -80,9 +46,12 @@ fun <T> List<T>.createPaginationResult(
commonObjectsNumber: Long
) = PaginationResult(
calculatePage(firstIndex, size),
size,
calculatePagesNumber(
commonObjectsNumber,
size
),
this,
commonObjectsNumber
size
)
fun <T> Pair<Long, List<T>>.createPaginationResult(

View File

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

View File

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

View File

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