An open-source cross-platform alternative to AirDrop
fix: share extension when LocalSend is not running
Tien Do Namcf48c539fdf3
diff --git a/app/lib/config/init.dart b/app/lib/config/init.dartindex cb5573b2..1353bd1b 100644--- a/app/lib/config/init.dart+++ b/app/lib/config/init.dart@@ -183,38 +183,22 @@ Future<void> postInit(BuildContext context, Ref ref, bool appStart) async {if (appStart) {if (defaultTargetPlatform == TargetPlatform.macOS) {- setupMethodCallHandler();-- final pendingFiles = await getPendingFiles();- if (pendingFiles.isNotEmpty) {- await ref.global.dispatchAsync(_HandleAppStartArgumentsAction(- args: pendingFiles,- ));- }-- // handle future dropped files+ // handle dropped filespendingFilesStream.listen((files) {ref.global.dispatchAsync(_HandleAppStartArgumentsAction(args: files,));- ref.redux(homePageControllerProvider).dispatch(ChangeTabAction(HomeTab.send));});- final pendingStrings = await getPendingStrings();- if (pendingStrings.isNotEmpty) {- for (final string in pendingStrings) {- ref.redux(selectedSendingFilesProvider).dispatch(AddMessageAction(message: string));- }- ref.redux(homePageControllerProvider).dispatch(ChangeTabAction(HomeTab.send));- }-- // handle future dropped strings+ // handle dropped stringspendingStringsStream.listen((pendingStrings) {for (final string in pendingStrings) {ref.redux(selectedSendingFilesProvider).dispatch(AddMessageAction(message: string));}ref.redux(homePageControllerProvider).dispatch(ChangeTabAction(HomeTab.send));});++ await setupMethodCallHandler();} else {final args = ref.read(appArgumentsProvider);await ref.global.dispatchAsync(_HandleAppStartArgumentsAction(diff --git a/app/lib/pages/debug/debug_page.dart b/app/lib/pages/debug/debug_page.dartindex 84cff7db..5d33bdee 100644--- a/app/lib/pages/debug/debug_page.dart+++ b/app/lib/pages/debug/debug_page.dart@@ -7,7 +7,6 @@ import 'package:localsend_app/pages/debug/http_logs_page.dart';import 'package:localsend_app/pages/debug/security_debug_page.dart';import 'package:localsend_app/provider/app_arguments_provider.dart';import 'package:localsend_app/provider/persistence_provider.dart';-import 'package:localsend_app/util/native/macos_channel.dart';import 'package:localsend_app/util/shared_preferences/shared_preferences_file.dart';import 'package:localsend_app/widget/debug_entry.dart';import 'package:refena_flutter/refena_flutter.dart';@@ -55,19 +54,6 @@ class DebugPage extends StatelessWidget {name: 'App Arguments',value: appArguments.isEmpty ? null : appArguments.map((e) => '"$e"').join(' '),),- if (defaultTargetPlatform == TargetPlatform.macOS)- FutureBuilder(- // ignore: discarded_futures- future: getPendingFiles().onError((error, stackTrace) async {- return <String>[error.toString()];- }),- builder: (context, snapshot) {- return DebugEntry(- name: 'Opened Files',- value: snapshot.hasData ? snapshot.data.toString() : 'Loading...',- );- },- ),DebugEntry(name: 'Dart SDK',value: Platform.version,diff --git a/app/lib/util/native/macos_channel.dart b/app/lib/util/native/macos_channel.dartindex 0a3980e0..c0d64ea5 100644--- a/app/lib/util/native/macos_channel.dart+++ b/app/lib/util/native/macos_channel.dart@@ -18,56 +18,15 @@ Future<void> setupStatusBar() async {});}-/// Returns a list of files the app was opened with.-/// This happens:-/// - on macOS when files are dropped onto the app Dock icon-/// - on macOS when files are dropped onto the app menu bar icon-/// - on macOS when files are opened with the app via Finder-/// - on macOS when files are shared to the app using the share extension (i.e. the system share menu)-Future<List<String>> getPendingFiles() async {- if (defaultTargetPlatform != TargetPlatform.macOS) {- return <String>[];- }-- final files = await _methodChannel.invokeMethod<List>('getPendingFiles');- if (files == null) {- throw PlatformException(- code: 'UNKNOWN',- message: 'Unable to get files',- );- }-- return files.cast<String>();-}--/// Returns a list of pending strings from the native swift runner.-/// This happens:-/// - on macOS when text is dropped onto the app Dock icon-/// - on macOS when text is dropped onto the app menu bar icon-/// - on macOS when text\web link are shared to the app using the share extension (i.e. the system share menu)-Future<List<String>> getPendingStrings() async {- if (defaultTargetPlatform != TargetPlatform.macOS) {- return <String>[];- }-- final strings = await _methodChannel.invokeMethod<List>('getPendingStrings');- if (strings == null) {- throw PlatformException(- code: 'UNKNOWN',- message: 'Unable to get strings',- );- }-- return strings.cast<String>();-}-final _pendingFilesStreamController = StreamController<List<String>>.broadcast();Stream<List<String>> get pendingFilesStream => _pendingFilesStreamController.stream;final _pendingStringsStreamController = StreamController<List<String>>.broadcast();Stream<List<String>> get pendingStringsStream => _pendingStringsStreamController.stream;-void setupMethodCallHandler() {+/// Sets up the method call handler.+/// Any call from native code is dropped until this method is called.+Future<void> setupMethodCallHandler() async {_methodChannel.setMethodCallHandler((call) async {switch (call.method) {case 'onPendingFiles':@@ -81,4 +40,6 @@ void setupMethodCallHandler() {break;}});++ await _methodChannel.invokeMethod('methodChannelInitialized');}diff --git a/app/macos/Runner/AppDelegate.swift b/app/macos/Runner/AppDelegate.swiftindex d02a16ba..0187665c 100644--- a/app/macos/Runner/AppDelegate.swift+++ b/app/macos/Runner/AppDelegate.swift@@ -8,6 +8,7 @@ class AppDelegate: FlutterAppDelegate {private var channel: FlutterMethodChannel?private var pendingFilesObservation: Defaults.Observation?private var pendingStringsObservation: Defaults.Observation?+ private var methodChannelInitialized = falseoverride func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {// LocalSend handles the close event manually@@ -27,12 +28,18 @@ class AppDelegate: FlutterAppDelegate {private func setupPendingItemsObservation() {self.pendingFilesObservation = Defaults.observe(.pendingFiles) { change inguard !Defaults[.pendingFiles].isEmpty else { return }- self.sendPendingItemsToFlutter()++ if self.methodChannelInitialized {+ self.sendPendingItemsToFlutter()+ }}self.pendingStringsObservation = Defaults.observe(.pendingStrings) { change inguard !Defaults[.pendingStrings].isEmpty else { return }- self.sendPendingItemsToFlutter()++ if self.methodChannelInitialized {+ self.sendPendingItemsToFlutter()+ }}}@@ -99,10 +106,10 @@ class AppDelegate: FlutterAppDelegate {}}- if !filePaths.isEmpty{+ if !filePaths.isEmpty {channel?.invokeMethod("onPendingFiles", arguments: filePaths)}- if !pendingStrings.isEmpty {+ if !pendingStrings.isEmpty {channel?.invokeMethod("onPendingStrings", arguments: pendingStrings)}@@ -115,29 +122,12 @@ class AppDelegate: FlutterAppDelegate {// START: handle opened filesprivate func handleFlutterCall(_ call: FlutterMethodCall, result: @escaping FlutterResult) {switch call.method {- case "getPendingFiles":- let pendingFileBookmarks = Defaults[.pendingFiles]- var filePaths: [String] = []-- for bookmark in pendingFileBookmarks {- if let url = SecurityScopedResourceManager.shared.startAccessing(bookmark: bookmark) {- filePaths.append(url.path)- }+ case "methodChannelInitialized":+ methodChannelInitialized = true+ if !Defaults[.pendingFiles].isEmpty || !Defaults[.pendingStrings].isEmpty {+ sendPendingItemsToFlutter()}-- result(filePaths)-- Defaults[.pendingFiles] = []-- channel?.invokeMethod("showLocalSendFromMenuBar", arguments: nil)- case "getPendingStrings":- let pendingStrings = Defaults[.pendingStrings]-- result(pendingStrings)-- Defaults[.pendingStrings] = []-- channel?.invokeMethod("showLocalSendFromMenuBar", arguments: nil)+ result(nil)case "setupStatusBar":let i18n = call.arguments as! [String: String]setupStatusBarItem(i18n: i18n)