From 6f7fe7ede341c82e5f9e140e7e66de1cede85b2b Mon Sep 17 00:00:00 2001 From: Alex Li Date: Thu, 16 Nov 2023 09:57:09 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20Use=20`viewAsset`=20in=20t?= =?UTF-8?q?he=20preview=20button=20(#508)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Resolves https://github.com/fluttercandies/flutter_wechat_assets_picker/discussions/507#discussioncomment-7572278. --- CHANGELOG.md | 6 + example/ios/Podfile | 6 +- example/macos/Podfile | 105 ++++++++---------- example/pubspec.yaml | 2 +- .../asset_picker_builder_delegate.dart | 56 +++------- pubspec.yaml | 2 +- 6 files changed, 74 insertions(+), 103 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ef826df..a839b351 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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.7.2 + +### Improvements + +- Use `viewAsset` in the preview button. + ## 8.7.1 ### Improvements diff --git a/example/ios/Podfile b/example/ios/Podfile index fb9d86a0..0cf927fd 100644 --- a/example/ios/Podfile +++ b/example/ios/Podfile @@ -46,8 +46,12 @@ def install_plugin_pods(application_path = nil, relative_symlink_dir, platform) plugin_pods.each do |plugin_hash| plugin_name = plugin_hash['name'] plugin_path = plugin_hash['path'] + # iOS and macOS code can be shared in "darwin" directory, otherwise + # respectively in "ios" or "macos" directories. + shared_darwin_source = plugin_hash.fetch('shared_darwin_source', false) + platform_directory = shared_darwin_source ? 'darwin' : platform if (plugin_name && plugin_path) - specPath = "#{plugin_path}/#{platform}/#{plugin_name}.podspec" + specPath = "#{plugin_path}/#{platform_directory}/#{plugin_name}.podspec" pod plugin_name, :path => specPath end end diff --git a/example/macos/Podfile b/example/macos/Podfile index 5bf4307c..9ce47397 100644 --- a/example/macos/Podfile +++ b/example/macos/Podfile @@ -9,74 +9,63 @@ project 'Runner', { 'Release' => :release, } -def parse_KV_file(file, separator='=') - file_abs_path = File.expand_path(file) - if !File.exists? file_abs_path - return []; +def flutter_root + generated_xcode_build_settings_path = File.expand_path(File.join('..', 'Flutter', 'ephemeral', 'Flutter-Generated.xcconfig'), __FILE__) + unless File.exist?(generated_xcode_build_settings_path) + raise "#{generated_xcode_build_settings_path} must exist. If you're running pod install manually, make sure \"flutter pub get\" is executed first" end - pods_ary = [] - skip_line_start_symbols = ["#", "/"] - File.foreach(file_abs_path) { |line| - next if skip_line_start_symbols.any? { |symbol| line =~ /^\s*#{symbol}/ } - plugin = line.split(pattern=separator) - if plugin.length == 2 - podname = plugin[0].strip() - path = plugin[1].strip() - podpath = File.expand_path("#{path}", file_abs_path) - pods_ary.push({:name => podname, :path => podpath}); - else - puts "Invalid plugin specification: #{line}" - end - } - return pods_ary -end -def pubspec_supports_macos(file) - file_abs_path = File.expand_path(file) - if !File.exists? file_abs_path - return false; + File.foreach(generated_xcode_build_settings_path) do |line| + matches = line.match(/FLUTTER_ROOT\=(.*)/) + return matches[1].strip if matches end - File.foreach(file_abs_path) { |line| - return true if line =~ /^\s*macos:/ - } - return false + raise "FLUTTER_ROOT not found in #{generated_xcode_build_settings_path}. Try deleting Flutter-Generated.xcconfig, then run \"flutter pub get\"" end -target 'Runner' do - use_frameworks! - use_modular_headers! +require File.expand_path(File.join('packages', 'flutter_tools', 'bin', 'podhelper'), flutter_root) + +flutter_macos_podfile_setup + +def install_plugin_pods(application_path = nil, relative_symlink_dir, platform) + # defined_in_file is set by CocoaPods and is a Pathname to the Podfile. + application_path ||= File.dirname(defined_in_file.realpath) if self.respond_to?(:defined_in_file) + raise 'Could not find application path' unless application_path # Prepare symlinks folder. We use symlinks to avoid having Podfile.lock # referring to absolute paths on developers' machines. - ephemeral_dir = File.join('Flutter', 'ephemeral') - symlink_dir = File.join(ephemeral_dir, '.symlinks') - symlink_plugins_dir = File.join(symlink_dir, 'plugins') - system("rm -rf #{symlink_dir}") - system("mkdir -p #{symlink_plugins_dir}") - # Flutter Pods - generated_xcconfig = parse_KV_file(File.join(ephemeral_dir, 'Flutter-Generated.xcconfig')) - if generated_xcconfig.empty? - puts "Flutter-Generated.xcconfig must exist. If you're running pod install manually, make sure flutter packages get is executed first." - end - generated_xcconfig.map { |p| - if p[:name] == 'FLUTTER_FRAMEWORK_DIR' - symlink = File.join(symlink_dir, 'flutter') - File.symlink(File.dirname(p[:path]), symlink) - pod 'FlutterMacOS', :path => File.join(symlink, File.basename(p[:path])) - end - } + symlink_dir = File.expand_path(relative_symlink_dir, application_path) + system('rm', '-rf', symlink_dir) # Avoid the complication of dependencies like FileUtils. + + symlink_plugins_dir = File.expand_path('plugins', symlink_dir) + system('mkdir', '-p', symlink_plugins_dir) - # Plugin Pods - plugin_pods = parse_KV_file('../.flutter-plugins') - plugin_pods.map { |p| - symlink = File.join(symlink_plugins_dir, p[:name]) - File.symlink(p[:path], symlink) - if pubspec_supports_macos(File.join(symlink, 'pubspec.yaml')) - pod p[:name], :path => File.join(symlink, 'macos') + plugins_file = File.join(application_path, '..', '.flutter-plugins-dependencies') + plugin_pods = flutter_parse_plugins_file(plugins_file, platform) + plugin_pods.each do |plugin_hash| + plugin_name = plugin_hash['name'] + plugin_path = plugin_hash['path'] + # iOS and macOS code can be shared in "darwin" directory, otherwise + # respectively in "ios" or "macos" directories. + shared_darwin_source = plugin_hash.fetch('shared_darwin_source', false) + platform_directory = shared_darwin_source ? 'darwin' : platform + if (plugin_name && plugin_path) + specPath = "#{plugin_path}/#{platform_directory}/#{plugin_name}.podspec" + pod plugin_name, :path => specPath end - } + end end -# Prevent Cocoapods from embedding a second Flutter framework and causing an error with the new Xcode build system. -install! 'cocoapods', :disable_input_output_paths => true +target 'Runner' do + use_frameworks! + use_modular_headers! + + flutter_install_macos_engine_pod(File.dirname(File.realpath(__FILE__))) + install_plugin_pods(File.dirname(File.realpath(__FILE__)), '.symlinks', 'macos') +end + +post_install do |installer| + installer.pods_project.targets.each do |target| + flutter_additional_macos_build_settings(target) + end +end diff --git a/example/pubspec.yaml b/example/pubspec.yaml index f96b56ec..74c98bb7 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -1,6 +1,6 @@ name: wechat_assets_picker_demo description: The demo project for the wechat_assets_picker package. -version: 8.7.1+45 +version: 8.7.2+46 publish_to: none environment: diff --git a/lib/src/delegates/asset_picker_builder_delegate.dart b/lib/src/delegates/asset_picker_builder_delegate.dart index 1045a87c..cd2d395a 100644 --- a/lib/src/delegates/asset_picker_builder_delegate.dart +++ b/lib/src/delegates/asset_picker_builder_delegate.dart @@ -875,19 +875,16 @@ class DefaultAssetPickerBuilderDelegate ) async { final DefaultAssetPickerProvider provider = context.read(); - bool selectedAllAndNotSelected() => - !provider.selectedAssets.contains(currentAsset) && - provider.selectedMaximumAssets; - bool selectedPhotosAndIsVideo() => - isWeChatMoment && - currentAsset.type == AssetType.video && - provider.selectedAssets.isNotEmpty; - // When we reached the maximum select count and the asset - // is not selected, do nothing. - // When the special type is WeChat Moment, pictures and videos cannot - // be selected at the same time. Video select should be banned if any - // pictures are selected. - if (selectedAllAndNotSelected() || selectedPhotosAndIsVideo()) { + // - When we reached the maximum select count and the asset is not selected, + // do nothing. + // - When the special type is WeChat Moment, pictures and videos cannot + // be selected at the same time. Video select should be banned if any + // pictures are selected. + if ((!provider.selectedAssets.contains(currentAsset) && + provider.selectedMaximumAssets) || + (isWeChatMoment && + currentAsset.type == AssetType.video && + provider.selectedAssets.isNotEmpty)) { return; } final List current; @@ -1924,33 +1921,6 @@ class DefaultAssetPickerBuilderDelegate @override Widget previewButton(BuildContext context) { - Future onTap() async { - final DefaultAssetPickerProvider p = - context.read(); - final List selectedAssets = p.selectedAssets; - final List selected; - if (isWeChatMoment) { - selected = selectedAssets - .where((AssetEntity e) => e.type == AssetType.image) - .toList(); - } else { - selected = selectedAssets; - } - final List? result = await AssetPickerViewer.pushToViewer( - context, - previewAssets: selected, - previewThumbnailSize: previewThumbnailSize, - selectPredicate: selectPredicate, - selectedAssets: selected, - selectorProvider: provider, - themeData: theme, - maxAssets: p.maxAssets, - ); - if (result != null) { - Navigator.of(context).maybePop(result); - } - } - return Consumer( builder: (_, DefaultAssetPickerProvider p, Widget? child) { return ValueListenableBuilder( @@ -1965,8 +1935,10 @@ class DefaultAssetPickerBuilderDelegate ); }, child: Consumer( - builder: (_, DefaultAssetPickerProvider p, __) => GestureDetector( - onTap: p.isSelectedNotEmpty ? onTap : null, + builder: (context, DefaultAssetPickerProvider p, __) => GestureDetector( + onTap: p.isSelectedNotEmpty + ? () => viewAsset(context, 0, p.selectedAssets.first) + : null, child: Selector( selector: (_, DefaultAssetPickerProvider p) => p.selectedDescriptions, diff --git a/pubspec.yaml b/pubspec.yaml index e4efcf2b..3f63307c 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: wechat_assets_picker -version: 8.7.1 +version: 8.7.2 description: | An image picker (also with videos and audio) for Flutter projects based on WeChat's UI,