From cdec8bac75bc039c47069f96bcfc2adb76752196 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 13 Jun 2021 11:48:00 +0600 Subject: [PATCH 1/3] start 0.5.9 --- CHANGELOG.md | 2 ++ gradle.properties | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3c155a10ae4..3a02e7ceb5f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # Changelog +## 0.5.9 + ## 0.5.8 * `Common`: diff --git a/gradle.properties b/gradle.properties index c71491698b6..19786d527a8 100644 --- a/gradle.properties +++ b/gradle.properties @@ -45,5 +45,5 @@ dokka_version=1.4.32 # Project data group=dev.inmo -version=0.5.8 -android_code_version=49 +version=0.5.9 +android_code_version=50 From 2516d5e381ddc4d13236615282ae4f0cbf30943a Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 13 Jun 2021 11:48:37 +0600 Subject: [PATCH 2/3] update OneToManyAndroidRepo --- CHANGELOG.md | 4 ++ .../repos/onetomany/OneToManyAndroidRepo.kt | 53 +++++++++++-------- 2 files changed, 36 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a02e7ceb5f..89524b285b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## 0.5.9 +* `Repos` + * `Common` + * `OneToManyAndroidRepo` got new primary constructor + ## 0.5.8 * `Common`: diff --git a/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/onetomany/OneToManyAndroidRepo.kt b/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/onetomany/OneToManyAndroidRepo.kt index bc910985df6..233e08f9b05 100644 --- a/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/onetomany/OneToManyAndroidRepo.kt +++ b/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/onetomany/OneToManyAndroidRepo.kt @@ -22,8 +22,10 @@ private val internalSerialFormat = Json { class OneToManyAndroidRepo( private val tableName: String, - private val keySerializer: KSerializer, - private val valueSerializer: KSerializer, + private val keyAsString: Key.() -> String, + private val valueAsString: Value.() -> String, + private val keyFromString: String.() -> Key, + private val valueFromString: String.() -> Value, private val helper: SQLiteOpenHelper ) : OneToManyKeyValueRepo { private val _onNewValue: MutableSharedFlow> = MutableSharedFlow() @@ -36,11 +38,6 @@ class OneToManyAndroidRepo( 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 { helper.blockingWritableTransaction { createTable( @@ -61,8 +58,8 @@ class OneToManyAndroidRepo( tableName, null, contentValuesOf( - idColumnName to k.asId(), - valueColumnName to v.asValue() + idColumnName to k.keyAsString(), + valueColumnName to v.valueAsString() ) ).also { if (it != -1L) { @@ -77,7 +74,7 @@ class OneToManyAndroidRepo( override suspend fun clear(k: Key) { helper.blockingWritableTransaction { - delete(tableName, "$idColumnName=?", arrayOf(k.asId())) + delete(tableName, "$idColumnName=?", arrayOf(k.keyAsString())) }.also { if (it > 0) { _onDataCleared.emit(k) @@ -88,7 +85,7 @@ class OneToManyAndroidRepo( override suspend fun set(toSet: Map>) { val (clearedKeys, inserted) = helper.blockingWritableTransaction { toSet.mapNotNull { (k, _) -> - if (delete(tableName, "$idColumnName=?", arrayOf(k.asId())) > 0) { + if (delete(tableName, "$idColumnName=?", arrayOf(k.keyAsString())) > 0) { k } else { null @@ -98,7 +95,7 @@ class OneToManyAndroidRepo( insert( tableName, null, - contentValuesOf(idColumnName to k.asId(), valueColumnName to v.asValue()) + contentValuesOf(idColumnName to k.keyAsString(), valueColumnName to v.valueAsString()) ) k to v } @@ -109,7 +106,7 @@ class OneToManyAndroidRepo( } override suspend fun contains(k: Key): Boolean = helper.blockingReadableTransaction { - select(tableName, selection = "$idColumnName=?", selectionArgs = arrayOf(k.asId()), limit = FirstPagePagination(1).limitClause()).use { + select(tableName, selection = "$idColumnName=?", selectionArgs = arrayOf(k.keyAsString()), limit = FirstPagePagination(1).limitClause()).use { it.count > 0 } } @@ -118,7 +115,7 @@ class OneToManyAndroidRepo( select( tableName, selection = "$idColumnName=? AND $valueColumnName=?", - selectionArgs = arrayOf(k.asId(), v.asValue()), + selectionArgs = arrayOf(k.keyAsString(), v.valueAsString()), limit = FirstPagePagination(1).limitClause() ).use { it.count > 0 @@ -134,7 +131,7 @@ class OneToManyAndroidRepo( }.toLong() override suspend fun count(k: Key): Long = helper.blockingReadableTransaction { - select(tableName, selection = "$idColumnName=?", selectionArgs = arrayOf(k.asId()), limit = FirstPagePagination(1).limitClause()).use { + select(tableName, selection = "$idColumnName=?", selectionArgs = arrayOf(k.keyAsString()), limit = FirstPagePagination(1).limitClause()).use { it.count } }.toLong() @@ -149,13 +146,13 @@ class OneToManyAndroidRepo( select( tableName, selection = "$idColumnName=?", - selectionArgs = arrayOf(k.asId()), + selectionArgs = arrayOf(k.keyAsString()), limit = resultPagination.limitClause() ).use { c -> mutableListOf().also { if (c.moveToFirst()) { do { - it.add(c.getString(valueColumnName).asValue()) + it.add(c.getString(valueColumnName).valueFromString()) } while (c.moveToNext()) } } @@ -179,7 +176,7 @@ class OneToManyAndroidRepo( mutableListOf().also { if (c.moveToFirst()) { do { - it.add(c.getString(idColumnName).asKey()) + it.add(c.getString(idColumnName).keyFromString()) } while (c.moveToNext()) } } @@ -200,13 +197,13 @@ class OneToManyAndroidRepo( select( tableName, selection = "$valueColumnName=?", - selectionArgs = arrayOf(v.asValue()), + selectionArgs = arrayOf(v.valueAsString()), limit = resultPagination.limitClause() ).use { c -> mutableListOf().also { if (c.moveToFirst()) { do { - it.add(c.getString(idColumnName).asKey()) + it.add(c.getString(idColumnName).keyFromString()) } while (c.moveToNext()) } } @@ -221,7 +218,7 @@ class OneToManyAndroidRepo( helper.blockingWritableTransaction { toRemove.flatMap { (k, vs) -> vs.mapNotNullA { v -> - if (delete(tableName, "$idColumnName=? AND $valueColumnName=?", arrayOf(k.asId(), v.asValue())) > 0) { + if (delete(tableName, "$idColumnName=? AND $valueColumnName=?", arrayOf(k.keyAsString(), v.valueAsString())) > 0) { k to v } else { null @@ -233,3 +230,17 @@ class OneToManyAndroidRepo( } } } + +fun OneToManyAndroidRepo( + tableName: String, + keySerializer: KSerializer, + valueSerializer: KSerializer, + helper: SQLiteOpenHelper +) = OneToManyAndroidRepo( + tableName, + { internalSerialFormat.encodeToString(keySerializer, this) }, + { internalSerialFormat.encodeToString(valueSerializer, this) }, + { internalSerialFormat.decodeFromString(keySerializer, this) }, + { internalSerialFormat.decodeFromString(valueSerializer, this) }, + helper +) From 7cccf7e56e92345256289122421437a7ad37c6e4 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 13 Jun 2021 11:50:48 +0600 Subject: [PATCH 3/3] upfix update --- .../micro_utils/repos/onetomany/OneToManyAndroidRepo.kt | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/onetomany/OneToManyAndroidRepo.kt b/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/onetomany/OneToManyAndroidRepo.kt index 233e08f9b05..d8c4c7d248a 100644 --- a/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/onetomany/OneToManyAndroidRepo.kt +++ b/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/onetomany/OneToManyAndroidRepo.kt @@ -20,6 +20,8 @@ private val internalSerialFormat = Json { ignoreUnknownKeys = true } +typealias KeyValuesAndroidRepo = OneToManyAndroidRepo + class OneToManyAndroidRepo( private val tableName: String, private val keyAsString: Key.() -> String, @@ -244,3 +246,10 @@ fun OneToManyAndroidRepo( { internalSerialFormat.decodeFromString(valueSerializer, this) }, helper ) + +fun KeyValuesAndroidRepo( + tableName: String, + keySerializer: KSerializer, + valueSerializer: KSerializer, + helper: SQLiteOpenHelper +) = OneToManyAndroidRepo(tableName, keySerializer, valueSerializer, helper)