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> {
|
||||
late FlutterCastFramework castFramework;
|
||||
|
||||
CastState _castState = CastState.idle;
|
||||
SessionState _sessionState = SessionState.idle;
|
||||
String _message = '';
|
||||
|
|
@ -21,11 +23,11 @@ class _MyAppState extends State<MyApp> {
|
|||
@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<MyApp> {
|
|||
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<MyApp> {
|
|||
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<MyApp> {
|
|||
|
||||
void _onSendMessage() {
|
||||
String message = this.textMessageController.text;
|
||||
FlutterCastFramework.castContext.sessionManager
|
||||
castFramework.castContext.sessionManager
|
||||
.sendMessage(castNamespace, message);
|
||||
}
|
||||
|
||||
|
|
@ -75,6 +77,7 @@ class _MyAppState extends State<MyApp> {
|
|||
child: Column(
|
||||
children: [
|
||||
CastButton(
|
||||
castFramework: castFramework,
|
||||
color: Colors.blue,
|
||||
),
|
||||
Text(
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<CastIcon> 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<CastIcon> with TickerProviderStateMixin {
|
|||
|
||||
setState(() {
|
||||
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 '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<String> namespaces = [];
|
||||
late List<String> namespaces = [];
|
||||
|
||||
static bool _isInitiated = false;
|
||||
|
||||
static _init() {
|
||||
CastFlutterApi.setup(CastFlutterApiImpl());
|
||||
FlutterCastFramework.create(List<String> 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<String?> 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
|
||||
}
|
||||
Loading…
Reference in a new issue