diff --git a/CHANGELOG.md b/CHANGELOG.md index 3cf67c882a4..d5c02ed3735 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,24 @@ # Changelog +## 0.7.4 + +* `Common`: + * New type `Either` +* `Serialization`: + * `TypedSerializer` + * New factory fun which accept vararg pairs of type and its serializer +* `Repos`: + * `Common` (`Android`): + * `AbstractMutableAndroidCRUDRepo` flows now will have extra buffer capacity instead of reply. It means that + android crud repo _WILL NOT_ send previous events to the + * `Exposed`: + * New parameter `AbstractExposedWriteCRUDRepo#replyCacheInFlows` + * KeyValue realization `ExposedKeyValueRepo` properties `_onNewValue` and `_onValueRemoved` now are available in + inheritors +* `Pagination`: + * `Common`: + * New types `getAllBy*` for current, next and custom paging + ## 0.7.3 * `Versions`: diff --git a/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/Either.kt b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/Either.kt new file mode 100644 index 00000000000..700580015b3 --- /dev/null +++ b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/Either.kt @@ -0,0 +1,65 @@ +package dev.inmo.micro_utils.common + +/** + * Realization of this interface will contains at least one not null - [t1] or [t2] + * + * @see EitherFirst + * @see EitherSecond + * @see Either.Companion.first + * @see Either.Companion.second + * @see Either.onFirst + * @see Either.onSecond + */ +sealed interface Either { + val t1: T1? + val t2: T2? + + companion object +} + +/** + * This type [Either] will always have not nullable [t1] + */ +data class EitherFirst( + override val t1: T1 +) : Either { + override val t2: T2? + get() = null +} + +/** + * This type [Either] will always have not nullable [t2] + */ +data class EitherSecond( + override val t2: T2 +) : Either { + override val t1: T1? + get() = null +} + +/** + * @return New instance of [EitherFirst] + */ +inline fun Either.Companion.first(t1: T1): Either = EitherFirst(t1) +/** + * @return New instance of [EitherSecond] + */ +inline fun Either.Companion.second(t1: T1): Either = EitherFirst(t1) + +/** + * Will call [block] in case when [Either.t1] of [this] is not null + */ +inline fun > E.onFirst(crossinline block: (T1) -> Unit): E { + val t1 = t1 + t1 ?.let(block) + return this +} + +/** + * Will call [block] in case when [Either.t2] of [this] is not null + */ +inline fun > E.onSecond(crossinline block: (T2) -> Unit): E { + val t2 = t2 + t2 ?.let(block) + return this +} diff --git a/gradle.properties b/gradle.properties index 26dd44c7d83..20f356055d4 100644 --- a/gradle.properties +++ b/gradle.properties @@ -45,5 +45,5 @@ dokka_version=1.5.31 # Project data group=dev.inmo -version=0.7.3 -android_code_version=77 +version=0.7.4 +android_code_version=78 diff --git a/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/utils/GetAll.kt b/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/utils/GetAll.kt index 5ad674cb746..8012a89e47d 100644 --- a/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/utils/GetAll.kt +++ b/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/utils/GetAll.kt @@ -16,6 +16,16 @@ suspend fun getAll( return results.toList() } +suspend fun R.getAllBy( + initialPagination: Pagination = FirstPagePagination(), + paginationMapper: R.(PaginationResult) -> Pagination?, + block: suspend R.(Pagination) -> PaginationResult +): List = getAll( + initialPagination, + { paginationMapper(it) }, + { block(it) } +) + suspend fun getAllWithNextPaging( initialPagination: Pagination = FirstPagePagination(), block: suspend (Pagination) -> PaginationResult @@ -25,6 +35,14 @@ suspend fun getAllWithNextPaging( block ) +suspend fun R.getAllByWithNextPaging( + initialPagination: Pagination = FirstPagePagination(), + block: suspend R.(Pagination) -> PaginationResult +): List = getAllWithNextPaging( + initialPagination, + { block(it) } +) + suspend fun getAllWithCurrentPaging( initialPagination: Pagination = FirstPagePagination(), block: suspend (Pagination) -> PaginationResult @@ -33,3 +51,11 @@ suspend fun getAllWithCurrentPaging( { it.currentPageIfNotEmpty() }, block ) + +suspend fun R.getAllByWithCurrentPaging( + initialPagination: Pagination = FirstPagePagination(), + block: suspend R.(Pagination) -> PaginationResult +): List = getAllWithCurrentPaging( + initialPagination, + { block(it) } +) diff --git a/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/crud/AbstractMutableAndroidCRUDRepo.kt b/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/crud/AbstractMutableAndroidCRUDRepo.kt index fc016c19a5c..29103d3f2ab 100644 --- a/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/crud/AbstractMutableAndroidCRUDRepo.kt +++ b/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/crud/AbstractMutableAndroidCRUDRepo.kt @@ -6,13 +6,15 @@ import dev.inmo.micro_utils.repos.* import kotlinx.coroutines.flow.* abstract class AbstractMutableAndroidCRUDRepo( - helper: StandardSQLHelper + helper: StandardSQLHelper, + replyInFlows: Int = 0, + extraBufferCapacityInFlows: Int = 64 ) : WriteStandardCRUDRepo, AbstractAndroidCRUDRepo(helper), StandardCRUDRepo { - protected val newObjectsChannel = MutableSharedFlow(64) - protected val updateObjectsChannel = MutableSharedFlow(64) - protected val deleteObjectsIdsChannel = MutableSharedFlow(64) + protected val newObjectsChannel = MutableSharedFlow(replyInFlows, extraBufferCapacityInFlows) + protected val updateObjectsChannel = MutableSharedFlow(replyInFlows, extraBufferCapacityInFlows) + protected val deleteObjectsIdsChannel = MutableSharedFlow(replyInFlows, extraBufferCapacityInFlows) override val newObjectsFlow: Flow = newObjectsChannel.asSharedFlow() override val updatedObjectsFlow: Flow = updateObjectsChannel.asSharedFlow() override val deletedObjectsIdsFlow: Flow = deleteObjectsIdsChannel.asSharedFlow() @@ -102,4 +104,4 @@ abstract class AbstractMutableAndroidCRUDRepo( flowsChannelsSize: Int = 0, - tableName: String = "" + tableName: String = "", + replyCacheInFlows: Int = 0 ) : AbstractExposedReadCRUDRepo(tableName), ExposedCRUDRepo, WriteStandardCRUDRepo { - protected val newObjectsChannel = MutableSharedFlow(flowsChannelsSize) - protected val updateObjectsChannel = MutableSharedFlow(flowsChannelsSize) - protected val deleteObjectsIdsChannel = MutableSharedFlow(flowsChannelsSize) + protected val newObjectsChannel = MutableSharedFlow(replyCacheInFlows, flowsChannelsSize) + protected val updateObjectsChannel = MutableSharedFlow(replyCacheInFlows, flowsChannelsSize) + protected val deleteObjectsIdsChannel = MutableSharedFlow(replyCacheInFlows, flowsChannelsSize) override val newObjectsFlow: Flow = newObjectsChannel.asSharedFlow() override val updatedObjectsFlow: Flow = updateObjectsChannel.asSharedFlow() diff --git a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/keyvalue/ExposedKeyValueRepo.kt b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/keyvalue/ExposedKeyValueRepo.kt index 74a2bef1c9d..ee50149845a 100644 --- a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/keyvalue/ExposedKeyValueRepo.kt +++ b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/keyvalue/ExposedKeyValueRepo.kt @@ -19,8 +19,8 @@ open class ExposedKeyValueRepo( valueColumnAllocator, tableName ) { - private val _onNewValue = MutableSharedFlow>() - private val _onValueRemoved = MutableSharedFlow() + protected val _onNewValue = MutableSharedFlow>() + protected val _onValueRemoved = MutableSharedFlow() override val onNewValue: Flow> = _onNewValue.asSharedFlow() override val onValueRemoved: Flow = _onValueRemoved.asSharedFlow() diff --git a/serialization/typed_serializer/src/commonMain/kotlin/dev/inmo/micro_utils/serialization/typed_serializer/TypedSerializer.kt b/serialization/typed_serializer/src/commonMain/kotlin/dev/inmo/micro_utils/serialization/typed_serializer/TypedSerializer.kt index 5e2632433cd..804a0bc637e 100644 --- a/serialization/typed_serializer/src/commonMain/kotlin/dev/inmo/micro_utils/serialization/typed_serializer/TypedSerializer.kt +++ b/serialization/typed_serializer/src/commonMain/kotlin/dev/inmo/micro_utils/serialization/typed_serializer/TypedSerializer.kt @@ -86,3 +86,7 @@ operator fun TypedSerializer.minusAssign(kClass: KClass) { inline fun TypedSerializer( presetSerializers: Map> = emptyMap() ) = TypedSerializer(T::class, presetSerializers) + +inline fun TypedSerializer( + vararg presetSerializers: Pair> +) = TypedSerializer(presetSerializers.toMap())