Skip to content

Commit

Permalink
🐛 Do not throw when querying not existing assets in bulk
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexV525 committed Oct 31, 2024
1 parent ca76016 commit 2b752df
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ object AndroidQDBUtils : IDBUtils {
)
cursor.use {
cursorWithRange(it, page * size, size) { cursor ->
cursor.toAssetEntity(context).apply {
cursor.toAssetEntity(context, throwIfNotExists = false)?.apply {
list.add(this)
}
}
Expand Down Expand Up @@ -203,7 +203,7 @@ object AndroidQDBUtils : IDBUtils {
)
cursor.use {
cursorWithRange(it, start, pageSize) { cursor ->
cursor.toAssetEntity(context).apply {
cursor.toAssetEntity(context, throwIfNotExists = false)?.apply {
list.add(this)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ object DBUtils : IDBUtils {
)
cursor.use {
while (it.moveToNext()) {
it.toAssetEntity(context).apply {
it.toAssetEntity(context, throwIfNotExists = false)?.apply {
list.add(this)
}
}
Expand Down Expand Up @@ -201,7 +201,7 @@ object DBUtils : IDBUtils {
)
cursor.use {
while (it.moveToNext()) {
it.toAssetEntity(context).apply {
it.toAssetEntity(context, throwIfNotExists = false)?.apply {
list.add(this)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,18 @@ interface IDBUtils {
return getDouble(getColumnIndex(columnName))
}

fun Cursor.toAssetEntity(context: Context, checkIfExists: Boolean = true): AssetEntity {
fun Cursor.toAssetEntity(
context: Context,
checkIfExists: Boolean = true,
throwIfNotExists: Boolean = true,
): AssetEntity? {
val id = getLong(_ID)
val path = getString(DATA)
if (checkIfExists && path.isNotBlank() && !File(path).exists()) {
throwMsg("Asset ($id) does not exists at its path ($path).")
if (throwIfNotExists) {
throwMsg("Asset ($id) does not exists at its path ($path).")
}
return null
}

val date = if (isAboveAndroidQ) {
Expand Down Expand Up @@ -699,7 +706,7 @@ interface IDBUtils {
val result = ArrayList<AssetEntity>()
it.moveToPosition(start - 1)
while (it.moveToNext()) {
val asset = it.toAssetEntity(context, false)
val asset = it.toAssetEntity(context, false) ?: continue
result.add(asset)
if (result.count() == end - start) {
break
Expand Down

0 comments on commit 2b752df

Please sign in to comment.