diff --git a/android/src/main/kotlin/com/gianlucaparadise/flutter_cast_framework/FlutterCastFrameworkPlugin.kt b/android/src/main/kotlin/com/gianlucaparadise/flutter_cast_framework/FlutterCastFrameworkPlugin.kt index 3d65295..dcd11a2 100644 --- a/android/src/main/kotlin/com/gianlucaparadise/flutter_cast_framework/FlutterCastFrameworkPlugin.kt +++ b/android/src/main/kotlin/com/gianlucaparadise/flutter_cast_framework/FlutterCastFrameworkPlugin.kt @@ -57,6 +57,8 @@ class FlutterCastFrameworkPlugin : FlutterPlugin, MethodCallHandler, ActivityAwa castApi = MyApi() HostApis.CastApi.setup(messenger, castApi) + flutterApi = HostApis.CastFlutterApi(messenger) + CastContext.getSharedInstance(applicationContext).addCastStateListener { i -> Log.d(TAG, "Cast state changed: $i") methodChannel.invokeMethod(MethodNames.onCastStateChanged, i) @@ -102,6 +104,7 @@ class FlutterCastFrameworkPlugin : FlutterPlugin, MethodCallHandler, ActivityAwa private var channel: MethodChannel? = null private var castApi : HostApis.CastApi? = null + private var flutterApi: HostApis.CastFlutterApi? = null private var applicationContext: Context? = null private var activity: Activity? = null @@ -112,11 +115,10 @@ class FlutterCastFrameworkPlugin : FlutterPlugin, MethodCallHandler, ActivityAwa Log.d(TAG, "Updating mCastSession - castSession changed: ${field != value}") // if (field == value) return // Despite the instances are the same, I need to re-attach the listener to every new session instance - val result = NamespaceResult(oldSession = field, newSession = value) - + val oldSession = field field = value - channel?.invokeMethod(MethodNames.getSessionMessageNamespaces, null, result) + flutterApi?.getSessionMessageNamespaces(getOnNamespaceResult(oldSession, newSession = value)) } //region LifecycleObserver @@ -174,36 +176,17 @@ class FlutterCastFrameworkPlugin : FlutterPlugin, MethodCallHandler, ActivityAwa } } - private inner class NamespaceResult(val oldSession: CastSession?, val newSession: CastSession?) : Result { - override fun notImplemented() { - Log.d(TAG, "Updating mCastSession - notImplemented") - } + private fun getOnNamespaceResult(oldSession: CastSession?, newSession: CastSession?) = HostApis.CastFlutterApi.Reply> { namespaces -> + Log.d(TAG, "Updating mCastSession - getOnNamespaceResult - param: $namespaces") + if (oldSession == null && newSession == null) return@Reply // nothing to do here + if (namespaces == null || !namespaces.any()) return@Reply // nothing to do here - override fun error(p0: String?, p1: String?, p2: Any?) { - Log.d(TAG, "Updating mCastSession - error - $p0 $p1 $p2") - } - - override fun success(args: Any?) { - Log.d(TAG, "Updating mCastSession - success - param: $args") - if (oldSession == null && newSession == null) return // nothing to do here - if (args == null) return // nothing to do here - - if (args !is ArrayList<*>) - throw IllegalArgumentException("${MethodNames.getSessionMessageNamespaces} method expects an ArrayList") - - if (!args.any()) return // nothing to do here - - if (args[0] !is String) - throw IllegalArgumentException("${MethodNames.getSessionMessageNamespaces} method expects an ArrayList") - - val namespaces = args as ArrayList - namespaces.forEach { - try { - oldSession?.removeMessageReceivedCallbacks(it) - newSession?.setMessageReceivedCallbacks(it, mMessageCastingChannel) - } catch (e: java.lang.Exception) { - Log.e(TAG, "Updating mCastSession - Exception while creating channel", e) - } + namespaces.forEach { namespace -> + try { + oldSession?.removeMessageReceivedCallbacks(namespace) + newSession?.setMessageReceivedCallbacks(namespace, mMessageCastingChannel) + } catch (e: java.lang.Exception) { + Log.e(TAG, "Updating mCastSession - Exception while creating channel", e) } } } diff --git a/android/src/main/kotlin/com/gianlucaparadise/flutter_cast_framework/MethodNames.kt b/android/src/main/kotlin/com/gianlucaparadise/flutter_cast_framework/MethodNames.kt index d04be91..3479f67 100644 --- a/android/src/main/kotlin/com/gianlucaparadise/flutter_cast_framework/MethodNames.kt +++ b/android/src/main/kotlin/com/gianlucaparadise/flutter_cast_framework/MethodNames.kt @@ -15,6 +15,5 @@ object MethodNames { const val onSessionSuspended = "SessionManager.onSessionSuspended" // end-region - const val getSessionMessageNamespaces = "CastSession.getSessionMessageNamespaces" const val onMessageReceived = "CastSession.onMessageReceived" } \ No newline at end of file diff --git a/ios/Classes/MethodNames.swift b/ios/Classes/MethodNames.swift index c0ea31b..e16290c 100644 --- a/ios/Classes/MethodNames.swift +++ b/ios/Classes/MethodNames.swift @@ -22,6 +22,5 @@ enum MethodNames : String { case onSessionSuspended = "SessionManager.onSessionSuspended" // end-region - case getSessionMessageNamespaces = "CastSession.getSessionMessageNamespaces" case onMessageReceived = "CastSession.onMessageReceived" } diff --git a/ios/Classes/SwiftFlutterCastFrameworkPlugin.swift b/ios/Classes/SwiftFlutterCastFrameworkPlugin.swift index b13cc8b..5b2fee1 100644 --- a/ios/Classes/SwiftFlutterCastFrameworkPlugin.swift +++ b/ios/Classes/SwiftFlutterCastFrameworkPlugin.swift @@ -5,8 +5,11 @@ import GoogleCast public class SwiftFlutterCastFrameworkPlugin: NSObject, FlutterPlugin, GCKSessionManagerListener, CastApi { public static func register(with registrar: FlutterPluginRegistrar) { let messenger : FlutterBinaryMessenger = registrar.messenger() + let channel = FlutterMethodChannel(name: "flutter_cast_framework", binaryMessenger: messenger) - let instance = SwiftFlutterCastFrameworkPlugin(channel: channel) + let flutterApi = CastFlutterApi.init(binaryMessenger: messenger) + + let instance = SwiftFlutterCastFrameworkPlugin(channel: channel, flutterApi: flutterApi) registrar.addMethodCallDelegate(instance, channel: channel) let api : CastApi & NSObjectProtocol = instance @@ -16,6 +19,7 @@ public class SwiftFlutterCastFrameworkPlugin: NSObject, FlutterPlugin, GCKSessio private let castContext: GCKCastContext private var castStateObserver: NSKeyValueObservation? private let channel: FlutterMethodChannel + private let flutterApi : CastFlutterApi private let sessionManager: GCKSessionManager @@ -32,14 +36,13 @@ public class SwiftFlutterCastFrameworkPlugin: NSObject, FlutterPlugin, GCKSessio _castSession = newValue - channel.invokeMethod(MethodNames.getSessionMessageNamespaces.rawValue, arguments: nil) { (args) in - print("Updating castSession - success - param: \(args ?? "-")") + flutterApi.getSessionMessageNamespaces { (namespaces, err) in + print("Updating castSession - getSessionMessageNamespaces success - param: \(namespaces.joined(separator: ", "))") if (oldSession == nil && newSession == nil) { return // nothing to do here } - let namespaces = args as? NSArray - if (namespaces == nil || namespaces?.count == 0) { + if (namespaces.count == 0) { return // nothing to do here } @@ -50,12 +53,7 @@ public class SwiftFlutterCastFrameworkPlugin: NSObject, FlutterPlugin, GCKSessio } } - namespaces?.forEach({ (namespaceRaw) in - if (!(namespaceRaw is String)) { - return - } - - let namespace = namespaceRaw as! String + namespaces.forEach({ (namespace) in let castingChannel = MessageCastingChannel.init(namespace: namespace, channel: self.channel) self.castingChannels[namespace] = castingChannel newSession?.add(castingChannel) @@ -64,11 +62,13 @@ public class SwiftFlutterCastFrameworkPlugin: NSObject, FlutterPlugin, GCKSessio } } - init(channel: FlutterMethodChannel) { + init(channel: FlutterMethodChannel, flutterApi : CastFlutterApi) { self.channel = channel self.castContext = GCKCastContext.sharedInstance() self.sessionManager = GCKCastContext.sharedInstance().sessionManager + self.flutterApi = flutterApi + super.init() self.castSession = GCKCastContext.sharedInstance().sessionManager.currentCastSession diff --git a/lib/src/MethodNames.dart b/lib/src/MethodNames.dart index 958969c..435d9a9 100644 --- a/lib/src/MethodNames.dart +++ b/lib/src/MethodNames.dart @@ -11,6 +11,5 @@ class PlatformMethodNames { static const onSessionResumeFailed = "SessionManager.onSessionResumeFailed"; static const onSessionSuspended = "SessionManager.onSessionSuspended"; - static const getSessionMessageNamespaces = "CastSession.getSessionMessageNamespaces"; static const onMessageReceived = "CastSession.onMessageReceived"; } \ No newline at end of file diff --git a/lib/src/flutter_cast_framework.dart b/lib/src/flutter_cast_framework.dart index 0bb1784..57e30a9 100644 --- a/lib/src/flutter_cast_framework.dart +++ b/lib/src/flutter_cast_framework.dart @@ -16,6 +16,7 @@ class FlutterCastFramework { static bool _isInitiated = false; static _init() { + CastFlutterApi.setup(CastFlutterApiImpl()); _channel.setMethodCallHandler((MethodCall call) async { String method = call.method; dynamic arguments = call.arguments; @@ -38,9 +39,6 @@ class FlutterCastFramework { castContext.sessionManager.onSessionStateChanged(method, arguments); break; - case PlatformMethodNames.getSessionMessageNamespaces: - return namespaces; - case PlatformMethodNames.onMessageReceived: castContext.sessionManager.platformOnMessageReceived(arguments); break; @@ -68,3 +66,10 @@ class FlutterCastFramework { return castContext; } } + +class CastFlutterApiImpl extends CastFlutterApi { + @override + List getSessionMessageNamespaces() { + return FlutterCastFramework.namespaces; + } +} \ No newline at end of file