From 925ff7a0dcec493e373f61b78ddef734d7bc02fc Mon Sep 17 00:00:00 2001 From: gianlucaparadise Date: Mon, 1 Nov 2021 18:51:47 +0100 Subject: [PATCH] Pigeon: Integrated `onCastStateChanged` API --- .../FlutterCastFrameworkPlugin.kt | 4 ++-- .../flutter_cast_framework/MethodNames.kt | 2 -- ios/Classes/MethodNames.swift | 2 -- ios/Classes/SwiftFlutterCastFrameworkPlugin.swift | 4 +++- lib/src/MethodNames.dart | 2 -- lib/src/cast/CastContext.dart | 3 +-- lib/src/flutter_cast_framework.dart | 10 ++++++---- 7 files changed, 12 insertions(+), 15 deletions(-) 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 dcd11a2..9995cd4 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 @@ -61,7 +61,7 @@ class FlutterCastFrameworkPlugin : FlutterPlugin, MethodCallHandler, ActivityAwa CastContext.getSharedInstance(applicationContext).addCastStateListener { i -> Log.d(TAG, "Cast state changed: $i") - methodChannel.invokeMethod(MethodNames.onCastStateChanged, i) + flutterApi?.onCastStateChanged(i.toLong(), null) } mSessionManager = CastContext.getSharedInstance(applicationContext).sessionManager @@ -139,7 +139,7 @@ class FlutterCastFrameworkPlugin : FlutterPlugin, MethodCallHandler, ActivityAwa return } val castState = CastContext.getSharedInstance(context).castState - channel?.invokeMethod(MethodNames.onCastStateChanged, castState) + flutterApi?.onCastStateChanged(castState.toLong()) { } } @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) 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 3479f67..3f0a16a 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 @@ -1,8 +1,6 @@ package com.gianlucaparadise.flutter_cast_framework object MethodNames { - const val onCastStateChanged = "CastContext.onCastStateChanged" - // region SessionManager const val onSessionStarting = "SessionManager.onSessionStarting" const val onSessionStarted = "SessionManager.onSessionStarted" diff --git a/ios/Classes/MethodNames.swift b/ios/Classes/MethodNames.swift index e16290c..71b31a6 100644 --- a/ios/Classes/MethodNames.swift +++ b/ios/Classes/MethodNames.swift @@ -8,8 +8,6 @@ import Foundation enum MethodNames : String { - case onCastStateChanged = "CastContext.onCastStateChanged" - // region SessionManager case onSessionStarting = "SessionManager.onSessionStarting" case onSessionStarted = "SessionManager.onSessionStarted" diff --git a/ios/Classes/SwiftFlutterCastFrameworkPlugin.swift b/ios/Classes/SwiftFlutterCastFrameworkPlugin.swift index 5b2fee1..d23106b 100644 --- a/ios/Classes/SwiftFlutterCastFrameworkPlugin.swift +++ b/ios/Classes/SwiftFlutterCastFrameworkPlugin.swift @@ -107,7 +107,9 @@ public class SwiftFlutterCastFrameworkPlugin: NSObject, FlutterPlugin, GCKSessio let castStateRaw = castState.rawValue // Android CastStates are 1-to-4, while iOS CastStates are 0-to-3. I align iOS to Android by adding 1 let castStateRawAdjusted = castStateRaw + 1 - self.channel.invokeMethod(MethodNames.onCastStateChanged.rawValue, arguments: castStateRawAdjusted) + self.flutterApi.onCastStateChangedCastState(NSNumber(value: castStateRawAdjusted)) { (_: Error?) in + + } } public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) { diff --git a/lib/src/MethodNames.dart b/lib/src/MethodNames.dart index 435d9a9..24dc67e 100644 --- a/lib/src/MethodNames.dart +++ b/lib/src/MethodNames.dart @@ -1,6 +1,4 @@ class PlatformMethodNames { - static const onCastStateChanged = "CastContext.onCastStateChanged"; - static const onSessionStarting = "SessionManager.onSessionStarting"; static const onSessionStarted = "SessionManager.onSessionStarted"; static const onSessionStartFailed = "SessionManager.onSessionStartFailed"; diff --git a/lib/src/cast/CastContext.dart b/lib/src/cast/CastContext.dart index bfdcbe6..6f66829 100644 --- a/lib/src/cast/CastContext.dart +++ b/lib/src/cast/CastContext.dart @@ -12,8 +12,7 @@ class CastContext { castApi.showCastDialog(); } - void onCastStateChanged(dynamic arguments) { - int castState = arguments; + void onCastStateChanged(int castState) { state.value = CastState.values[castState]; } diff --git a/lib/src/flutter_cast_framework.dart b/lib/src/flutter_cast_framework.dart index 57e30a9..c8d6ca3 100644 --- a/lib/src/flutter_cast_framework.dart +++ b/lib/src/flutter_cast_framework.dart @@ -23,10 +23,6 @@ class FlutterCastFramework { debugPrint("Method call on flutter: $method $arguments"); switch (method) { - case PlatformMethodNames.onCastStateChanged: - castContext.onCastStateChanged(arguments); - break; - case PlatformMethodNames.onSessionStarting: case PlatformMethodNames.onSessionStarted: case PlatformMethodNames.onSessionStartFailed: @@ -72,4 +68,10 @@ class CastFlutterApiImpl extends CastFlutterApi { List getSessionMessageNamespaces() { return FlutterCastFramework.namespaces; } + + @override + void onCastStateChanged(int castState) { + debugPrint("yoyo onCastStateChanged: $castState"); + FlutterCastFramework.castContext.onCastStateChanged(castState); + } } \ No newline at end of file