From eaab93c57897a1b94115a2b1002628e763b24e82 Mon Sep 17 00:00:00 2001 From: Alex Li Date: Mon, 5 Feb 2024 13:46:00 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20view=20assets=20with=20onl?= =?UTF-8?q?y=20selected=20(#546)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #536 --- CHANGELOG.md | 4 ++++ .../pickers/directory_file_asset_picker.dart | 7 ++++--- .../lib/customs/pickers/insta_asset_picker.dart | 5 ++++- guides/migration_guide.md | 11 +++++++++-- .../delegates/asset_picker_builder_delegate.dart | 14 +++++++------- 5 files changed, 28 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6095243f..e14dd4e6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,10 @@ that can be found in the LICENSE file. --> - Use `wechat_picker_library`. +### Fixes + +- Fix previewing selected assets behavior. + ## 9.0.0-dev.2 ### Fixes diff --git a/example/lib/customs/pickers/directory_file_asset_picker.dart b/example/lib/customs/pickers/directory_file_asset_picker.dart index 60121452..79799faa 100644 --- a/example/lib/customs/pickers/directory_file_asset_picker.dart +++ b/example/lib/customs/pickers/directory_file_asset_picker.dart @@ -381,8 +381,8 @@ class FileAssetPickerBuilder @override Future viewAsset( BuildContext context, - int index, - AssetEntity currentAsset, + int? index, + File currentAsset, ) async { final List? result = await Navigator.of(context).push?>( PageRouteBuilder>( @@ -393,7 +393,8 @@ class FileAssetPickerBuilder ) { return AssetPickerViewer( builder: FileAssetPickerViewerBuilderDelegate( - currentIndex: index, + currentIndex: + index ?? provider.selectedAssets.indexOf(currentAsset), previewAssets: provider.selectedAssets, provider: FileAssetPickerViewerProvider(provider.selectedAssets), themeData: AssetPicker.themeData(themeColor), diff --git a/example/lib/customs/pickers/insta_asset_picker.dart b/example/lib/customs/pickers/insta_asset_picker.dart index d0003572..879fd87d 100644 --- a/example/lib/customs/pickers/insta_asset_picker.dart +++ b/example/lib/customs/pickers/insta_asset_picker.dart @@ -378,9 +378,12 @@ class InstaAssetPickerBuilder extends DefaultAssetPickerBuilderDelegate { @override Future viewAsset( BuildContext context, - int index, + int? index, AssetEntity currentAsset, ) async { + if (index == null) { + return; + } // if is preview asset, unselect it if (provider.selectedAssets.isNotEmpty && _previewAsset.value == currentAsset) { diff --git a/guides/migration_guide.md b/guides/migration_guide.md index 6de65974..88720af6 100644 --- a/guides/migration_guide.md +++ b/guides/migration_guide.md @@ -19,7 +19,14 @@ This document gathered all breaking changes and migrations requirement between m ## 9.0.0 -### Summary +### View assets signature change + +`AssetPickerBuilderDelegate.viewAsset` has 2 changes: +- It now uses the generic type of the delegate rather than always `AssetEntity`. +- The `index` of arguments is now nullable, + to indicate the behavior of previewing selected assets only. + +### Permission request option integration `PermissionRequestOption` has been added to `AssetPickerDelegate.permissionCheck` and @@ -28,7 +35,7 @@ Classes that extend `AssetPickerDelegate` and override these methods must migrat Delegates that use `AssetPicker.permissionCheck` should choose whether to pass the request option. -### Details +#### Details Before: diff --git a/lib/src/delegates/asset_picker_builder_delegate.dart b/lib/src/delegates/asset_picker_builder_delegate.dart index 259a43e2..1c03810c 100644 --- a/lib/src/delegates/asset_picker_builder_delegate.dart +++ b/lib/src/delegates/asset_picker_builder_delegate.dart @@ -242,11 +242,7 @@ abstract class AssetPickerBuilderDelegate { /// Determine how to browse assets in the viewer. /// 定义如何在查看器中浏览资源 - Future viewAsset( - BuildContext context, - int index, - AssetEntity currentAsset, - ); + Future viewAsset(BuildContext context, int? index, Asset currentAsset); /// Yes, the build method. /// 没错,是它是它就是它,我们亲爱的 build 方法~ @@ -870,7 +866,7 @@ class DefaultAssetPickerBuilderDelegate @override Future viewAsset( BuildContext context, - int index, + int? index, AssetEntity currentAsset, ) async { final DefaultAssetPickerProvider provider = @@ -902,6 +898,10 @@ class DefaultAssetPickerBuilderDelegate selected = provider.selectedAssets; effectiveIndex = current.indexOf(currentAsset); } + } else if (index == null) { + current = provider.selectedAssets; + selected = provider.selectedAssets; + effectiveIndex = selected.indexOf(currentAsset); } else { current = provider.currentAssets; selected = provider.selectedAssets; @@ -1936,7 +1936,7 @@ class DefaultAssetPickerBuilderDelegate child: Consumer( builder: (context, DefaultAssetPickerProvider p, __) => GestureDetector( onTap: p.isSelectedNotEmpty - ? () => viewAsset(context, 0, p.selectedAssets.first) + ? () => viewAsset(context, null, p.selectedAssets.first) : null, child: Selector( selector: (_, DefaultAssetPickerProvider p) =>