From 15e9254e007e4c8c14e221c5bda2172daeecd9a5 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 13 Apr 2022 15:54:48 +0600 Subject: [PATCH] fixes in OneToManyAndroidRepo and adding of CursorIterator --- CHANGELOG.md | 5 ++++ .../inmo/micro_utils/repos/CursorIterator.kt | 27 +++++++++++++++++++ .../repos/onetomany/OneToManyAndroidRepo.kt | 8 +++++- 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/CursorIterator.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 190b3f631d1..b8ddee40a15 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ ## 0.9.20 +* `Repos`: + * `Common`: + * Fixes in `OneToManyAndroidRepo` + * New `CursorIterator` + ## 0.9.19 * `Versions`: diff --git a/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/CursorIterator.kt b/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/CursorIterator.kt new file mode 100644 index 00000000000..9c7d21fcbcd --- /dev/null +++ b/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/CursorIterator.kt @@ -0,0 +1,27 @@ +package dev.inmo.micro_utils.repos + +import android.database.Cursor + +class CursorIterator( + private val c: Cursor +) : Iterator { + private var i = 0 + + init { + c.moveToFirst() + } + override fun hasNext(): Boolean { + return i < c.count + } + + override fun next(): Cursor { + i++ + return if (c.moveToNext()) { + c + } else { + throw NoSuchElementException() + } + } +} + +operator fun Cursor.iterator(): CursorIterator = CursorIterator(this) 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 1cc184fc2b4..96be3bd2f77 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 @@ -143,7 +143,13 @@ class OneToManyAndroidRepo( }.toLong() override suspend fun count(k: Key): Long = helper.blockingReadableTransaction { - selectDistinct(tableName, columns = valueColumnArray, selection = "$idColumnName=?", selectionArgs = arrayOf(k.keyAsString()), limit = FirstPagePagination(1).limitClause()).use { + select( + tableName, + columns = valueColumnArray, + selection = "$idColumnName=?", + selectionArgs = arrayOf(k.keyAsString()), + limit = FirstPagePagination(1).limitClause() + ).use { it.count } }.toLong()