An open-source cross-platform alternative to AirDrop
refactor: simplified `methodChannelInitialized` awaiting (#1771)
Shlomob5b760a712a8
diff --git a/app/lib/util/native/macos_channel.dart b/app/lib/util/native/macos_channel.dartindex c0d64ea5..05d3cf2e 100644--- a/app/lib/util/native/macos_channel.dart+++ b/app/lib/util/native/macos_channel.dart@@ -18,14 +18,22 @@ Future<void> setupStatusBar() async {});}+// 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)final _pendingFilesStreamController = StreamController<List<String>>.broadcast();Stream<List<String>> get pendingFilesStream => _pendingFilesStreamController.stream;+/// 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)final _pendingStringsStreamController = StreamController<List<String>>.broadcast();Stream<List<String>> get pendingStringsStream => _pendingStringsStreamController.stream;/// Sets up the method call handler.-/// Any call from native code is dropped until this method is called.+/// Any call from swift native code is dropped until this method is called.Future<void> setupMethodCallHandler() async {_methodChannel.setMethodCallHandler((call) async {switch (call.method) {diff --git a/app/macos/Runner/AppDelegate.swift b/app/macos/Runner/AppDelegate.swiftindex 0187665c..7a83c58d 100644--- a/app/macos/Runner/AppDelegate.swift+++ b/app/macos/Runner/AppDelegate.swift@@ -8,7 +8,6 @@ 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@@ -20,26 +19,18 @@ class AppDelegate: FlutterAppDelegate {channel = FlutterMethodChannel(name: "main-delegate-channel", binaryMessenger: controller.engine.binaryMessenger)channel?.setMethodCallHandler(handleFlutterCall)- self.setupPendingItemsObservation()-self.setupDockIconTextDropEventListener()}private func setupPendingItemsObservation() {self.pendingFilesObservation = Defaults.observe(.pendingFiles) { change inguard !Defaults[.pendingFiles].isEmpty else { return }-- if self.methodChannelInitialized {- self.sendPendingItemsToFlutter()- }+ self.sendPendingItemsToFlutter()}self.pendingStringsObservation = Defaults.observe(.pendingStrings) { change inguard !Defaults[.pendingStrings].isEmpty else { return }-- if self.methodChannelInitialized {- self.sendPendingItemsToFlutter()- }+ self.sendPendingItemsToFlutter()}}@@ -115,7 +106,7 @@ class AppDelegate: FlutterAppDelegate {Defaults[.pendingFiles] = []Defaults[.pendingStrings] = []-+self.showLocalSendFromMenuBar()}@@ -123,10 +114,8 @@ class AppDelegate: FlutterAppDelegate {private func handleFlutterCall(_ call: FlutterMethodCall, result: @escaping FlutterResult) {switch call.method {case "methodChannelInitialized":- methodChannelInitialized = true- if !Defaults[.pendingFiles].isEmpty || !Defaults[.pendingStrings].isEmpty {- sendPendingItemsToFlutter()- }+ /// Any call to the channel is dropped until methodChannelInitialized is called from Flutter+ setupPendingItemsObservation()result(nil)case "setupStatusBar":let i18n = call.arguments as! [String: String]@@ -158,7 +147,7 @@ class AppDelegate: FlutterAppDelegate {}// END: handle opened files- /// Handle **text** droped onto the Dock icon+ /// Handle **text** dropped onto the Dock icon@objc func handleOpenContentsEvent(_ event: NSAppleEventDescriptor, withReplyEvent replyEvent: NSAppleEventDescriptor) {if let string = event.paramDescriptor(forKeyword: AEKeyword(keyDirectObject))?.stringValue {Defaults[.pendingStrings].append(string)