Skip to content

Commit

Permalink
🔥 Make the first asset count not blocking loads (fluttercandies#549)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexV525 authored Feb 6, 2024
1 parent 10dad6a commit dec7f51
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 53 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ that can be found in the LICENSE file. -->
### Improvements

- Use `wechat_picker_library`.
- Make the first asset count not blocking loads.

### Fixes

Expand Down
31 changes: 7 additions & 24 deletions lib/src/delegates/asset_picker_builder_delegate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1873,32 +1873,15 @@ class DefaultAssetPickerBuilderDelegate
end: 20,
),
child: ExcludeSemantics(
child: Row(
children: <Widget>[
Flexible(
child: Padding(
padding: const EdgeInsetsDirectional.only(
end: 10,
),
child: ScaleText(
name,
style: const TextStyle(fontSize: 17),
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
),
),
child: ScaleText.rich(
[
TextSpan(text: name),
if (semanticsCount != null)
ScaleText(
'($semanticsCount)',
style: TextStyle(
color: theme.textTheme.bodySmall?.color,
fontSize: 17,
),
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
TextSpan(text: ' ($semanticsCount)'),
],
style: const TextStyle(fontSize: 17),
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
),
),
Expand Down
50 changes: 21 additions & 29 deletions lib/src/provider/asset_picker_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,19 @@ abstract class AssetPickerProvider<Asset, Path> extends ChangeNotifier {
notifyListeners();
}

bool? _hasMoreToLoad;

/// Whether more assets are waiting for a load.
/// 是否还有更多资源可以加载
bool get hasMoreToLoad => _currentAssets.length < _totalAssetsCount!;
bool get hasMoreToLoad {
if (_hasMoreToLoad case final bool value) {
return value;
}
if (_totalAssetsCount case final int count) {
return _currentAssets.length < count;
}
return true;
}

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

@visibleForTesting
Expand Down Expand Up @@ -365,6 +372,8 @@ class DefaultAssetPickerProvider
if (_paths.isNotEmpty) {
_currentPath ??= _paths.first;
}

await getAssetsFromCurrentPath();
}

Completer<void>? _getAssetsFromPathCompleter;
Expand All @@ -380,18 +389,21 @@ class DefaultAssetPickerProvider
);
if (currentPage == 0) {
_currentAssets.clear();
} else if (list.isEmpty) {
_hasMoreToLoad = false;
}
_currentAssets.addAll(list);
_hasAssetsToDisplay = _currentAssets.isNotEmpty;
notifyListeners();
}

if (_getAssetsFromPathCompleter == null) {
_getAssetsFromPathCompleter = Completer<void>();
run().then((_) {
_getAssetsFromPathCompleter!.complete();
final completer = Completer<void>();
_getAssetsFromPathCompleter = completer;
run().then((r) {
completer.complete();
}).catchError((Object e, StackTrace s) {
_getAssetsFromPathCompleter!.completeError(e, s);
completer.completeError(e, s);
}).whenComplete(() {
_getAssetsFromPathCompleter = null;
});
Expand Down Expand Up @@ -505,26 +517,6 @@ class DefaultAssetPickerProvider
isAssetsEmpty = true;
return;
}
final PathWrapper<AssetPathEntity> wrapper = _currentPath!;
if (wrapper.assetCount == null) {
final int assetCount = await wrapper.path.assetCountAsync;
// If the picker was disposed (#492), stop fetching the assets.
if (!mounted) {
return;
}
if (_paths.isNotEmpty &&
_currentPath != null &&
_currentPath!.assetCount == null) {
_totalAssetsCount = assetCount;
_isAssetsEmpty = assetCount == 0;
_currentPath = wrapper.copyWith(assetCount: assetCount);
notifyListeners();
} else if (_currentPath?.assetCount case final int count) {
_totalAssetsCount = count;
_isAssetsEmpty = count == 0;
notifyListeners();
}
}
await getAssetsFromPath(0, currentPath!.path);
}
}

0 comments on commit dec7f51

Please sign in to comment.