Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(share_plus): Show NSSharingServicePicker asynchronously on main thread #1223

Merged
merged 3 commits into from Oct 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 14 additions & 8 deletions packages/share_plus/share_plus/example/lib/main.dart
Expand Up @@ -5,13 +5,12 @@
// ignore_for_file: public_member_api_docs

import 'dart:io';
import 'package:file_selector/file_selector.dart';
import 'package:flutter/material.dart';
import 'package:share_plus/share_plus.dart';
import 'package:image_picker/image_picker.dart';

import 'image_previews.dart';
import 'utils/file_picker_win.dart'
if (dart.library.html) 'utils/file_picker_web.dart';

void main() {
runApp(const DemoApp());
Expand Down Expand Up @@ -71,13 +70,20 @@ class DemoAppState extends State<DemoApp> {
title: const Text('Add image'),
onTap: () async {
// Using `package:image_picker` to get image from gallery.
if (Platform.isWindows) {
// Using `package:filepicker_windows` on Windows, since `package:image_picker` is not supported.
final path = await pickFile();
if (path != null) {
if (Platform.isMacOS ||
Platform.isLinux ||
Platform.isWindows) {
// Using `package:file_selector` on windows, macos & Linux, since `package:image_picker` is not supported.
const XTypeGroup typeGroup = XTypeGroup(
label: 'images',
extensions: <String>['jpg', 'jpeg', 'png', 'gif'],
);
final file = await openFile(
acceptedTypeGroups: <XTypeGroup>[typeGroup]);
if (file != null) {
setState(() {
imagePaths.add(path);
imageNames.add(path.split('\\').last);
imagePaths.add(file.path);
imageNames.add(file.name);
});
}
} else {
Expand Down

This file was deleted.

This file was deleted.

Expand Up @@ -8,5 +8,7 @@
<true/>
<key>com.apple.security.network.server</key>
<true/>
<key>com.apple.security.files.user-selected.read-only</key>
<true/>
</dict>
</plist>
Expand Up @@ -4,5 +4,7 @@
<dict>
<key>com.apple.security.app-sandbox</key>
<false/>
<key>com.apple.security.files.user-selected.read-only</key>
<true/>
</dict>
</plist>
2 changes: 1 addition & 1 deletion packages/share_plus/share_plus/example/pubspec.yaml
Expand Up @@ -7,7 +7,7 @@ dependencies:
share_plus:
path: ../
image_picker: ^0.8.4
filepicker_windows: ^2.0.2
file_selector: ^0.9.2+1
Seuleuzeuh marked this conversation as resolved.
Show resolved Hide resolved

dependency_overrides:
share_plus_linux:
Expand Down
Expand Up @@ -49,14 +49,16 @@ public class SharePlusMacosPlugin: NSObject, FlutterPlugin, NSSharingServicePick
}

private func shareItems(_ items: [Any], subject: String? = nil, origin: NSRect, view: NSView, callback: FlutterResult? = nil) {
let picker = NSSharingServicePicker(items: items)
if callback != nil {
picker.delegate = SharePlusMacosSuccessDelegate(subject: subject, callback: callback!).keep()
} else {
picker.delegate = self
self.subject = subject
DispatchQueue.main.async {
let picker = NSSharingServicePicker(items: items)
if callback != nil {
picker.delegate = SharePlusMacosSuccessDelegate(subject: subject, callback: callback!).keep()
} else {
picker.delegate = self
self.subject = subject
}
picker.show(relativeTo: origin, of: view, preferredEdge: NSRectEdge.maxY)
}
picker.show(relativeTo: origin, of: view, preferredEdge: NSRectEdge.maxY)
}

private func originRect(_ args: [String: Any]) -> NSRect {
Expand Down