mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-11-17 13:53:49 +00:00
update OneToManyAndroidRepo
This commit is contained in:
parent
cdec8bac75
commit
2516d5e381
@ -2,6 +2,10 @@
|
||||
|
||||
## 0.5.9
|
||||
|
||||
* `Repos`
|
||||
* `Common`
|
||||
* `OneToManyAndroidRepo` got new primary constructor
|
||||
|
||||
## 0.5.8
|
||||
|
||||
* `Common`:
|
||||
|
@ -22,8 +22,10 @@ private val internalSerialFormat = Json {
|
||||
|
||||
class OneToManyAndroidRepo<Key, Value>(
|
||||
private val tableName: String,
|
||||
private val keySerializer: KSerializer<Key>,
|
||||
private val valueSerializer: KSerializer<Value>,
|
||||
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<Key, Value> {
|
||||
private val _onNewValue: MutableSharedFlow<Pair<Key, Value>> = MutableSharedFlow()
|
||||
@ -36,11 +38,6 @@ class OneToManyAndroidRepo<Key, Value>(
|
||||
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<Key, Value>(
|
||||
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<Key, Value>(
|
||||
|
||||
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<Key, Value>(
|
||||
override suspend fun set(toSet: Map<Key, List<Value>>) {
|
||||
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<Key, Value>(
|
||||
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<Key, Value>(
|
||||
}
|
||||
|
||||
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<Key, Value>(
|
||||
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<Key, Value>(
|
||||
}.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<Key, Value>(
|
||||
select(
|
||||
tableName,
|
||||
selection = "$idColumnName=?",
|
||||
selectionArgs = arrayOf(k.asId()),
|
||||
selectionArgs = arrayOf(k.keyAsString()),
|
||||
limit = resultPagination.limitClause()
|
||||
).use { c ->
|
||||
mutableListOf<Value>().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<Key, Value>(
|
||||
mutableListOf<Key>().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<Key, Value>(
|
||||
select(
|
||||
tableName,
|
||||
selection = "$valueColumnName=?",
|
||||
selectionArgs = arrayOf(v.asValue()),
|
||||
selectionArgs = arrayOf(v.valueAsString()),
|
||||
limit = resultPagination.limitClause()
|
||||
).use { c ->
|
||||
mutableListOf<Key>().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<Key, Value>(
|
||||
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<Key, Value>(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun <Key, Value> OneToManyAndroidRepo(
|
||||
tableName: String,
|
||||
keySerializer: KSerializer<Key>,
|
||||
valueSerializer: KSerializer<Value>,
|
||||
helper: SQLiteOpenHelper
|
||||
) = OneToManyAndroidRepo(
|
||||
tableName,
|
||||
{ internalSerialFormat.encodeToString(keySerializer, this) },
|
||||
{ internalSerialFormat.encodeToString(valueSerializer, this) },
|
||||
{ internalSerialFormat.decodeFromString(keySerializer, this) },
|
||||
{ internalSerialFormat.decodeFromString(valueSerializer, this) },
|
||||
helper
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user