Skip to content

Commit dec7f51

Browse files
authored
🔥 Make the first asset count not blocking loads (#549)
1 parent 10dad6a commit dec7f51

File tree

3 files changed

+29
-53
lines changed

3 files changed

+29
-53
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ that can be found in the LICENSE file. -->
1212
### Improvements
1313

1414
- Use `wechat_picker_library`.
15+
- Make the first asset count not blocking loads.
1516

1617
### Fixes
1718

lib/src/delegates/asset_picker_builder_delegate.dart

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1873,32 +1873,15 @@ class DefaultAssetPickerBuilderDelegate
18731873
end: 20,
18741874
),
18751875
child: ExcludeSemantics(
1876-
child: Row(
1877-
children: <Widget>[
1878-
Flexible(
1879-
child: Padding(
1880-
padding: const EdgeInsetsDirectional.only(
1881-
end: 10,
1882-
),
1883-
child: ScaleText(
1884-
name,
1885-
style: const TextStyle(fontSize: 17),
1886-
maxLines: 1,
1887-
overflow: TextOverflow.ellipsis,
1888-
),
1889-
),
1890-
),
1876+
child: ScaleText.rich(
1877+
[
1878+
TextSpan(text: name),
18911879
if (semanticsCount != null)
1892-
ScaleText(
1893-
'($semanticsCount)',
1894-
style: TextStyle(
1895-
color: theme.textTheme.bodySmall?.color,
1896-
fontSize: 17,
1897-
),
1898-
maxLines: 1,
1899-
overflow: TextOverflow.ellipsis,
1900-
),
1880+
TextSpan(text: ' ($semanticsCount)'),
19011881
],
1882+
style: const TextStyle(fontSize: 17),
1883+
maxLines: 1,
1884+
overflow: TextOverflow.ellipsis,
19021885
),
19031886
),
19041887
),

lib/src/provider/asset_picker_provider.dart

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,19 @@ abstract class AssetPickerProvider<Asset, Path> extends ChangeNotifier {
121121
notifyListeners();
122122
}
123123

124+
bool? _hasMoreToLoad;
125+
124126
/// Whether more assets are waiting for a load.
125127
/// 是否还有更多资源可以加载
126-
bool get hasMoreToLoad => _currentAssets.length < _totalAssetsCount!;
128+
bool get hasMoreToLoad {
129+
if (_hasMoreToLoad case final bool value) {
130+
return value;
131+
}
132+
if (_totalAssetsCount case final int count) {
133+
return _currentAssets.length < count;
134+
}
135+
return true;
136+
}
127137

128138
/// The current page for assets list.
129139
/// 当前加载的资源列表分页数
@@ -263,10 +273,7 @@ class DefaultAssetPickerProvider
263273
}) {
264274
Singleton.sortPathDelegate = sortPathDelegate ?? SortPathDelegate.common;
265275
// Call [getAssetList] with route duration when constructing.
266-
Future<void>.delayed(initializeDelayDuration, () async {
267-
await getPaths();
268-
await getAssetsFromCurrentPath();
269-
});
276+
Future<void>.delayed(initializeDelayDuration, getPaths);
270277
}
271278

272279
@visibleForTesting
@@ -365,6 +372,8 @@ class DefaultAssetPickerProvider
365372
if (_paths.isNotEmpty) {
366373
_currentPath ??= _paths.first;
367374
}
375+
376+
await getAssetsFromCurrentPath();
368377
}
369378

370379
Completer<void>? _getAssetsFromPathCompleter;
@@ -380,18 +389,21 @@ class DefaultAssetPickerProvider
380389
);
381390
if (currentPage == 0) {
382391
_currentAssets.clear();
392+
} else if (list.isEmpty) {
393+
_hasMoreToLoad = false;
383394
}
384395
_currentAssets.addAll(list);
385396
_hasAssetsToDisplay = _currentAssets.isNotEmpty;
386397
notifyListeners();
387398
}
388399

389400
if (_getAssetsFromPathCompleter == null) {
390-
_getAssetsFromPathCompleter = Completer<void>();
391-
run().then((_) {
392-
_getAssetsFromPathCompleter!.complete();
401+
final completer = Completer<void>();
402+
_getAssetsFromPathCompleter = completer;
403+
run().then((r) {
404+
completer.complete();
393405
}).catchError((Object e, StackTrace s) {
394-
_getAssetsFromPathCompleter!.completeError(e, s);
406+
completer.completeError(e, s);
395407
}).whenComplete(() {
396408
_getAssetsFromPathCompleter = null;
397409
});
@@ -505,26 +517,6 @@ class DefaultAssetPickerProvider
505517
isAssetsEmpty = true;
506518
return;
507519
}
508-
final PathWrapper<AssetPathEntity> wrapper = _currentPath!;
509-
if (wrapper.assetCount == null) {
510-
final int assetCount = await wrapper.path.assetCountAsync;
511-
// If the picker was disposed (#492), stop fetching the assets.
512-
if (!mounted) {
513-
return;
514-
}
515-
if (_paths.isNotEmpty &&
516-
_currentPath != null &&
517-
_currentPath!.assetCount == null) {
518-
_totalAssetsCount = assetCount;
519-
_isAssetsEmpty = assetCount == 0;
520-
_currentPath = wrapper.copyWith(assetCount: assetCount);
521-
notifyListeners();
522-
} else if (_currentPath?.assetCount case final int count) {
523-
_totalAssetsCount = count;
524-
_isAssetsEmpty = count == 0;
525-
notifyListeners();
526-
}
527-
}
528520
await getAssetsFromPath(0, currentPath!.path);
529521
}
530522
}

0 commit comments

Comments
 (0)