Pigeon: Integrated SessionState handling APIs

This commit is contained in:
gianlucaparadise 2021-11-01 19:16:59 +01:00
parent 73f6818bfb
commit 043938fd0e
7 changed files with 96 additions and 101 deletions

View file

@ -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 { }
}
}
}

View file

@ -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"
}

View file

@ -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"
}

View file

@ -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
}
}
}

View file

@ -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";
}

View file

@ -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<SessionState> 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;
}
}

View file

@ -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
}