Improved framework removing static logic
This commit is contained in:
parent
f5aefbcec1
commit
976c1be481
4 changed files with 46 additions and 47 deletions
|
|
@ -10,6 +10,8 @@ class MyApp extends StatefulWidget {
|
||||||
}
|
}
|
||||||
|
|
||||||
class _MyAppState extends State<MyApp> {
|
class _MyAppState extends State<MyApp> {
|
||||||
|
late FlutterCastFramework castFramework;
|
||||||
|
|
||||||
CastState _castState = CastState.idle;
|
CastState _castState = CastState.idle;
|
||||||
SessionState _sessionState = SessionState.idle;
|
SessionState _sessionState = SessionState.idle;
|
||||||
String _message = '';
|
String _message = '';
|
||||||
|
|
@ -21,11 +23,11 @@ class _MyAppState extends State<MyApp> {
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
FlutterCastFramework.namespaces = [castNamespace];
|
castFramework = FlutterCastFramework.create([castNamespace]);
|
||||||
FlutterCastFramework.castContext.state.addListener(_onCastStateChanged);
|
castFramework.castContext.state.addListener(_onCastStateChanged);
|
||||||
FlutterCastFramework.castContext.sessionManager.state
|
castFramework.castContext.sessionManager.state
|
||||||
.addListener(_onSessionStateChanged);
|
.addListener(_onSessionStateChanged);
|
||||||
FlutterCastFramework.castContext.sessionManager.onMessageReceived =
|
castFramework.castContext.sessionManager.onMessageReceived =
|
||||||
_onMessageReceived;
|
_onMessageReceived;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -39,7 +41,7 @@ class _MyAppState extends State<MyApp> {
|
||||||
void _onCastStateChanged() {
|
void _onCastStateChanged() {
|
||||||
debugPrint("Cast state changed from example");
|
debugPrint("Cast state changed from example");
|
||||||
setState(() {
|
setState(() {
|
||||||
_castState = FlutterCastFramework.castContext.state.value;
|
_castState = castFramework.castContext.state.value;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -47,7 +49,7 @@ class _MyAppState extends State<MyApp> {
|
||||||
debugPrint("Session state changed from example");
|
debugPrint("Session state changed from example");
|
||||||
setState(() {
|
setState(() {
|
||||||
_sessionState =
|
_sessionState =
|
||||||
FlutterCastFramework.castContext.sessionManager.state.value;
|
castFramework.castContext.sessionManager.state.value;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -60,7 +62,7 @@ class _MyAppState extends State<MyApp> {
|
||||||
|
|
||||||
void _onSendMessage() {
|
void _onSendMessage() {
|
||||||
String message = this.textMessageController.text;
|
String message = this.textMessageController.text;
|
||||||
FlutterCastFramework.castContext.sessionManager
|
castFramework.castContext.sessionManager
|
||||||
.sendMessage(castNamespace, message);
|
.sendMessage(castNamespace, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -75,6 +77,7 @@ class _MyAppState extends State<MyApp> {
|
||||||
child: Column(
|
child: Column(
|
||||||
children: [
|
children: [
|
||||||
CastButton(
|
CastButton(
|
||||||
|
castFramework: castFramework,
|
||||||
color: Colors.blue,
|
color: Colors.blue,
|
||||||
),
|
),
|
||||||
Text(
|
Text(
|
||||||
|
|
|
||||||
|
|
@ -6,8 +6,10 @@ import 'CastIcon.dart';
|
||||||
class CastButton extends StatelessWidget {
|
class CastButton extends StatelessWidget {
|
||||||
final Color color;
|
final Color color;
|
||||||
final EdgeInsets padding;
|
final EdgeInsets padding;
|
||||||
|
final FlutterCastFramework castFramework;
|
||||||
|
|
||||||
CastButton({
|
CastButton({
|
||||||
|
required this.castFramework,
|
||||||
this.color = const Color(0xFFFFFFFF), // white
|
this.color = const Color(0xFFFFFFFF), // white
|
||||||
this.padding = const EdgeInsets.all(8.0),
|
this.padding = const EdgeInsets.all(8.0),
|
||||||
});
|
});
|
||||||
|
|
@ -18,9 +20,10 @@ class CastButton extends StatelessWidget {
|
||||||
child: Padding(
|
child: Padding(
|
||||||
padding: padding,
|
padding: padding,
|
||||||
child: CastIcon(
|
child: CastIcon(
|
||||||
|
castFramework: castFramework,
|
||||||
color: color,
|
color: color,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
onTap: () => FlutterCastFramework.castContext.showCastChooserDialog());
|
onTap: () => castFramework.castContext.showCastChooserDialog());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,8 +8,10 @@ const Color _defaultIconColor = Color.fromARGB(255, 255, 255, 255); // white
|
||||||
|
|
||||||
class CastIcon extends StatefulWidget {
|
class CastIcon extends StatefulWidget {
|
||||||
final Color color;
|
final Color color;
|
||||||
|
final FlutterCastFramework castFramework;
|
||||||
|
|
||||||
CastIcon({
|
CastIcon({
|
||||||
|
required this.castFramework,
|
||||||
this.color = _defaultIconColor,
|
this.color = _defaultIconColor,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -27,15 +29,16 @@ Widget _getButton(String assetName, Color color) {
|
||||||
}
|
}
|
||||||
|
|
||||||
class _CastIconState extends State<CastIcon> with TickerProviderStateMixin {
|
class _CastIconState extends State<CastIcon> with TickerProviderStateMixin {
|
||||||
CastState _castState = FlutterCastFramework.castContext.state.value;
|
late CastState _castState;
|
||||||
|
|
||||||
CastState get castState => _castState;
|
CastState get castState => _castState;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
|
var castContext = widget.castFramework.castContext;
|
||||||
|
|
||||||
FlutterCastFramework.castContext.state.addListener(_onCastStateChanged);
|
_castState = castContext.state.value;
|
||||||
|
castContext.state.addListener(_onCastStateChanged);
|
||||||
}
|
}
|
||||||
|
|
||||||
void _onCastStateChanged() {
|
void _onCastStateChanged() {
|
||||||
|
|
@ -43,7 +46,7 @@ class _CastIconState extends State<CastIcon> with TickerProviderStateMixin {
|
||||||
|
|
||||||
setState(() {
|
setState(() {
|
||||||
if (!mounted) return;
|
if (!mounted) return;
|
||||||
_castState = FlutterCastFramework.castContext.state.value;
|
_castState = widget.castFramework.castContext.state.value;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,95 +1,85 @@
|
||||||
|
import 'package:flutter/foundation.dart';
|
||||||
import 'package:flutter_cast_framework/cast.dart';
|
import 'package:flutter_cast_framework/cast.dart';
|
||||||
|
|
||||||
import 'PlatformBridgeApis.dart';
|
import 'PlatformBridgeApis.dart';
|
||||||
import 'cast/CastContext.dart';
|
import 'cast/CastContext.dart';
|
||||||
|
|
||||||
class FlutterCastFramework {
|
class FlutterCastFramework extends CastFlutterApi {
|
||||||
static final hostApi = CastHostApi();
|
final hostApi = CastHostApi();
|
||||||
|
late CastContext castContext;
|
||||||
|
|
||||||
/// List of namespaces to listen for custom messages
|
/// List of namespaces to listen for custom messages
|
||||||
static List<String> namespaces = [];
|
late List<String> namespaces = [];
|
||||||
|
|
||||||
static bool _isInitiated = false;
|
FlutterCastFramework.create(List<String> namespaces) {
|
||||||
|
debugPrint("FlutterCastFramework created!");
|
||||||
static _init() {
|
this.namespaces = namespaces;
|
||||||
CastFlutterApi.setup(CastFlutterApiImpl());
|
this.castContext = CastContext(hostApi);
|
||||||
|
CastFlutterApi.setup(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
static CastContext? _castContext;
|
//region CastFlutterApi implementation
|
||||||
|
|
||||||
// This must be the plugin entry point
|
|
||||||
static CastContext get castContext {
|
|
||||||
var castContext = _castContext;
|
|
||||||
if (!_isInitiated || castContext == null) {
|
|
||||||
_castContext = castContext = CastContext(hostApi);
|
|
||||||
// TODO: find a better way to init the plugin
|
|
||||||
_isInitiated = true;
|
|
||||||
_init();
|
|
||||||
}
|
|
||||||
return castContext;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class CastFlutterApiImpl extends CastFlutterApi {
|
|
||||||
@override
|
@override
|
||||||
List<String?> getSessionMessageNamespaces() {
|
List<String?> getSessionMessageNamespaces() {
|
||||||
return FlutterCastFramework.namespaces;
|
return namespaces;
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void onCastStateChanged(int castState) {
|
void onCastStateChanged(int castState) {
|
||||||
FlutterCastFramework.castContext.onCastStateChanged(castState);
|
castContext.onCastStateChanged(castState);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void onMessageReceived(CastMessage castMessage) {
|
void onMessageReceived(CastMessage castMessage) {
|
||||||
FlutterCastFramework.castContext.sessionManager.platformOnMessageReceived(castMessage);
|
castContext.sessionManager.platformOnMessageReceived(castMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
//region Session State handling
|
//region Session State handling
|
||||||
@override
|
@override
|
||||||
void onSessionEnded() {
|
void onSessionEnded() {
|
||||||
FlutterCastFramework.castContext.sessionManager.onSessionStateChanged(SessionState.session_ended);
|
castContext.sessionManager.onSessionStateChanged(SessionState.session_ended);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void onSessionEnding() {
|
void onSessionEnding() {
|
||||||
FlutterCastFramework.castContext.sessionManager.onSessionStateChanged(SessionState.session_ending);
|
castContext.sessionManager.onSessionStateChanged(SessionState.session_ending);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void onSessionResumeFailed() {
|
void onSessionResumeFailed() {
|
||||||
FlutterCastFramework.castContext.sessionManager.onSessionStateChanged(SessionState.session_resume_failed);
|
castContext.sessionManager.onSessionStateChanged(SessionState.session_resume_failed);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void onSessionResumed() {
|
void onSessionResumed() {
|
||||||
FlutterCastFramework.castContext.sessionManager.onSessionStateChanged(SessionState.session_resumed);
|
castContext.sessionManager.onSessionStateChanged(SessionState.session_resumed);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void onSessionResuming() {
|
void onSessionResuming() {
|
||||||
FlutterCastFramework.castContext.sessionManager.onSessionStateChanged(SessionState.session_resuming);
|
castContext.sessionManager.onSessionStateChanged(SessionState.session_resuming);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void onSessionStartFailed() {
|
void onSessionStartFailed() {
|
||||||
FlutterCastFramework.castContext.sessionManager.onSessionStateChanged(SessionState.session_start_failed);
|
castContext.sessionManager.onSessionStateChanged(SessionState.session_start_failed);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void onSessionStarted() {
|
void onSessionStarted() {
|
||||||
FlutterCastFramework.castContext.sessionManager.onSessionStateChanged(SessionState.session_started);
|
castContext.sessionManager.onSessionStateChanged(SessionState.session_started);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void onSessionStarting() {
|
void onSessionStarting() {
|
||||||
FlutterCastFramework.castContext.sessionManager.onSessionStateChanged(SessionState.session_starting);
|
castContext.sessionManager.onSessionStateChanged(SessionState.session_starting);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void onSessionSuspended() {
|
void onSessionSuspended() {
|
||||||
FlutterCastFramework.castContext.sessionManager.onSessionStateChanged(SessionState.session_suspended);
|
castContext.sessionManager.onSessionStateChanged(SessionState.session_suspended);
|
||||||
}
|
}
|
||||||
//endregion
|
//endregion
|
||||||
|
|
||||||
|
//endregion
|
||||||
}
|
}
|
||||||
Loading…
Reference in a new issue