Skip to content

Commit

Permalink
⚡️ Improve AssetPickerProvider.paths (#473)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexV525 authored Aug 5, 2023
1 parent 3755c1c commit 8d5707d
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 27 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ that can be found in the LICENSE file. -->

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

## 8.6.3

### Improvements

- Improve `AssetPickerProvider.paths`.

## 8.6.2

### Improvements
Expand Down
22 changes: 11 additions & 11 deletions example/lib/customs/pickers/directory_file_asset_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -290,15 +290,14 @@ class FileAssetPickerProvider extends AssetPickerProvider<File, Directory> {
@override
Future<void> getPaths() async {
currentAssets = <File>[];
paths.clear();
final Directory directory = await getApplicationDocumentsDirectory();
final PathWrapper<Directory> wrapper = PathWrapper<Directory>(
path: directory,
thumbnailData: await getThumbnailFromPath(
PathWrapper<Directory>(path: directory),
),
);
paths.add(wrapper);
paths = [wrapper];
currentPath = wrapper;
}

Expand All @@ -315,6 +314,11 @@ class FileAssetPickerProvider extends AssetPickerProvider<File, Directory> {
}
hasAssetsToDisplay = currentAssets.isNotEmpty;
totalAssetsCount = currentAssets.length;
isAssetsEmpty = totalAssetsCount == 0;
final PathWrapper<Directory> wrapper = currentPath!;
if (wrapper.assetCount == null) {
currentPath = currentPath!.copyWith(assetCount: totalAssetsCount);
}
}

@override
Expand Down Expand Up @@ -522,12 +526,8 @@ class FileAssetPickerBuilder
children: <Widget>[
Positioned.fill(
child: Selector<FileAssetPickerProvider, bool>(
selector: (
_,
FileAssetPickerProvider p,
) =>
p.hasAssetsToDisplay,
builder: (_, bool hasAssetsToDisplay, __) {
selector: (_, FileAssetPickerProvider p) => p.hasAssetsToDisplay,
builder: (BuildContext context, bool hasAssetsToDisplay, __) {
return AnimatedSwitcher(
duration: switchingPathDuration,
child: hasAssetsToDisplay
Expand Down Expand Up @@ -607,7 +607,7 @@ class FileAssetPickerBuilder
delegate: SliverChildBuilderDelegate(
(_, int index) => Builder(
builder: (BuildContext c) {
if (isAppleOS(context)) {
if (isAppleOS(c)) {
if (index < placeholderCount) {
return const SizedBox.shrink();
}
Expand Down Expand Up @@ -663,7 +663,7 @@ class FileAssetPickerBuilder
child: Selector<FileAssetPickerProvider, List<File>>(
selector: (_, FileAssetPickerProvider provider) =>
provider.currentAssets,
builder: (_, List<File> assets, __) => CustomScrollView(
builder: (context, List<File> assets, __) => CustomScrollView(
physics: const AlwaysScrollableScrollPhysics(),
controller: gridScrollController,
anchor: isAppleOS(context) ? anchor : 0,
Expand All @@ -676,7 +676,7 @@ class FileAssetPickerBuilder
appBarPreferredSize!.height,
),
),
sliverGrid(_, assets),
sliverGrid(context, assets),
// Ignore the gap when the [anchor] is not equal to 1.
if (isAppleOS(context) && anchor == 1)
SliverToBoxAdapter(
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: wechat_assets_picker_demo
description: The demo project for the wechat_assets_picker package.
version: 8.6.2+42
version: 8.6.3+43
publish_to: none

environment:
Expand Down
2 changes: 1 addition & 1 deletion lib/src/delegates/asset_picker_builder_delegate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,7 @@ class DefaultAssetPickerBuilderDelegate
..hasAssetsToDisplay = false
..isAssetsEmpty = true
..totalAssetsCount = 0
..paths.clear();
..paths = [];
return;
}
}
Expand Down
30 changes: 17 additions & 13 deletions lib/src/provider/asset_picker_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import '../delegates/sort_path_delegate.dart';
import '../internal/singleton.dart';
import '../models/path_wrapper.dart';

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

/// Selected assets before the picker starts picking.
/// 选择器开始选择前已选中的资源
final List<Asset> previousSelectedAssets;

/// Clear all fields when dispose.
/// 销毁时重置所有内容
@override
Expand Down Expand Up @@ -127,15 +131,18 @@ abstract class AssetPickerProvider<Asset, Path> extends ChangeNotifier {
notifyListeners();
}

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

set paths(List<PathWrapper<Path>> value) {
if (value != _paths) {
_paths = value;
notifyListeners();
}
}

/// Set thumbnail [data] for the specific [path].
/// 为指定的路径设置缩略图数据
void setPathThumbnail(Path path, Uint8List? data) {
Expand Down Expand Up @@ -177,11 +184,6 @@ abstract class AssetPickerProvider<Asset, Path> extends ChangeNotifier {
notifyListeners();
}

/// Selected assets before the picker starts picking.
/// 选择器开始选择前已选中的资源
List<Asset> get previousSelectedAssets => _previousSelectedAssets;
final List<Asset> _previousSelectedAssets;

/// Selected assets.
/// 已选中的资源
List<Asset> get selectedAssets => _selectedAssets;
Expand Down Expand Up @@ -232,6 +234,8 @@ abstract class AssetPickerProvider<Asset, Path> extends ChangeNotifier {
}
}

/// The default implementation of the [AssetPickerProvider] for the picker.
/// The `Asset` is [AssetEntity], and the `Path` is [AssetPathEntity].
class DefaultAssetPickerProvider
extends AssetPickerProvider<AssetEntity, AssetPathEntity> {
DefaultAssetPickerProvider({
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: wechat_assets_picker
version: 8.6.2
version: 8.6.3
description: |
An image picker (also with videos and audio)
for Flutter projects based on WeChat's UI,
Expand Down

0 comments on commit 8d5707d

Please sign in to comment.