From 976c1be481b545d7a81750f375efbcb378a941a3 Mon Sep 17 00:00:00 2001 From: gianlucaparadise Date: Tue, 2 Nov 2021 08:12:57 +0100 Subject: [PATCH] Improved framework removing static logic --- example/lib/main.dart | 17 ++++---- lib/src/cast/widgets/CastButton.dart | 5 ++- lib/src/cast/widgets/CastIcon.dart | 11 +++-- lib/src/flutter_cast_framework.dart | 60 ++++++++++++---------------- 4 files changed, 46 insertions(+), 47 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index 601bd6a..d49014e 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -10,6 +10,8 @@ class MyApp extends StatefulWidget { } class _MyAppState extends State { + late FlutterCastFramework castFramework; + CastState _castState = CastState.idle; SessionState _sessionState = SessionState.idle; String _message = ''; @@ -21,11 +23,11 @@ class _MyAppState extends State { @override void initState() { super.initState(); - FlutterCastFramework.namespaces = [castNamespace]; - FlutterCastFramework.castContext.state.addListener(_onCastStateChanged); - FlutterCastFramework.castContext.sessionManager.state + castFramework = FlutterCastFramework.create([castNamespace]); + castFramework.castContext.state.addListener(_onCastStateChanged); + castFramework.castContext.sessionManager.state .addListener(_onSessionStateChanged); - FlutterCastFramework.castContext.sessionManager.onMessageReceived = + castFramework.castContext.sessionManager.onMessageReceived = _onMessageReceived; } @@ -39,7 +41,7 @@ class _MyAppState extends State { void _onCastStateChanged() { debugPrint("Cast state changed from example"); setState(() { - _castState = FlutterCastFramework.castContext.state.value; + _castState = castFramework.castContext.state.value; }); } @@ -47,7 +49,7 @@ class _MyAppState extends State { debugPrint("Session state changed from example"); setState(() { _sessionState = - FlutterCastFramework.castContext.sessionManager.state.value; + castFramework.castContext.sessionManager.state.value; }); } @@ -60,7 +62,7 @@ class _MyAppState extends State { void _onSendMessage() { String message = this.textMessageController.text; - FlutterCastFramework.castContext.sessionManager + castFramework.castContext.sessionManager .sendMessage(castNamespace, message); } @@ -75,6 +77,7 @@ class _MyAppState extends State { child: Column( children: [ CastButton( + castFramework: castFramework, color: Colors.blue, ), Text( diff --git a/lib/src/cast/widgets/CastButton.dart b/lib/src/cast/widgets/CastButton.dart index d8f07bf..c9a57b7 100644 --- a/lib/src/cast/widgets/CastButton.dart +++ b/lib/src/cast/widgets/CastButton.dart @@ -6,8 +6,10 @@ import 'CastIcon.dart'; class CastButton extends StatelessWidget { final Color color; final EdgeInsets padding; + final FlutterCastFramework castFramework; CastButton({ + required this.castFramework, this.color = const Color(0xFFFFFFFF), // white this.padding = const EdgeInsets.all(8.0), }); @@ -18,9 +20,10 @@ class CastButton extends StatelessWidget { child: Padding( padding: padding, child: CastIcon( + castFramework: castFramework, color: color, ), ), - onTap: () => FlutterCastFramework.castContext.showCastChooserDialog()); + onTap: () => castFramework.castContext.showCastChooserDialog()); } } diff --git a/lib/src/cast/widgets/CastIcon.dart b/lib/src/cast/widgets/CastIcon.dart index 52c9c65..142d89d 100644 --- a/lib/src/cast/widgets/CastIcon.dart +++ b/lib/src/cast/widgets/CastIcon.dart @@ -8,8 +8,10 @@ const Color _defaultIconColor = Color.fromARGB(255, 255, 255, 255); // white class CastIcon extends StatefulWidget { final Color color; + final FlutterCastFramework castFramework; CastIcon({ + required this.castFramework, this.color = _defaultIconColor, }); @@ -27,15 +29,16 @@ Widget _getButton(String assetName, Color color) { } class _CastIconState extends State with TickerProviderStateMixin { - CastState _castState = FlutterCastFramework.castContext.state.value; - + late CastState _castState; CastState get castState => _castState; @override void initState() { super.initState(); + var castContext = widget.castFramework.castContext; - FlutterCastFramework.castContext.state.addListener(_onCastStateChanged); + _castState = castContext.state.value; + castContext.state.addListener(_onCastStateChanged); } void _onCastStateChanged() { @@ -43,7 +46,7 @@ class _CastIconState extends State with TickerProviderStateMixin { setState(() { if (!mounted) return; - _castState = FlutterCastFramework.castContext.state.value; + _castState = widget.castFramework.castContext.state.value; }); } diff --git a/lib/src/flutter_cast_framework.dart b/lib/src/flutter_cast_framework.dart index 42bbd59..e25496c 100644 --- a/lib/src/flutter_cast_framework.dart +++ b/lib/src/flutter_cast_framework.dart @@ -1,95 +1,85 @@ +import 'package:flutter/foundation.dart'; import 'package:flutter_cast_framework/cast.dart'; import 'PlatformBridgeApis.dart'; import 'cast/CastContext.dart'; -class FlutterCastFramework { - static final hostApi = CastHostApi(); +class FlutterCastFramework extends CastFlutterApi { + final hostApi = CastHostApi(); + late CastContext castContext; /// List of namespaces to listen for custom messages - static List namespaces = []; + late List namespaces = []; - static bool _isInitiated = false; - - static _init() { - CastFlutterApi.setup(CastFlutterApiImpl()); + FlutterCastFramework.create(List namespaces) { + debugPrint("FlutterCastFramework created!"); + this.namespaces = namespaces; + this.castContext = CastContext(hostApi); + CastFlutterApi.setup(this); } - static CastContext? _castContext; - - // 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 { + //region CastFlutterApi implementation @override List getSessionMessageNamespaces() { - return FlutterCastFramework.namespaces; + return namespaces; } @override void onCastStateChanged(int castState) { - FlutterCastFramework.castContext.onCastStateChanged(castState); + castContext.onCastStateChanged(castState); } @override void onMessageReceived(CastMessage castMessage) { - FlutterCastFramework.castContext.sessionManager.platformOnMessageReceived(castMessage); + castContext.sessionManager.platformOnMessageReceived(castMessage); } //region Session State handling @override void onSessionEnded() { - FlutterCastFramework.castContext.sessionManager.onSessionStateChanged(SessionState.session_ended); + castContext.sessionManager.onSessionStateChanged(SessionState.session_ended); } @override void onSessionEnding() { - FlutterCastFramework.castContext.sessionManager.onSessionStateChanged(SessionState.session_ending); + castContext.sessionManager.onSessionStateChanged(SessionState.session_ending); } @override void onSessionResumeFailed() { - FlutterCastFramework.castContext.sessionManager.onSessionStateChanged(SessionState.session_resume_failed); + castContext.sessionManager.onSessionStateChanged(SessionState.session_resume_failed); } @override void onSessionResumed() { - FlutterCastFramework.castContext.sessionManager.onSessionStateChanged(SessionState.session_resumed); + castContext.sessionManager.onSessionStateChanged(SessionState.session_resumed); } @override void onSessionResuming() { - FlutterCastFramework.castContext.sessionManager.onSessionStateChanged(SessionState.session_resuming); + castContext.sessionManager.onSessionStateChanged(SessionState.session_resuming); } @override void onSessionStartFailed() { - FlutterCastFramework.castContext.sessionManager.onSessionStateChanged(SessionState.session_start_failed); + castContext.sessionManager.onSessionStateChanged(SessionState.session_start_failed); } @override void onSessionStarted() { - FlutterCastFramework.castContext.sessionManager.onSessionStateChanged(SessionState.session_started); + castContext.sessionManager.onSessionStateChanged(SessionState.session_started); } @override void onSessionStarting() { - FlutterCastFramework.castContext.sessionManager.onSessionStateChanged(SessionState.session_starting); + castContext.sessionManager.onSessionStateChanged(SessionState.session_starting); } @override void onSessionSuspended() { - FlutterCastFramework.castContext.sessionManager.onSessionStateChanged(SessionState.session_suspended); + castContext.sessionManager.onSessionStateChanged(SessionState.session_suspended); } //endregion + + //endregion } \ No newline at end of file