Skip to content

Commit 8d5707d

Browse files
authored
⚡️ Improve AssetPickerProvider.paths (#473)
1 parent 3755c1c commit 8d5707d

File tree

6 files changed

+37
-27
lines changed

6 files changed

+37
-27
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ that can be found in the LICENSE file. -->
66

77
See the [Migration Guide](guides/migration_guide.md) for the details of breaking changes between versions.
88

9+
## 8.6.3
10+
11+
### Improvements
12+
13+
- Improve `AssetPickerProvider.paths`.
14+
915
## 8.6.2
1016

1117
### Improvements

example/lib/customs/pickers/directory_file_asset_picker.dart

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -290,15 +290,14 @@ class FileAssetPickerProvider extends AssetPickerProvider<File, Directory> {
290290
@override
291291
Future<void> getPaths() async {
292292
currentAssets = <File>[];
293-
paths.clear();
294293
final Directory directory = await getApplicationDocumentsDirectory();
295294
final PathWrapper<Directory> wrapper = PathWrapper<Directory>(
296295
path: directory,
297296
thumbnailData: await getThumbnailFromPath(
298297
PathWrapper<Directory>(path: directory),
299298
),
300299
);
301-
paths.add(wrapper);
300+
paths = [wrapper];
302301
currentPath = wrapper;
303302
}
304303

@@ -315,6 +314,11 @@ class FileAssetPickerProvider extends AssetPickerProvider<File, Directory> {
315314
}
316315
hasAssetsToDisplay = currentAssets.isNotEmpty;
317316
totalAssetsCount = currentAssets.length;
317+
isAssetsEmpty = totalAssetsCount == 0;
318+
final PathWrapper<Directory> wrapper = currentPath!;
319+
if (wrapper.assetCount == null) {
320+
currentPath = currentPath!.copyWith(assetCount: totalAssetsCount);
321+
}
318322
}
319323

320324
@override
@@ -522,12 +526,8 @@ class FileAssetPickerBuilder
522526
children: <Widget>[
523527
Positioned.fill(
524528
child: Selector<FileAssetPickerProvider, bool>(
525-
selector: (
526-
_,
527-
FileAssetPickerProvider p,
528-
) =>
529-
p.hasAssetsToDisplay,
530-
builder: (_, bool hasAssetsToDisplay, __) {
529+
selector: (_, FileAssetPickerProvider p) => p.hasAssetsToDisplay,
530+
builder: (BuildContext context, bool hasAssetsToDisplay, __) {
531531
return AnimatedSwitcher(
532532
duration: switchingPathDuration,
533533
child: hasAssetsToDisplay
@@ -607,7 +607,7 @@ class FileAssetPickerBuilder
607607
delegate: SliverChildBuilderDelegate(
608608
(_, int index) => Builder(
609609
builder: (BuildContext c) {
610-
if (isAppleOS(context)) {
610+
if (isAppleOS(c)) {
611611
if (index < placeholderCount) {
612612
return const SizedBox.shrink();
613613
}
@@ -663,7 +663,7 @@ class FileAssetPickerBuilder
663663
child: Selector<FileAssetPickerProvider, List<File>>(
664664
selector: (_, FileAssetPickerProvider provider) =>
665665
provider.currentAssets,
666-
builder: (_, List<File> assets, __) => CustomScrollView(
666+
builder: (context, List<File> assets, __) => CustomScrollView(
667667
physics: const AlwaysScrollableScrollPhysics(),
668668
controller: gridScrollController,
669669
anchor: isAppleOS(context) ? anchor : 0,
@@ -676,7 +676,7 @@ class FileAssetPickerBuilder
676676
appBarPreferredSize!.height,
677677
),
678678
),
679-
sliverGrid(_, assets),
679+
sliverGrid(context, assets),
680680
// Ignore the gap when the [anchor] is not equal to 1.
681681
if (isAppleOS(context) && anchor == 1)
682682
SliverToBoxAdapter(

example/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: wechat_assets_picker_demo
22
description: The demo project for the wechat_assets_picker package.
3-
version: 8.6.2+42
3+
version: 8.6.3+43
44
publish_to: none
55

66
environment:

lib/src/delegates/asset_picker_builder_delegate.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,7 @@ class DefaultAssetPickerBuilderDelegate
832832
..hasAssetsToDisplay = false
833833
..isAssetsEmpty = true
834834
..totalAssetsCount = 0
835-
..paths.clear();
835+
..paths = [];
836836
return;
837837
}
838838
}

lib/src/provider/asset_picker_provider.dart

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import '../delegates/sort_path_delegate.dart';
1515
import '../internal/singleton.dart';
1616
import '../models/path_wrapper.dart';
1717

18-
/// [ChangeNotifier] for assets picker.
18+
/// Helps the assets picker to manage [Path]s and [Asset]s.
1919
///
2020
/// The provider maintain all methods that control assets and paths.
2121
/// By extending it you can customize how you can get all assets or paths,
@@ -29,7 +29,7 @@ abstract class AssetPickerProvider<Asset, Path> extends ChangeNotifier {
2929
List<Asset>? selectedAssets,
3030
}) : assert(maxAssets > 0, 'maxAssets must be greater than 0.'),
3131
assert(pageSize > 0, 'pageSize must be greater than 0.'),
32-
_previousSelectedAssets =
32+
previousSelectedAssets =
3333
selectedAssets?.toList(growable: false) ?? List<Asset>.empty(),
3434
_selectedAssets =
3535
selectedAssets?.toList() ?? List<Asset>.empty(growable: true);
@@ -48,6 +48,10 @@ abstract class AssetPickerProvider<Asset, Path> extends ChangeNotifier {
4848
/// 路径选择器中缩略图的大小
4949
final ThumbnailSize pathThumbnailSize;
5050

51+
/// Selected assets before the picker starts picking.
52+
/// 选择器开始选择前已选中的资源
53+
final List<Asset> previousSelectedAssets;
54+
5155
/// Clear all fields when dispose.
5256
/// 销毁时重置所有内容
5357
@override
@@ -127,15 +131,18 @@ abstract class AssetPickerProvider<Asset, Path> extends ChangeNotifier {
127131
notifyListeners();
128132
}
129133

130-
/// Map for all path entity.
131-
/// 所有包含资源的路径里列表
132-
///
133-
/// Using [Map] in order to save the thumbnail data
134-
/// for the first asset under the path.
135-
/// 使用 [Map] 来保存路径下第一个资源的缩略图数据
134+
/// List for all path entity wrapped by [PathWrapper].
135+
/// 所有资源路径的列表,以 [PathWrapper] 包装
136136
List<PathWrapper<Path>> get paths => _paths;
137137
List<PathWrapper<Path>> _paths = <PathWrapper<Path>>[];
138138

139+
set paths(List<PathWrapper<Path>> value) {
140+
if (value != _paths) {
141+
_paths = value;
142+
notifyListeners();
143+
}
144+
}
145+
139146
/// Set thumbnail [data] for the specific [path].
140147
/// 为指定的路径设置缩略图数据
141148
void setPathThumbnail(Path path, Uint8List? data) {
@@ -177,11 +184,6 @@ abstract class AssetPickerProvider<Asset, Path> extends ChangeNotifier {
177184
notifyListeners();
178185
}
179186

180-
/// Selected assets before the picker starts picking.
181-
/// 选择器开始选择前已选中的资源
182-
List<Asset> get previousSelectedAssets => _previousSelectedAssets;
183-
final List<Asset> _previousSelectedAssets;
184-
185187
/// Selected assets.
186188
/// 已选中的资源
187189
List<Asset> get selectedAssets => _selectedAssets;
@@ -232,6 +234,8 @@ abstract class AssetPickerProvider<Asset, Path> extends ChangeNotifier {
232234
}
233235
}
234236

237+
/// The default implementation of the [AssetPickerProvider] for the picker.
238+
/// The `Asset` is [AssetEntity], and the `Path` is [AssetPathEntity].
235239
class DefaultAssetPickerProvider
236240
extends AssetPickerProvider<AssetEntity, AssetPathEntity> {
237241
DefaultAssetPickerProvider({

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: wechat_assets_picker
2-
version: 8.6.2
2+
version: 8.6.3
33
description: |
44
An image picker (also with videos and audio)
55
for Flutter projects based on WeChat's UI,

0 commit comments

Comments
 (0)