mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2025-09-17 14:29:24 +00:00
Compare commits
71 Commits
Author | SHA1 | Date | |
---|---|---|---|
3347a6d189 | |||
47b3b42949 | |||
e985631621 | |||
e15034bfa4 | |||
e5dbcd25dd | |||
3714c02c12 | |||
fa636b4146 | |||
3d825aebc3 | |||
629884a396 | |||
e7a0fa4e8f | |||
1a41f37a9d | |||
07a65e0bb5 | |||
615f7f99c3 | |||
3de5558ed4 | |||
6bbe3a271f | |||
8bee29f683 | |||
c40f0fdcb9 | |||
30d1453a12 | |||
75fa88b00d | |||
0f817ad212 | |||
d7b46ae0d4 | |||
b6be14ecca | |||
33dbfc6f69 | |||
fcacdcd544 | |||
dd2fc5a86f | |||
0f8a6f6bde | |||
1efd94181d | |||
71ff0232aa | |||
63921cd984 | |||
051e03bed3 | |||
a051394f4f | |||
b872babe45 | |||
5a9cabc4bd | |||
3ba630684a | |||
498cd12f94 | |||
062848f2e4 | |||
d4b4547718 | |||
22cd440dd7 | |||
6fc64526d4 | |||
08075dfafe | |||
efcb25622e | |||
5ebf29d1fb | |||
b7d0ce3c97 | |||
e20929aec4 | |||
f74167cc65 | |||
c55cd5342e | |||
237d89d611 | |||
aff9b52f1c | |||
d8cc6ed06e | |||
dffafa54c3 | |||
776a3af497 | |||
5b3d9e8d64 | |||
90b7d74a0c | |||
5b596c76e0 | |||
c59e601e2e | |||
5ce71ee6f6 | |||
97dadf517a | |||
a739e874bf | |||
c73cb14615 | |||
d9464f7b90 | |||
aeee41680e | |||
c914f8c44a | |||
abfda3627c | |||
5b5bfa02db | |||
0fdb072385 | |||
7b4c9d59b0 | |||
bd5923716f | |||
64cc42a23b | |||
325f178763 | |||
fc8d0e52ef | |||
e58348907e |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -10,5 +10,6 @@ build/
|
|||||||
out/
|
out/
|
||||||
|
|
||||||
secret.gradle
|
secret.gradle
|
||||||
|
local.properties
|
||||||
|
|
||||||
publishing.sh
|
publishing.sh
|
||||||
|
20
.travis.yml
20
.travis.yml
@@ -1,13 +1,27 @@
|
|||||||
language: java
|
language: android
|
||||||
install: true
|
install: true
|
||||||
|
|
||||||
os: linux
|
os: linux
|
||||||
dist: trusty
|
dist: trusty
|
||||||
jdk: oraclejdk8
|
jdk: oraclejdk8
|
||||||
|
|
||||||
|
android:
|
||||||
|
components:
|
||||||
|
- tools
|
||||||
|
- platform-tools
|
||||||
|
- build-tools-30.0.2
|
||||||
|
- android-30
|
||||||
|
- add-on
|
||||||
|
- extra
|
||||||
|
|
||||||
|
before_script:
|
||||||
|
- yes | /usr/local/android-sdk/tools/bin/sdkmanager "build-tools;30.0.2"
|
||||||
|
- yes | /usr/local/android-sdk/tools/bin/sdkmanager "platforms;android-30"
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
include:
|
include:
|
||||||
- stage: build
|
- stage: build
|
||||||
script: ./gradlew build -s -x jvmTest -x jsIrTest -x jsIrBrowserTest -x jsIrNodeTest -x jsLegacyTest -x jsLegacyBrowserTest -x jsLegacyNodeTest
|
script: ./gradlew build -s -x jvmTest -x jsIrTest -x jsIrBrowserTest -x jsIrNodeTest -x jsLegacyTest -x jsLegacyBrowserTest -x jsLegacyNodeTest
|
||||||
- state: test
|
# Tests are temporarily disabled on public travis due to the problems of launching
|
||||||
script: ./gradlew allTests
|
# - state: test
|
||||||
|
# script: ./gradlew allTests
|
||||||
|
145
CHANGELOG.md
145
CHANGELOG.md
@@ -1,5 +1,148 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## 0.4.5
|
||||||
|
|
||||||
|
* `Android`
|
||||||
|
* `Alerts`
|
||||||
|
* `Common`
|
||||||
|
* Project has been created
|
||||||
|
* `RecyclerView`
|
||||||
|
* Project has been created
|
||||||
|
* `Common`
|
||||||
|
* Annotation `PreviewFeature` has been added
|
||||||
|
* `Android`
|
||||||
|
* Added tools to work with visibility in more comfortable way
|
||||||
|
* Added tools to work with disabled/enabled state in more comfortable way
|
||||||
|
* Added tools to work with expanded/collapsed state in more comfortable way (in preview mode)
|
||||||
|
|
||||||
|
## 0.4.4
|
||||||
|
|
||||||
|
* `Versions`:
|
||||||
|
* `Klock`: `1.12.1` -> `2.0.0`
|
||||||
|
* `Commons`:
|
||||||
|
* Update left items functionality to include work with `GridLayoutManager`
|
||||||
|
* `Repos`:
|
||||||
|
* Add interface `VersionsRepo`
|
||||||
|
* Add default realization of `VersionsRepo` named `StandardVersionsRepo` which use `StandardVersionsRepoProxy`
|
||||||
|
to get access to some end-store
|
||||||
|
* Add default realization of `StandardVersionsRepoProxy` based on `KeyValue` repos
|
||||||
|
* Add realizations of `StandardVersionsRepoProxy` for exposed and android (`SQL` and `SharedPreferences`)
|
||||||
|
* `Commons`:
|
||||||
|
* In Android fully reworked transactions functions
|
||||||
|
* Now `DatabaseCoroutineContext` is a shortcut for `Dispatchers.IO`
|
||||||
|
|
||||||
|
## 0.4.3
|
||||||
|
|
||||||
|
* `Versions`:
|
||||||
|
* `Kotlin`: `1.4.10` -> `1.4.20`
|
||||||
|
* `Common`:
|
||||||
|
* Two new extensions for Android:
|
||||||
|
* `Resources#getSp`
|
||||||
|
* `Resources#getDp`
|
||||||
|
|
||||||
|
## 0.4.2
|
||||||
|
|
||||||
|
* `Repos`:
|
||||||
|
* Add `WriteOneToManyKeyValueRepo#set` function and extensions
|
||||||
|
|
||||||
|
## 0.4.1
|
||||||
|
|
||||||
|
* `Repos`:
|
||||||
|
* Fixed error in `ExposedKeyValueRepo` related to negative size of shared flow
|
||||||
|
* Fixed error in `ExposedKeyValueRepo` related to avoiding of table initiation
|
||||||
|
|
||||||
|
## 0.4.0
|
||||||
|
|
||||||
|
* `Repos`:
|
||||||
|
* `ReadOneToManyKeyValueRepo` got `keys` method with value parameter
|
||||||
|
* All implementations inside of this library has been updated
|
||||||
|
* `ReadStandardKeyValueRepo` got `keys` method with value parameter
|
||||||
|
* All implementations inside of this library has been updated
|
||||||
|
* New extensions `withMapper`
|
||||||
|
|
||||||
|
## 0.3.3
|
||||||
|
|
||||||
|
* `Coroutines`:
|
||||||
|
* New extension `Flow<T>#plus`
|
||||||
|
|
||||||
|
## 0.3.2
|
||||||
|
|
||||||
|
* `Versions`:
|
||||||
|
* `Coroutines`: `1.4.1` -> `1.4.2`
|
||||||
|
* `Repos`:
|
||||||
|
* `Common`:
|
||||||
|
* New inline function `mapper` for simplier creating of `MapperRepo` objects
|
||||||
|
* Extensions `withMapper` for keyvalue repos and onetomany repos
|
||||||
|
|
||||||
|
## 0.3.1
|
||||||
|
|
||||||
|
**ANDROID PACKAGES**
|
||||||
|
|
||||||
|
* `Android`:
|
||||||
|
* `RecyclerView`:
|
||||||
|
* Library has been created
|
||||||
|
* `Common`
|
||||||
|
* Now available package `dev.inmo:micro_utils.common-android`
|
||||||
|
* `Coroutines`
|
||||||
|
* Now available package `dev.inmo:micro_utils.coroutines-android`
|
||||||
|
* `Ktor`
|
||||||
|
* `Common`
|
||||||
|
* Now available package `dev.inmo:micro_utils.ktor.common-android`
|
||||||
|
* `Client`
|
||||||
|
* Now available package `dev.inmo:micro_utils.ktor.client-android`
|
||||||
|
* `MimeTypes`
|
||||||
|
* Now available package `dev.inmo:micro_utils.mime_types-android`
|
||||||
|
* `Pagination`
|
||||||
|
* `Common`
|
||||||
|
* Now available package `dev.inmo:micro_utils.pagination.common-android`
|
||||||
|
* `Ktor`
|
||||||
|
* `Common`
|
||||||
|
* Now available package `dev.inmo:micro_utils.pagination.ktor.common-android`
|
||||||
|
* `Repos`
|
||||||
|
* `Common`
|
||||||
|
* Now available package `dev.inmo:micro_utils.repos.common-android`
|
||||||
|
* Now it is possible to use default realizations of repos abstractions natively on android
|
||||||
|
* `Inmemory`
|
||||||
|
* Now available package `dev.inmo:micro_utils.repos.inmemory-android`
|
||||||
|
* `Ktor`
|
||||||
|
* `Common`
|
||||||
|
* Now available package `dev.inmo:micro_utils.repos.ktor.common-android`
|
||||||
|
* `Common`
|
||||||
|
* Now available package `dev.inmo:micro_utils.repos.ktor.client-android`
|
||||||
|
|
||||||
|
## 0.3.0
|
||||||
|
|
||||||
|
All deprecations has been removed
|
||||||
|
|
||||||
|
* `Repos`:
|
||||||
|
* `Common`:
|
||||||
|
* `KeyValue` and `OneToMany` repos lost their deprecated methods
|
||||||
|
* `OneToMany` write repos got additional extensions for mutation of repo
|
||||||
|
* `KeyValue` write repos got additional extensions for mutation of repo
|
||||||
|
* New interface `MapperRepo` and new classes which are using this:
|
||||||
|
* `KeyValue`
|
||||||
|
* `MapperReadStandardKeyValueRepo`
|
||||||
|
* `MapperWriteStandardKeyValueRepo`
|
||||||
|
* `MapperStandardKeyValueRepo`
|
||||||
|
* `OneToMany`
|
||||||
|
* `MapperReadOneToManyKeyValueRepo`
|
||||||
|
* `MapperWriteOneToManyKeyValueRepo`
|
||||||
|
* `MapperOneToManyKeyValueRepo`
|
||||||
|
* `Exposed`:
|
||||||
|
* CRUD realizations replaced their channels to shared flows
|
||||||
|
|
||||||
|
## 0.2.7
|
||||||
|
|
||||||
|
* `Versions`:
|
||||||
|
* `Coroutines`: `1.4.0` -> `1.4.1`
|
||||||
|
* `Repos`:
|
||||||
|
* `WriteStandardKeyValueRepo` got new methods `set` and `unset` with collections
|
||||||
|
* All standard realizations of repos got collections methods realizations
|
||||||
|
* All old usages of `BroadcastFlow` and `BroadcastChannel` has been replaced with `MutableSharedFlow`
|
||||||
|
* `Ktor`:
|
||||||
|
* `Server`:
|
||||||
|
* Fixed incorrect answer for `keyvalue`
|
||||||
|
|
||||||
## 0.2.6
|
## 0.2.6
|
||||||
|
|
||||||
* `Pagination`
|
* `Pagination`
|
||||||
@@ -132,4 +275,4 @@
|
|||||||
|
|
||||||
## 0.1.0
|
## 0.1.0
|
||||||
|
|
||||||
Inited :)
|
Inited :)
|
||||||
|
17
android/alerts/common/build.gradle
Normal file
17
android/alerts/common/build.gradle
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
plugins {
|
||||||
|
id "org.jetbrains.kotlin.multiplatform"
|
||||||
|
id "org.jetbrains.kotlin.plugin.serialization"
|
||||||
|
id "com.android.library"
|
||||||
|
}
|
||||||
|
|
||||||
|
apply from: "$mppAndroidProjectPresetPath"
|
||||||
|
|
||||||
|
kotlin {
|
||||||
|
sourceSets {
|
||||||
|
androidMain {
|
||||||
|
dependencies {
|
||||||
|
api "androidx.appcompat:appcompat-resources:$appcompat_version"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
1
android/alerts/common/src/main/AndroidManifest.xml
Normal file
1
android/alerts/common/src/main/AndroidManifest.xml
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<manifest package="dev.inmo.micro_utils.android.alerts.common"/>
|
@@ -0,0 +1,55 @@
|
|||||||
|
@file:Suppress("NOTHING_TO_INLINE", "unused")
|
||||||
|
|
||||||
|
package dev.inmo.micro_utils.android.alerts.common
|
||||||
|
|
||||||
|
import android.app.AlertDialog
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.DialogInterface
|
||||||
|
|
||||||
|
typealias AlertDialogCallback = (DialogInterface) -> Unit
|
||||||
|
|
||||||
|
inline fun Context.createAlertDialogTemplate(
|
||||||
|
title: String? = null,
|
||||||
|
positivePair: Pair<String, AlertDialogCallback?>? = null,
|
||||||
|
neutralPair: Pair<String, AlertDialogCallback?>? = null,
|
||||||
|
negativePair: Pair<String, AlertDialogCallback?>? = null
|
||||||
|
): AlertDialog.Builder {
|
||||||
|
val builder = AlertDialog.Builder(this)
|
||||||
|
|
||||||
|
title ?.let {
|
||||||
|
builder.setTitle(title)
|
||||||
|
}
|
||||||
|
|
||||||
|
positivePair ?. let {
|
||||||
|
builder.setPositiveButton(it.first) { di, _ -> it.second ?. invoke(di) }
|
||||||
|
}
|
||||||
|
negativePair ?. let {
|
||||||
|
builder.setNegativeButton(it.first) { di, _ -> it.second ?. invoke(di) }
|
||||||
|
}
|
||||||
|
neutralPair ?. let {
|
||||||
|
builder.setNeutralButton(it.first) { di, _ -> it.second ?. invoke(di) }
|
||||||
|
}
|
||||||
|
|
||||||
|
return builder
|
||||||
|
}
|
||||||
|
|
||||||
|
inline fun Context.createAlertDialogTemplateWithResources(
|
||||||
|
title: Int? = null,
|
||||||
|
positivePair: Pair<Int, AlertDialogCallback?>? = null,
|
||||||
|
neutralPair: Pair<Int, AlertDialogCallback?>? = null,
|
||||||
|
negativePair: Pair<Int, AlertDialogCallback?>? = null
|
||||||
|
): AlertDialog.Builder = createAlertDialogTemplate(
|
||||||
|
title ?.let { getString(it) },
|
||||||
|
positivePair ?.let { getString(it.first) to it.second },
|
||||||
|
neutralPair ?.let { getString(it.first) to it.second },
|
||||||
|
negativePair ?.let { getString(it.first) to it.second }
|
||||||
|
)
|
||||||
|
|
||||||
|
inline fun AlertDialog.setDismissChecker(noinline checker: () -> Boolean) : AlertDialog {
|
||||||
|
setOnDismissListener {
|
||||||
|
if (!checker()) {
|
||||||
|
show()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return this
|
||||||
|
}
|
@@ -0,0 +1,38 @@
|
|||||||
|
@file:Suppress("NOTHING_TO_INLINE", "unused")
|
||||||
|
|
||||||
|
package dev.inmo.micro_utils.android.alerts.common
|
||||||
|
|
||||||
|
import android.app.AlertDialog
|
||||||
|
import android.content.Context
|
||||||
|
import android.view.View
|
||||||
|
|
||||||
|
inline fun <T: View> Context.createCustomViewAlertDialog(
|
||||||
|
title: String? = null,
|
||||||
|
positivePair: Pair<String, AlertDialogCallback?>? = null,
|
||||||
|
neutralPair: Pair<String, AlertDialogCallback?>? = null,
|
||||||
|
negativePair: Pair<String, AlertDialogCallback?>? = null,
|
||||||
|
show: Boolean = true,
|
||||||
|
viewCreator: (Context) -> T
|
||||||
|
): AlertDialog = createAlertDialogTemplate(
|
||||||
|
title, positivePair, neutralPair, negativePair
|
||||||
|
).apply {
|
||||||
|
setView(viewCreator(this@createCustomViewAlertDialog))
|
||||||
|
}.create().apply {
|
||||||
|
if (show) show()
|
||||||
|
}
|
||||||
|
|
||||||
|
inline fun <T: View> Context.createCustomViewAlertDialogWithResources(
|
||||||
|
title: Int? = null,
|
||||||
|
positivePair: Pair<Int, AlertDialogCallback?>? = null,
|
||||||
|
neutralPair: Pair<Int, AlertDialogCallback?>? = null,
|
||||||
|
negativePair: Pair<Int, AlertDialogCallback?>? = null,
|
||||||
|
show: Boolean = true,
|
||||||
|
viewCreator: (Context) -> T
|
||||||
|
): AlertDialog = createCustomViewAlertDialog(
|
||||||
|
title ?.let { getString(it) },
|
||||||
|
positivePair ?.let { getString(it.first) to it.second },
|
||||||
|
neutralPair ?.let { getString(it.first) to it.second },
|
||||||
|
negativePair ?.let { getString(it.first) to it.second },
|
||||||
|
show,
|
||||||
|
viewCreator
|
||||||
|
)
|
@@ -0,0 +1,45 @@
|
|||||||
|
@file:Suppress("NOTHING_TO_INLINE", "unused")
|
||||||
|
|
||||||
|
package dev.inmo.micro_utils.android.alerts.common
|
||||||
|
|
||||||
|
import android.app.AlertDialog
|
||||||
|
import android.content.Context
|
||||||
|
import androidx.annotation.StringRes
|
||||||
|
|
||||||
|
inline fun Context.createSimpleTextAlertDialog(
|
||||||
|
text: String,
|
||||||
|
title: String? = null,
|
||||||
|
positivePair: Pair<String, AlertDialogCallback?>? = null,
|
||||||
|
neutralPair: Pair<String, AlertDialogCallback?>? = null,
|
||||||
|
negativePair: Pair<String, AlertDialogCallback?>? = null,
|
||||||
|
show: Boolean = true
|
||||||
|
): AlertDialog = createAlertDialogTemplate(
|
||||||
|
title,
|
||||||
|
positivePair,
|
||||||
|
neutralPair,
|
||||||
|
negativePair
|
||||||
|
).apply {
|
||||||
|
setMessage(text)
|
||||||
|
}.create().apply {
|
||||||
|
if (show) {
|
||||||
|
show()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inline fun Context.createSimpleTextAlertDialog(
|
||||||
|
@StringRes
|
||||||
|
text: Int,
|
||||||
|
@StringRes
|
||||||
|
title: Int? = null,
|
||||||
|
positivePair: Pair<Int, AlertDialogCallback?>? = null,
|
||||||
|
neutralPair: Pair<Int, AlertDialogCallback?>? = null,
|
||||||
|
negativePair: Pair<Int, AlertDialogCallback?>? = null,
|
||||||
|
show: Boolean = true
|
||||||
|
): AlertDialog = createSimpleTextAlertDialog(
|
||||||
|
getString(text),
|
||||||
|
title ?.let { getString(it) },
|
||||||
|
positivePair ?.let { getString(it.first) to it.second },
|
||||||
|
neutralPair ?.let { getString(it.first) to it.second },
|
||||||
|
negativePair ?.let { getString(it.first) to it.second },
|
||||||
|
show
|
||||||
|
)
|
18
android/alerts/recyclerview/build.gradle
Normal file
18
android/alerts/recyclerview/build.gradle
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
plugins {
|
||||||
|
id "org.jetbrains.kotlin.multiplatform"
|
||||||
|
id "org.jetbrains.kotlin.plugin.serialization"
|
||||||
|
id "com.android.library"
|
||||||
|
}
|
||||||
|
|
||||||
|
apply from: "$mppAndroidProjectPresetPath"
|
||||||
|
|
||||||
|
kotlin {
|
||||||
|
sourceSets {
|
||||||
|
commonMain {
|
||||||
|
dependencies {
|
||||||
|
api internalProject("micro_utils.android.alerts.common")
|
||||||
|
api internalProject("micro_utils.android.recyclerview")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
1
android/alerts/recyclerview/src/main/AndroidManifest.xml
Normal file
1
android/alerts/recyclerview/src/main/AndroidManifest.xml
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<manifest package="dev.inmo.micro_utils.android.alerts.recyclerview"/>
|
@@ -0,0 +1,65 @@
|
|||||||
|
@file:Suppress("unused")
|
||||||
|
|
||||||
|
package dev.inmo.micro_utils.android.alerts.recyclerview
|
||||||
|
|
||||||
|
import android.app.AlertDialog
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.DialogInterface
|
||||||
|
import android.view.ViewGroup
|
||||||
|
import android.widget.TextView
|
||||||
|
import dev.inmo.micro_utils.android.alerts.common.AlertDialogCallback
|
||||||
|
import dev.inmo.micro_utils.android.recyclerview.*
|
||||||
|
|
||||||
|
data class AlertAction(
|
||||||
|
val title: String,
|
||||||
|
val callback: (DialogInterface) -> Unit
|
||||||
|
)
|
||||||
|
|
||||||
|
private class ActionViewHolder(
|
||||||
|
container: ViewGroup, dialogInterfaceGetter: () -> DialogInterface
|
||||||
|
) : AbstractStandardViewHolder<AlertAction>(container, android.R.layout.simple_list_item_1) {
|
||||||
|
private lateinit var action: AlertAction
|
||||||
|
private val textView: TextView
|
||||||
|
get() = itemView.findViewById(android.R.id.text1)
|
||||||
|
|
||||||
|
init {
|
||||||
|
itemView.setOnClickListener {
|
||||||
|
action.callback(dialogInterfaceGetter())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onBind(item: AlertAction) {
|
||||||
|
action = item
|
||||||
|
textView.text = item.title
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private class ActionsRecyclerViewAdapter(
|
||||||
|
data: List<AlertAction>,
|
||||||
|
private val dialogInterfaceGetter: () -> DialogInterface
|
||||||
|
) : RecyclerViewAdapter<AlertAction>(data) {
|
||||||
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AbstractViewHolder<AlertAction> = ActionViewHolder(
|
||||||
|
parent, dialogInterfaceGetter
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Context.createActionsAlertDialog(
|
||||||
|
actions: List<AlertAction>,
|
||||||
|
title: Int? = null,
|
||||||
|
positivePair: Pair<Int, AlertDialogCallback?>? = null,
|
||||||
|
neutralPair: Pair<Int, AlertDialogCallback?>? = null,
|
||||||
|
negativePair: Pair<Int, AlertDialogCallback?>? = null,
|
||||||
|
show: Boolean = true
|
||||||
|
): AlertDialog {
|
||||||
|
lateinit var dialogInterface: DialogInterface
|
||||||
|
|
||||||
|
return createRecyclerViewDialog(
|
||||||
|
title, positivePair, neutralPair, negativePair, show
|
||||||
|
) {
|
||||||
|
ActionsRecyclerViewAdapter(
|
||||||
|
actions
|
||||||
|
) {
|
||||||
|
dialogInterface
|
||||||
|
}
|
||||||
|
}.also { dialogInterface = it }
|
||||||
|
}
|
@@ -0,0 +1,43 @@
|
|||||||
|
package dev.inmo.micro_utils.android.alerts.recyclerview
|
||||||
|
|
||||||
|
import android.app.AlertDialog
|
||||||
|
import android.content.Context
|
||||||
|
import android.widget.LinearLayout
|
||||||
|
import androidx.recyclerview.widget.LinearLayoutManager
|
||||||
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
|
import dev.inmo.micro_utils.android.alerts.common.AlertDialogCallback
|
||||||
|
import dev.inmo.micro_utils.android.alerts.common.createCustomViewAlertDialogWithResources
|
||||||
|
|
||||||
|
fun Context.createRecyclerViewDialog(
|
||||||
|
title: Int? = null,
|
||||||
|
positivePair: Pair<Int, AlertDialogCallback?>? = null,
|
||||||
|
neutralPair: Pair<Int, AlertDialogCallback?>? = null,
|
||||||
|
negativePair: Pair<Int, AlertDialogCallback?>? = null,
|
||||||
|
show: Boolean = true,
|
||||||
|
layoutManager: RecyclerView.LayoutManager = LinearLayoutManager(this),
|
||||||
|
marginOfRecyclerView: Int = 8, // dp
|
||||||
|
recyclerViewSetUp: RecyclerView.() -> Unit = {},
|
||||||
|
adapterFactory: () -> RecyclerView.Adapter<*>
|
||||||
|
): AlertDialog {
|
||||||
|
val recyclerView = RecyclerView(this).apply {
|
||||||
|
layoutParams = LinearLayout.LayoutParams(
|
||||||
|
LinearLayout.LayoutParams.MATCH_PARENT,
|
||||||
|
LinearLayout.LayoutParams.WRAP_CONTENT
|
||||||
|
).apply {
|
||||||
|
setMargins(marginOfRecyclerView, marginOfRecyclerView, marginOfRecyclerView, marginOfRecyclerView)
|
||||||
|
}
|
||||||
|
this.layoutManager = layoutManager
|
||||||
|
adapter = adapterFactory()
|
||||||
|
recyclerViewSetUp()
|
||||||
|
}
|
||||||
|
|
||||||
|
return createCustomViewAlertDialogWithResources(
|
||||||
|
title,
|
||||||
|
positivePair,
|
||||||
|
neutralPair,
|
||||||
|
negativePair,
|
||||||
|
show
|
||||||
|
) {
|
||||||
|
recyclerView
|
||||||
|
}
|
||||||
|
}
|
22
android/recyclerview/build.gradle
Normal file
22
android/recyclerview/build.gradle
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
plugins {
|
||||||
|
id "org.jetbrains.kotlin.multiplatform"
|
||||||
|
id "org.jetbrains.kotlin.plugin.serialization"
|
||||||
|
id "com.android.library"
|
||||||
|
}
|
||||||
|
|
||||||
|
apply from: "$mppAndroidProjectPresetPath"
|
||||||
|
|
||||||
|
kotlin {
|
||||||
|
sourceSets {
|
||||||
|
commonMain {
|
||||||
|
dependencies {
|
||||||
|
api "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlin_coroutines_version"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
androidMain {
|
||||||
|
dependencies {
|
||||||
|
api "androidx.recyclerview:recyclerview:$androidx_recycler_version"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
1
android/recyclerview/src/main/AndroidManifest.xml
Normal file
1
android/recyclerview/src/main/AndroidManifest.xml
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<manifest package="dev.inmo.micro_utils.android.recyclerview"/>
|
@@ -0,0 +1,22 @@
|
|||||||
|
package dev.inmo.micro_utils.android.recyclerview
|
||||||
|
|
||||||
|
import android.view.LayoutInflater
|
||||||
|
import android.view.View
|
||||||
|
import android.view.ViewGroup
|
||||||
|
|
||||||
|
abstract class AbstractStandardViewHolder<T>(
|
||||||
|
inflater: LayoutInflater,
|
||||||
|
container: ViewGroup?,
|
||||||
|
viewId: Int,
|
||||||
|
onViewInflated: ((View) -> Unit)? = null
|
||||||
|
) : AbstractViewHolder<T>(
|
||||||
|
inflater.inflate(viewId, container, false).also {
|
||||||
|
onViewInflated ?.invoke(it)
|
||||||
|
}
|
||||||
|
) {
|
||||||
|
constructor(
|
||||||
|
container: ViewGroup,
|
||||||
|
viewId: Int,
|
||||||
|
onViewInflated: ((View) -> Unit)? = null
|
||||||
|
) : this(LayoutInflater.from(container.context), container, viewId, onViewInflated)
|
||||||
|
}
|
@@ -0,0 +1,10 @@
|
|||||||
|
package dev.inmo.micro_utils.android.recyclerview
|
||||||
|
|
||||||
|
import android.view.View
|
||||||
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
|
|
||||||
|
abstract class AbstractViewHolder<in T>(
|
||||||
|
view: View
|
||||||
|
) : RecyclerView.ViewHolder(view) {
|
||||||
|
abstract fun onBind(item: T)
|
||||||
|
}
|
@@ -0,0 +1,9 @@
|
|||||||
|
package dev.inmo.micro_utils.android.recyclerview
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.widget.LinearLayout
|
||||||
|
import androidx.recyclerview.widget.DividerItemDecoration
|
||||||
|
|
||||||
|
val Context.recyclerViewItemsDecoration
|
||||||
|
get() = DividerItemDecoration(this, LinearLayout.VERTICAL)
|
||||||
|
|
@@ -0,0 +1,53 @@
|
|||||||
|
package dev.inmo.micro_utils.android.recyclerview
|
||||||
|
|
||||||
|
import androidx.recyclerview.widget.*
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.flow.*
|
||||||
|
|
||||||
|
@Suppress("NOTHING_TO_INLINE")
|
||||||
|
private inline fun RecyclerView.LayoutManager.findLastVisibleItemPositionGetter(): (() -> Int)? = when (this) {
|
||||||
|
is LinearLayoutManager -> ::findLastVisibleItemPosition
|
||||||
|
is GridLayoutManager -> ::findLastVisibleItemPosition
|
||||||
|
else -> null
|
||||||
|
}
|
||||||
|
|
||||||
|
fun RecyclerView.lastVisibleItemFlow(
|
||||||
|
completingScope: CoroutineScope
|
||||||
|
): Flow<Int> {
|
||||||
|
val lastVisibleElementFun: () -> Int = layoutManager ?.findLastVisibleItemPositionGetter() ?: error("Currently supported only linear and grid layout manager")
|
||||||
|
val lastVisibleFlow = MutableStateFlow(lastVisibleElementFun())
|
||||||
|
addOnScrollListener(
|
||||||
|
object : RecyclerView.OnScrollListener() {
|
||||||
|
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
|
||||||
|
super.onScrolled(recyclerView, dx, dy)
|
||||||
|
lastVisibleFlow.value = lastVisibleElementFun()
|
||||||
|
}
|
||||||
|
}.also { scrollListener ->
|
||||||
|
lastVisibleFlow.onCompletion {
|
||||||
|
removeOnScrollListener(scrollListener)
|
||||||
|
}.launchIn(completingScope)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
return lastVisibleFlow.asStateFlow()
|
||||||
|
}
|
||||||
|
|
||||||
|
inline fun Flow<Int>.mapLeftItems(
|
||||||
|
crossinline countGetter: () -> Int
|
||||||
|
): Flow<Int> = map { countGetter() - it }
|
||||||
|
|
||||||
|
inline fun Flow<Int>.mapRequireFilling(
|
||||||
|
minimalLeftItems: Int,
|
||||||
|
crossinline countGetter: () -> Int
|
||||||
|
): Flow<Int> = mapLeftItems(countGetter).mapNotNull {
|
||||||
|
if (it < minimalLeftItems) {
|
||||||
|
it
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inline fun RecyclerView.mapRequireFilling(
|
||||||
|
minimalLeftItems: Int,
|
||||||
|
completingScope: CoroutineScope,
|
||||||
|
crossinline countGetter: () -> Int
|
||||||
|
): Flow<Int> = lastVisibleItemFlow(completingScope).mapRequireFilling(minimalLeftItems, countGetter)
|
@@ -0,0 +1,68 @@
|
|||||||
|
package dev.inmo.micro_utils.android.recyclerview
|
||||||
|
|
||||||
|
import android.view.View
|
||||||
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
|
|
||||||
|
|
||||||
|
abstract class RecyclerViewAdapter<T>(
|
||||||
|
val data: List<T>
|
||||||
|
): RecyclerView.Adapter<AbstractViewHolder<T>>() {
|
||||||
|
var emptyView: View? = null
|
||||||
|
set(value) {
|
||||||
|
field = value
|
||||||
|
checkEmpty()
|
||||||
|
}
|
||||||
|
|
||||||
|
init {
|
||||||
|
registerAdapterDataObserver(
|
||||||
|
object : RecyclerView.AdapterDataObserver() {
|
||||||
|
override fun onItemRangeChanged(positionStart: Int, itemCount: Int) {
|
||||||
|
super.onItemRangeChanged(positionStart, itemCount)
|
||||||
|
checkEmpty()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onItemRangeChanged(positionStart: Int, itemCount: Int, payload: Any?) {
|
||||||
|
super.onItemRangeChanged(positionStart, itemCount, payload)
|
||||||
|
checkEmpty()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onChanged() {
|
||||||
|
super.onChanged()
|
||||||
|
checkEmpty()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onItemRangeRemoved(positionStart: Int, itemCount: Int) {
|
||||||
|
super.onItemRangeRemoved(positionStart, itemCount)
|
||||||
|
checkEmpty()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onItemRangeMoved(fromPosition: Int, toPosition: Int, itemCount: Int) {
|
||||||
|
super.onItemRangeMoved(fromPosition, toPosition, itemCount)
|
||||||
|
checkEmpty()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
|
||||||
|
super.onItemRangeInserted(positionStart, itemCount)
|
||||||
|
checkEmpty()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
checkEmpty()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getItemCount(): Int = data.size
|
||||||
|
|
||||||
|
override fun onBindViewHolder(holder: AbstractViewHolder<T>, position: Int) {
|
||||||
|
holder.onBind(data[position])
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun checkEmpty() {
|
||||||
|
emptyView ?. let {
|
||||||
|
if (data.isEmpty()) {
|
||||||
|
it.visibility = View.VISIBLE
|
||||||
|
} else {
|
||||||
|
it.visibility = View.GONE
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -1,16 +1,19 @@
|
|||||||
buildscript {
|
buildscript {
|
||||||
repositories {
|
repositories {
|
||||||
mavenLocal()
|
|
||||||
jcenter()
|
jcenter()
|
||||||
|
google()
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
|
mavenLocal()
|
||||||
maven { url "https://plugins.gradle.org/m2/" }
|
maven { url "https://plugins.gradle.org/m2/" }
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
|
classpath 'com.android.tools.build:gradle:4.0.2'
|
||||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||||
classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"
|
classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"
|
||||||
classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:$gradle_bintray_plugin_version"
|
classpath "com.getkeepsafe.dexcount:dexcount-gradle-plugin:$dexcount_version"
|
||||||
classpath "com.github.breadmoirai:github-release:$github_release_plugin_version"
|
classpath "com.github.breadmoirai:github-release:$github_release_plugin_version"
|
||||||
|
classpath "org.jetbrains.dokka:dokka-gradle-plugin:$dokka_version"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -19,6 +22,7 @@ allprojects {
|
|||||||
mavenLocal()
|
mavenLocal()
|
||||||
jcenter()
|
jcenter()
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
|
google()
|
||||||
maven { url "https://kotlin.bintray.com/kotlinx" }
|
maven { url "https://kotlin.bintray.com/kotlinx" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -12,4 +12,7 @@ function assert_success() {
|
|||||||
export RELEASE_MODE=true
|
export RELEASE_MODE=true
|
||||||
project="$1"
|
project="$1"
|
||||||
|
|
||||||
assert_success ./gradlew clean "$project:clean" "$project:build" "$project:publishToMavenLocal" "$project:bintrayUpload"
|
assert_success ./gradlew clean
|
||||||
|
assert_success ./gradlew "$project:build"
|
||||||
|
assert_success ./gradlew "$project:publishToMavenLocal"
|
||||||
|
assert_success ./gradlew "$project:bintrayUpload"
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
plugins {
|
plugins {
|
||||||
id "org.jetbrains.kotlin.multiplatform"
|
id "org.jetbrains.kotlin.multiplatform"
|
||||||
id "org.jetbrains.kotlin.plugin.serialization"
|
id "org.jetbrains.kotlin.plugin.serialization"
|
||||||
|
id "com.android.library"
|
||||||
}
|
}
|
||||||
|
|
||||||
apply from: "$mppProjectWithSerializationPresetPath"
|
apply from: "$mppProjectWithSerializationPresetPath"
|
||||||
|
@@ -0,0 +1,19 @@
|
|||||||
|
package dev.inmo.micro_utils.common
|
||||||
|
|
||||||
|
@RequiresOptIn(
|
||||||
|
"It is possible, that behaviour of this thing will be changed or removed in future releases",
|
||||||
|
RequiresOptIn.Level.WARNING
|
||||||
|
)
|
||||||
|
@Target(
|
||||||
|
AnnotationTarget.CLASS,
|
||||||
|
AnnotationTarget.CONSTRUCTOR,
|
||||||
|
AnnotationTarget.FIELD,
|
||||||
|
AnnotationTarget.PROPERTY,
|
||||||
|
AnnotationTarget.PROPERTY_GETTER,
|
||||||
|
AnnotationTarget.PROPERTY_SETTER,
|
||||||
|
AnnotationTarget.FUNCTION,
|
||||||
|
AnnotationTarget.TYPE,
|
||||||
|
AnnotationTarget.TYPEALIAS,
|
||||||
|
AnnotationTarget.TYPE_PARAMETER
|
||||||
|
)
|
||||||
|
annotation class PreviewFeature
|
@@ -149,18 +149,3 @@ inline fun <T> StrictDiff(old: Iterable<T>, new: Iterable<T>) = old.calculateDif
|
|||||||
inline fun <T> Iterable<T>.calculateStrictDiff(
|
inline fun <T> Iterable<T>.calculateStrictDiff(
|
||||||
other: Iterable<T>
|
other: Iterable<T>
|
||||||
) = calculateDiff(other, strictComparison = true)
|
) = calculateDiff(other, strictComparison = true)
|
||||||
|
|
||||||
/**
|
|
||||||
* Compare one-to-one
|
|
||||||
*/
|
|
||||||
@Deprecated("Will be removed or replaced with some new function. Use calculateDiff instead")
|
|
||||||
inline fun <T> Iterable<T>.syncWith(
|
|
||||||
other: Iterable<T>,
|
|
||||||
noinline removed: (List<T>) -> Unit = {},
|
|
||||||
noinline added: (List<T>) -> Unit = {}
|
|
||||||
) {
|
|
||||||
calculateDiff(other).also {
|
|
||||||
removed(it.removed.map { it.value })
|
|
||||||
added(it.added.map { it.value })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
1
common/src/main/AndroidManifest.xml
Normal file
1
common/src/main/AndroidManifest.xml
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<manifest package="dev.inmo.micro_utils.common"/>
|
@@ -0,0 +1,13 @@
|
|||||||
|
@file:Suppress("NOTHING_TO_INLINE")
|
||||||
|
|
||||||
|
package dev.inmo.micro_utils.common
|
||||||
|
|
||||||
|
import android.content.res.Resources
|
||||||
|
|
||||||
|
inline fun Resources.getSp(
|
||||||
|
resId: Int
|
||||||
|
) = getDimension(resId) / displayMetrics.scaledDensity
|
||||||
|
|
||||||
|
inline fun Resources.getDp(
|
||||||
|
resId: Int
|
||||||
|
) = getDimension(resId) * displayMetrics.density
|
@@ -0,0 +1,76 @@
|
|||||||
|
package dev.inmo.micro_utils.common
|
||||||
|
|
||||||
|
import android.view.View
|
||||||
|
import android.view.ViewGroup
|
||||||
|
import android.view.animation.Animation
|
||||||
|
import android.view.animation.Transformation
|
||||||
|
|
||||||
|
@PreviewFeature
|
||||||
|
fun View.expand(
|
||||||
|
duration: Long = 500,
|
||||||
|
targetWidth: Int = ViewGroup.LayoutParams.MATCH_PARENT,
|
||||||
|
targetHeight: Int = ViewGroup.LayoutParams.WRAP_CONTENT
|
||||||
|
) {
|
||||||
|
measure(targetWidth, targetHeight)
|
||||||
|
val measuredHeight: Int = measuredHeight
|
||||||
|
layoutParams.height = 0
|
||||||
|
visibility = View.VISIBLE
|
||||||
|
val a: Animation = object : Animation() {
|
||||||
|
override fun applyTransformation(interpolatedTime: Float, t: Transformation?) {
|
||||||
|
super.applyTransformation(interpolatedTime, t)
|
||||||
|
layoutParams.height = if (interpolatedTime == 1f) targetHeight else (measuredHeight * interpolatedTime).toInt()
|
||||||
|
requestLayout()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun willChangeBounds(): Boolean {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
a.duration = duration
|
||||||
|
|
||||||
|
startAnimation(a)
|
||||||
|
}
|
||||||
|
|
||||||
|
@PreviewFeature
|
||||||
|
fun View.collapse(duration: Long = 500) {
|
||||||
|
val initialHeight: Int = measuredHeight
|
||||||
|
val a: Animation = object : Animation() {
|
||||||
|
override fun applyTransformation(interpolatedTime: Float, t: Transformation?) {
|
||||||
|
if (interpolatedTime == 1f) {
|
||||||
|
visibility = View.GONE
|
||||||
|
} else {
|
||||||
|
layoutParams.height = initialHeight - (initialHeight * interpolatedTime).toInt()
|
||||||
|
requestLayout()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun willChangeBounds(): Boolean {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
a.duration = duration
|
||||||
|
|
||||||
|
startAnimation(a)
|
||||||
|
}
|
||||||
|
|
||||||
|
@PreviewFeature
|
||||||
|
inline val View.isCollapsed
|
||||||
|
get() = visibility == View.GONE
|
||||||
|
|
||||||
|
@PreviewFeature
|
||||||
|
inline val View.isExpanded
|
||||||
|
get() = !isCollapsed
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return true in case of expanding
|
||||||
|
*/
|
||||||
|
@PreviewFeature
|
||||||
|
fun View.toggleExpandState(duration: Long = 500): Boolean = if (isCollapsed) {
|
||||||
|
expand(duration)
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
collapse(duration)
|
||||||
|
false
|
||||||
|
}
|
@@ -0,0 +1,7 @@
|
|||||||
|
package dev.inmo.micro_utils.common
|
||||||
|
|
||||||
|
@Suppress("UNCHECKED_CAST", "SimplifiableCall")
|
||||||
|
inline fun <T, R> Iterable<T>.mapNotNullA(transform: (T) -> R?): List<R> = map(transform).filter { it != null } as List<R>
|
||||||
|
|
||||||
|
@Suppress("UNCHECKED_CAST", "SimplifiableCall")
|
||||||
|
inline fun <T, R> Array<T>.mapNotNullA(mapper: (T) -> R?): List<R> = map(mapper).filter { it != null } as List<R>
|
@@ -0,0 +1,34 @@
|
|||||||
|
@file:Suppress("NOTHING_TO_INLINE", "unused")
|
||||||
|
|
||||||
|
package dev.inmo.micro_utils.common
|
||||||
|
|
||||||
|
import android.view.View
|
||||||
|
import android.view.ViewGroup
|
||||||
|
|
||||||
|
inline val View.enabled
|
||||||
|
get() = isEnabled
|
||||||
|
|
||||||
|
inline val View.disabled
|
||||||
|
get() = !enabled
|
||||||
|
|
||||||
|
fun View.disable() {
|
||||||
|
if (this is ViewGroup) {
|
||||||
|
(0 until childCount).forEach { getChildAt(it).disable() }
|
||||||
|
}
|
||||||
|
isEnabled = false
|
||||||
|
}
|
||||||
|
|
||||||
|
fun View.enable() {
|
||||||
|
if (this is ViewGroup) {
|
||||||
|
(0 until childCount).forEach { getChildAt(it).enable() }
|
||||||
|
}
|
||||||
|
isEnabled = true
|
||||||
|
}
|
||||||
|
|
||||||
|
fun View.toggleEnabledState(enabled: Boolean) {
|
||||||
|
if (enabled) {
|
||||||
|
enable()
|
||||||
|
} else {
|
||||||
|
disable()
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,35 @@
|
|||||||
|
@file:Suppress("NOTHING_TO_INLINE", "unused")
|
||||||
|
|
||||||
|
package dev.inmo.micro_utils.common
|
||||||
|
|
||||||
|
import android.view.View
|
||||||
|
|
||||||
|
inline val View.gone
|
||||||
|
get() = visibility == View.GONE
|
||||||
|
inline fun View.gone() {
|
||||||
|
visibility = View.GONE
|
||||||
|
}
|
||||||
|
|
||||||
|
inline val View.hidden
|
||||||
|
get() = visibility == View.INVISIBLE
|
||||||
|
inline fun View.hide() {
|
||||||
|
visibility = View.INVISIBLE
|
||||||
|
}
|
||||||
|
|
||||||
|
inline val View.shown
|
||||||
|
get() = visibility == View.VISIBLE
|
||||||
|
inline fun View.show() {
|
||||||
|
visibility = View.VISIBLE
|
||||||
|
}
|
||||||
|
|
||||||
|
fun View.toggleVisibility(goneOnHide: Boolean = true) {
|
||||||
|
if (isShown) {
|
||||||
|
if (goneOnHide) {
|
||||||
|
gone()
|
||||||
|
} else {
|
||||||
|
hide()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
show()
|
||||||
|
}
|
||||||
|
}
|
@@ -1,6 +1,7 @@
|
|||||||
plugins {
|
plugins {
|
||||||
id "org.jetbrains.kotlin.multiplatform"
|
id "org.jetbrains.kotlin.multiplatform"
|
||||||
id "org.jetbrains.kotlin.plugin.serialization"
|
id "org.jetbrains.kotlin.plugin.serialization"
|
||||||
|
id "com.android.library"
|
||||||
}
|
}
|
||||||
|
|
||||||
apply from: "$mppProjectWithSerializationPresetPath"
|
apply from: "$mppProjectWithSerializationPresetPath"
|
||||||
@@ -12,5 +13,10 @@ kotlin {
|
|||||||
api "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlin_coroutines_version"
|
api "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlin_coroutines_version"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
androidMain {
|
||||||
|
dependencies {
|
||||||
|
api "org.jetbrains.kotlinx:kotlinx-coroutines-android:$kotlin_coroutines_version"
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -1,24 +0,0 @@
|
|||||||
package dev.inmo.micro_utils.coroutines
|
|
||||||
|
|
||||||
import kotlinx.coroutines.channels.*
|
|
||||||
import kotlinx.coroutines.flow.Flow
|
|
||||||
import kotlinx.coroutines.flow.asFlow
|
|
||||||
|
|
||||||
@Suppress("FunctionName")
|
|
||||||
@Deprecated("Deprecated due to stabilization of SharedFlow and StateFlow")
|
|
||||||
fun <T> BroadcastFlow(
|
|
||||||
internalChannelSize: Int = Channel.BUFFERED
|
|
||||||
): BroadcastFlow<T> {
|
|
||||||
val channel = BroadcastChannel<T>(internalChannelSize)
|
|
||||||
|
|
||||||
return BroadcastFlow(
|
|
||||||
channel,
|
|
||||||
channel.asFlow()
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated("Deprecated due to stabilization of SharedFlow and StateFlow")
|
|
||||||
class BroadcastFlow<T> internal constructor(
|
|
||||||
private val channel: BroadcastChannel<T>,
|
|
||||||
private val flow: Flow<T>
|
|
||||||
): Flow<T> by flow, SendChannel<T> by channel
|
|
@@ -1,68 +0,0 @@
|
|||||||
package dev.inmo.micro_utils.coroutines
|
|
||||||
|
|
||||||
import kotlinx.coroutines.CoroutineScope
|
|
||||||
import kotlinx.coroutines.channels.BroadcastChannel
|
|
||||||
import kotlinx.coroutines.channels.Channel
|
|
||||||
import kotlinx.coroutines.flow.*
|
|
||||||
|
|
||||||
const val defaultBroadcastStateFlowReplayCacheSize = 1
|
|
||||||
|
|
||||||
@Deprecated("Deprecated due to stabilization of SharedFlow and StateFlow")
|
|
||||||
class BroadcastStateFlow<T> internal constructor(
|
|
||||||
parentFlow: Flow<T>,
|
|
||||||
initial: T,
|
|
||||||
replayCacheSize: Int = defaultBroadcastStateFlowReplayCacheSize,
|
|
||||||
replayScope: CoroutineScope
|
|
||||||
) : StateFlow<T>, Flow<T> by parentFlow {
|
|
||||||
private val deque = ArrayDeque<T>(1).also {
|
|
||||||
it.add(initial)
|
|
||||||
}
|
|
||||||
override val replayCache: List<T>
|
|
||||||
get() = deque.toList()
|
|
||||||
override val value: T
|
|
||||||
get() = deque.last()
|
|
||||||
|
|
||||||
init {
|
|
||||||
if (replayCacheSize < 1) {
|
|
||||||
error("Replay cache size can't be less than 1, but was $replayCacheSize")
|
|
||||||
}
|
|
||||||
parentFlow.onEach {
|
|
||||||
deque.addLast(it)
|
|
||||||
if (deque.size > replayCacheSize) {
|
|
||||||
deque.removeFirst()
|
|
||||||
}
|
|
||||||
}.launchIn(replayScope)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated("Deprecated due to stabilization of SharedFlow and StateFlow")
|
|
||||||
fun <T> BroadcastChannel<T>.asStateFlow(
|
|
||||||
value: T,
|
|
||||||
scope: CoroutineScope,
|
|
||||||
replayCacheSize: Int = defaultBroadcastStateFlowReplayCacheSize
|
|
||||||
): StateFlow<T> = BroadcastStateFlow(asFlow(), value, replayCacheSize, scope)
|
|
||||||
|
|
||||||
@Deprecated("Deprecated due to stabilization of SharedFlow and StateFlow")
|
|
||||||
fun <T> BroadcastChannel<T?>.asStateFlow(
|
|
||||||
scope: CoroutineScope,
|
|
||||||
replayCacheSize: Int = defaultBroadcastStateFlowReplayCacheSize
|
|
||||||
): StateFlow<T?> = asStateFlow(null, scope, replayCacheSize)
|
|
||||||
|
|
||||||
@Deprecated("Deprecated due to stabilization of SharedFlow and StateFlow")
|
|
||||||
fun <T> broadcastStateFlow(
|
|
||||||
initial: T, scope: CoroutineScope,
|
|
||||||
channelSize: Int = Channel.BUFFERED,
|
|
||||||
replayCacheSize: Int = defaultBroadcastStateFlowReplayCacheSize
|
|
||||||
) = BroadcastChannel<T>(
|
|
||||||
channelSize
|
|
||||||
).let {
|
|
||||||
it to it.asStateFlow(initial, scope, replayCacheSize)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated("Deprecated due to stabilization of SharedFlow and StateFlow")
|
|
||||||
fun <T> broadcastStateFlow(
|
|
||||||
scope: CoroutineScope,
|
|
||||||
channelSize: Int = Channel.BUFFERED,
|
|
||||||
replayCacheSize: Int = defaultBroadcastStateFlowReplayCacheSize
|
|
||||||
) = broadcastStateFlow<T?>(null, scope, channelSize, replayCacheSize)
|
|
||||||
|
|
@@ -0,0 +1,8 @@
|
|||||||
|
@file:Suppress("NOTHING_TO_INLINE")
|
||||||
|
|
||||||
|
package dev.inmo.micro_utils.coroutines
|
||||||
|
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import kotlinx.coroutines.flow.merge
|
||||||
|
|
||||||
|
inline operator fun <T> Flow<T>.plus(other: Flow<T>) = merge(this, other)
|
1
coroutines/src/main/AndroidManifest.xml
Normal file
1
coroutines/src/main/AndroidManifest.xml
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<manifest package="dev.inmo.micro_utils.coroutines"/>
|
@@ -0,0 +1,10 @@
|
|||||||
|
package dev.inmo.micro_utils.coroutines
|
||||||
|
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
|
|
||||||
|
suspend inline fun <T> doInUI(noinline block: suspend CoroutineScope.() -> T) = withContext(
|
||||||
|
Dispatchers.Main,
|
||||||
|
block
|
||||||
|
)
|
38
defaultAndroidSettings
Normal file
38
defaultAndroidSettings
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
android {
|
||||||
|
compileSdkVersion "$android_compileSdkVersion".toInteger()
|
||||||
|
buildToolsVersion "$android_buildToolsVersion"
|
||||||
|
|
||||||
|
defaultConfig {
|
||||||
|
minSdkVersion "$android_minSdkVersion".toInteger()
|
||||||
|
targetSdkVersion "$android_compileSdkVersion".toInteger()
|
||||||
|
versionCode "${android_code_version}".toInteger()
|
||||||
|
versionName "$version"
|
||||||
|
}
|
||||||
|
buildTypes {
|
||||||
|
release {
|
||||||
|
minifyEnabled false
|
||||||
|
}
|
||||||
|
debug {
|
||||||
|
debuggable true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
packagingOptions {
|
||||||
|
exclude 'META-INF/kotlinx-serialization-runtime.kotlin_module'
|
||||||
|
exclude 'META-INF/kotlinx-serialization-cbor.kotlin_module'
|
||||||
|
exclude 'META-INF/kotlinx-serialization-properties.kotlin_module'
|
||||||
|
}
|
||||||
|
|
||||||
|
compileOptions {
|
||||||
|
sourceCompatibility JavaVersion.VERSION_1_8
|
||||||
|
targetCompatibility JavaVersion.VERSION_1_8
|
||||||
|
}
|
||||||
|
|
||||||
|
kotlinOptions {
|
||||||
|
jvmTarget = JavaVersion.VERSION_1_8.toString()
|
||||||
|
}
|
||||||
|
|
||||||
|
sourceSets {
|
||||||
|
main.java.srcDirs += 'src/main/kotlin'
|
||||||
|
}
|
||||||
|
}
|
@@ -1,26 +1,14 @@
|
|||||||
buildscript {
|
|
||||||
repositories {
|
|
||||||
mavenLocal()
|
|
||||||
jcenter()
|
|
||||||
mavenCentral()
|
|
||||||
}
|
|
||||||
|
|
||||||
dependencies {
|
|
||||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
|
||||||
classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"
|
|
||||||
classpath "org.jetbrains.dokka:dokka-gradle-plugin:$dokka_version"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
plugins {
|
plugins {
|
||||||
id "org.jetbrains.kotlin.multiplatform"
|
id "org.jetbrains.kotlin.multiplatform"
|
||||||
id "org.jetbrains.kotlin.plugin.serialization"
|
id "org.jetbrains.kotlin.plugin.serialization"
|
||||||
id "org.jetbrains.dokka" version "$dokka_version"
|
id "com.android.library"
|
||||||
|
id "org.jetbrains.dokka"
|
||||||
}
|
}
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
mavenLocal()
|
mavenLocal()
|
||||||
jcenter()
|
jcenter()
|
||||||
|
google()
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -30,6 +18,7 @@ kotlin {
|
|||||||
browser()
|
browser()
|
||||||
nodejs()
|
nodejs()
|
||||||
}
|
}
|
||||||
|
android {}
|
||||||
|
|
||||||
sourceSets {
|
sourceSets {
|
||||||
commonMain {
|
commonMain {
|
||||||
@@ -41,6 +30,57 @@ kotlin {
|
|||||||
it != project
|
it != project
|
||||||
&& it.hasProperty("kotlin")
|
&& it.hasProperty("kotlin")
|
||||||
&& it.kotlin.sourceSets.any { it.name.contains("commonMain") }
|
&& it.kotlin.sourceSets.any { it.name.contains("commonMain") }
|
||||||
|
&& it.kotlin.sourceSets.any { it.name.contains("jsMain") }
|
||||||
|
&& it.kotlin.sourceSets.any { it.name.contains("jvmMain") }
|
||||||
|
&& it.kotlin.sourceSets.any { it.name.contains("androidMain") }
|
||||||
|
) {
|
||||||
|
api it
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
jsMain {
|
||||||
|
dependencies {
|
||||||
|
implementation kotlin('stdlib')
|
||||||
|
|
||||||
|
project.parent.subprojects.forEach {
|
||||||
|
if (
|
||||||
|
it != project
|
||||||
|
&& it.hasProperty("kotlin")
|
||||||
|
&& it.kotlin.sourceSets.any { it.name.contains("commonMain") }
|
||||||
|
&& it.kotlin.sourceSets.any { it.name.contains("jsMain") }
|
||||||
|
) {
|
||||||
|
api it
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
jvmMain {
|
||||||
|
dependencies {
|
||||||
|
implementation kotlin('stdlib')
|
||||||
|
|
||||||
|
project.parent.subprojects.forEach {
|
||||||
|
if (
|
||||||
|
it != project
|
||||||
|
&& it.hasProperty("kotlin")
|
||||||
|
&& it.kotlin.sourceSets.any { it.name.contains("commonMain") }
|
||||||
|
&& it.kotlin.sourceSets.any { it.name.contains("jvmMain") }
|
||||||
|
) {
|
||||||
|
api it
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
androidMain {
|
||||||
|
dependencies {
|
||||||
|
implementation kotlin('stdlib')
|
||||||
|
|
||||||
|
project.parent.subprojects.forEach {
|
||||||
|
if (
|
||||||
|
it != project
|
||||||
|
&& it.hasProperty("kotlin")
|
||||||
|
&& it.kotlin.sourceSets.any { it.name.contains("commonMain") }
|
||||||
|
&& it.kotlin.sourceSets.any { it.name.contains("androidMain") }
|
||||||
) {
|
) {
|
||||||
api it
|
api it
|
||||||
}
|
}
|
||||||
@@ -84,5 +124,11 @@ tasks.dokkaHtml {
|
|||||||
named("jvmMain") {
|
named("jvmMain") {
|
||||||
sourceRoots.setFrom(findSourcesWithName("jvmMain", "commonMain"))
|
sourceRoots.setFrom(findSourcesWithName("jvmMain", "commonMain"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
named("androidMain") {
|
||||||
|
sourceRoots.setFrom(findSourcesWithName("androidMain", "commonMain"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
apply from: "$defaultAndroidSettingsPresetPath"
|
||||||
|
@@ -1,3 +0,0 @@
|
|||||||
dokka_version=1.4.0
|
|
||||||
|
|
||||||
org.gradle.jvmargs=-Xmx1024m
|
|
1
dokka/src/main/AndroidManifest.xml
Normal file
1
dokka/src/main/AndroidManifest.xml
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<manifest package="dev.inmo.dokka"/>
|
@@ -10,17 +10,21 @@ allprojects {
|
|||||||
}
|
}
|
||||||
|
|
||||||
internalProject = { name ->
|
internalProject = { name ->
|
||||||
if (releaseMode) {
|
// if (releaseMode) {
|
||||||
"$group:$name:$version"
|
// "$group:$name:$version"
|
||||||
} else {
|
// } else {
|
||||||
projectByName("$name")
|
// projectByName("$name")
|
||||||
}
|
// }
|
||||||
|
projectByName("$name")
|
||||||
}
|
}
|
||||||
|
|
||||||
releaseMode = (project.hasProperty('RELEASE_MODE') && project.property('RELEASE_MODE') == "true") || System.getenv('RELEASE_MODE') == "true"
|
releaseMode = (project.hasProperty('RELEASE_MODE') && project.property('RELEASE_MODE') == "true") || System.getenv('RELEASE_MODE') == "true"
|
||||||
|
|
||||||
mppProjectWithSerializationPresetPath = "${rootProject.projectDir.absolutePath}/mppProjectWithSerialization"
|
mppProjectWithSerializationPresetPath = "${rootProject.projectDir.absolutePath}/mppProjectWithSerialization"
|
||||||
mppJavaProjectPresetPath = "${rootProject.projectDir.absolutePath}/mppJavaProject"
|
mppJavaProjectPresetPath = "${rootProject.projectDir.absolutePath}/mppJavaProject"
|
||||||
|
mppAndroidProjectPresetPath = "${rootProject.projectDir.absolutePath}/mppAndroidProject"
|
||||||
|
|
||||||
|
defaultAndroidSettingsPresetPath = "${rootProject.projectDir.absolutePath}/defaultAndroidSettings"
|
||||||
|
|
||||||
publishGradlePath = "${rootProject.projectDir.absolutePath}/publish.gradle"
|
publishGradlePath = "${rootProject.projectDir.absolutePath}/publish.gradle"
|
||||||
publishMavenPath = "${rootProject.projectDir.absolutePath}/maven.publish.gradle"
|
publishMavenPath = "${rootProject.projectDir.absolutePath}/maven.publish.gradle"
|
||||||
|
@@ -3,20 +3,43 @@ org.gradle.parallel=true
|
|||||||
kotlin.js.generate.externals=true
|
kotlin.js.generate.externals=true
|
||||||
kotlin.incremental=true
|
kotlin.incremental=true
|
||||||
kotlin.incremental.js=true
|
kotlin.incremental.js=true
|
||||||
|
android.useAndroidX=true
|
||||||
|
android.enableJetifier=true
|
||||||
|
org.gradle.jvmargs=-Xmx2048m
|
||||||
|
|
||||||
kotlin_version=1.4.10
|
kotlin_version=1.4.20
|
||||||
kotlin_coroutines_version=1.4.0
|
kotlin_coroutines_version=1.4.1
|
||||||
kotlin_serialisation_core_version=1.0.1
|
kotlin_serialisation_core_version=1.0.1
|
||||||
kotlin_exposed_version=0.28.1
|
kotlin_exposed_version=0.28.1
|
||||||
|
|
||||||
ktor_version=1.4.1
|
ktor_version=1.4.2
|
||||||
|
|
||||||
klockVersion=1.12.1
|
klockVersion=2.0.0
|
||||||
|
|
||||||
gradle_bintray_plugin_version=1.8.5
|
|
||||||
github_release_plugin_version=2.2.12
|
github_release_plugin_version=2.2.12
|
||||||
|
|
||||||
uuidVersion=0.2.2
|
uuidVersion=0.2.2
|
||||||
|
|
||||||
|
# ANDROID
|
||||||
|
|
||||||
|
core_ktx_version=1.3.2
|
||||||
|
androidx_recycler_version=1.1.0
|
||||||
|
appcompat_version=1.2.0
|
||||||
|
|
||||||
|
android_minSdkVersion=19
|
||||||
|
android_compileSdkVersion=30
|
||||||
|
android_buildToolsVersion=30.0.2
|
||||||
|
dexcount_version=2.0.0-RC1
|
||||||
|
junit_version=4.12
|
||||||
|
test_ext_junit_version=1.1.2
|
||||||
|
espresso_core=3.3.0
|
||||||
|
|
||||||
|
# Dokka
|
||||||
|
|
||||||
|
dokka_version=1.4.0
|
||||||
|
|
||||||
|
# Project data
|
||||||
|
|
||||||
group=dev.inmo
|
group=dev.inmo
|
||||||
version=0.2.6
|
version=0.4.5
|
||||||
|
android_code_version=9
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
plugins {
|
plugins {
|
||||||
id "org.jetbrains.kotlin.multiplatform"
|
id "org.jetbrains.kotlin.multiplatform"
|
||||||
id "org.jetbrains.kotlin.plugin.serialization"
|
id "org.jetbrains.kotlin.plugin.serialization"
|
||||||
|
id "com.android.library"
|
||||||
}
|
}
|
||||||
|
|
||||||
apply from: "$mppProjectWithSerializationPresetPath"
|
apply from: "$mppProjectWithSerializationPresetPath"
|
||||||
|
1
ktor/client/src/main/AndroidManifest.xml
Normal file
1
ktor/client/src/main/AndroidManifest.xml
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<manifest package="dev.inmo.micro_utils.ktor.client"/>
|
@@ -1,6 +1,7 @@
|
|||||||
plugins {
|
plugins {
|
||||||
id "org.jetbrains.kotlin.multiplatform"
|
id "org.jetbrains.kotlin.multiplatform"
|
||||||
id "org.jetbrains.kotlin.plugin.serialization"
|
id "org.jetbrains.kotlin.plugin.serialization"
|
||||||
|
id "com.android.library"
|
||||||
}
|
}
|
||||||
|
|
||||||
apply from: "$mppProjectWithSerializationPresetPath"
|
apply from: "$mppProjectWithSerializationPresetPath"
|
||||||
|
1
ktor/common/src/main/AndroidManifest.xml
Normal file
1
ktor/common/src/main/AndroidManifest.xml
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<manifest package="dev.inmo.micro_utils.ktor.common"/>
|
@@ -1,60 +0,0 @@
|
|||||||
apply plugin: 'maven-publish'
|
|
||||||
|
|
||||||
task javadocsJar(type: Jar) {
|
|
||||||
classifier = 'javadoc'
|
|
||||||
}
|
|
||||||
|
|
||||||
afterEvaluate {
|
|
||||||
project.publishing.publications.all {
|
|
||||||
// rename artifacts
|
|
||||||
groupId "${project.group}"
|
|
||||||
if (it.name.contains('kotlinMultiplatform')) {
|
|
||||||
artifactId = "${project.name}"
|
|
||||||
} else {
|
|
||||||
artifactId = "${project.name}-$name"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
publishing {
|
|
||||||
publications.all {
|
|
||||||
artifact javadocsJar
|
|
||||||
|
|
||||||
pom {
|
|
||||||
description = ""
|
|
||||||
name = "${project.name}"
|
|
||||||
url = "https://git.inmo.dev/InsanusMokrassar/MicroUtils_mirror"
|
|
||||||
|
|
||||||
scm {
|
|
||||||
developerConnection = "scm:git:[fetch=]ssh://git@git.inmo.dev:8322/InsanusMokrassar/MicroUtils_mirror.git[push=]ssh://git@git.inmo.dev:8322/InsanusMokrassar/MicroUtils_mirror.git"
|
|
||||||
url = "ssh://git@git.inmo.dev:8322/InsanusMokrassar/MicroUtils_mirror.git"
|
|
||||||
}
|
|
||||||
|
|
||||||
developers {
|
|
||||||
|
|
||||||
developer {
|
|
||||||
id = "InsanusMokrassar"
|
|
||||||
name = "Aleksei Ovsiannikov"
|
|
||||||
email = "ovsyannikov.alexey95@gmail.com"
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
developer {
|
|
||||||
id = "000Sanya"
|
|
||||||
name = "Syrov Aleksandr"
|
|
||||||
email = "000sanya.000sanya@gmail.com"
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
licenses {
|
|
||||||
|
|
||||||
license {
|
|
||||||
name = "Apache Software License 2.0"
|
|
||||||
url = "https://git.inmo.dev/InsanusMokrassar/MicroUtils_mirror/src/master/LICENSE"
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,6 +1,7 @@
|
|||||||
plugins {
|
plugins {
|
||||||
id "org.jetbrains.kotlin.multiplatform"
|
id "org.jetbrains.kotlin.multiplatform"
|
||||||
id "org.jetbrains.kotlin.plugin.serialization"
|
id "org.jetbrains.kotlin.plugin.serialization"
|
||||||
|
id "com.android.library"
|
||||||
}
|
}
|
||||||
|
|
||||||
apply from: "$mppProjectWithSerializationPresetPath"
|
apply from: "$mppProjectWithSerializationPresetPath"
|
||||||
|
1
mime_types/src/main/AndroidManifest.xml
Normal file
1
mime_types/src/main/AndroidManifest.xml
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<manifest package="dev.inmo.micro_utils.mime_types"/>
|
26
mppAndroidProject
Normal file
26
mppAndroidProject
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
project.version = "$version"
|
||||||
|
project.group = "$group"
|
||||||
|
|
||||||
|
apply from: "$publishGradlePath"
|
||||||
|
|
||||||
|
kotlin {
|
||||||
|
android {
|
||||||
|
publishAllLibraryVariants()
|
||||||
|
}
|
||||||
|
|
||||||
|
sourceSets {
|
||||||
|
commonMain {
|
||||||
|
dependencies {
|
||||||
|
implementation kotlin('stdlib')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
commonTest {
|
||||||
|
dependencies {
|
||||||
|
implementation kotlin('test-common')
|
||||||
|
implementation kotlin('test-annotations-common')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
apply from: "$defaultAndroidSettingsPresetPath"
|
@@ -9,6 +9,9 @@ kotlin {
|
|||||||
browser()
|
browser()
|
||||||
nodejs()
|
nodejs()
|
||||||
}
|
}
|
||||||
|
android {
|
||||||
|
publishAllLibraryVariants()
|
||||||
|
}
|
||||||
|
|
||||||
sourceSets {
|
sourceSets {
|
||||||
commonMain {
|
commonMain {
|
||||||
@@ -34,5 +37,14 @@ kotlin {
|
|||||||
implementation kotlin('test-junit')
|
implementation kotlin('test-junit')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
androidTest {
|
||||||
|
dependencies {
|
||||||
|
implementation kotlin('test-junit')
|
||||||
|
implementation "androidx.test.ext:junit:$test_ext_junit_version"
|
||||||
|
implementation "androidx.test.espresso:espresso-core:$espresso_core"
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
apply from: "$defaultAndroidSettingsPresetPath"
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
plugins {
|
plugins {
|
||||||
id "org.jetbrains.kotlin.multiplatform"
|
id "org.jetbrains.kotlin.multiplatform"
|
||||||
id "org.jetbrains.kotlin.plugin.serialization"
|
id "org.jetbrains.kotlin.plugin.serialization"
|
||||||
|
id "com.android.library"
|
||||||
}
|
}
|
||||||
|
|
||||||
apply from: "$mppProjectWithSerializationPresetPath"
|
apply from: "$mppProjectWithSerializationPresetPath"
|
||||||
|
1
pagination/common/src/main/AndroidManifest.xml
Normal file
1
pagination/common/src/main/AndroidManifest.xml
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<manifest package="dev.inmo.micro_utils.pagination.common"/>
|
@@ -1,6 +1,7 @@
|
|||||||
plugins {
|
plugins {
|
||||||
id "org.jetbrains.kotlin.multiplatform"
|
id "org.jetbrains.kotlin.multiplatform"
|
||||||
id "org.jetbrains.kotlin.plugin.serialization"
|
id "org.jetbrains.kotlin.plugin.serialization"
|
||||||
|
id "com.android.library"
|
||||||
}
|
}
|
||||||
|
|
||||||
apply from: "$mppProjectWithSerializationPresetPath"
|
apply from: "$mppProjectWithSerializationPresetPath"
|
||||||
|
1
pagination/ktor/common/src/main/AndroidManifest.xml
Normal file
1
pagination/ktor/common/src/main/AndroidManifest.xml
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<manifest package="dev.inmo.micro_utils.pagination.ktor.common"/>
|
@@ -1 +1 @@
|
|||||||
{"bintrayConfig":{"repo":"MicroUtils","packageName":"${project.name}","packageVcs":"https://github.com/InsanusMokrassar/MicroUtils","autoPublish":true,"overridePublish":true},"licenses":[{"id":"Apache-2.0","title":"Apache Software License 2.0","url":"https://git.inmo.dev/InsanusMokrassar/MicroUtils_mirror/src/master/LICENSE"}],"mavenConfig":{"name":"${project.name}","description":"","url":"https://git.inmo.dev/InsanusMokrassar/MicroUtils_mirror","vcsUrl":"ssh://git@git.inmo.dev:8322/InsanusMokrassar/MicroUtils_mirror.git","developers":[{"id":"InsanusMokrassar","name":"Aleksei Ovsiannikov","eMail":"ovsyannikov.alexey95@gmail.com"},{"id":"000Sanya","name":"Syrov Aleksandr","eMail":"000sanya.000sanya@gmail.com"}]},"type":"Multiplatform"}
|
{"bintrayConfig":{"repo":"MicroUtils","packageName":"${project.name}","packageVcs":"https://github.com/InsanusMokrassar/MicroUtils","autoPublish":true,"overridePublish":true},"licenses":[{"id":"Apache-2.0","title":"Apache Software License 2.0","url":"https://git.inmo.dev/InsanusMokrassar/MicroUtils_mirror/src/master/LICENSE"}],"mavenConfig":{"name":"${project.name}","description":"","url":"https://git.inmo.dev/InsanusMokrassar/MicroUtils_mirror","vcsUrl":"ssh://git@git.inmo.dev:8322/InsanusMokrassar/MicroUtils_mirror.git","developers":[{"id":"InsanusMokrassar","name":"Aleksei Ovsiannikov","eMail":"ovsyannikov.alexey95@gmail.com"},{"id":"000Sanya","name":"Syrov Aleksandr","eMail":"000sanya.000sanya@gmail.com"}]}}
|
115
publish.gradle
115
publish.gradle
@@ -1,59 +1,72 @@
|
|||||||
apply plugin: 'com.jfrog.bintray'
|
apply plugin: 'maven-publish'
|
||||||
|
|
||||||
apply from: "$publishMavenPath"
|
task javadocsJar(type: Jar) {
|
||||||
|
classifier = 'javadoc'
|
||||||
bintray {
|
|
||||||
user = project.hasProperty('BINTRAY_USER') ? project.property('BINTRAY_USER') : System.getenv('BINTRAY_USER')
|
|
||||||
key = project.hasProperty('BINTRAY_KEY') ? project.property('BINTRAY_KEY') : System.getenv('BINTRAY_KEY')
|
|
||||||
filesSpec {
|
|
||||||
from "${buildDir}/publications/"
|
|
||||||
eachFile {
|
|
||||||
String directorySubname = it.getFile().parentFile.name
|
|
||||||
if (it.getName() == "module.json") {
|
|
||||||
if (directorySubname == "kotlinMultiplatform") {
|
|
||||||
it.setPath("${project.name}/${project.version}/${project.name}-${project.version}.module")
|
|
||||||
} else {
|
|
||||||
it.setPath("${project.name}-${directorySubname}/${project.version}/${project.name}-${directorySubname}-${project.version}.module")
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (directorySubname == "kotlinMultiplatform" && it.getName() == "pom-default.xml") {
|
|
||||||
it.setPath("${project.name}/${project.version}/${project.name}-${project.version}.pom")
|
|
||||||
} else {
|
|
||||||
it.exclude()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
into "${project.group}".replace(".", "/")
|
|
||||||
}
|
|
||||||
|
|
||||||
publish = true
|
|
||||||
override = true
|
|
||||||
|
|
||||||
pkg {
|
|
||||||
repo = "MicroUtils"
|
|
||||||
name = "${project.name}"
|
|
||||||
vcsUrl = "https://github.com/InsanusMokrassar/MicroUtils"
|
|
||||||
licenses = ["Apache-2.0"]
|
|
||||||
version {
|
|
||||||
name = "${project.version}"
|
|
||||||
released = new Date()
|
|
||||||
vcsTag = "${project.version}"
|
|
||||||
gpg {
|
|
||||||
sign = true
|
|
||||||
passphrase = project.hasProperty('signing.gnupg.passphrase') ? project.property('signing.gnupg.passphrase') : System.getenv('signing.gnupg.passphrase')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bintrayUpload.doFirst {
|
afterEvaluate {
|
||||||
publications = publishing.publications.collect {
|
project.publishing.publications.all {
|
||||||
|
// rename artifacts
|
||||||
|
groupId "${project.group}"
|
||||||
if (it.name.contains('kotlinMultiplatform')) {
|
if (it.name.contains('kotlinMultiplatform')) {
|
||||||
null
|
artifactId = "${project.name}"
|
||||||
} else {
|
} else {
|
||||||
it.name
|
artifactId = "${project.name}-$name"
|
||||||
}
|
}
|
||||||
} - null
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bintrayUpload.dependsOn publishToMavenLocal
|
publishing {
|
||||||
|
publications.all {
|
||||||
|
artifact javadocsJar
|
||||||
|
|
||||||
|
pom {
|
||||||
|
description = ""
|
||||||
|
name = "${project.name}"
|
||||||
|
url = "https://git.inmo.dev/InsanusMokrassar/MicroUtils_mirror"
|
||||||
|
|
||||||
|
scm {
|
||||||
|
developerConnection = "scm:git:[fetch=]ssh://git@git.inmo.dev:8322/InsanusMokrassar/MicroUtils_mirror.git[push=]ssh://git@git.inmo.dev:8322/InsanusMokrassar/MicroUtils_mirror.git"
|
||||||
|
url = "ssh://git@git.inmo.dev:8322/InsanusMokrassar/MicroUtils_mirror.git"
|
||||||
|
}
|
||||||
|
|
||||||
|
developers {
|
||||||
|
|
||||||
|
developer {
|
||||||
|
id = "InsanusMokrassar"
|
||||||
|
name = "Aleksei Ovsiannikov"
|
||||||
|
email = "ovsyannikov.alexey95@gmail.com"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
developer {
|
||||||
|
id = "000Sanya"
|
||||||
|
name = "Syrov Aleksandr"
|
||||||
|
email = "000sanya.000sanya@gmail.com"
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
licenses {
|
||||||
|
|
||||||
|
license {
|
||||||
|
name = "Apache Software License 2.0"
|
||||||
|
url = "https://git.inmo.dev/InsanusMokrassar/MicroUtils_mirror/src/master/LICENSE"
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
repositories {
|
||||||
|
maven {
|
||||||
|
name = "bintray"
|
||||||
|
url = uri("https://api.bintray.com/maven/${project.hasProperty('BINTRAY_USER') ? project.property('BINTRAY_USER') : System.getenv('BINTRAY_USER')}/MicroUtils/${project.name}/;publish=1;override=1")
|
||||||
|
credentials {
|
||||||
|
username = project.hasProperty('BINTRAY_USER') ? project.property('BINTRAY_USER') : System.getenv('BINTRAY_USER')
|
||||||
|
password = project.hasProperty('BINTRAY_KEY') ? project.property('BINTRAY_KEY') : System.getenv('BINTRAY_KEY')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@@ -1,6 +1,7 @@
|
|||||||
plugins {
|
plugins {
|
||||||
id "org.jetbrains.kotlin.multiplatform"
|
id "org.jetbrains.kotlin.multiplatform"
|
||||||
id "org.jetbrains.kotlin.plugin.serialization"
|
id "org.jetbrains.kotlin.plugin.serialization"
|
||||||
|
id "com.android.library"
|
||||||
}
|
}
|
||||||
|
|
||||||
apply from: "$mppProjectWithSerializationPresetPath"
|
apply from: "$mppProjectWithSerializationPresetPath"
|
||||||
@@ -15,5 +16,13 @@ kotlin {
|
|||||||
api "com.benasher44:uuid:$uuidVersion"
|
api "com.benasher44:uuid:$uuidVersion"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
androidMain {
|
||||||
|
dependencies {
|
||||||
|
api "androidx.core:core-ktx:$core_ktx_version"
|
||||||
|
api internalProject("micro_utils.common")
|
||||||
|
api internalProject("micro_utils.coroutines")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -0,0 +1,21 @@
|
|||||||
|
package dev.inmo.micro_utils.repos
|
||||||
|
|
||||||
|
interface MapperRepo<FromKey, FromValue, ToKey, ToValue> {
|
||||||
|
suspend fun FromKey.toOutKey() = this as ToKey
|
||||||
|
suspend fun FromValue.toOutValue() = this as ToValue
|
||||||
|
|
||||||
|
suspend fun ToKey.toInnerKey() = this as FromKey
|
||||||
|
suspend fun ToValue.toInnerValue() = this as FromValue
|
||||||
|
}
|
||||||
|
|
||||||
|
inline fun <reified FromKey, reified FromValue, reified ToKey, reified ToValue> mapper(
|
||||||
|
crossinline keyFromToTo: suspend FromKey.() -> ToKey = { this as ToKey },
|
||||||
|
crossinline valueFromToTo: suspend FromValue.() -> ToValue = { this as ToValue },
|
||||||
|
crossinline keyToToFrom: suspend ToKey.() -> FromKey = { this as FromKey },
|
||||||
|
crossinline valueToToFrom: suspend ToValue.() -> FromValue = { this as FromValue },
|
||||||
|
) = object : MapperRepo<FromKey, FromValue, ToKey, ToValue> {
|
||||||
|
override suspend fun FromKey.toOutKey(): ToKey = keyFromToTo()
|
||||||
|
override suspend fun FromValue.toOutValue(): ToValue = valueFromToTo()
|
||||||
|
override suspend fun ToKey.toInnerKey(): FromKey = keyToToFrom()
|
||||||
|
override suspend fun ToValue.toInnerValue(): FromValue = valueToToFrom()
|
||||||
|
}
|
@@ -6,6 +6,7 @@ import kotlinx.coroutines.flow.Flow
|
|||||||
interface ReadOneToManyKeyValueRepo<Key, Value> : Repo {
|
interface ReadOneToManyKeyValueRepo<Key, Value> : Repo {
|
||||||
suspend fun get(k: Key, pagination: Pagination, reversed: Boolean = false): PaginationResult<Value>
|
suspend fun get(k: Key, pagination: Pagination, reversed: Boolean = false): PaginationResult<Value>
|
||||||
suspend fun keys(pagination: Pagination, reversed: Boolean = false): PaginationResult<Key>
|
suspend fun keys(pagination: Pagination, reversed: Boolean = false): PaginationResult<Key>
|
||||||
|
suspend fun keys(v: Value, pagination: Pagination, reversed: Boolean = false): PaginationResult<Key>
|
||||||
suspend fun contains(k: Key): Boolean
|
suspend fun contains(k: Key): Boolean
|
||||||
suspend fun contains(k: Key, v: Value): Boolean
|
suspend fun contains(k: Key, v: Value): Boolean
|
||||||
suspend fun count(k: Key): Long
|
suspend fun count(k: Key): Long
|
||||||
@@ -35,59 +36,72 @@ interface ReadOneToManyKeyValueRepo<Key, Value> : Repo {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@Deprecated("Renamed", ReplaceWith("ReadOneToManyKeyValueRepo", "dev.inmo.micro_utils.repos.ReadOneToManyKeyValueRepo"))
|
|
||||||
typealias OneToManyReadKeyValueRepo<Key, Value> = ReadOneToManyKeyValueRepo<Key, Value>
|
|
||||||
|
|
||||||
interface WriteOneToManyKeyValueRepo<Key, Value> : Repo {
|
interface WriteOneToManyKeyValueRepo<Key, Value> : Repo {
|
||||||
val onNewValue: Flow<Pair<Key, Value>>
|
val onNewValue: Flow<Pair<Key, Value>>
|
||||||
val onValueRemoved: Flow<Pair<Key, Value>>
|
val onValueRemoved: Flow<Pair<Key, Value>>
|
||||||
val onDataCleared: Flow<Key>
|
val onDataCleared: Flow<Key>
|
||||||
|
|
||||||
suspend fun add(toAdd: Map<Key, List<Value>>) = toAdd.forEach { (k, values) ->
|
suspend fun add(toAdd: Map<Key, List<Value>>)
|
||||||
values.forEach { v ->
|
|
||||||
add(k, v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@Deprecated("Will be extracted as extension for other add method")
|
|
||||||
suspend fun add(k: Key, v: Value)
|
|
||||||
|
|
||||||
suspend fun remove(toRemove: Map<Key, List<Value>>) = toRemove.forEach { (k, values) ->
|
suspend fun remove(toRemove: Map<Key, List<Value>>)
|
||||||
values.forEach { v ->
|
|
||||||
remove(k, v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@Deprecated("Will be extracted as extension for other remove method")
|
|
||||||
suspend fun remove(k: Key, v: Value)
|
|
||||||
|
|
||||||
suspend fun clear(k: Key)
|
suspend fun clear(k: Key)
|
||||||
|
|
||||||
|
suspend fun set(toSet: Map<Key, List<Value>>) {
|
||||||
|
toSet.keys.forEach { key -> clear(key) }
|
||||||
|
add(toSet)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@Deprecated("Renamed", ReplaceWith("WriteOneToManyKeyValueRepo", "dev.inmo.micro_utils.repos.WriteOneToManyKeyValueRepo"))
|
|
||||||
typealias OneToManyWriteKeyValueRepo<Key, Value> = WriteOneToManyKeyValueRepo<Key, Value>
|
suspend inline fun <Key, Value, REPO : WriteOneToManyKeyValueRepo<Key, Value>> REPO.add(
|
||||||
|
keysAndValues: List<Pair<Key, List<Value>>>
|
||||||
|
) = add(keysAndValues.toMap())
|
||||||
|
|
||||||
|
suspend inline fun <Key, Value, REPO : WriteOneToManyKeyValueRepo<Key, Value>> REPO.add(
|
||||||
|
vararg keysAndValues: Pair<Key, List<Value>>
|
||||||
|
) = add(keysAndValues.toMap())
|
||||||
|
|
||||||
|
suspend inline fun <Key, Value> WriteOneToManyKeyValueRepo<Key, Value>.add(
|
||||||
|
k: Key, v: List<Value>
|
||||||
|
) = add(mapOf(k to v))
|
||||||
|
|
||||||
|
suspend inline fun <Key, Value> WriteOneToManyKeyValueRepo<Key, Value>.add(
|
||||||
|
k: Key, vararg v: Value
|
||||||
|
) = add(k, v.toList())
|
||||||
|
|
||||||
|
suspend inline fun <Key, Value, REPO : WriteOneToManyKeyValueRepo<Key, Value>> REPO.set(
|
||||||
|
keysAndValues: List<Pair<Key, List<Value>>>
|
||||||
|
) = set(keysAndValues.toMap())
|
||||||
|
|
||||||
|
suspend inline fun <Key, Value, REPO : WriteOneToManyKeyValueRepo<Key, Value>> REPO.set(
|
||||||
|
vararg keysAndValues: Pair<Key, List<Value>>
|
||||||
|
) = set(keysAndValues.toMap())
|
||||||
|
|
||||||
|
suspend inline fun <Key, Value> WriteOneToManyKeyValueRepo<Key, Value>.set(
|
||||||
|
k: Key, v: List<Value>
|
||||||
|
) = set(mapOf(k to v))
|
||||||
|
|
||||||
|
suspend inline fun <Key, Value> WriteOneToManyKeyValueRepo<Key, Value>.set(
|
||||||
|
k: Key, vararg v: Value
|
||||||
|
) = set(k, v.toList())
|
||||||
|
|
||||||
interface OneToManyKeyValueRepo<Key, Value> : ReadOneToManyKeyValueRepo<Key, Value>, WriteOneToManyKeyValueRepo<Key, Value>
|
interface OneToManyKeyValueRepo<Key, Value> : ReadOneToManyKeyValueRepo<Key, Value>, WriteOneToManyKeyValueRepo<Key, Value>
|
||||||
|
|
||||||
suspend inline fun <Key, Value, REPO : WriteOneToManyKeyValueRepo<Key, Value>> REPO.add(
|
suspend inline fun <Key, Value> WriteOneToManyKeyValueRepo<Key, Value>.remove(
|
||||||
k: Key,
|
|
||||||
vararg v: Value
|
|
||||||
) = add(mapOf(k to v.toList()))
|
|
||||||
|
|
||||||
suspend inline fun <Key, Value, REPO : WriteOneToManyKeyValueRepo<Key, Value>> REPO.add(
|
|
||||||
keysAndValues: List<Pair<Key, List<Value>>>
|
|
||||||
) = add(keysAndValues.toMap())
|
|
||||||
|
|
||||||
suspend inline fun <Key, Value, REPO : WriteOneToManyKeyValueRepo<Key, Value>> REPO.add(
|
|
||||||
vararg keysAndValues: Pair<Key, List<Value>>
|
|
||||||
) = add(keysAndValues.toMap())
|
|
||||||
|
|
||||||
suspend inline fun <Key, Value, REPO : WriteOneToManyKeyValueRepo<Key, Value>> REPO.remove(
|
|
||||||
k: Key,
|
|
||||||
vararg v: Value
|
|
||||||
) = remove(mapOf(k to v.toList()))
|
|
||||||
|
|
||||||
suspend inline fun <Key, Value, REPO : WriteOneToManyKeyValueRepo<Key, Value>> REPO.remove(
|
|
||||||
keysAndValues: List<Pair<Key, List<Value>>>
|
keysAndValues: List<Pair<Key, List<Value>>>
|
||||||
) = remove(keysAndValues.toMap())
|
) = remove(keysAndValues.toMap())
|
||||||
|
|
||||||
suspend inline fun <Key, Value, REPO : WriteOneToManyKeyValueRepo<Key, Value>> REPO.remove(
|
suspend inline fun <Key, Value> WriteOneToManyKeyValueRepo<Key, Value>.remove(
|
||||||
vararg keysAndValues: Pair<Key, List<Value>>
|
vararg keysAndValues: Pair<Key, List<Value>>
|
||||||
) = remove(keysAndValues.toMap())
|
) = remove(keysAndValues.toMap())
|
||||||
|
|
||||||
|
suspend inline fun <Key, Value> WriteOneToManyKeyValueRepo<Key, Value>.remove(
|
||||||
|
k: Key,
|
||||||
|
v: List<Value>
|
||||||
|
) = remove(mapOf(k to v))
|
||||||
|
|
||||||
|
suspend inline fun <Key, Value> WriteOneToManyKeyValueRepo<Key, Value>.remove(
|
||||||
|
k: Key,
|
||||||
|
vararg v: Value
|
||||||
|
) = remove(k, v.toList())
|
||||||
|
@@ -8,6 +8,7 @@ interface ReadStandardKeyValueRepo<Key, Value> : Repo {
|
|||||||
suspend fun get(k: Key): Value?
|
suspend fun get(k: Key): Value?
|
||||||
suspend fun values(pagination: Pagination, reversed: Boolean = false): PaginationResult<Value>
|
suspend fun values(pagination: Pagination, reversed: Boolean = false): PaginationResult<Value>
|
||||||
suspend fun keys(pagination: Pagination, reversed: Boolean = false): PaginationResult<Key>
|
suspend fun keys(pagination: Pagination, reversed: Boolean = false): PaginationResult<Key>
|
||||||
|
suspend fun keys(v: Value, pagination: Pagination, reversed: Boolean = false): PaginationResult<Key>
|
||||||
suspend fun contains(key: Key): Boolean
|
suspend fun contains(key: Key): Boolean
|
||||||
suspend fun count(): Long
|
suspend fun count(): Long
|
||||||
}
|
}
|
||||||
@@ -16,8 +17,20 @@ interface WriteStandardKeyValueRepo<Key, Value> : Repo {
|
|||||||
val onNewValue: Flow<Pair<Key, Value>>
|
val onNewValue: Flow<Pair<Key, Value>>
|
||||||
val onValueRemoved: Flow<Key>
|
val onValueRemoved: Flow<Key>
|
||||||
|
|
||||||
suspend fun set(k: Key, v: Value)
|
suspend fun set(toSet: Map<Key, Value>)
|
||||||
suspend fun unset(k: Key)
|
suspend fun unset(toUnset: List<Key>)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
suspend inline fun <Key, Value> WriteStandardKeyValueRepo<Key, Value>.set(
|
||||||
|
vararg toSet: Pair<Key, Value>
|
||||||
|
) = set(toSet.toMap())
|
||||||
|
|
||||||
|
suspend inline fun <Key, Value> WriteStandardKeyValueRepo<Key, Value>.set(
|
||||||
|
k: Key, v: Value
|
||||||
|
) = set(k to v)
|
||||||
|
|
||||||
|
suspend inline fun <Key, Value> WriteStandardKeyValueRepo<Key, Value>.unset(
|
||||||
|
vararg k: Key
|
||||||
|
) = unset(k.toList())
|
||||||
|
|
||||||
interface StandardKeyValueRepo<Key, Value> : ReadStandardKeyValueRepo<Key, Value>, WriteStandardKeyValueRepo<Key, Value>
|
interface StandardKeyValueRepo<Key, Value> : ReadStandardKeyValueRepo<Key, Value>, WriteStandardKeyValueRepo<Key, Value>
|
@@ -0,0 +1,146 @@
|
|||||||
|
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.repos.*
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import kotlinx.coroutines.flow.map
|
||||||
|
|
||||||
|
open class MapperReadStandardKeyValueRepo<FromKey, FromValue, ToKey, ToValue>(
|
||||||
|
private val to: ReadStandardKeyValueRepo<ToKey, ToValue>,
|
||||||
|
mapper: MapperRepo<FromKey, FromValue, ToKey, ToValue>
|
||||||
|
) : ReadStandardKeyValueRepo<FromKey, FromValue>, MapperRepo<FromKey, FromValue, ToKey, ToValue> by mapper {
|
||||||
|
override suspend fun get(k: FromKey): FromValue? = to.get(
|
||||||
|
k.toOutKey()
|
||||||
|
) ?.toInnerValue()
|
||||||
|
|
||||||
|
override suspend fun values(
|
||||||
|
pagination: Pagination,
|
||||||
|
reversed: Boolean
|
||||||
|
): PaginationResult<FromValue> = to.values(
|
||||||
|
pagination,
|
||||||
|
reversed
|
||||||
|
).let {
|
||||||
|
PaginationResult(
|
||||||
|
it.page,
|
||||||
|
it.pagesNumber,
|
||||||
|
it.results.map { it.toInnerValue() },
|
||||||
|
it.size
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun keys(
|
||||||
|
pagination: Pagination,
|
||||||
|
reversed: Boolean
|
||||||
|
): PaginationResult<FromKey> = to.keys(
|
||||||
|
pagination,
|
||||||
|
reversed
|
||||||
|
).let {
|
||||||
|
PaginationResult(
|
||||||
|
it.page,
|
||||||
|
it.pagesNumber,
|
||||||
|
it.results.map { it.toInnerKey() },
|
||||||
|
it.size
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun keys(
|
||||||
|
v: FromValue,
|
||||||
|
pagination: Pagination,
|
||||||
|
reversed: Boolean
|
||||||
|
): PaginationResult<FromKey> = to.keys(
|
||||||
|
v.toOutValue(),
|
||||||
|
pagination,
|
||||||
|
reversed
|
||||||
|
).let {
|
||||||
|
PaginationResult(
|
||||||
|
it.page,
|
||||||
|
it.pagesNumber,
|
||||||
|
it.results.map { it.toInnerKey() },
|
||||||
|
it.size
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun contains(key: FromKey): Boolean = to.contains(
|
||||||
|
key.toOutKey()
|
||||||
|
)
|
||||||
|
|
||||||
|
override suspend fun count(): Long = to.count()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Suppress("NOTHING_TO_INLINE")
|
||||||
|
inline fun <FromKey, FromValue, ToKey, ToValue> ReadStandardKeyValueRepo<ToKey, ToValue>.withMapper(
|
||||||
|
mapper: MapperRepo<FromKey, FromValue, ToKey, ToValue>
|
||||||
|
): ReadStandardKeyValueRepo<FromKey, FromValue> = MapperReadStandardKeyValueRepo(this, mapper)
|
||||||
|
|
||||||
|
@Suppress("NOTHING_TO_INLINE")
|
||||||
|
inline fun <reified FromKey, reified FromValue, reified ToKey, reified ToValue> ReadStandardKeyValueRepo<ToKey, ToValue>.withMapper(
|
||||||
|
crossinline keyFromToTo: suspend FromKey.() -> ToKey = { this as ToKey },
|
||||||
|
crossinline valueFromToTo: suspend FromValue.() -> ToValue = { this as ToValue },
|
||||||
|
crossinline keyToToFrom: suspend ToKey.() -> FromKey = { this as FromKey },
|
||||||
|
crossinline valueToToFrom: suspend ToValue.() -> FromValue = { this as FromValue },
|
||||||
|
): ReadStandardKeyValueRepo<FromKey, FromValue> = withMapper(
|
||||||
|
mapper(keyFromToTo, valueFromToTo, keyToToFrom, valueToToFrom)
|
||||||
|
)
|
||||||
|
|
||||||
|
open class MapperWriteStandardKeyValueRepo<FromKey, FromValue, ToKey, ToValue>(
|
||||||
|
private val to: WriteStandardKeyValueRepo<ToKey, ToValue>,
|
||||||
|
mapper: MapperRepo<FromKey, FromValue, ToKey, ToValue>
|
||||||
|
) : WriteStandardKeyValueRepo<FromKey, FromValue>, MapperRepo<FromKey, FromValue, ToKey, ToValue> by mapper {
|
||||||
|
override val onNewValue: Flow<Pair<FromKey, FromValue>> = to.onNewValue.map { (k, v) ->
|
||||||
|
k.toInnerKey() to v.toInnerValue()
|
||||||
|
}
|
||||||
|
override val onValueRemoved: Flow<FromKey> = to.onValueRemoved.map { k ->
|
||||||
|
k.toInnerKey()
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun set(toSet: Map<FromKey, FromValue>) = to.set(
|
||||||
|
toSet.map { (k, v) ->
|
||||||
|
k.toOutKey() to v.toOutValue()
|
||||||
|
}.toMap()
|
||||||
|
)
|
||||||
|
|
||||||
|
override suspend fun unset(toUnset: List<FromKey>) = to.unset(
|
||||||
|
toUnset.map { k ->
|
||||||
|
k.toOutKey()
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Suppress("NOTHING_TO_INLINE")
|
||||||
|
inline fun <FromKey, FromValue, ToKey, ToValue> WriteStandardKeyValueRepo<ToKey, ToValue>.withMapper(
|
||||||
|
mapper: MapperRepo<FromKey, FromValue, ToKey, ToValue>
|
||||||
|
): WriteStandardKeyValueRepo<FromKey, FromValue> = MapperWriteStandardKeyValueRepo(this, mapper)
|
||||||
|
|
||||||
|
@Suppress("NOTHING_TO_INLINE")
|
||||||
|
inline fun <reified FromKey, reified FromValue, reified ToKey, reified ToValue> WriteStandardKeyValueRepo<ToKey, ToValue>.withMapper(
|
||||||
|
crossinline keyFromToTo: suspend FromKey.() -> ToKey = { this as ToKey },
|
||||||
|
crossinline valueFromToTo: suspend FromValue.() -> ToValue = { this as ToValue },
|
||||||
|
crossinline keyToToFrom: suspend ToKey.() -> FromKey = { this as FromKey },
|
||||||
|
crossinline valueToToFrom: suspend ToValue.() -> FromValue = { this as FromValue },
|
||||||
|
): WriteStandardKeyValueRepo<FromKey, FromValue> = withMapper(
|
||||||
|
mapper(keyFromToTo, valueFromToTo, keyToToFrom, valueToToFrom)
|
||||||
|
)
|
||||||
|
|
||||||
|
open class MapperStandardKeyValueRepo<FromKey, FromValue, ToKey, ToValue>(
|
||||||
|
private val to: StandardKeyValueRepo<ToKey, ToValue>,
|
||||||
|
mapper: MapperRepo<FromKey, FromValue, ToKey, ToValue>
|
||||||
|
) : StandardKeyValueRepo<FromKey, FromValue>,
|
||||||
|
MapperRepo<FromKey, FromValue, ToKey, ToValue> by mapper,
|
||||||
|
ReadStandardKeyValueRepo<FromKey, FromValue> by MapperReadStandardKeyValueRepo(to, mapper),
|
||||||
|
WriteStandardKeyValueRepo<FromKey, FromValue> by MapperWriteStandardKeyValueRepo(to, mapper)
|
||||||
|
|
||||||
|
@Suppress("NOTHING_TO_INLINE")
|
||||||
|
inline fun <FromKey, FromValue, ToKey, ToValue> StandardKeyValueRepo<ToKey, ToValue>.withMapper(
|
||||||
|
mapper: MapperRepo<FromKey, FromValue, ToKey, ToValue>
|
||||||
|
): StandardKeyValueRepo<FromKey, FromValue> = MapperStandardKeyValueRepo(this, mapper)
|
||||||
|
|
||||||
|
@Suppress("NOTHING_TO_INLINE")
|
||||||
|
inline fun <reified FromKey, reified FromValue, reified ToKey, reified ToValue> StandardKeyValueRepo<ToKey, ToValue>.withMapper(
|
||||||
|
crossinline keyFromToTo: suspend FromKey.() -> ToKey = { this as ToKey },
|
||||||
|
crossinline valueFromToTo: suspend FromValue.() -> ToValue = { this as ToValue },
|
||||||
|
crossinline keyToToFrom: suspend ToKey.() -> FromKey = { this as FromKey },
|
||||||
|
crossinline valueToToFrom: suspend ToValue.() -> FromValue = { this as FromValue },
|
||||||
|
): StandardKeyValueRepo<FromKey, FromValue> = withMapper(
|
||||||
|
mapper(keyFromToTo, valueFromToTo, keyToToFrom, valueToToFrom)
|
||||||
|
)
|
@@ -0,0 +1,155 @@
|
|||||||
|
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.repos.*
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import kotlinx.coroutines.flow.map
|
||||||
|
|
||||||
|
open class MapperReadOneToManyKeyValueRepo<FromKey, FromValue, ToKey, ToValue>(
|
||||||
|
private val to: ReadOneToManyKeyValueRepo<ToKey, ToValue>,
|
||||||
|
mapper: MapperRepo<FromKey, FromValue, ToKey, ToValue>
|
||||||
|
) : ReadOneToManyKeyValueRepo<FromKey, FromValue>, MapperRepo<FromKey, FromValue, ToKey, ToValue> by mapper {
|
||||||
|
override suspend fun get(
|
||||||
|
k: FromKey,
|
||||||
|
pagination: Pagination,
|
||||||
|
reversed: Boolean
|
||||||
|
): PaginationResult<FromValue> = to.get(
|
||||||
|
k.toOutKey(),
|
||||||
|
pagination,
|
||||||
|
reversed
|
||||||
|
).let {
|
||||||
|
PaginationResult(
|
||||||
|
it.page,
|
||||||
|
it.pagesNumber,
|
||||||
|
it.results.map { it.toInnerValue() },
|
||||||
|
it.size
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun keys(
|
||||||
|
pagination: Pagination,
|
||||||
|
reversed: Boolean
|
||||||
|
): PaginationResult<FromKey> = to.keys(
|
||||||
|
pagination,
|
||||||
|
reversed
|
||||||
|
).let {
|
||||||
|
PaginationResult(
|
||||||
|
it.page,
|
||||||
|
it.pagesNumber,
|
||||||
|
it.results.map { it.toInnerKey() },
|
||||||
|
it.size
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun keys(
|
||||||
|
v: FromValue,
|
||||||
|
pagination: Pagination,
|
||||||
|
reversed: Boolean
|
||||||
|
): PaginationResult<FromKey> = to.keys(
|
||||||
|
v.toOutValue(),
|
||||||
|
pagination,
|
||||||
|
reversed
|
||||||
|
).let {
|
||||||
|
PaginationResult(
|
||||||
|
it.page,
|
||||||
|
it.pagesNumber,
|
||||||
|
it.results.map { it.toInnerKey() },
|
||||||
|
it.size
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun contains(k: FromKey): Boolean = to.contains(k.toOutKey())
|
||||||
|
override suspend fun contains(k: FromKey, v: FromValue): Boolean = to.contains(k.toOutKey(), v.toOutValue())
|
||||||
|
|
||||||
|
override suspend fun count(): Long = to.count()
|
||||||
|
override suspend fun count(k: FromKey): Long = to.count(k.toOutKey())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Suppress("NOTHING_TO_INLINE")
|
||||||
|
inline fun <FromKey, FromValue, ToKey, ToValue> ReadOneToManyKeyValueRepo<ToKey, ToValue>.withMapper(
|
||||||
|
mapper: MapperRepo<FromKey, FromValue, ToKey, ToValue>
|
||||||
|
): ReadOneToManyKeyValueRepo<FromKey, FromValue> = MapperReadOneToManyKeyValueRepo(this, mapper)
|
||||||
|
|
||||||
|
@Suppress("NOTHING_TO_INLINE")
|
||||||
|
inline fun <reified FromKey, reified FromValue, reified ToKey, reified ToValue> ReadOneToManyKeyValueRepo<ToKey, ToValue>.withMapper(
|
||||||
|
crossinline keyFromToTo: suspend FromKey.() -> ToKey = { this as ToKey },
|
||||||
|
crossinline valueFromToTo: suspend FromValue.() -> ToValue = { this as ToValue },
|
||||||
|
crossinline keyToToFrom: suspend ToKey.() -> FromKey = { this as FromKey },
|
||||||
|
crossinline valueToToFrom: suspend ToValue.() -> FromValue = { this as FromValue },
|
||||||
|
): ReadOneToManyKeyValueRepo<FromKey, FromValue> = withMapper(
|
||||||
|
mapper(keyFromToTo, valueFromToTo, keyToToFrom, valueToToFrom)
|
||||||
|
)
|
||||||
|
|
||||||
|
open class MapperWriteOneToManyKeyValueRepo<FromKey, FromValue, ToKey, ToValue>(
|
||||||
|
private val to: WriteOneToManyKeyValueRepo<ToKey, ToValue>,
|
||||||
|
mapper: MapperRepo<FromKey, FromValue, ToKey, ToValue>
|
||||||
|
) : WriteOneToManyKeyValueRepo<FromKey, FromValue>, MapperRepo<FromKey, FromValue, ToKey, ToValue> by mapper {
|
||||||
|
override val onNewValue: Flow<Pair<FromKey, FromValue>> = to.onNewValue.map { (k, v) ->
|
||||||
|
k.toInnerKey() to v.toInnerValue()
|
||||||
|
}
|
||||||
|
override val onValueRemoved: Flow<Pair<FromKey, FromValue>> = to.onValueRemoved.map { (k, v) ->
|
||||||
|
k.toInnerKey() to v.toInnerValue()
|
||||||
|
}
|
||||||
|
override val onDataCleared: Flow<FromKey> = to.onDataCleared.map { k ->
|
||||||
|
k.toInnerKey()
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun add(toAdd: Map<FromKey, List<FromValue>>) = to.add(
|
||||||
|
toAdd.map { (k, v) ->
|
||||||
|
k.toOutKey() to v.map { it.toOutValue() }
|
||||||
|
}.toMap()
|
||||||
|
)
|
||||||
|
|
||||||
|
override suspend fun remove(toRemove: Map<FromKey, List<FromValue>>) = to.remove(
|
||||||
|
toRemove.map { (k, v) ->
|
||||||
|
k.toOutKey() to v.map { it.toOutValue() }
|
||||||
|
}.toMap()
|
||||||
|
)
|
||||||
|
|
||||||
|
override suspend fun set(toSet: Map<FromKey, List<FromValue>>) {
|
||||||
|
to.set(
|
||||||
|
toSet.map { (k, vs) -> k.toOutKey() to vs.map { v -> v.toOutValue() } }.toMap()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun clear(k: FromKey) = to.clear(k.toOutKey())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Suppress("NOTHING_TO_INLINE")
|
||||||
|
inline fun <FromKey, FromValue, ToKey, ToValue> WriteOneToManyKeyValueRepo<ToKey, ToValue>.withMapper(
|
||||||
|
mapper: MapperRepo<FromKey, FromValue, ToKey, ToValue>
|
||||||
|
): WriteOneToManyKeyValueRepo<FromKey, FromValue> = MapperWriteOneToManyKeyValueRepo(this, mapper)
|
||||||
|
|
||||||
|
@Suppress("NOTHING_TO_INLINE")
|
||||||
|
inline fun <reified FromKey, reified FromValue, reified ToKey, reified ToValue> WriteOneToManyKeyValueRepo<ToKey, ToValue>.withMapper(
|
||||||
|
crossinline keyFromToTo: suspend FromKey.() -> ToKey = { this as ToKey },
|
||||||
|
crossinline valueFromToTo: suspend FromValue.() -> ToValue = { this as ToValue },
|
||||||
|
crossinline keyToToFrom: suspend ToKey.() -> FromKey = { this as FromKey },
|
||||||
|
crossinline valueToToFrom: suspend ToValue.() -> FromValue = { this as FromValue },
|
||||||
|
): WriteOneToManyKeyValueRepo<FromKey, FromValue> = withMapper(
|
||||||
|
mapper(keyFromToTo, valueFromToTo, keyToToFrom, valueToToFrom)
|
||||||
|
)
|
||||||
|
|
||||||
|
open class MapperOneToManyKeyValueRepo<FromKey, FromValue, ToKey, ToValue>(
|
||||||
|
private val to: OneToManyKeyValueRepo<ToKey, ToValue>,
|
||||||
|
mapper: MapperRepo<FromKey, FromValue, ToKey, ToValue>
|
||||||
|
) : OneToManyKeyValueRepo<FromKey, FromValue>,
|
||||||
|
MapperRepo<FromKey, FromValue, ToKey, ToValue> by mapper,
|
||||||
|
ReadOneToManyKeyValueRepo<FromKey, FromValue> by MapperReadOneToManyKeyValueRepo(to, mapper),
|
||||||
|
WriteOneToManyKeyValueRepo<FromKey, FromValue> by MapperWriteOneToManyKeyValueRepo(to, mapper)
|
||||||
|
|
||||||
|
@Suppress("NOTHING_TO_INLINE")
|
||||||
|
inline fun <FromKey, FromValue, ToKey, ToValue> OneToManyKeyValueRepo<ToKey, ToValue>.withMapper(
|
||||||
|
mapper: MapperRepo<FromKey, FromValue, ToKey, ToValue>
|
||||||
|
): OneToManyKeyValueRepo<FromKey, FromValue> = MapperOneToManyKeyValueRepo(this, mapper)
|
||||||
|
|
||||||
|
@Suppress("NOTHING_TO_INLINE")
|
||||||
|
inline fun <reified FromKey, reified FromValue, reified ToKey, reified ToValue> OneToManyKeyValueRepo<ToKey, ToValue>.withMapper(
|
||||||
|
crossinline keyFromToTo: suspend FromKey.() -> ToKey = { this as ToKey },
|
||||||
|
crossinline valueFromToTo: suspend FromValue.() -> ToValue = { this as ToValue },
|
||||||
|
crossinline keyToToFrom: suspend ToKey.() -> FromKey = { this as FromKey },
|
||||||
|
crossinline valueToToFrom: suspend ToValue.() -> FromValue = { this as FromValue },
|
||||||
|
): OneToManyKeyValueRepo<FromKey, FromValue> = withMapper(
|
||||||
|
mapper(keyFromToTo, valueFromToTo, keyToToFrom, valueToToFrom)
|
||||||
|
)
|
@@ -0,0 +1,13 @@
|
|||||||
|
package dev.inmo.micro_utils.repos.versions
|
||||||
|
|
||||||
|
import dev.inmo.micro_utils.repos.StandardKeyValueRepo
|
||||||
|
import dev.inmo.micro_utils.repos.set
|
||||||
|
|
||||||
|
class KeyValueBasedVersionsRepoProxy<T>(
|
||||||
|
private val keyValueStore: StandardKeyValueRepo<String, Int>,
|
||||||
|
override val database: T
|
||||||
|
) : StandardVersionsRepoProxy<T> {
|
||||||
|
override suspend fun getTableVersion(tableName: String): Int? = keyValueStore.get(tableName)
|
||||||
|
|
||||||
|
override suspend fun updateTableVersion(tableName: String, version: Int) { keyValueStore.set(tableName, version) }
|
||||||
|
}
|
@@ -0,0 +1,36 @@
|
|||||||
|
package dev.inmo.micro_utils.repos.versions
|
||||||
|
|
||||||
|
import dev.inmo.micro_utils.repos.Repo
|
||||||
|
|
||||||
|
interface StandardVersionsRepoProxy<T> : Repo {
|
||||||
|
val database: T
|
||||||
|
|
||||||
|
suspend fun getTableVersion(tableName: String): Int?
|
||||||
|
suspend fun updateTableVersion(tableName: String, version: Int)
|
||||||
|
}
|
||||||
|
|
||||||
|
class StandardVersionsRepo<T>(
|
||||||
|
private val proxy: StandardVersionsRepoProxy<T>
|
||||||
|
) : VersionsRepo<T> {
|
||||||
|
override suspend fun setTableVersion(
|
||||||
|
tableName: String,
|
||||||
|
version: Int,
|
||||||
|
onCreate: suspend T.() -> Unit,
|
||||||
|
onUpdate: suspend T.(from: Int, to: Int) -> Unit
|
||||||
|
) {
|
||||||
|
var savedVersion = proxy.getTableVersion(tableName)
|
||||||
|
if (savedVersion == null) {
|
||||||
|
proxy.database.onCreate()
|
||||||
|
proxy.updateTableVersion(tableName, version)
|
||||||
|
} else {
|
||||||
|
while (savedVersion != null && savedVersion < version) {
|
||||||
|
val newVersion = savedVersion + 1
|
||||||
|
|
||||||
|
proxy.database.onUpdate(savedVersion, newVersion)
|
||||||
|
|
||||||
|
proxy.updateTableVersion(tableName, newVersion)
|
||||||
|
savedVersion = newVersion
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,31 @@
|
|||||||
|
package dev.inmo.micro_utils.repos.versions
|
||||||
|
|
||||||
|
import dev.inmo.micro_utils.repos.Repo
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This interface has been created due to requirement to work with different versions of databases and make some
|
||||||
|
* migrations between versions
|
||||||
|
*
|
||||||
|
* @param T It is a type of database, which will be used by this repo to retrieve current table version and update it
|
||||||
|
*/
|
||||||
|
interface VersionsRepo<T> : Repo {
|
||||||
|
/**
|
||||||
|
* By default, instance of this interface will check that version of table with name [tableName] is less than
|
||||||
|
* [version] or is absent
|
||||||
|
*
|
||||||
|
* * In case if [tableName] didn't found, will be called [onCreate] and version of table will be set up to [version]
|
||||||
|
* * In case if [tableName] have version less than parameter [version], it will increase version one-by-one
|
||||||
|
* until database version will be equal to [version]
|
||||||
|
*
|
||||||
|
* @param version Current version of table
|
||||||
|
* @param onCreate This callback will be called in case when table have no information about table
|
||||||
|
* @param onUpdate This callback will be called after **iterative** changing of version. It is expected that parameter
|
||||||
|
* "to" will always be greater than "from"
|
||||||
|
*/
|
||||||
|
suspend fun setTableVersion(
|
||||||
|
tableName: String,
|
||||||
|
version: Int,
|
||||||
|
onCreate: suspend T.() -> Unit = {},
|
||||||
|
onUpdate: suspend T.(from: Int, to: Int) -> Unit = { _, _ ->}
|
||||||
|
)
|
||||||
|
}
|
1
repos/common/src/main/AndroidManifest.xml
Normal file
1
repos/common/src/main/AndroidManifest.xml
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<manifest package="dev.inmo.micro_utils.repos.common"/>
|
@@ -0,0 +1,46 @@
|
|||||||
|
package dev.inmo.micro_utils.repos
|
||||||
|
|
||||||
|
sealed class ColumnType(
|
||||||
|
typeName: String,
|
||||||
|
nullable: Boolean
|
||||||
|
) {
|
||||||
|
open val asType: String = "$typeName${if (!nullable) " not null" else ""}"
|
||||||
|
sealed class Text(
|
||||||
|
nullable: Boolean
|
||||||
|
) : ColumnType("text", nullable) {
|
||||||
|
object NULLABLE : Text(true)
|
||||||
|
object NOT_NULLABLE : Text(false)
|
||||||
|
}
|
||||||
|
sealed class Numeric(
|
||||||
|
typeName: String,
|
||||||
|
autoincrement: Boolean = false,
|
||||||
|
primaryKey: Boolean = false,
|
||||||
|
nullable: Boolean = false
|
||||||
|
) : ColumnType(
|
||||||
|
typeName,
|
||||||
|
nullable
|
||||||
|
) {
|
||||||
|
override val asType: String = "${super.asType}${if (primaryKey) " primary key" else ""}${if (autoincrement) " autoincrement" else ""}"
|
||||||
|
|
||||||
|
class INTEGER(
|
||||||
|
autoincrement: Boolean = false,
|
||||||
|
primaryKey: Boolean = false,
|
||||||
|
nullable: Boolean = false
|
||||||
|
) : Numeric(
|
||||||
|
"integer",
|
||||||
|
autoincrement,
|
||||||
|
primaryKey,
|
||||||
|
nullable
|
||||||
|
)
|
||||||
|
class DOUBLE(autoincrement: Boolean = false, primaryKey: Boolean = false, nullable: Boolean = false) : Numeric(
|
||||||
|
"double",
|
||||||
|
autoincrement,
|
||||||
|
primaryKey,
|
||||||
|
nullable
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun toString(): String {
|
||||||
|
return asType
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,8 @@
|
|||||||
|
package dev.inmo.micro_utils.repos
|
||||||
|
|
||||||
|
import androidx.core.content.contentValuesOf
|
||||||
|
|
||||||
|
@Suppress("UNCHECKED_CAST", "SimplifiableCall")
|
||||||
|
fun contentValuesOfNotNull(vararg pairs: Pair<String, Any?>?) = contentValuesOf(
|
||||||
|
*(pairs.filter { it != null } as List<Pair<String, Any?>>).toTypedArray()
|
||||||
|
)
|
@@ -0,0 +1,21 @@
|
|||||||
|
package dev.inmo.micro_utils.repos
|
||||||
|
|
||||||
|
import android.database.Cursor
|
||||||
|
|
||||||
|
inline fun <T> Cursor.map(
|
||||||
|
block: (Cursor) -> T
|
||||||
|
): List<T> {
|
||||||
|
val result = mutableListOf<T>()
|
||||||
|
if (moveToFirst()) {
|
||||||
|
do {
|
||||||
|
result.add(block(this))
|
||||||
|
} while (moveToNext())
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Cursor.firstOrNull(): Cursor? = if (moveToFirst()) {
|
||||||
|
this
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
@@ -0,0 +1,6 @@
|
|||||||
|
package dev.inmo.micro_utils.repos
|
||||||
|
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlin.coroutines.CoroutineContext
|
||||||
|
|
||||||
|
val DatabaseCoroutineContext: CoroutineContext = Dispatchers.IO
|
@@ -0,0 +1,64 @@
|
|||||||
|
package dev.inmo.micro_utils.repos
|
||||||
|
|
||||||
|
import android.database.Cursor
|
||||||
|
import android.database.sqlite.SQLiteDatabase
|
||||||
|
|
||||||
|
fun createTableQuery(
|
||||||
|
tableName: String,
|
||||||
|
vararg columnsToTypes: Pair<String, ColumnType>
|
||||||
|
) = "create table $tableName (${columnsToTypes.joinToString(", ") { "${it.first} ${it.second}" }});"
|
||||||
|
|
||||||
|
fun SQLiteDatabase.createTable(
|
||||||
|
tableName: String,
|
||||||
|
vararg columnsToTypes: Pair<String, ColumnType>,
|
||||||
|
onInit: (SQLiteDatabase.() -> Unit)? = null
|
||||||
|
): Boolean {
|
||||||
|
val existing = rawQuery("SELECT name FROM sqlite_master WHERE type='table' AND name='$tableName'", null).use {
|
||||||
|
it.count > 0
|
||||||
|
}
|
||||||
|
return if (existing) {
|
||||||
|
false
|
||||||
|
// TODO:: add upgrade opportunity
|
||||||
|
} else {
|
||||||
|
execSQL(createTableQuery(tableName, *columnsToTypes))
|
||||||
|
onInit ?.invoke(this)
|
||||||
|
true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Cursor.getString(columnName: String) = getString(
|
||||||
|
getColumnIndex(columnName)
|
||||||
|
)
|
||||||
|
|
||||||
|
fun Cursor.getLong(columnName: String) = getLong(
|
||||||
|
getColumnIndex(columnName)
|
||||||
|
)
|
||||||
|
|
||||||
|
fun Cursor.getInt(columnName: String) = getInt(
|
||||||
|
getColumnIndex(columnName)
|
||||||
|
)
|
||||||
|
|
||||||
|
fun Cursor.getDouble(columnName: String) = getDouble(
|
||||||
|
getColumnIndex(columnName)
|
||||||
|
)
|
||||||
|
|
||||||
|
fun SQLiteDatabase.select(
|
||||||
|
table: String,
|
||||||
|
columns: Array<String>? = null,
|
||||||
|
selection: String? = null,
|
||||||
|
selectionArgs: Array<String>? = null,
|
||||||
|
groupBy: String? = null,
|
||||||
|
having: String? = null,
|
||||||
|
orderBy: String? = null,
|
||||||
|
limit: String? = null
|
||||||
|
) = query(
|
||||||
|
table, columns, selection, selectionArgs, groupBy, having, orderBy, limit
|
||||||
|
)
|
||||||
|
|
||||||
|
fun makePlaceholders(count: Int): String {
|
||||||
|
return (0 until count).joinToString { "?" }
|
||||||
|
}
|
||||||
|
|
||||||
|
fun makeStringPlaceholders(count: Int): String {
|
||||||
|
return (0 until count).joinToString { "\"?\"" }
|
||||||
|
}
|
@@ -0,0 +1,8 @@
|
|||||||
|
package dev.inmo.micro_utils.repos
|
||||||
|
|
||||||
|
import dev.inmo.micro_utils.pagination.Pagination
|
||||||
|
import dev.inmo.micro_utils.pagination.firstIndex
|
||||||
|
|
||||||
|
fun limitClause(size: Long, since: Long? = null) = "${since ?.let { "$it, " } ?: ""}$size"
|
||||||
|
fun limitClause(size: Int, since: Int? = null) = limitClause(size.toLong(), since ?.toLong())
|
||||||
|
fun Pagination.limitClause() = limitClause(size, firstIndex)
|
@@ -0,0 +1,87 @@
|
|||||||
|
package dev.inmo.micro_utils.repos
|
||||||
|
|
||||||
|
import android.database.sqlite.SQLiteDatabase
|
||||||
|
import dev.inmo.micro_utils.coroutines.safely
|
||||||
|
import kotlinx.coroutines.*
|
||||||
|
import kotlinx.coroutines.sync.Mutex
|
||||||
|
import kotlinx.coroutines.sync.withLock
|
||||||
|
import java.util.concurrent.Executors
|
||||||
|
import kotlin.coroutines.CoroutineContext
|
||||||
|
import kotlin.coroutines.coroutineContext
|
||||||
|
|
||||||
|
private object ContextsPool {
|
||||||
|
private val contexts = mutableListOf<CoroutineContext>()
|
||||||
|
private val mutex = Mutex(locked = false)
|
||||||
|
private val freeContexts = mutableListOf<CoroutineContext>()
|
||||||
|
|
||||||
|
suspend fun acquireContext(): CoroutineContext {
|
||||||
|
return mutex.withLock {
|
||||||
|
freeContexts.removeFirstOrNull() ?: Executors.newSingleThreadExecutor().asCoroutineDispatcher().also {
|
||||||
|
contexts.add(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun freeContext(context: CoroutineContext) {
|
||||||
|
return mutex.withLock {
|
||||||
|
if (context in contexts && context !in freeContexts) {
|
||||||
|
freeContexts.add(context)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun <T> use(block: suspend (CoroutineContext) -> T): T = acquireContext().let {
|
||||||
|
try {
|
||||||
|
block(it)
|
||||||
|
} finally {
|
||||||
|
freeContext(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class TransactionContext(
|
||||||
|
val databaseContext: CoroutineContext
|
||||||
|
): CoroutineContext.Element {
|
||||||
|
override val key: CoroutineContext.Key<TransactionContext> = TransactionContext
|
||||||
|
|
||||||
|
companion object : CoroutineContext.Key<TransactionContext>
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun <T> SQLiteDatabase.transaction(block: suspend SQLiteDatabase.() -> T): T {
|
||||||
|
return coroutineContext[TransactionContext] ?.let {
|
||||||
|
withContext(it.databaseContext) {
|
||||||
|
block()
|
||||||
|
}
|
||||||
|
} ?: ContextsPool.use { context ->
|
||||||
|
withContext(TransactionContext(context) + context) {
|
||||||
|
beginTransaction()
|
||||||
|
safely(
|
||||||
|
{
|
||||||
|
endTransaction()
|
||||||
|
throw it
|
||||||
|
}
|
||||||
|
) {
|
||||||
|
block().also {
|
||||||
|
setTransactionSuccessful()
|
||||||
|
endTransaction()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inline fun <T> SQLiteDatabase.inlineTransaction(block: SQLiteDatabase.() -> T): T {
|
||||||
|
return when {
|
||||||
|
inTransaction() -> block()
|
||||||
|
else -> {
|
||||||
|
beginTransaction()
|
||||||
|
try {
|
||||||
|
block().also {
|
||||||
|
setTransactionSuccessful()
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
endTransaction()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,7 @@
|
|||||||
|
package dev.inmo.micro_utils.repos
|
||||||
|
|
||||||
|
val internalId = "_id"
|
||||||
|
val internalIdType = ColumnType.Numeric.INTEGER(
|
||||||
|
autoincrement = true,
|
||||||
|
primaryKey = true
|
||||||
|
)
|
@@ -0,0 +1,55 @@
|
|||||||
|
package dev.inmo.micro_utils.repos
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.database.DatabaseErrorHandler
|
||||||
|
import android.database.sqlite.SQLiteDatabase
|
||||||
|
import android.database.sqlite.SQLiteOpenHelper
|
||||||
|
import dev.inmo.micro_utils.coroutines.safely
|
||||||
|
import dev.inmo.micro_utils.repos.versions.*
|
||||||
|
import kotlin.coroutines.Continuation
|
||||||
|
import kotlin.coroutines.resume
|
||||||
|
import kotlin.coroutines.resumeWithException
|
||||||
|
|
||||||
|
private data class CallbackContinuationPair<T> (
|
||||||
|
val callback: suspend SQLiteDatabase.() -> T,
|
||||||
|
val continuation: Continuation<T>
|
||||||
|
) {
|
||||||
|
suspend fun SQLiteDatabase.execute() {
|
||||||
|
safely(
|
||||||
|
{
|
||||||
|
continuation.resumeWithException(it)
|
||||||
|
}
|
||||||
|
) {
|
||||||
|
continuation.resume(callback())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class StandardSQLHelper(
|
||||||
|
context: Context,
|
||||||
|
name: String,
|
||||||
|
factory: SQLiteDatabase.CursorFactory? = null,
|
||||||
|
version: Int = 1,
|
||||||
|
errorHandler: DatabaseErrorHandler? = null
|
||||||
|
) {
|
||||||
|
val sqlOpenHelper = object : SQLiteOpenHelper(context, name, factory, version, errorHandler) {
|
||||||
|
override fun onCreate(db: SQLiteDatabase?) {}
|
||||||
|
|
||||||
|
override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {}
|
||||||
|
}
|
||||||
|
val versionsRepo: VersionsRepo<SQLiteOpenHelper> by lazy {
|
||||||
|
StandardVersionsRepo(AndroidSQLStandardVersionsRepoProxy(sqlOpenHelper))
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun <T> writableTransaction(block: suspend SQLiteDatabase.() -> T): T = sqlOpenHelper.writableTransaction(block)
|
||||||
|
|
||||||
|
suspend fun <T> readableTransaction(block: suspend SQLiteDatabase.() -> T): T = sqlOpenHelper.readableTransaction(block)
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun <T> SQLiteOpenHelper.writableTransaction(block: suspend SQLiteDatabase.() -> T): T {
|
||||||
|
return writableDatabase.transaction(block)
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun <T> SQLiteOpenHelper.readableTransaction(block: suspend SQLiteDatabase.() -> T): T {
|
||||||
|
return readableDatabase.transaction(block)
|
||||||
|
}
|
@@ -0,0 +1,67 @@
|
|||||||
|
package dev.inmo.micro_utils.repos.crud
|
||||||
|
|
||||||
|
import android.database.Cursor
|
||||||
|
import android.database.sqlite.SQLiteDatabase
|
||||||
|
import dev.inmo.micro_utils.pagination.Pagination
|
||||||
|
import dev.inmo.micro_utils.pagination.PaginationResult
|
||||||
|
import dev.inmo.micro_utils.pagination.createPaginationResult
|
||||||
|
import dev.inmo.micro_utils.repos.*
|
||||||
|
|
||||||
|
val <T> T.asId: String
|
||||||
|
get() = (this as? String) ?: this!!.toString()
|
||||||
|
|
||||||
|
abstract class AbstractAndroidCRUDRepo<ObjectType, IdType>(
|
||||||
|
protected val helper: StandardSQLHelper
|
||||||
|
) : ReadStandardCRUDRepo<ObjectType, IdType> {
|
||||||
|
protected abstract val tableName: String
|
||||||
|
protected abstract val idColumnName: String
|
||||||
|
protected abstract suspend fun Cursor.toObject(): ObjectType
|
||||||
|
protected fun SQLiteDatabase.count(): Long = select(tableName).use {
|
||||||
|
it.count
|
||||||
|
}.toLong()
|
||||||
|
|
||||||
|
override suspend fun contains(id: IdType): Boolean = helper.readableTransaction {
|
||||||
|
select(
|
||||||
|
tableName,
|
||||||
|
null,
|
||||||
|
"$idColumnName=?",
|
||||||
|
arrayOf(id.asId)
|
||||||
|
).use {
|
||||||
|
it.count > 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun getById(id: IdType): ObjectType? = helper.readableTransaction {
|
||||||
|
select(
|
||||||
|
tableName,
|
||||||
|
selection = "$idColumnName=?",
|
||||||
|
selectionArgs = arrayOf(id.asId),
|
||||||
|
limit = limitClause(1)
|
||||||
|
).use { c ->
|
||||||
|
if (c.moveToFirst()) {
|
||||||
|
c.toObject()
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun getByPagination(pagination: Pagination): PaginationResult<ObjectType> {
|
||||||
|
return helper.readableTransaction {
|
||||||
|
select(
|
||||||
|
tableName,
|
||||||
|
limit = pagination.limitClause()
|
||||||
|
).use {
|
||||||
|
if (it.moveToFirst()) {
|
||||||
|
val resultList = mutableListOf(it.toObject())
|
||||||
|
while (it.moveToNext()) {
|
||||||
|
resultList.add(it.toObject())
|
||||||
|
}
|
||||||
|
resultList.createPaginationResult(pagination, count())
|
||||||
|
} else {
|
||||||
|
emptyList<ObjectType>().createPaginationResult(pagination, 0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,104 @@
|
|||||||
|
package dev.inmo.micro_utils.repos.crud
|
||||||
|
|
||||||
|
import android.content.ContentValues
|
||||||
|
import dev.inmo.micro_utils.common.mapNotNullA
|
||||||
|
import dev.inmo.micro_utils.repos.*
|
||||||
|
import kotlinx.coroutines.channels.BroadcastChannel
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import kotlinx.coroutines.flow.asFlow
|
||||||
|
|
||||||
|
abstract class AbstractMutableAndroidCRUDRepo<ObjectType, IdType, InputValueType>(
|
||||||
|
helper: StandardSQLHelper
|
||||||
|
) : WriteStandardCRUDRepo<ObjectType, IdType, InputValueType>,
|
||||||
|
AbstractAndroidCRUDRepo<ObjectType, IdType>(helper) {
|
||||||
|
protected val newObjectsChannel = BroadcastChannel<ObjectType>(64)
|
||||||
|
protected val updateObjectsChannel = BroadcastChannel<ObjectType>(64)
|
||||||
|
protected val deleteObjectsIdsChannel = BroadcastChannel<IdType>(64)
|
||||||
|
override val newObjectsFlow: Flow<ObjectType> = newObjectsChannel.asFlow()
|
||||||
|
override val updatedObjectsFlow: Flow<ObjectType> = updateObjectsChannel.asFlow()
|
||||||
|
override val deletedObjectsIdsFlow: Flow<IdType> = deleteObjectsIdsChannel.asFlow()
|
||||||
|
|
||||||
|
protected abstract suspend fun InputValueType.asContentValues(id: IdType? = null): ContentValues
|
||||||
|
|
||||||
|
override suspend fun create(values: List<InputValueType>): List<ObjectType> {
|
||||||
|
val indexes = helper.writableTransaction {
|
||||||
|
values.map {
|
||||||
|
insert(tableName, null, it.asContentValues())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return helper.readableTransaction {
|
||||||
|
indexes.mapNotNullA { i ->
|
||||||
|
select(
|
||||||
|
tableName,
|
||||||
|
selection = "$internalId=?",
|
||||||
|
selectionArgs = arrayOf(i.toString())
|
||||||
|
).use { c ->
|
||||||
|
if (c.moveToFirst()) {
|
||||||
|
c.toObject()
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}.also {
|
||||||
|
it.forEach {
|
||||||
|
newObjectsChannel.send(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun deleteById(ids: List<IdType>) {
|
||||||
|
val deleted = mutableListOf<IdType>()
|
||||||
|
helper.writableTransaction {
|
||||||
|
ids.forEach { id ->
|
||||||
|
delete(tableName, "$idColumnName=?", arrayOf(id.asId)).also {
|
||||||
|
if (it > 0) {
|
||||||
|
deleted.add(id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
deleted.forEach {
|
||||||
|
deleteObjectsIdsChannel.send(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun update(id: IdType, value: InputValueType): ObjectType? {
|
||||||
|
val asContentValues = value.asContentValues(id)
|
||||||
|
if (asContentValues.keySet().isNotEmpty()) {
|
||||||
|
helper.writableTransaction {
|
||||||
|
update(
|
||||||
|
tableName,
|
||||||
|
asContentValues,
|
||||||
|
"$idColumnName=?",
|
||||||
|
arrayOf(id.asId)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return getById(id) ?.also {
|
||||||
|
updateObjectsChannel.send(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun update(values: List<UpdatedValuePair<IdType, InputValueType>>): List<ObjectType> {
|
||||||
|
helper.writableTransaction {
|
||||||
|
values.forEach { (id, value) ->
|
||||||
|
update(
|
||||||
|
tableName,
|
||||||
|
value.asContentValues(id),
|
||||||
|
"$idColumnName=?",
|
||||||
|
arrayOf(id.asId)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return values.mapNotNullA {
|
||||||
|
getById(it.first)
|
||||||
|
}.also {
|
||||||
|
it.forEach {
|
||||||
|
updateObjectsChannel.send(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun count(): Long = helper.readableTransaction { select(tableName).use { it.count.toLong() } }
|
||||||
|
}
|
@@ -0,0 +1,144 @@
|
|||||||
|
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.utils.paginate
|
||||||
|
import dev.inmo.micro_utils.pagination.utils.reverse
|
||||||
|
import dev.inmo.micro_utils.repos.StandardKeyValueRepo
|
||||||
|
import kotlinx.coroutines.channels.BroadcastChannel
|
||||||
|
import kotlinx.coroutines.channels.Channel
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import kotlinx.coroutines.flow.asFlow
|
||||||
|
|
||||||
|
private val cache = HashMap<String, KeyValueStore<*>>()
|
||||||
|
|
||||||
|
fun <T : Any> Context.keyValueStore(
|
||||||
|
name: String = "default",
|
||||||
|
cacheValues: Boolean = false
|
||||||
|
): StandardKeyValueRepo<String, T> {
|
||||||
|
return cache.getOrPut(name) {
|
||||||
|
KeyValueStore<T>(this, name, cacheValues)
|
||||||
|
} as KeyValueStore<T>
|
||||||
|
}
|
||||||
|
|
||||||
|
class KeyValueStore<T : Any> internal constructor (
|
||||||
|
c: Context,
|
||||||
|
preferencesName: String,
|
||||||
|
useCache: Boolean = false
|
||||||
|
) : SharedPreferences.OnSharedPreferenceChangeListener, StandardKeyValueRepo<String, T> {
|
||||||
|
private val sharedPreferences = c.getSharedPreferences(preferencesName, Context.MODE_PRIVATE)
|
||||||
|
|
||||||
|
private val cachedData = if (useCache) {
|
||||||
|
mutableMapOf<String, Any>()
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
|
||||||
|
private val onNewValueChannel = BroadcastChannel<Pair<String, T>>(Channel.BUFFERED)
|
||||||
|
private val onValueRemovedChannel = BroadcastChannel<String>(Channel.BUFFERED)
|
||||||
|
|
||||||
|
override val onNewValue: Flow<Pair<String, T>> = onNewValueChannel.asFlow()
|
||||||
|
override val onValueRemoved: Flow<String> = onValueRemovedChannel.asFlow()
|
||||||
|
|
||||||
|
init {
|
||||||
|
cachedData ?.let {
|
||||||
|
sharedPreferences.all.forEach {
|
||||||
|
if (it.value != null) {
|
||||||
|
cachedData[it.key] = it.value as Any
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sharedPreferences.registerOnSharedPreferenceChangeListener(this)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onSharedPreferenceChanged(sp: SharedPreferences, key: String) {
|
||||||
|
val value = sp.all[key]
|
||||||
|
cachedData ?: return
|
||||||
|
if (value != null) {
|
||||||
|
cachedData[key] = value
|
||||||
|
} else {
|
||||||
|
cachedData.remove(key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun get(k: String): T? {
|
||||||
|
return (cachedData ?. get(k) ?: sharedPreferences.all[k]) as? T
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun values(pagination: Pagination, reversed: Boolean): PaginationResult<T> {
|
||||||
|
val resultPagination = if (reversed) pagination.reverse(count()) else pagination
|
||||||
|
return sharedPreferences.all.values.paginate(
|
||||||
|
resultPagination
|
||||||
|
).let {
|
||||||
|
PaginationResult(
|
||||||
|
it.page,
|
||||||
|
it.pagesNumber,
|
||||||
|
it.results.map { it as T }.let { if (reversed) it.reversed() else it },
|
||||||
|
it.size
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun keys(pagination: Pagination, reversed: Boolean): PaginationResult<String> {
|
||||||
|
val resultPagination = if (reversed) pagination.reverse(count()) else pagination
|
||||||
|
return sharedPreferences.all.keys.paginate(
|
||||||
|
resultPagination
|
||||||
|
).let {
|
||||||
|
PaginationResult(
|
||||||
|
it.page,
|
||||||
|
it.pagesNumber,
|
||||||
|
it.results.let { if (reversed) it.reversed() else it },
|
||||||
|
it.size
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun keys(v: T, pagination: Pagination, reversed: Boolean): PaginationResult<String> {
|
||||||
|
val resultPagination = if (reversed) pagination.reverse(count()) else pagination
|
||||||
|
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
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun contains(key: String): Boolean = sharedPreferences.contains(key)
|
||||||
|
|
||||||
|
override suspend fun count(): Long = sharedPreferences.all.size.toLong()
|
||||||
|
|
||||||
|
override suspend fun set(toSet: Map<String, T>) {
|
||||||
|
sharedPreferences.edit {
|
||||||
|
toSet.forEach { (k, v) ->
|
||||||
|
when(v) {
|
||||||
|
is Int -> putInt(k, v)
|
||||||
|
is Long -> putLong(k, v)
|
||||||
|
is Float -> putFloat(k, v)
|
||||||
|
is String -> putString(k, v)
|
||||||
|
is Boolean -> putBoolean(k, v)
|
||||||
|
is Set<*> -> putStringSet(k, v.map { (it as? String) ?: it.toString() }.toSet())
|
||||||
|
else -> error(
|
||||||
|
"Currently supported only primitive types and set for SharedPreferences KeyValue repos"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
toSet.forEach { (k, v) ->
|
||||||
|
onNewValueChannel.send(k to v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun unset(toUnset: List<String>) {
|
||||||
|
sharedPreferences.edit {
|
||||||
|
toUnset.forEach { remove(it) }
|
||||||
|
}
|
||||||
|
toUnset.forEach { onValueRemovedChannel.send(it) }
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,237 @@
|
|||||||
|
package dev.inmo.micro_utils.repos.onetomany
|
||||||
|
|
||||||
|
import android.database.sqlite.SQLiteOpenHelper
|
||||||
|
import androidx.core.content.contentValuesOf
|
||||||
|
import dev.inmo.micro_utils.common.mapNotNullA
|
||||||
|
import dev.inmo.micro_utils.pagination.FirstPagePagination
|
||||||
|
import dev.inmo.micro_utils.pagination.Pagination
|
||||||
|
import dev.inmo.micro_utils.pagination.PaginationResult
|
||||||
|
import dev.inmo.micro_utils.pagination.createPaginationResult
|
||||||
|
import dev.inmo.micro_utils.pagination.utils.reverse
|
||||||
|
import dev.inmo.micro_utils.repos.*
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||||
|
import kotlinx.coroutines.flow.asSharedFlow
|
||||||
|
import kotlinx.coroutines.runBlocking
|
||||||
|
import kotlinx.serialization.KSerializer
|
||||||
|
import kotlinx.serialization.json.Json
|
||||||
|
|
||||||
|
private val internalSerialFormat = Json {
|
||||||
|
ignoreUnknownKeys = true
|
||||||
|
}
|
||||||
|
|
||||||
|
class OneToManyAndroidRepo<Key, Value>(
|
||||||
|
private val tableName: String,
|
||||||
|
private val keySerializer: KSerializer<Key>,
|
||||||
|
private val valueSerializer: KSerializer<Value>,
|
||||||
|
private val helper: SQLiteOpenHelper
|
||||||
|
) : OneToManyKeyValueRepo<Key, Value> {
|
||||||
|
private val _onNewValue: MutableSharedFlow<Pair<Key, Value>> = MutableSharedFlow()
|
||||||
|
override val onNewValue: Flow<Pair<Key, Value>> = _onNewValue.asSharedFlow()
|
||||||
|
private val _onValueRemoved: MutableSharedFlow<Pair<Key, Value>> = MutableSharedFlow()
|
||||||
|
override val onValueRemoved: Flow<Pair<Key, Value>> = _onValueRemoved.asSharedFlow()
|
||||||
|
private val _onDataCleared = MutableSharedFlow<Key>()
|
||||||
|
override val onDataCleared: Flow<Key> = _onDataCleared.asSharedFlow()
|
||||||
|
|
||||||
|
private val idColumnName = "id"
|
||||||
|
private val valueColumnName = "value"
|
||||||
|
|
||||||
|
private fun Key.asId() = internalSerialFormat.encodeToString(keySerializer, this)
|
||||||
|
private fun Value.asValue() = internalSerialFormat.encodeToString(valueSerializer, this)
|
||||||
|
private fun String.asValue(): Value = internalSerialFormat.decodeFromString(valueSerializer, this)
|
||||||
|
private fun String.asKey(): Key = internalSerialFormat.decodeFromString(keySerializer, this)
|
||||||
|
|
||||||
|
init {
|
||||||
|
runBlocking(DatabaseCoroutineContext) {
|
||||||
|
helper.writableTransaction {
|
||||||
|
createTable(
|
||||||
|
tableName,
|
||||||
|
internalId to internalIdType,
|
||||||
|
idColumnName to ColumnType.Text.NOT_NULLABLE,
|
||||||
|
valueColumnName to ColumnType.Text.NULLABLE
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun add(toAdd: Map<Key, List<Value>>) {
|
||||||
|
val added = mutableListOf<Pair<Key, Value>>()
|
||||||
|
helper.writableTransaction {
|
||||||
|
toAdd.forEach { (k, values) ->
|
||||||
|
values.forEach { v ->
|
||||||
|
insert(
|
||||||
|
tableName,
|
||||||
|
null,
|
||||||
|
contentValuesOf(
|
||||||
|
idColumnName to k.asId(),
|
||||||
|
valueColumnName to v.asValue()
|
||||||
|
)
|
||||||
|
).also {
|
||||||
|
if (it != -1L) {
|
||||||
|
added.add(k to v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
added.forEach { _onNewValue.emit(it) }
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun clear(k: Key) {
|
||||||
|
helper.writableTransaction {
|
||||||
|
delete(tableName, "$idColumnName=?", arrayOf(k.asId()))
|
||||||
|
}.also {
|
||||||
|
if (it > 0) {
|
||||||
|
_onDataCleared.emit(k)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun set(toSet: Map<Key, List<Value>>) {
|
||||||
|
val (clearedKeys, inserted) = helper.writableTransaction {
|
||||||
|
toSet.mapNotNull { (k, _) ->
|
||||||
|
if (delete(tableName, "$idColumnName=?", arrayOf(k.asId())) > 0) {
|
||||||
|
k
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
} to toSet.flatMap { (k, values) ->
|
||||||
|
values.map { v ->
|
||||||
|
insert(
|
||||||
|
tableName,
|
||||||
|
null,
|
||||||
|
contentValuesOf(idColumnName to k.asId(), valueColumnName to v.asValue())
|
||||||
|
)
|
||||||
|
k to v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
clearedKeys.forEach { _onDataCleared.emit(it) }
|
||||||
|
inserted.forEach { newPair -> _onNewValue.emit(newPair) }
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun contains(k: Key): Boolean = helper.readableTransaction {
|
||||||
|
select(tableName, selection = "$idColumnName=?", selectionArgs = arrayOf(k.asId()), limit = FirstPagePagination(1).limitClause()).use {
|
||||||
|
it.count > 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun contains(k: Key, v: Value): Boolean = helper.readableTransaction {
|
||||||
|
select(
|
||||||
|
tableName,
|
||||||
|
selection = "$idColumnName=? AND $valueColumnName=?",
|
||||||
|
selectionArgs = arrayOf(k.asId(), v.asValue()),
|
||||||
|
limit = FirstPagePagination(1).limitClause()
|
||||||
|
).use {
|
||||||
|
it.count > 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun count(): Long =helper.readableTransaction {
|
||||||
|
select(
|
||||||
|
tableName
|
||||||
|
).use {
|
||||||
|
it.count
|
||||||
|
}
|
||||||
|
}.toLong()
|
||||||
|
|
||||||
|
override suspend fun count(k: Key): Long = helper.readableTransaction {
|
||||||
|
select(tableName, selection = "$idColumnName=?", selectionArgs = arrayOf(k.asId()), limit = FirstPagePagination(1).limitClause()).use {
|
||||||
|
it.count
|
||||||
|
}
|
||||||
|
}.toLong()
|
||||||
|
|
||||||
|
override suspend fun get(
|
||||||
|
k: Key,
|
||||||
|
pagination: Pagination,
|
||||||
|
reversed: Boolean
|
||||||
|
): PaginationResult<Value> = count(k).let { count ->
|
||||||
|
val resultPagination = pagination.let { if (reversed) pagination.reverse(count) else pagination }
|
||||||
|
helper.readableTransaction {
|
||||||
|
select(
|
||||||
|
tableName,
|
||||||
|
selection = "$idColumnName=?",
|
||||||
|
selectionArgs = arrayOf(k.asId()),
|
||||||
|
limit = resultPagination.limitClause()
|
||||||
|
).use { c ->
|
||||||
|
mutableListOf<Value>().also {
|
||||||
|
if (c.moveToFirst()) {
|
||||||
|
do {
|
||||||
|
it.add(c.getString(valueColumnName).asValue())
|
||||||
|
} while (c.moveToNext())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}.createPaginationResult(
|
||||||
|
pagination,
|
||||||
|
count
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun keys(
|
||||||
|
pagination: Pagination,
|
||||||
|
reversed: Boolean
|
||||||
|
): PaginationResult<Key> = count().let { count ->
|
||||||
|
val resultPagination = pagination.let { if (reversed) pagination.reverse(count) else pagination }
|
||||||
|
helper.readableTransaction {
|
||||||
|
select(
|
||||||
|
tableName,
|
||||||
|
limit = resultPagination.limitClause()
|
||||||
|
).use { c ->
|
||||||
|
mutableListOf<Key>().also {
|
||||||
|
if (c.moveToFirst()) {
|
||||||
|
do {
|
||||||
|
it.add(c.getString(idColumnName).asKey())
|
||||||
|
} while (c.moveToNext())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}.createPaginationResult(
|
||||||
|
pagination,
|
||||||
|
count
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun keys(
|
||||||
|
v: Value,
|
||||||
|
pagination: Pagination,
|
||||||
|
reversed: Boolean
|
||||||
|
): PaginationResult<Key> = count().let { count ->
|
||||||
|
val resultPagination = pagination.let { if (reversed) pagination.reverse(count) else pagination }
|
||||||
|
helper.readableTransaction {
|
||||||
|
select(
|
||||||
|
tableName,
|
||||||
|
selection = "$valueColumnName=?",
|
||||||
|
selectionArgs = arrayOf(v.asValue()),
|
||||||
|
limit = resultPagination.limitClause()
|
||||||
|
).use { c ->
|
||||||
|
mutableListOf<Key>().also {
|
||||||
|
if (c.moveToFirst()) {
|
||||||
|
do {
|
||||||
|
it.add(c.getString(idColumnName).asKey())
|
||||||
|
} while (c.moveToNext())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}.createPaginationResult(
|
||||||
|
pagination,
|
||||||
|
count
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun remove(toRemove: Map<Key, List<Value>>) {
|
||||||
|
helper.writableTransaction {
|
||||||
|
toRemove.flatMap { (k, vs) ->
|
||||||
|
vs.mapNotNullA { v ->
|
||||||
|
if (delete(tableName, "$idColumnName=? AND $valueColumnName=?", arrayOf(k.asId(), v.asValue())) > 0) {
|
||||||
|
k to v
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}.forEach { (k, v) ->
|
||||||
|
_onValueRemoved.emit(k to v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,63 @@
|
|||||||
|
package dev.inmo.micro_utils.repos.versions
|
||||||
|
|
||||||
|
import android.database.sqlite.SQLiteOpenHelper
|
||||||
|
import androidx.core.content.contentValuesOf
|
||||||
|
import dev.inmo.micro_utils.repos.*
|
||||||
|
import kotlinx.coroutines.runBlocking
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Will create [VersionsRepo] based on [SQLiteOpenHelper] with table inside of [database]
|
||||||
|
*/
|
||||||
|
@Suppress("NOTHING_TO_INLINE")
|
||||||
|
inline fun versionsRepo(database: SQLiteOpenHelper): VersionsRepo<SQLiteOpenHelper> = StandardVersionsRepo(
|
||||||
|
AndroidSQLStandardVersionsRepoProxy(database)
|
||||||
|
)
|
||||||
|
|
||||||
|
class AndroidSQLStandardVersionsRepoProxy(
|
||||||
|
override val database: SQLiteOpenHelper
|
||||||
|
) : StandardVersionsRepoProxy<SQLiteOpenHelper> {
|
||||||
|
private val tableName: String = "AndroidSQLStandardVersionsRepo"
|
||||||
|
private val tableNameColumnName = "tableName"
|
||||||
|
private val tableVersionColumnName = "version"
|
||||||
|
|
||||||
|
init {
|
||||||
|
runBlocking(DatabaseCoroutineContext) {
|
||||||
|
database.writableTransaction {
|
||||||
|
createTable(
|
||||||
|
tableName,
|
||||||
|
tableNameColumnName to ColumnType.Text.NOT_NULLABLE,
|
||||||
|
tableVersionColumnName to ColumnType.Numeric.INTEGER()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun getTableVersion(table: String): Int? = database.writableTransaction {
|
||||||
|
select(
|
||||||
|
tableName,
|
||||||
|
selection = "$tableNameColumnName=?",
|
||||||
|
selectionArgs = arrayOf(table),
|
||||||
|
limit = limitClause(1)
|
||||||
|
).use {
|
||||||
|
if (it.moveToFirst()) {
|
||||||
|
it.getInt(tableVersionColumnName)
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun updateTableVersion(table: String, version: Int) {
|
||||||
|
database.writableTransaction {
|
||||||
|
val updated = update(
|
||||||
|
tableName,
|
||||||
|
contentValuesOf(tableVersionColumnName to version),
|
||||||
|
"$tableNameColumnName=?",
|
||||||
|
arrayOf(table)
|
||||||
|
) > 0
|
||||||
|
if (!updated) {
|
||||||
|
insert(tableName, null, contentValuesOf(tableNameColumnName to table, tableVersionColumnName to version))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,49 @@
|
|||||||
|
@file:Suppress("NOTHING_TO_INLINE")
|
||||||
|
|
||||||
|
package dev.inmo.micro_utils.repos.versions
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.database.sqlite.SQLiteOpenHelper
|
||||||
|
import androidx.core.content.contentValuesOf
|
||||||
|
import dev.inmo.micro_utils.repos.*
|
||||||
|
import dev.inmo.micro_utils.repos.keyvalue.keyValueStore
|
||||||
|
import kotlinx.coroutines.runBlocking
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Will create [VersionsRepo] based on [T], but versions will be stored in [StandardKeyValueRepo]
|
||||||
|
*
|
||||||
|
* @receiver Will be used to create [KeyValueBasedVersionsRepoProxy] via [keyValueStore] and pass it to [StandardVersionsRepo]
|
||||||
|
*
|
||||||
|
* @see [KeyValueBasedVersionsRepoProxy]
|
||||||
|
* @see [keyValueStore]
|
||||||
|
*/
|
||||||
|
inline fun <T> Context.versionsKeyValueRepo(
|
||||||
|
database: T
|
||||||
|
): VersionsRepo<T> = StandardVersionsRepo(
|
||||||
|
KeyValueBasedVersionsRepoProxy(
|
||||||
|
keyValueStore("SPVersionsRepo"),
|
||||||
|
database
|
||||||
|
)
|
||||||
|
)
|
||||||
|
/**
|
||||||
|
* Will create [VersionsRepo] based on [SQLiteOpenHelper], but versions will be stored in [StandardKeyValueRepo]
|
||||||
|
*
|
||||||
|
* @receiver Will be used to create [StandardKeyValueRepo] via [keyValueStore] and pass it to [StandardVersionsRepo]
|
||||||
|
*
|
||||||
|
* @see [keyValueStore]
|
||||||
|
*/
|
||||||
|
inline fun Context.versionsKeyValueRepoForSQL(
|
||||||
|
database: SQLiteOpenHelper
|
||||||
|
) = versionsKeyValueRepo(database)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Will create [VersionsRepo] based on [SQLiteOpenHelper], but versions will be stored in [StandardKeyValueRepo]
|
||||||
|
*
|
||||||
|
* @param context Will be used to create [StandardKeyValueRepo] via [keyValueStore] and pass it to [StandardVersionsRepo]
|
||||||
|
*
|
||||||
|
* @see [keyValueStore]
|
||||||
|
*/
|
||||||
|
inline fun versionsRepo(
|
||||||
|
context: Context,
|
||||||
|
database: SQLiteOpenHelper
|
||||||
|
) = context.versionsKeyValueRepoForSQL(database)
|
@@ -1,9 +1,7 @@
|
|||||||
package dev.inmo.micro_utils.repos.exposed
|
package dev.inmo.micro_utils.repos.exposed
|
||||||
|
|
||||||
import kotlinx.coroutines.channels.Channel
|
|
||||||
|
|
||||||
abstract class AbstractExposedCRUDRepo<ObjectType, IdType, InputValueType>(
|
abstract class AbstractExposedCRUDRepo<ObjectType, IdType, InputValueType>(
|
||||||
flowsChannelsSize: Int = Channel.BUFFERED,
|
flowsChannelsSize: Int = 0,
|
||||||
tableName: String = ""
|
tableName: String = ""
|
||||||
) :
|
) :
|
||||||
AbstractExposedWriteCRUDRepo<ObjectType, IdType, InputValueType>(
|
AbstractExposedWriteCRUDRepo<ObjectType, IdType, InputValueType>(
|
||||||
|
@@ -3,28 +3,27 @@ package dev.inmo.micro_utils.repos.exposed
|
|||||||
import dev.inmo.micro_utils.repos.UpdatedValuePair
|
import dev.inmo.micro_utils.repos.UpdatedValuePair
|
||||||
import dev.inmo.micro_utils.repos.WriteStandardCRUDRepo
|
import dev.inmo.micro_utils.repos.WriteStandardCRUDRepo
|
||||||
import kotlinx.coroutines.channels.BroadcastChannel
|
import kotlinx.coroutines.channels.BroadcastChannel
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.*
|
||||||
import kotlinx.coroutines.flow.asFlow
|
|
||||||
import org.jetbrains.exposed.sql.*
|
import org.jetbrains.exposed.sql.*
|
||||||
import org.jetbrains.exposed.sql.statements.InsertStatement
|
import org.jetbrains.exposed.sql.statements.InsertStatement
|
||||||
import org.jetbrains.exposed.sql.statements.UpdateStatement
|
import org.jetbrains.exposed.sql.statements.UpdateStatement
|
||||||
import org.jetbrains.exposed.sql.transactions.transaction
|
import org.jetbrains.exposed.sql.transactions.transaction
|
||||||
|
|
||||||
abstract class AbstractExposedWriteCRUDRepo<ObjectType, IdType, InputValueType>(
|
abstract class AbstractExposedWriteCRUDRepo<ObjectType, IdType, InputValueType>(
|
||||||
flowsChannelsSize: Int = 64,
|
flowsChannelsSize: Int = 0,
|
||||||
tableName: String = ""
|
tableName: String = ""
|
||||||
) :
|
) :
|
||||||
AbstractExposedReadCRUDRepo<ObjectType, IdType>(tableName),
|
AbstractExposedReadCRUDRepo<ObjectType, IdType>(tableName),
|
||||||
ExposedCRUDRepo<ObjectType, IdType>,
|
ExposedCRUDRepo<ObjectType, IdType>,
|
||||||
WriteStandardCRUDRepo<ObjectType, IdType, InputValueType>
|
WriteStandardCRUDRepo<ObjectType, IdType, InputValueType>
|
||||||
{
|
{
|
||||||
protected val newObjectsChannel = BroadcastChannel<ObjectType>(flowsChannelsSize)
|
protected val newObjectsChannel = MutableSharedFlow<ObjectType>(flowsChannelsSize)
|
||||||
protected val updateObjectsChannel = BroadcastChannel<ObjectType>(flowsChannelsSize)
|
protected val updateObjectsChannel = MutableSharedFlow<ObjectType>(flowsChannelsSize)
|
||||||
protected val deleteObjectsIdsChannel = BroadcastChannel<IdType>(flowsChannelsSize)
|
protected val deleteObjectsIdsChannel = MutableSharedFlow<IdType>(flowsChannelsSize)
|
||||||
|
|
||||||
override val newObjectsFlow: Flow<ObjectType> = newObjectsChannel.asFlow()
|
override val newObjectsFlow: Flow<ObjectType> = newObjectsChannel.asSharedFlow()
|
||||||
override val updatedObjectsFlow: Flow<ObjectType> = updateObjectsChannel.asFlow()
|
override val updatedObjectsFlow: Flow<ObjectType> = updateObjectsChannel.asSharedFlow()
|
||||||
override val deletedObjectsIdsFlow: Flow<IdType> = deleteObjectsIdsChannel.asFlow()
|
override val deletedObjectsIdsFlow: Flow<IdType> = deleteObjectsIdsChannel.asSharedFlow()
|
||||||
|
|
||||||
abstract val InsertStatement<Number>.asObject: ObjectType
|
abstract val InsertStatement<Number>.asObject: ObjectType
|
||||||
abstract val selectByIds: SqlExpressionBuilder.(List<out IdType>) -> Op<Boolean>
|
abstract val selectByIds: SqlExpressionBuilder.(List<out IdType>) -> Op<Boolean>
|
||||||
@@ -45,7 +44,7 @@ abstract class AbstractExposedWriteCRUDRepo<ObjectType, IdType, InputValueType>(
|
|||||||
values.map { value -> createWithoutNotification(value) }
|
values.map { value -> createWithoutNotification(value) }
|
||||||
}.also {
|
}.also {
|
||||||
it.forEach {
|
it.forEach {
|
||||||
newObjectsChannel.send(it)
|
newObjectsChannel.emit(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -75,7 +74,7 @@ abstract class AbstractExposedWriteCRUDRepo<ObjectType, IdType, InputValueType>(
|
|||||||
onBeforeUpdate(listOf(id to value))
|
onBeforeUpdate(listOf(id to value))
|
||||||
return updateWithoutNotification(id, value).also {
|
return updateWithoutNotification(id, value).also {
|
||||||
if (it != null) {
|
if (it != null) {
|
||||||
updateObjectsChannel.send(it)
|
updateObjectsChannel.emit(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -89,7 +88,7 @@ abstract class AbstractExposedWriteCRUDRepo<ObjectType, IdType, InputValueType>(
|
|||||||
} as List<ObjectType>
|
} as List<ObjectType>
|
||||||
).also {
|
).also {
|
||||||
it.forEach {
|
it.forEach {
|
||||||
updateObjectsChannel.send(it)
|
updateObjectsChannel.emit(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -2,10 +2,9 @@ package dev.inmo.micro_utils.repos.exposed.keyvalue
|
|||||||
|
|
||||||
import dev.inmo.micro_utils.repos.StandardKeyValueRepo
|
import dev.inmo.micro_utils.repos.StandardKeyValueRepo
|
||||||
import dev.inmo.micro_utils.repos.exposed.ColumnAllocator
|
import dev.inmo.micro_utils.repos.exposed.ColumnAllocator
|
||||||
import kotlinx.coroutines.channels.BroadcastChannel
|
import dev.inmo.micro_utils.repos.exposed.initTable
|
||||||
import kotlinx.coroutines.channels.Channel
|
import kotlinx.coroutines.channels.Channel
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.*
|
||||||
import kotlinx.coroutines.flow.asFlow
|
|
||||||
import org.jetbrains.exposed.sql.*
|
import org.jetbrains.exposed.sql.*
|
||||||
import org.jetbrains.exposed.sql.transactions.transaction
|
import org.jetbrains.exposed.sql.transactions.transaction
|
||||||
|
|
||||||
@@ -20,35 +19,45 @@ open class ExposedKeyValueRepo<Key, Value>(
|
|||||||
valueColumnAllocator,
|
valueColumnAllocator,
|
||||||
tableName
|
tableName
|
||||||
) {
|
) {
|
||||||
private val onNewValueChannel = BroadcastChannel<Pair<Key, Value>>(Channel.BUFFERED)
|
private val _onNewValue = MutableSharedFlow<Pair<Key, Value>>()
|
||||||
private val onValueRemovedChannel = BroadcastChannel<Key>(Channel.BUFFERED)
|
private val _onValueRemoved = MutableSharedFlow<Key>()
|
||||||
|
|
||||||
override val onNewValue: Flow<Pair<Key, Value>> = onNewValueChannel.asFlow()
|
override val onNewValue: Flow<Pair<Key, Value>> = _onNewValue.asSharedFlow()
|
||||||
override val onValueRemoved: Flow<Key> = onValueRemovedChannel.asFlow()
|
override val onValueRemoved: Flow<Key> = _onValueRemoved.asSharedFlow()
|
||||||
|
|
||||||
override suspend fun set(k: Key, v: Value) {
|
override suspend fun set(toSet: Map<Key, Value>) {
|
||||||
transaction(database) {
|
transaction(database) {
|
||||||
if (select { keyColumn.eq(k) }.limit(1).any()) {
|
toSet.mapNotNull { (k, v) ->
|
||||||
update({ keyColumn.eq(k) }) {
|
if (update({ keyColumn.eq(k) }) { it[valueColumn] = v } > 0) {
|
||||||
it[valueColumn] = v
|
k to v
|
||||||
}
|
} else {
|
||||||
} else {
|
val inserted = insert {
|
||||||
insert {
|
it[keyColumn] = k
|
||||||
it[keyColumn] = k
|
it[valueColumn] = v
|
||||||
it[valueColumn] = v
|
}.getOrNull(keyColumn) != null
|
||||||
|
if (inserted) {
|
||||||
|
k to v
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}.forEach {
|
||||||
|
_onNewValue.emit(it)
|
||||||
}
|
}
|
||||||
onNewValueChannel.send(k to v)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun unset(k: Key) {
|
override suspend fun unset(toUnset: List<Key>) {
|
||||||
transaction(database) {
|
transaction(database) {
|
||||||
deleteWhere { keyColumn.eq(k) }
|
toUnset.mapNotNull {
|
||||||
|
if (deleteWhere { keyColumn.eq(it) } > 0) {
|
||||||
|
it
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}.forEach {
|
||||||
|
_onValueRemoved.emit(it)
|
||||||
}
|
}
|
||||||
onValueRemovedChannel.send(k)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Deprecated("Renamed", ReplaceWith("ExposedKeyValueRepo", "dev.inmo.micro_utils.repos.exposed.keyvalue.ExposedKeyValueRepo"))
|
|
||||||
typealias AbstractExposedKeyValueRepo<Key, Value> = ExposedKeyValueRepo<Key, Value>
|
|
||||||
|
@@ -2,8 +2,7 @@ package dev.inmo.micro_utils.repos.exposed.keyvalue
|
|||||||
|
|
||||||
import dev.inmo.micro_utils.pagination.*
|
import dev.inmo.micro_utils.pagination.*
|
||||||
import dev.inmo.micro_utils.repos.ReadStandardKeyValueRepo
|
import dev.inmo.micro_utils.repos.ReadStandardKeyValueRepo
|
||||||
import dev.inmo.micro_utils.repos.exposed.ColumnAllocator
|
import dev.inmo.micro_utils.repos.exposed.*
|
||||||
import dev.inmo.micro_utils.repos.exposed.ExposedRepo
|
|
||||||
import org.jetbrains.exposed.sql.*
|
import org.jetbrains.exposed.sql.*
|
||||||
import org.jetbrains.exposed.sql.transactions.transaction
|
import org.jetbrains.exposed.sql.transactions.transaction
|
||||||
|
|
||||||
@@ -17,6 +16,8 @@ open class ExposedReadKeyValueRepo<Key, Value>(
|
|||||||
protected val valueColumn: Column<Value> = valueColumnAllocator()
|
protected val valueColumn: Column<Value> = valueColumnAllocator()
|
||||||
override val primaryKey: PrimaryKey = PrimaryKey(keyColumn, valueColumn)
|
override val primaryKey: PrimaryKey = PrimaryKey(keyColumn, valueColumn)
|
||||||
|
|
||||||
|
init { initTable() }
|
||||||
|
|
||||||
override suspend fun get(k: Key): Value? = transaction(database) {
|
override suspend fun get(k: Key): Value? = transaction(database) {
|
||||||
select { keyColumn.eq(k) }.limit(1).firstOrNull() ?.getOrNull(valueColumn)
|
select { keyColumn.eq(k) }.limit(1).firstOrNull() ?.getOrNull(valueColumn)
|
||||||
}
|
}
|
||||||
@@ -33,12 +34,19 @@ open class ExposedReadKeyValueRepo<Key, Value>(
|
|||||||
}
|
}
|
||||||
}.createPaginationResult(pagination, count())
|
}.createPaginationResult(pagination, count())
|
||||||
|
|
||||||
|
override suspend fun keys(v: Value, pagination: Pagination, reversed: Boolean): PaginationResult<Key> = transaction(database) {
|
||||||
|
select { valueColumn.eq(v) }.let {
|
||||||
|
it.count() to it.paginate(pagination, keyColumn to if (reversed) SortOrder.DESC else SortOrder.ASC).map {
|
||||||
|
it[keyColumn]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}.let { (count, list) ->
|
||||||
|
list.createPaginationResult(pagination, count)
|
||||||
|
}
|
||||||
|
|
||||||
override suspend fun values(pagination: Pagination, reversed: Boolean): PaginationResult<Value> = transaction(database) {
|
override suspend fun values(pagination: Pagination, reversed: Boolean): PaginationResult<Value> = transaction(database) {
|
||||||
selectAll().paginate(pagination, keyColumn to if (reversed) SortOrder.DESC else SortOrder.ASC).map {
|
selectAll().paginate(pagination, keyColumn to if (reversed) SortOrder.DESC else SortOrder.ASC).map {
|
||||||
it[valueColumn]
|
it[valueColumn]
|
||||||
}
|
}
|
||||||
}.createPaginationResult(pagination, count())
|
}.createPaginationResult(pagination, count())
|
||||||
}
|
}
|
||||||
|
|
||||||
@Deprecated("Renamed", ReplaceWith("ExposedReadKeyValueRepo", "dev.inmo.micro_utils.repos.exposed.keyvalue.ExposedReadKeyValueRepo"))
|
|
||||||
typealias AbstractExposedReadKeyValueRepo<Key, Value> = ExposedReadKeyValueRepo<Key, Value>
|
|
||||||
|
@@ -1,9 +1,8 @@
|
|||||||
package dev.inmo.micro_utils.repos.exposed.onetomany
|
package dev.inmo.micro_utils.repos.exposed.onetomany
|
||||||
|
|
||||||
import dev.inmo.micro_utils.coroutines.BroadcastFlow
|
|
||||||
import dev.inmo.micro_utils.repos.OneToManyKeyValueRepo
|
import dev.inmo.micro_utils.repos.OneToManyKeyValueRepo
|
||||||
import dev.inmo.micro_utils.repos.exposed.ColumnAllocator
|
import dev.inmo.micro_utils.repos.exposed.ColumnAllocator
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.*
|
||||||
import org.jetbrains.exposed.sql.*
|
import org.jetbrains.exposed.sql.*
|
||||||
import org.jetbrains.exposed.sql.transactions.transaction
|
import org.jetbrains.exposed.sql.transactions.transaction
|
||||||
|
|
||||||
@@ -18,40 +17,48 @@ open class ExposedOneToManyKeyValueRepo<Key, Value>(
|
|||||||
valueColumnAllocator,
|
valueColumnAllocator,
|
||||||
tableName
|
tableName
|
||||||
) {
|
) {
|
||||||
protected val _onNewValue: BroadcastFlow<Pair<Key, Value>> = BroadcastFlow()
|
protected val _onNewValue: MutableSharedFlow<Pair<Key, Value>> = MutableSharedFlow()
|
||||||
override val onNewValue: Flow<Pair<Key, Value>>
|
override val onNewValue: Flow<Pair<Key, Value>>
|
||||||
get() = _onNewValue
|
get() = _onNewValue
|
||||||
protected val _onValueRemoved: BroadcastFlow<Pair<Key, Value>> = BroadcastFlow()
|
protected val _onValueRemoved: MutableSharedFlow<Pair<Key, Value>> = MutableSharedFlow()
|
||||||
override val onValueRemoved: Flow<Pair<Key, Value>>
|
override val onValueRemoved: Flow<Pair<Key, Value>>
|
||||||
get() = _onValueRemoved
|
get() = _onValueRemoved
|
||||||
protected val _onDataCleared: BroadcastFlow<Key> = BroadcastFlow()
|
protected val _onDataCleared: MutableSharedFlow<Key> = MutableSharedFlow()
|
||||||
override val onDataCleared: Flow<Key>
|
override val onDataCleared: Flow<Key>
|
||||||
get() = _onDataCleared
|
get() = _onDataCleared
|
||||||
|
|
||||||
override suspend fun add(k: Key, v: Value) {
|
override suspend fun add(toAdd: Map<Key, List<Value>>) {
|
||||||
transaction(database) {
|
transaction(database) {
|
||||||
insert {
|
toAdd.keys.flatMap { k ->
|
||||||
it[keyColumn] = k
|
toAdd[k] ?.mapNotNull { v ->
|
||||||
it[valueColumn] = v
|
insertIgnore {
|
||||||
|
it[keyColumn] = k
|
||||||
|
it[valueColumn] = v
|
||||||
|
}.getOrNull(keyColumn) ?.let { k to v }
|
||||||
|
} ?: emptyList()
|
||||||
}
|
}
|
||||||
}.also { _onNewValue.send(k to v) }
|
}.forEach { _onNewValue.emit(it) }
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun remove(k: Key, v: Value) {
|
override suspend fun remove(toRemove: Map<Key, List<Value>>) {
|
||||||
transaction(database) {
|
transaction(database) {
|
||||||
deleteWhere { keyColumn.eq(k).and(valueColumn.eq(v)) }
|
toRemove.keys.flatMap { k ->
|
||||||
}.also { _onValueRemoved.send(k to v) }
|
toRemove[k] ?.mapNotNull { v ->
|
||||||
|
if (deleteIgnoreWhere { keyColumn.eq(k).and(valueColumn.eq(v)) } > 0 ) {
|
||||||
|
k to v
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
} ?: emptyList()
|
||||||
|
}
|
||||||
|
}.forEach {
|
||||||
|
_onValueRemoved.emit(it)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun clear(k: Key) {
|
override suspend fun clear(k: Key) {
|
||||||
transaction(database) {
|
transaction(database) {
|
||||||
deleteWhere { keyColumn.eq(k) }
|
deleteWhere { keyColumn.eq(k) }
|
||||||
}.also { _onDataCleared.send(k) }
|
}.also { _onDataCleared.emit(k) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Deprecated("Renamed", ReplaceWith("ExposedOneToManyKeyValueRepo", "dev.inmo.micro_utils.repos.exposed.onetomany.ExposedOneToManyKeyValueRepo"))
|
|
||||||
typealias AbstractOneToManyExposedKeyValueRepo<Key, Value> = ExposedOneToManyKeyValueRepo<Key, Value>
|
|
||||||
|
|
||||||
@Deprecated("Renamed", ReplaceWith("ExposedOneToManyKeyValueRepo", "dev.inmo.micro_utils.repos.exposed.onetomany.ExposedOneToManyKeyValueRepo"))
|
|
||||||
typealias AbstractExposedOneToManyKeyValueRepo<Key, Value> = ExposedOneToManyKeyValueRepo<Key, Value>
|
|
||||||
|
@@ -39,6 +39,21 @@ open class ExposedReadOneToManyKeyValueRepo<Key, Value>(
|
|||||||
count()
|
count()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
override suspend fun keys(
|
||||||
|
v: Value,
|
||||||
|
pagination: Pagination,
|
||||||
|
reversed: Boolean
|
||||||
|
): PaginationResult<Key> = transaction(database) {
|
||||||
|
select { valueColumn.eq(v) }.let {
|
||||||
|
it.count() to it.paginate(pagination, keyColumn, reversed).map { it[keyColumn] }
|
||||||
|
}
|
||||||
|
}.let { (count, list) ->
|
||||||
|
list.createPaginationResult(
|
||||||
|
pagination,
|
||||||
|
count
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
override suspend fun contains(k: Key): Boolean = transaction(database) {
|
override suspend fun contains(k: Key): Boolean = transaction(database) {
|
||||||
select { keyColumn.eq(k) }.limit(1).any()
|
select { keyColumn.eq(k) }.limit(1).any()
|
||||||
}
|
}
|
||||||
@@ -47,9 +62,3 @@ open class ExposedReadOneToManyKeyValueRepo<Key, Value>(
|
|||||||
select { keyColumn.eq(k).and(valueColumn.eq(v)) }.limit(1).any()
|
select { keyColumn.eq(k).and(valueColumn.eq(v)) }.limit(1).any()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Deprecated("Renamed", ReplaceWith("ExposedReadOneToManyKeyValueRepo", "dev.inmo.micro_utils.repos.exposed.onetomany.ExposedReadOneToManyKeyValueRepo"))
|
|
||||||
typealias AbstractOneToManyExposedReadKeyValueRepo<Key, Value> = ExposedReadOneToManyKeyValueRepo<Key, Value>
|
|
||||||
|
|
||||||
@Deprecated("Renamed", ReplaceWith("ExposedReadOneToManyKeyValueRepo", "dev.inmo.micro_utils.repos.exposed.onetomany.ExposedReadOneToManyKeyValueRepo"))
|
|
||||||
typealias AbstractExposedReadOneToManyKeyValueRepo<Key, Value> = ExposedReadOneToManyKeyValueRepo<Key, Value>
|
|
||||||
|
@@ -0,0 +1,48 @@
|
|||||||
|
package dev.inmo.micro_utils.repos.exposed.versions
|
||||||
|
|
||||||
|
import dev.inmo.micro_utils.repos.exposed.ExposedRepo
|
||||||
|
import dev.inmo.micro_utils.repos.exposed.initTable
|
||||||
|
import dev.inmo.micro_utils.repos.versions.*
|
||||||
|
import org.jetbrains.exposed.sql.*
|
||||||
|
import org.jetbrains.exposed.sql.transactions.transaction
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Use this method to create [StandardVersionsRepo] based on [Database] with [ExposedStandardVersionsRepoProxy] as
|
||||||
|
* [StandardVersionsRepoProxy]
|
||||||
|
*/
|
||||||
|
@Suppress("NOTHING_TO_INLINE")
|
||||||
|
inline fun versionsRepo(database: Database): VersionsRepo<Database> = StandardVersionsRepo(
|
||||||
|
ExposedStandardVersionsRepoProxy(database)
|
||||||
|
)
|
||||||
|
|
||||||
|
class ExposedStandardVersionsRepoProxy(
|
||||||
|
override val database: Database
|
||||||
|
) : StandardVersionsRepoProxy<Database>, Table("ExposedVersionsProxy"), ExposedRepo {
|
||||||
|
private val tableNameColumn = text("tableName")
|
||||||
|
private val tableVersionColumn = integer("tableName")
|
||||||
|
|
||||||
|
init {
|
||||||
|
initTable()
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun getTableVersion(tableName: String): Int? = transaction(database) {
|
||||||
|
select { tableNameColumn.eq(tableName) }.limit(1).firstOrNull() ?.getOrNull(tableVersionColumn)
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun updateTableVersion(tableName: String, version: Int) {
|
||||||
|
transaction(database) {
|
||||||
|
val updated = update(
|
||||||
|
{ tableNameColumn.eq(tableName) }
|
||||||
|
) {
|
||||||
|
it[tableVersionColumn] = version
|
||||||
|
} > 0
|
||||||
|
if (!updated) {
|
||||||
|
insert {
|
||||||
|
it[tableNameColumn] = tableName
|
||||||
|
it[tableVersionColumn] = version
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -1,6 +1,7 @@
|
|||||||
plugins {
|
plugins {
|
||||||
id "org.jetbrains.kotlin.multiplatform"
|
id "org.jetbrains.kotlin.multiplatform"
|
||||||
id "org.jetbrains.kotlin.plugin.serialization"
|
id "org.jetbrains.kotlin.plugin.serialization"
|
||||||
|
id "com.android.library"
|
||||||
}
|
}
|
||||||
|
|
||||||
apply from: "$mppProjectWithSerializationPresetPath"
|
apply from: "$mppProjectWithSerializationPresetPath"
|
||||||
|
@@ -1,8 +1,7 @@
|
|||||||
package dev.inmo.micro_utils.repos
|
package dev.inmo.micro_utils.repos
|
||||||
|
|
||||||
import dev.inmo.micro_utils.coroutines.BroadcastFlow
|
|
||||||
import dev.inmo.micro_utils.pagination.*
|
import dev.inmo.micro_utils.pagination.*
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.*
|
||||||
|
|
||||||
class ReadMapCRUDRepo<ObjectType, IdType>(
|
class ReadMapCRUDRepo<ObjectType, IdType>(
|
||||||
private val map: Map<IdType, ObjectType> = emptyMap()
|
private val map: Map<IdType, ObjectType> = emptyMap()
|
||||||
@@ -26,15 +25,12 @@ class ReadMapCRUDRepo<ObjectType, IdType>(
|
|||||||
abstract class WriteMapCRUDRepo<ObjectType, IdType, InputValueType>(
|
abstract class WriteMapCRUDRepo<ObjectType, IdType, InputValueType>(
|
||||||
private val map: MutableMap<IdType, ObjectType> = mutableMapOf()
|
private val map: MutableMap<IdType, ObjectType> = mutableMapOf()
|
||||||
) : WriteStandardCRUDRepo<ObjectType, IdType, InputValueType> {
|
) : WriteStandardCRUDRepo<ObjectType, IdType, InputValueType> {
|
||||||
private val _newObjectsFlow: BroadcastFlow<ObjectType> = BroadcastFlow()
|
private val _newObjectsFlow: MutableSharedFlow<ObjectType> = MutableSharedFlow()
|
||||||
override val newObjectsFlow: Flow<ObjectType>
|
override val newObjectsFlow: Flow<ObjectType> = _newObjectsFlow.asSharedFlow()
|
||||||
get() = _newObjectsFlow
|
private val _updatedObjectsFlow: MutableSharedFlow<ObjectType> = MutableSharedFlow()
|
||||||
private val _updatedObjectsFlow: BroadcastFlow<ObjectType> = BroadcastFlow()
|
override val updatedObjectsFlow: Flow<ObjectType> = _updatedObjectsFlow.asSharedFlow()
|
||||||
override val updatedObjectsFlow: Flow<ObjectType>
|
private val _deletedObjectsIdsFlow: MutableSharedFlow<IdType> = MutableSharedFlow()
|
||||||
get() = _updatedObjectsFlow
|
override val deletedObjectsIdsFlow: Flow<IdType> = _deletedObjectsIdsFlow.asSharedFlow()
|
||||||
private val _deletedObjectsIdsFlow: BroadcastFlow<IdType> = BroadcastFlow()
|
|
||||||
override val deletedObjectsIdsFlow: Flow<IdType>
|
|
||||||
get() = _deletedObjectsIdsFlow
|
|
||||||
|
|
||||||
protected abstract suspend fun updateObject(newValue: InputValueType, id: IdType, old: ObjectType): ObjectType
|
protected abstract suspend fun updateObject(newValue: InputValueType, id: IdType, old: ObjectType): ObjectType
|
||||||
protected abstract suspend fun createObject(newValue: InputValueType): Pair<IdType, ObjectType>
|
protected abstract suspend fun createObject(newValue: InputValueType): Pair<IdType, ObjectType>
|
||||||
@@ -44,7 +40,7 @@ abstract class WriteMapCRUDRepo<ObjectType, IdType, InputValueType>(
|
|||||||
val (id, newObject) = createObject(it)
|
val (id, newObject) = createObject(it)
|
||||||
map[id] = newObject
|
map[id] = newObject
|
||||||
newObject.also { _ ->
|
newObject.also { _ ->
|
||||||
_newObjectsFlow.send(newObject)
|
_newObjectsFlow.emit(newObject)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -54,7 +50,7 @@ abstract class WriteMapCRUDRepo<ObjectType, IdType, InputValueType>(
|
|||||||
|
|
||||||
return newValue.also {
|
return newValue.also {
|
||||||
map[id] = it
|
map[id] = it
|
||||||
_updatedObjectsFlow.send(it)
|
_updatedObjectsFlow.emit(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,7 +60,7 @@ abstract class WriteMapCRUDRepo<ObjectType, IdType, InputValueType>(
|
|||||||
|
|
||||||
override suspend fun deleteById(ids: List<IdType>) {
|
override suspend fun deleteById(ids: List<IdType>) {
|
||||||
ids.forEach {
|
ids.forEach {
|
||||||
map.remove(it) ?.also { _ -> _deletedObjectsIdsFlow.send(it) }
|
map.remove(it) ?.also { _ -> _deletedObjectsIdsFlow.emit(it) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,11 +1,11 @@
|
|||||||
package dev.inmo.micro_utils.repos
|
package dev.inmo.micro_utils.repos
|
||||||
|
|
||||||
import dev.inmo.micro_utils.coroutines.BroadcastFlow
|
|
||||||
import dev.inmo.micro_utils.pagination.Pagination
|
import dev.inmo.micro_utils.pagination.Pagination
|
||||||
import dev.inmo.micro_utils.pagination.PaginationResult
|
import dev.inmo.micro_utils.pagination.PaginationResult
|
||||||
import dev.inmo.micro_utils.pagination.utils.paginate
|
import dev.inmo.micro_utils.pagination.utils.paginate
|
||||||
import dev.inmo.micro_utils.pagination.utils.reverse
|
import dev.inmo.micro_utils.pagination.utils.reverse
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||||
|
|
||||||
class ReadMapKeyValueRepo<Key, Value>(
|
class ReadMapKeyValueRepo<Key, Value>(
|
||||||
private val map: Map<Key, Value> = emptyMap()
|
private val map: Map<Key, Value> = emptyMap()
|
||||||
@@ -39,6 +39,18 @@ class ReadMapKeyValueRepo<Key, Value>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override suspend fun keys(v: Value, pagination: Pagination, reversed: Boolean): PaginationResult<Key> {
|
||||||
|
val keys: List<Key> = map.mapNotNull { (k, value) -> if (v == value) k else null }
|
||||||
|
val actualPagination = if (reversed) pagination.reverse(keys.size) else pagination
|
||||||
|
return keys.paginate(actualPagination).let {
|
||||||
|
if (reversed) {
|
||||||
|
it.copy(results = it.results.reversed())
|
||||||
|
} else {
|
||||||
|
it
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override suspend fun contains(key: Key): Boolean = map.containsKey(key)
|
override suspend fun contains(key: Key): Boolean = map.containsKey(key)
|
||||||
|
|
||||||
override suspend fun count(): Long = map.size.toLong()
|
override suspend fun count(): Long = map.size.toLong()
|
||||||
@@ -47,20 +59,22 @@ class ReadMapKeyValueRepo<Key, Value>(
|
|||||||
class WriteMapKeyValueRepo<Key, Value>(
|
class WriteMapKeyValueRepo<Key, Value>(
|
||||||
private val map: MutableMap<Key, Value> = mutableMapOf()
|
private val map: MutableMap<Key, Value> = mutableMapOf()
|
||||||
) : WriteStandardKeyValueRepo<Key, Value> {
|
) : WriteStandardKeyValueRepo<Key, Value> {
|
||||||
private val _onNewValue: BroadcastFlow<Pair<Key, Value>> = BroadcastFlow()
|
private val _onNewValue: MutableSharedFlow<Pair<Key, Value>> = MutableSharedFlow()
|
||||||
override val onNewValue: Flow<Pair<Key, Value>>
|
override val onNewValue: Flow<Pair<Key, Value>>
|
||||||
get() = _onNewValue
|
get() = _onNewValue
|
||||||
private val _onValueRemoved: BroadcastFlow<Key> = BroadcastFlow()
|
private val _onValueRemoved: MutableSharedFlow<Key> = MutableSharedFlow()
|
||||||
override val onValueRemoved: Flow<Key>
|
override val onValueRemoved: Flow<Key>
|
||||||
get() = _onValueRemoved
|
get() = _onValueRemoved
|
||||||
|
|
||||||
override suspend fun set(k: Key, v: Value) {
|
override suspend fun set(toSet: Map<Key, Value>) {
|
||||||
map[k] = v
|
map.putAll(toSet)
|
||||||
_onNewValue.send(k to v)
|
toSet.forEach { (k, v) -> _onNewValue.emit(k to v) }
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun unset(k: Key) {
|
override suspend fun unset(toUnset: List<Key>) {
|
||||||
map.remove(k) ?.also { _onValueRemoved.send(k) }
|
toUnset.forEach { k ->
|
||||||
|
map.remove(k) ?.also { _ -> _onValueRemoved.emit(k) }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,10 +1,9 @@
|
|||||||
package dev.inmo.micro_utils.repos
|
package dev.inmo.micro_utils.repos
|
||||||
|
|
||||||
import dev.inmo.micro_utils.coroutines.BroadcastFlow
|
|
||||||
import dev.inmo.micro_utils.pagination.*
|
import dev.inmo.micro_utils.pagination.*
|
||||||
import dev.inmo.micro_utils.pagination.utils.paginate
|
import dev.inmo.micro_utils.pagination.utils.paginate
|
||||||
import dev.inmo.micro_utils.pagination.utils.reverse
|
import dev.inmo.micro_utils.pagination.utils.reverse
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.*
|
||||||
|
|
||||||
class MapReadOneToManyKeyValueRepo<Key, Value>(
|
class MapReadOneToManyKeyValueRepo<Key, Value>(
|
||||||
private val map: Map<Key, List<Value>> = emptyMap()
|
private val map: Map<Key, List<Value>> = emptyMap()
|
||||||
@@ -33,6 +32,18 @@ class MapReadOneToManyKeyValueRepo<Key, Value>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override suspend fun keys(v: Value, pagination: Pagination, reversed: Boolean): PaginationResult<Key> {
|
||||||
|
val keys = map.keys.filter { map[it] ?.contains(v) == true }
|
||||||
|
val actualPagination = if (reversed) pagination.reverse(keys.size) else pagination
|
||||||
|
return keys.paginate(actualPagination).let {
|
||||||
|
if (reversed) {
|
||||||
|
it.copy(results = it.results.reversed())
|
||||||
|
} else {
|
||||||
|
it
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override suspend fun contains(k: Key): Boolean = map.containsKey(k)
|
override suspend fun contains(k: Key): Boolean = map.containsKey(k)
|
||||||
|
|
||||||
override suspend fun contains(k: Key, v: Value): Boolean = map[k] ?.contains(v) == true
|
override suspend fun contains(k: Key, v: Value): Boolean = map[k] ?.contains(v) == true
|
||||||
@@ -45,27 +56,35 @@ class MapReadOneToManyKeyValueRepo<Key, Value>(
|
|||||||
class MapWriteOneToManyKeyValueRepo<Key, Value>(
|
class MapWriteOneToManyKeyValueRepo<Key, Value>(
|
||||||
private val map: MutableMap<Key, MutableList<Value>> = mutableMapOf()
|
private val map: MutableMap<Key, MutableList<Value>> = mutableMapOf()
|
||||||
) : WriteOneToManyKeyValueRepo<Key, Value> {
|
) : WriteOneToManyKeyValueRepo<Key, Value> {
|
||||||
private val _onNewValue: BroadcastFlow<Pair<Key, Value>> = BroadcastFlow()
|
private val _onNewValue: MutableSharedFlow<Pair<Key, Value>> = MutableSharedFlow()
|
||||||
override val onNewValue: Flow<Pair<Key, Value>>
|
override val onNewValue: Flow<Pair<Key, Value>> = _onNewValue.asSharedFlow()
|
||||||
get() = _onNewValue
|
private val _onValueRemoved: MutableSharedFlow<Pair<Key, Value>> = MutableSharedFlow()
|
||||||
private val _onValueRemoved: BroadcastFlow<Pair<Key, Value>> = BroadcastFlow()
|
override val onValueRemoved: Flow<Pair<Key, Value>> = _onValueRemoved.asSharedFlow()
|
||||||
override val onValueRemoved: Flow<Pair<Key, Value>>
|
private val _onDataCleared: MutableSharedFlow<Key> = MutableSharedFlow()
|
||||||
get() = _onValueRemoved
|
override val onDataCleared: Flow<Key> = _onDataCleared.asSharedFlow()
|
||||||
private val _onDataCleared: BroadcastFlow<Key> = BroadcastFlow()
|
|
||||||
override val onDataCleared: Flow<Key>
|
|
||||||
get() = _onDataCleared
|
|
||||||
|
|
||||||
override suspend fun add(k: Key, v: Value) {
|
override suspend fun add(toAdd: Map<Key, List<Value>>) {
|
||||||
map.getOrPut(k) { mutableListOf() }.add(v)
|
toAdd.keys.forEach { k ->
|
||||||
_onNewValue.send(k to v)
|
if (map.getOrPut(k) { mutableListOf() }.addAll(toAdd[k] ?: return@forEach)) {
|
||||||
|
toAdd[k] ?.forEach { v ->
|
||||||
|
_onNewValue.emit(k to v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun remove(k: Key, v: Value) {
|
override suspend fun remove(toRemove: Map<Key, List<Value>>) {
|
||||||
map[k] ?.remove(v) ?.also { _onValueRemoved.send(k to v) }
|
toRemove.keys.forEach { k ->
|
||||||
|
if (map[k] ?.removeAll(toRemove[k] ?: return@forEach) == true) {
|
||||||
|
toRemove[k] ?.forEach { v ->
|
||||||
|
_onValueRemoved.emit(k to v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun clear(k: Key) {
|
override suspend fun clear(k: Key) {
|
||||||
map.remove(k) ?.also { _onDataCleared.send(k) }
|
map.remove(k) ?.also { _onDataCleared.emit(k) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
1
repos/inmemory/src/main/AndroidManifest.xml
Normal file
1
repos/inmemory/src/main/AndroidManifest.xml
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<manifest package="dev.inmo.micro_utils.repos.inmemory"/>
|
@@ -1,6 +1,7 @@
|
|||||||
plugins {
|
plugins {
|
||||||
id "org.jetbrains.kotlin.multiplatform"
|
id "org.jetbrains.kotlin.multiplatform"
|
||||||
id "org.jetbrains.kotlin.plugin.serialization"
|
id "org.jetbrains.kotlin.plugin.serialization"
|
||||||
|
id "com.android.library"
|
||||||
}
|
}
|
||||||
|
|
||||||
apply from: "$mppProjectWithSerializationPresetPath"
|
apply from: "$mppProjectWithSerializationPresetPath"
|
||||||
|
@@ -6,6 +6,7 @@ import dev.inmo.micro_utils.ktor.common.buildStandardUrl
|
|||||||
import dev.inmo.micro_utils.pagination.*
|
import dev.inmo.micro_utils.pagination.*
|
||||||
import dev.inmo.micro_utils.repos.ReadStandardKeyValueRepo
|
import dev.inmo.micro_utils.repos.ReadStandardKeyValueRepo
|
||||||
import dev.inmo.micro_utils.repos.ktor.common.key_value.*
|
import dev.inmo.micro_utils.repos.ktor.common.key_value.*
|
||||||
|
import dev.inmo.micro_utils.repos.ktor.common.valueParameterName
|
||||||
import io.ktor.client.HttpClient
|
import io.ktor.client.HttpClient
|
||||||
import kotlinx.serialization.KSerializer
|
import kotlinx.serialization.KSerializer
|
||||||
import kotlinx.serialization.builtins.serializer
|
import kotlinx.serialization.builtins.serializer
|
||||||
@@ -50,6 +51,18 @@ class KtorReadStandardKeyValueRepo<Key, Value> (
|
|||||||
PaginationResult.serializer(keySerializer)
|
PaginationResult.serializer(keySerializer)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
override suspend fun keys(v: Value, pagination: Pagination, reversed: Boolean): PaginationResult<Key> = client.uniget(
|
||||||
|
buildStandardUrl(
|
||||||
|
baseUrl,
|
||||||
|
keysRoute,
|
||||||
|
mapOf(
|
||||||
|
valueParameterName to valueSerializer.encodeUrlQueryValue(v),
|
||||||
|
reversedParameterName to Boolean.serializer().encodeUrlQueryValue(reversed)
|
||||||
|
) + pagination.asUrlQueryParts
|
||||||
|
),
|
||||||
|
PaginationResult.serializer(keySerializer)
|
||||||
|
)
|
||||||
|
|
||||||
override suspend fun contains(key: Key): Boolean = client.uniget(
|
override suspend fun contains(key: Key): Boolean = client.uniget(
|
||||||
buildStandardUrl(
|
buildStandardUrl(
|
||||||
baseUrl,
|
baseUrl,
|
||||||
@@ -69,6 +82,3 @@ class KtorReadStandardKeyValueRepo<Key, Value> (
|
|||||||
Long.serializer()
|
Long.serializer()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Deprecated("Renamed", ReplaceWith("KtorReadStandardKeyValueRepo", "dev.inmo.micro_utils.repos.ktor.client.key_value.KtorReadStandardKeyValueRepo"))
|
|
||||||
typealias KtorStandartReadKeyValueRepo<Key, Value> = KtorReadStandardKeyValueRepo<Key, Value>
|
|
||||||
|
@@ -7,8 +7,7 @@ import dev.inmo.micro_utils.repos.ktor.common.key_value.*
|
|||||||
import io.ktor.client.HttpClient
|
import io.ktor.client.HttpClient
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
import kotlinx.serialization.KSerializer
|
import kotlinx.serialization.KSerializer
|
||||||
import kotlinx.serialization.builtins.PairSerializer
|
import kotlinx.serialization.builtins.*
|
||||||
import kotlinx.serialization.builtins.serializer
|
|
||||||
|
|
||||||
class KtorWriteStandardKeyValueRepo<K, V> (
|
class KtorWriteStandardKeyValueRepo<K, V> (
|
||||||
private var baseUrl: String,
|
private var baseUrl: String,
|
||||||
@@ -16,6 +15,8 @@ class KtorWriteStandardKeyValueRepo<K, V> (
|
|||||||
private var keySerializer: KSerializer<K>,
|
private var keySerializer: KSerializer<K>,
|
||||||
private var valueSerializer: KSerializer<V>,
|
private var valueSerializer: KSerializer<V>,
|
||||||
) : WriteStandardKeyValueRepo<K, V> {
|
) : WriteStandardKeyValueRepo<K, V> {
|
||||||
|
private val keyValueMapSerializer = MapSerializer(keySerializer, valueSerializer)
|
||||||
|
private val keysListSerializer = ListSerializer(keySerializer)
|
||||||
override val onNewValue: Flow<Pair<K, V>> = client.createStandardWebsocketFlow(
|
override val onNewValue: Flow<Pair<K, V>> = client.createStandardWebsocketFlow(
|
||||||
buildStandardUrl(baseUrl, onNewValueRoute),
|
buildStandardUrl(baseUrl, onNewValueRoute),
|
||||||
deserializer = PairSerializer(keySerializer, valueSerializer)
|
deserializer = PairSerializer(keySerializer, valueSerializer)
|
||||||
@@ -26,24 +27,21 @@ class KtorWriteStandardKeyValueRepo<K, V> (
|
|||||||
deserializer = keySerializer
|
deserializer = keySerializer
|
||||||
)
|
)
|
||||||
|
|
||||||
override suspend fun set(k: K, v: V) = client.unipost(
|
override suspend fun set(toSet: Map<K, V>) = client.unipost(
|
||||||
buildStandardUrl(
|
buildStandardUrl(
|
||||||
baseUrl,
|
baseUrl,
|
||||||
setRoute
|
setRoute
|
||||||
),
|
),
|
||||||
BodyPair(KeyValuePostObject.serializer(keySerializer, valueSerializer), KeyValuePostObject(k, v)),
|
BodyPair(keyValueMapSerializer, toSet),
|
||||||
Unit.serializer()
|
Unit.serializer()
|
||||||
)
|
)
|
||||||
|
|
||||||
override suspend fun unset(k: K) = client.unipost(
|
override suspend fun unset(toUnset: List<K>) = client.unipost(
|
||||||
buildStandardUrl(
|
buildStandardUrl(
|
||||||
baseUrl,
|
baseUrl,
|
||||||
unsetRoute,
|
unsetRoute,
|
||||||
),
|
),
|
||||||
BodyPair(keySerializer, k),
|
BodyPair(keysListSerializer, toUnset),
|
||||||
Unit.serializer()
|
Unit.serializer()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Deprecated("Renamed", ReplaceWith("KtorWriteStandardKeyValueRepo", "dev.inmo.micro_utils.repos.ktor.client.key_value.KtorWriteStandardKeyValueRepo"))
|
|
||||||
typealias KtorStandartWriteKeyValueRepo<K, V> = KtorWriteStandardKeyValueRepo<K, V>
|
|
||||||
|
@@ -5,7 +5,10 @@ import dev.inmo.micro_utils.ktor.client.uniget
|
|||||||
import dev.inmo.micro_utils.ktor.common.buildStandardUrl
|
import dev.inmo.micro_utils.ktor.common.buildStandardUrl
|
||||||
import dev.inmo.micro_utils.pagination.*
|
import dev.inmo.micro_utils.pagination.*
|
||||||
import dev.inmo.micro_utils.repos.ReadOneToManyKeyValueRepo
|
import dev.inmo.micro_utils.repos.ReadOneToManyKeyValueRepo
|
||||||
|
import dev.inmo.micro_utils.repos.ktor.common.keyParameterName
|
||||||
import dev.inmo.micro_utils.repos.ktor.common.one_to_many.*
|
import dev.inmo.micro_utils.repos.ktor.common.one_to_many.*
|
||||||
|
import dev.inmo.micro_utils.repos.ktor.common.reversedParameterName
|
||||||
|
import dev.inmo.micro_utils.repos.ktor.common.valueParameterName
|
||||||
import io.ktor.client.HttpClient
|
import io.ktor.client.HttpClient
|
||||||
import kotlinx.serialization.KSerializer
|
import kotlinx.serialization.KSerializer
|
||||||
import kotlinx.serialization.builtins.serializer
|
import kotlinx.serialization.builtins.serializer
|
||||||
@@ -42,6 +45,18 @@ class KtorReadOneToManyKeyValueRepo<Key, Value> (
|
|||||||
paginationKeyResultSerializer
|
paginationKeyResultSerializer
|
||||||
)
|
)
|
||||||
|
|
||||||
|
override suspend fun keys(v: Value, pagination: Pagination, reversed: Boolean): PaginationResult<Key> = client.uniget(
|
||||||
|
buildStandardUrl(
|
||||||
|
baseUrl,
|
||||||
|
keysRoute,
|
||||||
|
mapOf(
|
||||||
|
valueParameterName to valueSerializer.encodeUrlQueryValue(v),
|
||||||
|
reversedParameterName to Boolean.serializer().encodeUrlQueryValue(reversed)
|
||||||
|
) + pagination.asUrlQueryParts
|
||||||
|
),
|
||||||
|
paginationKeyResultSerializer
|
||||||
|
)
|
||||||
|
|
||||||
override suspend fun contains(k: Key): Boolean = client.uniget(
|
override suspend fun contains(k: Key): Boolean = client.uniget(
|
||||||
buildStandardUrl(
|
buildStandardUrl(
|
||||||
baseUrl,
|
baseUrl,
|
||||||
|
@@ -7,8 +7,7 @@ import dev.inmo.micro_utils.repos.ktor.common.one_to_many.*
|
|||||||
import io.ktor.client.HttpClient
|
import io.ktor.client.HttpClient
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
import kotlinx.serialization.KSerializer
|
import kotlinx.serialization.KSerializer
|
||||||
import kotlinx.serialization.builtins.PairSerializer
|
import kotlinx.serialization.builtins.*
|
||||||
import kotlinx.serialization.builtins.serializer
|
|
||||||
|
|
||||||
class KtorWriteOneToManyKeyValueRepo<Key, Value> (
|
class KtorWriteOneToManyKeyValueRepo<Key, Value> (
|
||||||
private val baseUrl: String,
|
private val baseUrl: String,
|
||||||
@@ -17,6 +16,7 @@ class KtorWriteOneToManyKeyValueRepo<Key, Value> (
|
|||||||
private val valueSerializer: KSerializer<Value>
|
private val valueSerializer: KSerializer<Value>
|
||||||
) : WriteOneToManyKeyValueRepo<Key, Value> {
|
) : WriteOneToManyKeyValueRepo<Key, Value> {
|
||||||
private val keyValueSerializer = PairSerializer(keySerializer, valueSerializer)
|
private val keyValueSerializer = PairSerializer(keySerializer, valueSerializer)
|
||||||
|
private val keyValueMapSerializer = MapSerializer(keySerializer, ListSerializer(valueSerializer))
|
||||||
override val onNewValue: Flow<Pair<Key, Value>> = client.createStandardWebsocketFlow(
|
override val onNewValue: Flow<Pair<Key, Value>> = client.createStandardWebsocketFlow(
|
||||||
buildStandardUrl(baseUrl, onNewValueRoute),
|
buildStandardUrl(baseUrl, onNewValueRoute),
|
||||||
deserializer = keyValueSerializer
|
deserializer = keyValueSerializer
|
||||||
@@ -30,25 +30,24 @@ class KtorWriteOneToManyKeyValueRepo<Key, Value> (
|
|||||||
deserializer = keySerializer
|
deserializer = keySerializer
|
||||||
)
|
)
|
||||||
|
|
||||||
override suspend fun add(k: Key, v: Value) = client.unipost(
|
override suspend fun remove(toRemove: Map<Key, List<Value>>) = client.unipost(
|
||||||
buildStandardUrl(
|
|
||||||
baseUrl,
|
|
||||||
addRoute,
|
|
||||||
),
|
|
||||||
BodyPair(keyValueSerializer, k to v),
|
|
||||||
Unit.serializer(),
|
|
||||||
)
|
|
||||||
|
|
||||||
override suspend fun remove(k: Key, v: Value) = client.unipost(
|
|
||||||
buildStandardUrl(
|
buildStandardUrl(
|
||||||
baseUrl,
|
baseUrl,
|
||||||
removeRoute,
|
removeRoute,
|
||||||
),
|
),
|
||||||
BodyPair(keyValueSerializer, k to v),
|
BodyPair(keyValueMapSerializer, toRemove),
|
||||||
Unit.serializer(),
|
Unit.serializer(),
|
||||||
)
|
)
|
||||||
|
|
||||||
override suspend fun clear(k: Key) = client.unipost(
|
override suspend fun add(toAdd: Map<Key, List<Value>>) = client.unipost(
|
||||||
|
buildStandardUrl(
|
||||||
|
baseUrl,
|
||||||
|
addRoute,
|
||||||
|
),
|
||||||
|
BodyPair(keyValueMapSerializer, toAdd),
|
||||||
|
Unit.serializer(),
|
||||||
|
)
|
||||||
|
override suspend fun clear(k: Key) = client.unipost(
|
||||||
buildStandardUrl(
|
buildStandardUrl(
|
||||||
baseUrl,
|
baseUrl,
|
||||||
clearRoute,
|
clearRoute,
|
||||||
@@ -56,4 +55,13 @@ class KtorWriteOneToManyKeyValueRepo<Key, Value> (
|
|||||||
BodyPair(keySerializer, k),
|
BodyPair(keySerializer, k),
|
||||||
Unit.serializer(),
|
Unit.serializer(),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
override suspend fun set(toSet: Map<Key, List<Value>>) = client.unipost(
|
||||||
|
buildStandardUrl(
|
||||||
|
baseUrl,
|
||||||
|
setRoute,
|
||||||
|
),
|
||||||
|
BodyPair(keyValueMapSerializer, toSet),
|
||||||
|
Unit.serializer(),
|
||||||
|
)
|
||||||
}
|
}
|
1
repos/ktor/client/src/main/AndroidManifest.xml
Normal file
1
repos/ktor/client/src/main/AndroidManifest.xml
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<manifest package="dev.inmo.micro_utils.repos.ktor.client"/>
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user