Skip to content

Commit

Permalink
⚡️ Split assets fetching into separate steps (#596)
Browse files Browse the repository at this point in the history
### Changes
1. Change the fetching steps to loading the first album first, then the
others.
2. Use `AdvancedCustomFilter` by default to order items in desc without
extra search queries to decrease the query complexity.

- Resolves #496
- Resolves #526
- Resolves #547
  • Loading branch information
AlexV525 committed Jun 20, 2024
1 parent 04bfb65 commit c019624
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 23 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ that can be found in the LICENSE file. -->
- Support limited permission displays on Android.
- Improves the limited overlay padding on Android.
- Adds permission request lock for the picker state.

## 9.0.5
- Speeding up by splitting asset loading into separate steps.
- Speeding up using `AdvancedCustomFilter` rather than `FilterOptionGroup` by default.

### Fixes

Expand Down
42 changes: 23 additions & 19 deletions lib/src/provider/asset_picker_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,10 @@ class DefaultAssetPickerProvider
}) {
Singleton.sortPathDelegate = sortPathDelegate ?? SortPathDelegate.common;
// Call [getAssetList] with route duration when constructing.
Future<void>.delayed(initializeDelayDuration, getPaths);
Future<void>.delayed(initializeDelayDuration, () async {
await getPaths(onlyAll: true);
await getPaths(onlyAll: false);
});
}

@visibleForTesting
Expand Down Expand Up @@ -326,41 +329,40 @@ class DefaultAssetPickerProvider
}

@override
Future<void> getPaths() async {
Future<void> getPaths({bool onlyAll = false}) async {
final PMFilter options;
final PMFilter? fog = filterOptions;
if (fog is FilterOptionGroup?) {
// Initial base options.
// Enable need title for audios to get proper display.
final FilterOptionGroup newOptions = FilterOptionGroup(
final fog = filterOptions;
if (fog == null) {
options = AdvancedCustomFilter(
orderBy: [OrderByItem.desc(CustomColumns.base.createDate)],
);
} else if (fog is FilterOptionGroup) {
final newOptions = FilterOptionGroup(
imageOption: const FilterOption(
sizeConstraint: SizeConstraint(ignoreSize: true),
),
audioOption: const FilterOption(
// Enable title for audios to get proper display.
needTitle: true,
sizeConstraint: SizeConstraint(ignoreSize: true),
),
containsPathModified: sortPathsByModifiedDate,
createTimeCond: DateTimeCond.def().copyWith(ignore: true),
updateTimeCond: DateTimeCond.def().copyWith(ignore: true),
);
// Merge user's filter options into base options if it's not null.
if (fog != null) {
newOptions.merge(fog);
}
newOptions.merge(fog);
options = newOptions;
} else {
options = fog;
}

final List<AssetPathEntity> list = await PhotoManager.getAssetPathList(
final list = await PhotoManager.getAssetPathList(
type: requestType,
filterOption: options,
onlyAll: onlyAll,
);

_paths = list
.map((AssetPathEntity p) => PathWrapper<AssetPathEntity>(path: p))
.toList();
_paths = list.map((p) => PathWrapper<AssetPathEntity>(path: p)).toList();
// Sort path using sort path delegate.
Singleton.sortPathDelegate.sort(_paths);
// Use sync method to avoid unnecessary wait.
Expand All @@ -373,17 +375,19 @@ class DefaultAssetPickerProvider
_currentPath ??= _paths.first;
}

await getAssetsFromCurrentPath();
if (onlyAll) {
await getAssetsFromCurrentPath();
}
}

Completer<void>? _getAssetsFromPathCompleter;

@override
Future<void> getAssetsFromPath([int? page, AssetPathEntity? path]) {
Future<void> run() async {
final int currentPage = page ?? currentAssetsListPage;
final AssetPathEntity currentPath = path ?? this.currentPath!.path;
final List<AssetEntity> list = await currentPath.getAssetListPaged(
final currentPage = page ?? currentAssetsListPage;
final currentPath = path ?? this.currentPath!.path;
final list = await currentPath.getAssetListPaged(
page: currentPage,
size: pageSize,
);
Expand Down
9 changes: 7 additions & 2 deletions lib/src/widget/asset_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// in the LICENSE file.

import 'dart:async';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
Expand Down Expand Up @@ -121,8 +122,12 @@ class AssetPickerState<Asset, Path> extends State<AssetPicker<Asset, Path>>
super.didChangeAppLifecycleState(state);
if (state == AppLifecycleState.resumed) {
requestPermission().then((ps) {
if (mounted) {
widget.builder.permission.value = ps;
if (!mounted) {
return;
}
widget.builder.permission.value = ps;
if (ps == PermissionState.limited && Platform.isAndroid) {
_onLimitedAssetsUpdated(const MethodCall(''));
}
});
}
Expand Down

0 comments on commit c019624

Please sign in to comment.