Skip to content

Commit

Permalink
🐛 Do not throw when querying not existing assets in bulk on Android (#…
Browse files Browse the repository at this point in the history
…1216)

Resolves #1214
  • Loading branch information
AlexV525 authored Nov 1, 2024
1 parent ca76016 commit 59c5d3c
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 11 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ To know more about breaking changes, see the [Migration Guide][].

*None.*

## 3.6.1

### Fixes

- Do not throw when querying not existing assets in bulk on Android.

## 3.6.0

### Features
Expand Down
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
10 changes: 8 additions & 2 deletions lib/src/internal/plugin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,10 @@ class PhotoManagerPlugin with BasePlugin, IosPlugin, AndroidPlugin, OhosPlugin {
return result;
}

/// Use pagination to get album content.
/// Obtain assets with the pagination.
///
/// The length of returned assets might be less than requested.
/// Not existing assets will be excluded from the result.
Future<List<AssetEntity>> getAssetListPaged(
String id, {
required PMFilter optionGroup,
Expand All @@ -204,7 +207,10 @@ class PhotoManagerPlugin with BasePlugin, IosPlugin, AndroidPlugin, OhosPlugin {
return ConvertUtils.convertToAssetList(result.cast());
}

/// Asset in the specified range.
/// Obtain assets in the specified range.
///
/// The length of returned assets might be less than requested.
/// Not existing assets will be excluded from the result.
Future<List<AssetEntity>> getAssetListRange(
String id, {
required RequestType type,
Expand Down
8 changes: 7 additions & 1 deletion lib/src/types/entity.dart
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ class AssetPathEntity {
///
/// [page] should starts with and greater than 0.
/// [size] is item count of current [page].
///
/// The length of returned assets might be less than requested.
/// Not existing assets will be excluded from the result.
Future<List<AssetEntity>> getAssetListPaged({
required int page,
required int size,
Expand All @@ -206,8 +209,11 @@ class AssetPathEntity {
/// Getting assets in range using [start] and [end].
///
/// The [start] and [end] are similar to [String.substring], but it'll return
/// the maxmium assets if the total count of assets is fewer than the range,
/// the maximum assets if the total count of assets is fewer than the range,
/// instead of throwing a [RangeError] like [String.substring].
///
/// The length of returned assets might be less than requested.
/// Not existing assets will be excluded from the result.
Future<List<AssetEntity>> getAssetListRange({
required int start,
required int end,
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: photo_manager
description: A Flutter plugin that provides album assets abstraction management APIs on Android, iOS, macOS, and OpenHarmony.
repository: https://github.com/fluttercandies/flutter_photo_manager
version: 3.6.0
version: 3.6.1

environment:
sdk: ">=2.13.0 <4.0.0"
Expand Down

0 comments on commit 59c5d3c

Please sign in to comment.