2016-06-29 15:47:52 +02:00
|
|
|
package org.mariotaku.ktextension
|
|
|
|
|
|
|
|
import android.database.Cursor
|
2016-12-29 06:50:18 +01:00
|
|
|
import org.mariotaku.library.objectcursor.ObjectCursor
|
|
|
|
import java.util.*
|
2016-06-29 15:47:52 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by mariotaku on 16/6/29.
|
|
|
|
*/
|
|
|
|
|
2017-09-20 13:28:14 +02:00
|
|
|
fun Cursor.safeMoveToPosition(pos: Int) = try {
|
|
|
|
moveToPosition(pos)
|
|
|
|
} catch (e: IllegalStateException) {
|
|
|
|
false
|
2016-12-29 06:50:18 +01:00
|
|
|
}
|
|
|
|
|
2017-09-20 13:28:14 +02:00
|
|
|
fun Cursor.safeGetLong(columnIndex: Int, def: Long = -1) = try {
|
|
|
|
getLong(columnIndex)
|
|
|
|
} catch (e: IllegalStateException) {
|
|
|
|
def
|
2017-03-23 15:22:07 +01:00
|
|
|
}
|
|
|
|
|
2017-09-20 13:28:14 +02:00
|
|
|
fun Cursor.safeGetInt(columnIndex: Int, def: Int = -1) = try {
|
|
|
|
getInt(columnIndex)
|
|
|
|
} catch (e: IllegalStateException) {
|
|
|
|
def
|
2017-04-12 17:38:30 +02:00
|
|
|
}
|
|
|
|
|
2020-06-08 22:22:25 +02:00
|
|
|
fun Cursor.safeGetString(columnIndex: Int, def: String = ""): String = try {
|
2017-09-20 13:28:14 +02:00
|
|
|
getString(columnIndex)
|
|
|
|
} catch (e: IllegalStateException) {
|
|
|
|
def
|
2017-07-07 10:01:59 +02:00
|
|
|
}
|
|
|
|
|
2016-12-29 06:50:18 +01:00
|
|
|
fun <T> Cursor.map(indices: ObjectCursor.CursorIndices<T>): List<T> {
|
|
|
|
val list = ArrayList<T>()
|
|
|
|
moveToFirst()
|
|
|
|
while (!isAfterLast) {
|
|
|
|
list.add(indices.newObject(this))
|
|
|
|
moveToNext()
|
|
|
|
}
|
|
|
|
return list
|
2017-01-05 14:08:49 +01:00
|
|
|
}
|
|
|
|
|
2017-01-12 17:26:44 +01:00
|
|
|
val Cursor.isEmpty: Boolean
|
2017-04-06 17:27:47 +02:00
|
|
|
get() = count == 0
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param limit -1 for no limit
|
|
|
|
* @return Remaining count, -1 if no rows present
|
|
|
|
*/
|
|
|
|
inline fun Cursor.forEachRow(limit: Int = -1, action: (cur: Cursor, pos: Int) -> Boolean): Int {
|
|
|
|
moveToFirst()
|
|
|
|
var current = 0
|
|
|
|
while (!isAfterLast) {
|
|
|
|
@Suppress("ConvertTwoComparisonsToRangeCheck")
|
|
|
|
if (limit >= 0 && current >= limit) break
|
|
|
|
if (action(this, current)) {
|
|
|
|
current++
|
|
|
|
}
|
|
|
|
moveToNext()
|
|
|
|
}
|
|
|
|
return count - position
|
|
|
|
}
|