Skip to content

Commit c019624

Browse files
authored
⚡️ Split assets fetching into separate steps (#596)
### 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
1 parent 04bfb65 commit c019624

File tree

3 files changed

+32
-23
lines changed

3 files changed

+32
-23
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ that can be found in the LICENSE file. -->
1818
- Support limited permission displays on Android.
1919
- Improves the limited overlay padding on Android.
2020
- Adds permission request lock for the picker state.
21-
22-
## 9.0.5
21+
- Speeding up by splitting asset loading into separate steps.
22+
- Speeding up using `AdvancedCustomFilter` rather than `FilterOptionGroup` by default.
2323

2424
### Fixes
2525

lib/src/provider/asset_picker_provider.dart

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,10 @@ class DefaultAssetPickerProvider
273273
}) {
274274
Singleton.sortPathDelegate = sortPathDelegate ?? SortPathDelegate.common;
275275
// Call [getAssetList] with route duration when constructing.
276-
Future<void>.delayed(initializeDelayDuration, getPaths);
276+
Future<void>.delayed(initializeDelayDuration, () async {
277+
await getPaths(onlyAll: true);
278+
await getPaths(onlyAll: false);
279+
});
277280
}
278281

279282
@visibleForTesting
@@ -326,41 +329,40 @@ class DefaultAssetPickerProvider
326329
}
327330

328331
@override
329-
Future<void> getPaths() async {
332+
Future<void> getPaths({bool onlyAll = false}) async {
330333
final PMFilter options;
331-
final PMFilter? fog = filterOptions;
332-
if (fog is FilterOptionGroup?) {
333-
// Initial base options.
334-
// Enable need title for audios to get proper display.
335-
final FilterOptionGroup newOptions = FilterOptionGroup(
334+
final fog = filterOptions;
335+
if (fog == null) {
336+
options = AdvancedCustomFilter(
337+
orderBy: [OrderByItem.desc(CustomColumns.base.createDate)],
338+
);
339+
} else if (fog is FilterOptionGroup) {
340+
final newOptions = FilterOptionGroup(
336341
imageOption: const FilterOption(
337342
sizeConstraint: SizeConstraint(ignoreSize: true),
338343
),
339344
audioOption: const FilterOption(
345+
// Enable title for audios to get proper display.
340346
needTitle: true,
341347
sizeConstraint: SizeConstraint(ignoreSize: true),
342348
),
343349
containsPathModified: sortPathsByModifiedDate,
344350
createTimeCond: DateTimeCond.def().copyWith(ignore: true),
345351
updateTimeCond: DateTimeCond.def().copyWith(ignore: true),
346352
);
347-
// Merge user's filter options into base options if it's not null.
348-
if (fog != null) {
349-
newOptions.merge(fog);
350-
}
353+
newOptions.merge(fog);
351354
options = newOptions;
352355
} else {
353356
options = fog;
354357
}
355358

356-
final List<AssetPathEntity> list = await PhotoManager.getAssetPathList(
359+
final list = await PhotoManager.getAssetPathList(
357360
type: requestType,
358361
filterOption: options,
362+
onlyAll: onlyAll,
359363
);
360364

361-
_paths = list
362-
.map((AssetPathEntity p) => PathWrapper<AssetPathEntity>(path: p))
363-
.toList();
365+
_paths = list.map((p) => PathWrapper<AssetPathEntity>(path: p)).toList();
364366
// Sort path using sort path delegate.
365367
Singleton.sortPathDelegate.sort(_paths);
366368
// Use sync method to avoid unnecessary wait.
@@ -373,17 +375,19 @@ class DefaultAssetPickerProvider
373375
_currentPath ??= _paths.first;
374376
}
375377

376-
await getAssetsFromCurrentPath();
378+
if (onlyAll) {
379+
await getAssetsFromCurrentPath();
380+
}
377381
}
378382

379383
Completer<void>? _getAssetsFromPathCompleter;
380384

381385
@override
382386
Future<void> getAssetsFromPath([int? page, AssetPathEntity? path]) {
383387
Future<void> run() async {
384-
final int currentPage = page ?? currentAssetsListPage;
385-
final AssetPathEntity currentPath = path ?? this.currentPath!.path;
386-
final List<AssetEntity> list = await currentPath.getAssetListPaged(
388+
final currentPage = page ?? currentAssetsListPage;
389+
final currentPath = path ?? this.currentPath!.path;
390+
final list = await currentPath.getAssetListPaged(
387391
page: currentPage,
388392
size: pageSize,
389393
);

lib/src/widget/asset_picker.dart

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// in the LICENSE file.
44

55
import 'dart:async';
6+
import 'dart:io';
67

78
import 'package:flutter/material.dart';
89
import 'package:flutter/services.dart';
@@ -121,8 +122,12 @@ class AssetPickerState<Asset, Path> extends State<AssetPicker<Asset, Path>>
121122
super.didChangeAppLifecycleState(state);
122123
if (state == AppLifecycleState.resumed) {
123124
requestPermission().then((ps) {
124-
if (mounted) {
125-
widget.builder.permission.value = ps;
125+
if (!mounted) {
126+
return;
127+
}
128+
widget.builder.permission.value = ps;
129+
if (ps == PermissionState.limited && Platform.isAndroid) {
130+
_onLimitedAssetsUpdated(const MethodCall(''));
126131
}
127132
});
128133
}

0 commit comments

Comments
 (0)