Skip to content

Commit

Permalink
⚡️ Use viewAsset in the preview button (fluttercandies#508)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexV525 authored Nov 16, 2023
1 parent 52039a3 commit 6f7fe7e
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 103 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.7.2

### Improvements

- Use `viewAsset` in the preview button.

## 8.7.1

### Improvements
Expand Down
6 changes: 5 additions & 1 deletion example/ios/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
105 changes: 47 additions & 58 deletions example/macos/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
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.7.1+45
version: 8.7.2+46
publish_to: none

environment:
Expand Down
56 changes: 14 additions & 42 deletions lib/src/delegates/asset_picker_builder_delegate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -875,19 +875,16 @@ class DefaultAssetPickerBuilderDelegate
) async {
final DefaultAssetPickerProvider provider =
context.read<DefaultAssetPickerProvider>();
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<AssetEntity> current;
Expand Down Expand Up @@ -1924,33 +1921,6 @@ class DefaultAssetPickerBuilderDelegate

@override
Widget previewButton(BuildContext context) {
Future<void> onTap() async {
final DefaultAssetPickerProvider p =
context.read<DefaultAssetPickerProvider>();
final List<AssetEntity> selectedAssets = p.selectedAssets;
final List<AssetEntity> selected;
if (isWeChatMoment) {
selected = selectedAssets
.where((AssetEntity e) => e.type == AssetType.image)
.toList();
} else {
selected = selectedAssets;
}
final List<AssetEntity>? 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<DefaultAssetPickerProvider>(
builder: (_, DefaultAssetPickerProvider p, Widget? child) {
return ValueListenableBuilder<bool>(
Expand All @@ -1965,8 +1935,10 @@ class DefaultAssetPickerBuilderDelegate
);
},
child: Consumer<DefaultAssetPickerProvider>(
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<DefaultAssetPickerProvider, String>(
selector: (_, DefaultAssetPickerProvider p) =>
p.selectedDescriptions,
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.7.1
version: 8.7.2
description: |
An image picker (also with videos and audio)
for Flutter projects based on WeChat's UI,
Expand Down

0 comments on commit 6f7fe7e

Please sign in to comment.