Skip to content

Commit

Permalink
fix(share_plus): Show NSSharingServicePicker asynchronously on main t…
Browse files Browse the repository at this point in the history
…hread (#1223)
  • Loading branch information
Seuleuzeuh committed Oct 12, 2022
1 parent 405c400 commit e403cfa
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 29 deletions.
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

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

0 comments on commit e403cfa

Please sign in to comment.