From 043938fd0ea8a24dd91d29ad2f642411fee0548e Mon Sep 17 00:00:00 2001 From: gianlucaparadise Date: Mon, 1 Nov 2021 19:16:59 +0100 Subject: [PATCH] Pigeon: Integrated SessionState handling APIs --- .../FlutterCastFrameworkPlugin.kt | 36 +++++------ .../flutter_cast_framework/MethodNames.kt | 12 ---- ios/Classes/MethodNames.swift | 12 ---- .../SwiftFlutterCastFrameworkPlugin.swift | 24 +++++--- lib/src/MethodNames.dart | 10 ---- lib/src/cast/SessionManager.dart | 43 +++++-------- lib/src/flutter_cast_framework.dart | 60 +++++++++++++++---- 7 files changed, 96 insertions(+), 101 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 9995cd4..c00acbb 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 @@ -195,56 +195,56 @@ class FlutterCastFrameworkPlugin : FlutterPlugin, MethodCallHandler, ActivityAwa private var TAG = "SessionManagerListenerImpl" override fun onSessionSuspended(session: CastSession?, p1: Int) { - Log.d(TAG, "onSessionSuspended - channel is null? ${channel == null}") - channel?.invokeMethod(MethodNames.onSessionSuspended, null) + Log.d(TAG, "onSessionSuspended") + flutterApi?.onSessionSuspended { } } override fun onSessionStarting(session: CastSession?) { - Log.d(TAG, "onSessionStarting - channel is null? ${channel == null}") - channel?.invokeMethod(MethodNames.onSessionStarting, null) + Log.d(TAG, "onSessionStarting") + flutterApi?.onSessionStarting { } mCastSession = session } override fun onSessionResuming(session: CastSession?, p1: String?) { - Log.d(TAG, "onSessionResuming - channel is null? ${channel == null}") - channel?.invokeMethod(MethodNames.onSessionResuming, null) + Log.d(TAG, "onSessionResuming") + flutterApi?.onSessionResuming { } mCastSession = session } override fun onSessionEnding(session: CastSession?) { - Log.d(TAG, "onSessionEnding - channel is null? ${channel == null}") - channel?.invokeMethod(MethodNames.onSessionEnding, null) + Log.d(TAG, "onSessionEnding") + flutterApi?.onSessionEnding { } } override fun onSessionStartFailed(session: CastSession?, p1: Int) { - Log.d(TAG, "onSessionStartFailed - channel is null? ${channel == null}") - channel?.invokeMethod(MethodNames.onSessionStartFailed, null) + Log.d(TAG, "onSessionStartFailed") + flutterApi?.onSessionStartFailed { } } override fun onSessionResumeFailed(session: CastSession?, p1: Int) { - Log.d(TAG, "onSessionResumeFailed - channel is null? ${channel == null}") - channel?.invokeMethod(MethodNames.onSessionResumeFailed, null) + Log.d(TAG, "onSessionResumeFailed") + flutterApi?.onSessionResumeFailed { } } override fun onSessionStarted(session: CastSession, sessionId: String) { - Log.d(TAG, "onSessionStarted - channel is null? ${channel == null}") - channel?.invokeMethod(MethodNames.onSessionStarted, null) + Log.d(TAG, "onSessionStarted") + flutterApi?.onSessionStarted { } mCastSession = session } override fun onSessionResumed(session: CastSession, wasSuspended: Boolean) { - Log.d(TAG, "onSessionResumed - channel is null? ${channel == null}") - channel?.invokeMethod(MethodNames.onSessionResumed, null) + Log.d(TAG, "onSessionResumed") + flutterApi?.onSessionResumed { } mCastSession = session } override fun onSessionEnded(session: CastSession, error: Int) { - Log.d(TAG, "onSessionEnded - channel is null? ${channel == null}") - channel?.invokeMethod(MethodNames.onSessionEnded, null) + Log.d(TAG, "onSessionEnded") + flutterApi?.onSessionEnded { } } } } 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 3f0a16a..cce8525 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,17 +1,5 @@ package com.gianlucaparadise.flutter_cast_framework object MethodNames { - // region SessionManager - const val onSessionStarting = "SessionManager.onSessionStarting" - const val onSessionStarted = "SessionManager.onSessionStarted" - const val onSessionStartFailed = "SessionManager.onSessionStartFailed" - const val onSessionEnding = "SessionManager.onSessionEnding" - const val onSessionEnded = "SessionManager.onSessionEnded" - const val onSessionResuming = "SessionManager.onSessionResuming" - const val onSessionResumed = "SessionManager.onSessionResumed" - const val onSessionResumeFailed = "SessionManager.onSessionResumeFailed" - const val onSessionSuspended = "SessionManager.onSessionSuspended" - // end-region - const val onMessageReceived = "CastSession.onMessageReceived" } \ No newline at end of file diff --git a/ios/Classes/MethodNames.swift b/ios/Classes/MethodNames.swift index 71b31a6..c40dcd9 100644 --- a/ios/Classes/MethodNames.swift +++ b/ios/Classes/MethodNames.swift @@ -8,17 +8,5 @@ import Foundation enum MethodNames : String { - // region SessionManager - case onSessionStarting = "SessionManager.onSessionStarting" - case onSessionStarted = "SessionManager.onSessionStarted" - case onSessionStartFailed = "SessionManager.onSessionStartFailed" - case onSessionEnding = "SessionManager.onSessionEnding" - case onSessionEnded = "SessionManager.onSessionEnded" - case onSessionResuming = "SessionManager.onSessionResuming" - case onSessionResumed = "SessionManager.onSessionResumed" - case onSessionResumeFailed = "SessionManager.onSessionResumeFailed" - case onSessionSuspended = "SessionManager.onSessionSuspended" - // end-region - case onMessageReceived = "CastSession.onMessageReceived" } diff --git a/ios/Classes/SwiftFlutterCastFrameworkPlugin.swift b/ios/Classes/SwiftFlutterCastFrameworkPlugin.swift index d23106b..67f6f59 100644 --- a/ios/Classes/SwiftFlutterCastFrameworkPlugin.swift +++ b/ios/Classes/SwiftFlutterCastFrameworkPlugin.swift @@ -135,13 +135,15 @@ public class SwiftFlutterCastFrameworkPlugin: NSObject, FlutterPlugin, GCKSessio // onSessionSuspended public func sessionManager(_ sessionManager: GCKSessionManager, didSuspend session: GCKCastSession, with reason: GCKConnectionSuspendReason) { print("SessionListener: didSuspend") - channel.invokeMethod(MethodNames.onSessionSuspended.rawValue, arguments: nil) + flutterApi.onSessionSuspended { (_:Error?) in + } } // onSessionStarting public func sessionManager(_ sessionManager: GCKSessionManager, willStart session: GCKCastSession) { print("SessionListener: willStart") - channel.invokeMethod(MethodNames.onSessionStarting.rawValue, arguments: nil) + flutterApi.onSessionStarting { (_:Error?) in + } castSession = session } @@ -149,7 +151,8 @@ public class SwiftFlutterCastFrameworkPlugin: NSObject, FlutterPlugin, GCKSessio // onSessionResuming public func sessionManager(_ sessionManager: GCKSessionManager, willResumeCastSession session: GCKCastSession) { print("SessionListener: willResumeCastSession") - channel.invokeMethod(MethodNames.onSessionResuming.rawValue, arguments: nil) + flutterApi.onSessionResuming { (_:Error?) in + } castSession = session } @@ -157,13 +160,15 @@ public class SwiftFlutterCastFrameworkPlugin: NSObject, FlutterPlugin, GCKSessio // onSessionEnding public func sessionManager(_ sessionManager: GCKSessionManager, willEnd session: GCKCastSession) { print("SessionListener: willEnd") - channel.invokeMethod(MethodNames.onSessionEnding.rawValue, arguments: nil) + flutterApi.onSessionEnding { (_:Error?) in + } } // onSessionStartFailed public func sessionManager(_ sessionManager: GCKSessionManager, didFailToStart session: GCKCastSession, withError error: Error) { print("SessionListener: didFailToStart") - channel.invokeMethod(MethodNames.onSessionStartFailed.rawValue, arguments: nil) + flutterApi.onSessionStartFailed { (_:Error?) in + } } // onSessionResumeFailed - Can't find this on iOS @@ -171,7 +176,8 @@ public class SwiftFlutterCastFrameworkPlugin: NSObject, FlutterPlugin, GCKSessio // onSessionStarted public func sessionManager(_ sessionManager: GCKSessionManager, didStart session: GCKCastSession) { print("SessionListener: didStart") - channel.invokeMethod(MethodNames.onSessionStarted.rawValue, arguments: nil) + flutterApi.onSessionStarted { (_:Error?) in + } castSession = session } @@ -179,7 +185,8 @@ public class SwiftFlutterCastFrameworkPlugin: NSObject, FlutterPlugin, GCKSessio // onSessionResumed public func sessionManager(_ sessionManager: GCKSessionManager, didResumeCastSession session: GCKCastSession) { print("SessionListener: didResumeCastSession") - channel.invokeMethod(MethodNames.onSessionResumed.rawValue, arguments: nil) + flutterApi.onSessionResumed { (_:Error?) in + } castSession = session } @@ -187,6 +194,7 @@ public class SwiftFlutterCastFrameworkPlugin: NSObject, FlutterPlugin, GCKSessio // onSessionEnded public func sessionManager(_ sessionManager: GCKSessionManager, didEnd session: GCKCastSession, withError error: Error?) { print("SessionListener: didEnd") - channel.invokeMethod(MethodNames.onSessionEnded.rawValue, arguments: nil) + flutterApi.onSessionEnded { (_:Error?) in + } } } diff --git a/lib/src/MethodNames.dart b/lib/src/MethodNames.dart index 24dc67e..53b6623 100644 --- a/lib/src/MethodNames.dart +++ b/lib/src/MethodNames.dart @@ -1,13 +1,3 @@ class PlatformMethodNames { - static const onSessionStarting = "SessionManager.onSessionStarting"; - static const onSessionStarted = "SessionManager.onSessionStarted"; - static const onSessionStartFailed = "SessionManager.onSessionStartFailed"; - static const onSessionEnding = "SessionManager.onSessionEnding"; - static const onSessionEnded = "SessionManager.onSessionEnded"; - static const onSessionResuming = "SessionManager.onSessionResuming"; - static const onSessionResumed = "SessionManager.onSessionResumed"; - static const onSessionResumeFailed = "SessionManager.onSessionResumeFailed"; - static const onSessionSuspended = "SessionManager.onSessionSuspended"; - static const onMessageReceived = "CastSession.onMessageReceived"; } \ No newline at end of file diff --git a/lib/src/cast/SessionManager.dart b/lib/src/cast/SessionManager.dart index 32cc0b4..d1287fe 100644 --- a/lib/src/cast/SessionManager.dart +++ b/lib/src/cast/SessionManager.dart @@ -1,7 +1,5 @@ import 'package:flutter/foundation.dart'; -import 'package:flutter/services.dart'; import '../HostApis.dart'; -import '../MethodNames.dart'; class SessionManager { final CastApi castApi; @@ -10,34 +8,21 @@ class SessionManager { final ValueNotifier state = ValueNotifier(SessionState.idle); - void onSessionStateChanged(String method, dynamic arguments) { - switch (method) { - case PlatformMethodNames.onSessionStarting: - state.value = SessionState.session_starting; + void onSessionStateChanged(SessionState sessionState) { + switch (sessionState) { + case SessionState.session_starting: + case SessionState.session_started: + case SessionState.session_start_failed: + case SessionState.session_ending: + case SessionState.session_ended: + case SessionState.session_resuming: + case SessionState.session_resumed: + case SessionState.session_resume_failed: + case SessionState.session_suspended: + state.value = sessionState; break; - case PlatformMethodNames.onSessionStarted: - state.value = SessionState.session_started; - break; - case PlatformMethodNames.onSessionStartFailed: - state.value = SessionState.session_start_failed; - break; - case PlatformMethodNames.onSessionEnding: - state.value = SessionState.session_ending; - break; - case PlatformMethodNames.onSessionEnded: - state.value = SessionState.session_ended; - break; - case PlatformMethodNames.onSessionResuming: - state.value = SessionState.session_resuming; - break; - case PlatformMethodNames.onSessionResumed: - state.value = SessionState.session_resumed; - break; - case PlatformMethodNames.onSessionResumeFailed: - state.value = SessionState.session_resume_failed; - break; - case PlatformMethodNames.onSessionSuspended: - state.value = SessionState.session_suspended; + case SessionState.idle: + // Not raised break; } } diff --git a/lib/src/flutter_cast_framework.dart b/lib/src/flutter_cast_framework.dart index c8d6ca3..2837c43 100644 --- a/lib/src/flutter_cast_framework.dart +++ b/lib/src/flutter_cast_framework.dart @@ -1,5 +1,6 @@ import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; +import 'package:flutter_cast_framework/cast.dart'; import 'HostApis.dart'; import 'MethodNames.dart'; @@ -23,18 +24,6 @@ class FlutterCastFramework { debugPrint("Method call on flutter: $method $arguments"); switch (method) { - case PlatformMethodNames.onSessionStarting: - case PlatformMethodNames.onSessionStarted: - case PlatformMethodNames.onSessionStartFailed: - case PlatformMethodNames.onSessionEnding: - case PlatformMethodNames.onSessionEnded: - case PlatformMethodNames.onSessionResuming: - case PlatformMethodNames.onSessionResumed: - case PlatformMethodNames.onSessionResumeFailed: - case PlatformMethodNames.onSessionSuspended: - castContext.sessionManager.onSessionStateChanged(method, arguments); - break; - case PlatformMethodNames.onMessageReceived: castContext.sessionManager.platformOnMessageReceived(arguments); break; @@ -74,4 +63,51 @@ class CastFlutterApiImpl extends CastFlutterApi { debugPrint("yoyo onCastStateChanged: $castState"); FlutterCastFramework.castContext.onCastStateChanged(castState); } + + //region Session State handling + @override + void onSessionEnded() { + FlutterCastFramework.castContext.sessionManager.onSessionStateChanged(SessionState.session_ended); + } + + @override + void onSessionEnding() { + FlutterCastFramework.castContext.sessionManager.onSessionStateChanged(SessionState.session_ending); + } + + @override + void onSessionResumeFailed() { + FlutterCastFramework.castContext.sessionManager.onSessionStateChanged(SessionState.session_resume_failed); + } + + @override + void onSessionResumed() { + FlutterCastFramework.castContext.sessionManager.onSessionStateChanged(SessionState.session_resumed); + } + + @override + void onSessionResuming() { + FlutterCastFramework.castContext.sessionManager.onSessionStateChanged(SessionState.session_resuming); + } + + @override + void onSessionStartFailed() { + FlutterCastFramework.castContext.sessionManager.onSessionStateChanged(SessionState.session_start_failed); + } + + @override + void onSessionStarted() { + FlutterCastFramework.castContext.sessionManager.onSessionStateChanged(SessionState.session_started); + } + + @override + void onSessionStarting() { + FlutterCastFramework.castContext.sessionManager.onSessionStateChanged(SessionState.session_starting); + } + + @override + void onSessionSuspended() { + FlutterCastFramework.castContext.sessionManager.onSessionStateChanged(SessionState.session_suspended); + } + //endregion } \ No newline at end of file