From 865f22a682b09aa7e8c07eb009a4cb7420b3c9dd Mon Sep 17 00:00:00 2001 From: Ming Ming Date: Thu, 7 Sep 2023 00:26:02 +0800 Subject: [PATCH] Add ios implementation of platform_message_relay --- np_platform_message_relay/.metadata | 3 + np_platform_message_relay/ios/.gitignore | 38 ++++++++++ np_platform_message_relay/ios/Assets/.gitkeep | 0 np_platform_message_relay/ios/Classes/K.swift | 5 ++ .../Classes/MessageRelayChannelHandler.swift | 73 +++++++++++++++++++ .../NpPlatformMessageRelayPlugin.swift | 18 +++++ .../ios/np_platform_message_relay.podspec | 24 ++++++ np_platform_message_relay/pubspec.yaml | 2 + 8 files changed, 163 insertions(+) create mode 100644 np_platform_message_relay/ios/.gitignore create mode 100644 np_platform_message_relay/ios/Assets/.gitkeep create mode 100644 np_platform_message_relay/ios/Classes/K.swift create mode 100644 np_platform_message_relay/ios/Classes/MessageRelayChannelHandler.swift create mode 100644 np_platform_message_relay/ios/Classes/NpPlatformMessageRelayPlugin.swift create mode 100644 np_platform_message_relay/ios/np_platform_message_relay.podspec diff --git a/np_platform_message_relay/.metadata b/np_platform_message_relay/.metadata index b0ed2b59..0d60550b 100644 --- a/np_platform_message_relay/.metadata +++ b/np_platform_message_relay/.metadata @@ -18,6 +18,9 @@ migration: - platform: android create_revision: 4d9e56e694b656610ab87fcf2efbcd226e0ed8cf base_revision: 4d9e56e694b656610ab87fcf2efbcd226e0ed8cf + - platform: ios + create_revision: 4d9e56e694b656610ab87fcf2efbcd226e0ed8cf + base_revision: 4d9e56e694b656610ab87fcf2efbcd226e0ed8cf # User provided section diff --git a/np_platform_message_relay/ios/.gitignore b/np_platform_message_relay/ios/.gitignore new file mode 100644 index 00000000..0c885071 --- /dev/null +++ b/np_platform_message_relay/ios/.gitignore @@ -0,0 +1,38 @@ +.idea/ +.vagrant/ +.sconsign.dblite +.svn/ + +.DS_Store +*.swp +profile + +DerivedData/ +build/ +GeneratedPluginRegistrant.h +GeneratedPluginRegistrant.m + +.generated/ + +*.pbxuser +*.mode1v3 +*.mode2v3 +*.perspectivev3 + +!default.pbxuser +!default.mode1v3 +!default.mode2v3 +!default.perspectivev3 + +xcuserdata + +*.moved-aside + +*.pyc +*sync/ +Icon? +.tags* + +/Flutter/Generated.xcconfig +/Flutter/ephemeral/ +/Flutter/flutter_export_environment.sh \ No newline at end of file diff --git a/np_platform_message_relay/ios/Assets/.gitkeep b/np_platform_message_relay/ios/Assets/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/np_platform_message_relay/ios/Classes/K.swift b/np_platform_message_relay/ios/Classes/K.swift new file mode 100644 index 00000000..0b6de28f --- /dev/null +++ b/np_platform_message_relay/ios/Classes/K.swift @@ -0,0 +1,5 @@ +import Foundation + +struct K { + static let libId = "com.nkming.nc_photos.np_platform_message_relay" +} diff --git a/np_platform_message_relay/ios/Classes/MessageRelayChannelHandler.swift b/np_platform_message_relay/ios/Classes/MessageRelayChannelHandler.swift new file mode 100644 index 00000000..ff3878aa --- /dev/null +++ b/np_platform_message_relay/ios/Classes/MessageRelayChannelHandler.swift @@ -0,0 +1,73 @@ +import Flutter +import Foundation +import NpIosCore + +class MessageRelayChannelHandler: NSObject, FlutterStreamHandler { + override init() { + do { + Self.idLock.lock() + defer { Self.idLock.unlock() } + id = Self.nextId + Self.nextId += 1 + } + super.init() + } + + func onMethodCall(call: FlutterMethodCall, result: FlutterResult) { + let args = call.arguments as? Dictionary + do { + switch call.method { + case "broadcast": + try broadcast( + event: (args?["event"] as? String).unwrap(), + data: args?["data"] as? String, + result: result + ) + + default: + result(FlutterMethodNotImplemented) + } + } catch let error as AppError { + result(FlutterError(code: "systemException", message: error.message, details: "\(error.stackTrace)")) + } catch { + result(FlutterError(code: "systemException", message: "\(error)", details: nil)) + } + } + + func onListen(withArguments arguments: Any?, eventSink events: @escaping FlutterEventSink) -> FlutterError? { + Self.evLock.lock() + defer { Self.evLock.unlock() } + Self.eventSinks[id] = events + return nil + } + + func onCancel(withArguments arguments: Any?) -> FlutterError? { + Self.evLock.lock() + defer { Self.evLock.unlock() } + Self.eventSinks.removeValue(forKey: id) + return nil + } + + private func broadcast(event: String, data: String?, result: FlutterResult) { + Self.evLock.lock() + defer { Self.evLock.unlock() } + for s in Self.eventSinks.values { + var map = ["event": event] + if data != nil { + map["data"] = data + } + s(data) + } + result(nil) + } + + static let eventChannel = "\(K.libId)/message_relay_event" + static let methodChannel = "\(K.libId)/message_relay_method" + + private static var eventSinks: [Int:FlutterEventSink] = [:] + private static var evLock = NSRecursiveLock() + private static var nextId = 0 + private static var idLock = NSRecursiveLock() + + private let id: Int +} diff --git a/np_platform_message_relay/ios/Classes/NpPlatformMessageRelayPlugin.swift b/np_platform_message_relay/ios/Classes/NpPlatformMessageRelayPlugin.swift new file mode 100644 index 00000000..2425f05e --- /dev/null +++ b/np_platform_message_relay/ios/Classes/NpPlatformMessageRelayPlugin.swift @@ -0,0 +1,18 @@ +import Flutter +import UIKit + +public class NpPlatformMessageRelayPlugin: NSObject, FlutterPlugin { + public static func register(with registrar: FlutterPluginRegistrar) { + let handler = MessageRelayChannelHandler() + let eventChannel = FlutterEventChannel( + name: MessageRelayChannelHandler.eventChannel, + binaryMessenger: registrar.messenger() + ) + eventChannel.setStreamHandler(handler) + let methodChannel = FlutterMethodChannel( + name: MessageRelayChannelHandler.methodChannel, + binaryMessenger: registrar.messenger() + ) + methodChannel.setMethodCallHandler(handler.onMethodCall) + } +} diff --git a/np_platform_message_relay/ios/np_platform_message_relay.podspec b/np_platform_message_relay/ios/np_platform_message_relay.podspec new file mode 100644 index 00000000..e337b5fe --- /dev/null +++ b/np_platform_message_relay/ios/np_platform_message_relay.podspec @@ -0,0 +1,24 @@ +# +# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html. +# Run `pod lib lint np_platform_message_relay.podspec` to validate before publishing. +# +Pod::Spec.new do |s| + s.name = 'np_platform_message_relay' + s.version = '0.0.1' + s.summary = 'A new Flutter plugin project.' + s.description = <<-DESC +A new Flutter plugin project. + DESC + s.homepage = 'http://example.com' + s.license = { :type => 'GPLv3' } + s.author = { 'Your Company' => 'email@example.com' } + s.source = { :path => '.' } + s.source_files = 'Classes/**/*' + s.dependency 'Flutter' + s.dependency 'NpIosCore' + s.platform = :ios, '12.0' + + # Flutter.framework does not contain a i386 slice. + s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES', 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'i386' } + s.swift_version = '5.0' +end diff --git a/np_platform_message_relay/pubspec.yaml b/np_platform_message_relay/pubspec.yaml index 2522c67b..5a19d430 100644 --- a/np_platform_message_relay/pubspec.yaml +++ b/np_platform_message_relay/pubspec.yaml @@ -28,3 +28,5 @@ flutter: android: package: com.nkming.nc_photos.np_platform_message_relay pluginClass: NpPlatformMessageRelayPlugin + ios: + pluginClass: NpPlatformMessageRelayPlugin