Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Do not throw when querying not existing assets in bulk on Android #1216

Merged
merged 3 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading