Initial CastContext setup;
CastState handling
This commit is contained in:
parent
e8dc95aaa1
commit
344c5b6b8a
5 changed files with 65 additions and 33 deletions
|
|
@ -1,17 +1,26 @@
|
|||
package com.gianlucaparadise.flutter_cast_framework
|
||||
|
||||
import android.util.Log
|
||||
import com.google.android.gms.cast.framework.CastContext
|
||||
import io.flutter.plugin.common.MethodCall
|
||||
import io.flutter.plugin.common.MethodChannel
|
||||
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
|
||||
import io.flutter.plugin.common.MethodChannel.Result
|
||||
import io.flutter.plugin.common.PluginRegistry.Registrar
|
||||
|
||||
class FlutterCastFrameworkPlugin: MethodCallHandler {
|
||||
class FlutterCastFrameworkPlugin(registrar: Registrar, private val channel: MethodChannel): MethodCallHandler {
|
||||
companion object {
|
||||
@JvmStatic
|
||||
fun registerWith(registrar: Registrar) {
|
||||
val channel = MethodChannel(registrar.messenger(), "flutter_cast_framework")
|
||||
channel.setMethodCallHandler(FlutterCastFrameworkPlugin())
|
||||
channel.setMethodCallHandler(FlutterCastFrameworkPlugin(registrar, channel))
|
||||
}
|
||||
}
|
||||
|
||||
init {
|
||||
CastContext.getSharedInstance(registrar.activeContext()).addCastStateListener { i ->
|
||||
Log.d("Android", "Method call on flutter: $i")
|
||||
channel.invokeMethod("onCastStateChanged", i)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,5 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_cast_framework/cast/CastContext.dart';
|
||||
import 'package:flutter_cast_framework/flutter_cast_framework.dart';
|
||||
|
||||
void main() => runApp(MyApp());
|
||||
|
|
@ -12,31 +10,17 @@ class MyApp extends StatefulWidget {
|
|||
}
|
||||
|
||||
class _MyAppState extends State<MyApp> {
|
||||
String _platformVersion = 'Unknown';
|
||||
CastState _castState;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
initPlatformState();
|
||||
FlutterCastFramework.castContext.state.addListener(_onCastStateChanged);
|
||||
}
|
||||
|
||||
// Platform messages are asynchronous, so we initialize in an async method.
|
||||
Future<void> initPlatformState() async {
|
||||
String platformVersion;
|
||||
// Platform messages may fail, so we use a try/catch PlatformException.
|
||||
try {
|
||||
platformVersion = await FlutterCastFramework.platformVersion;
|
||||
} on PlatformException {
|
||||
platformVersion = 'Failed to get platform version.';
|
||||
}
|
||||
|
||||
// If the widget was removed from the tree while the asynchronous platform
|
||||
// message was in flight, we want to discard the reply rather than calling
|
||||
// setState to update our non-existent appearance.
|
||||
if (!mounted) return;
|
||||
|
||||
void _onCastStateChanged() {
|
||||
setState(() {
|
||||
_platformVersion = platformVersion;
|
||||
_castState = FlutterCastFramework.castContext.state.value;
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -48,7 +32,7 @@ class _MyAppState extends State<MyApp> {
|
|||
title: const Text('Cast plugin example app'),
|
||||
),
|
||||
body: Center(
|
||||
child: Text('Running on: $_platformVersion\n'),
|
||||
child: Text('Cast State: $_castState'),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
|
|
|||
19
lib/cast/CastContext.dart
Normal file
19
lib/cast/CastContext.dart
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
import 'package:flutter/foundation.dart';
|
||||
|
||||
class CastContext {
|
||||
static final _instance = new CastContext._internal();
|
||||
|
||||
CastContext._internal();
|
||||
|
||||
static CastContext get instance => _instance;
|
||||
|
||||
final ValueNotifier<CastState> state = ValueNotifier(CastState.unavailable);
|
||||
}
|
||||
|
||||
enum CastState {
|
||||
default_state, // 0
|
||||
unavailable, // 1
|
||||
unconnected, // 2
|
||||
connecting, // 3
|
||||
connected, // 4
|
||||
}
|
||||
|
|
@ -1,13 +1,34 @@
|
|||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_cast_framework/cast/CastContext.dart';
|
||||
|
||||
class FlutterCastFramework {
|
||||
static const MethodChannel _channel =
|
||||
const MethodChannel('flutter_cast_framework');
|
||||
|
||||
static Future<String> get platformVersion async {
|
||||
final String version = await _channel.invokeMethod('getPlatformVersion');
|
||||
return version;
|
||||
static const MethodChannel _channel = const MethodChannel('flutter_cast_framework');
|
||||
|
||||
static bool _isInitiated = false;
|
||||
static _init() {
|
||||
_channel.setMethodCallHandler((MethodCall call) async {
|
||||
String method = call.method;
|
||||
dynamic arguments = call.arguments;
|
||||
debugPrint("Method call on flutter: $method $arguments");
|
||||
|
||||
switch (method) {
|
||||
case "onCastStateChanged":
|
||||
int castState = arguments;
|
||||
castContext.state.value = CastState.values[castState];
|
||||
break;
|
||||
|
||||
default:
|
||||
debugPrint("Method not handled: $method");
|
||||
break;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// This must be the plugin entry point
|
||||
static CastContext get castContext {
|
||||
if (!_isInitiated) _init();
|
||||
return CastContext.instance;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:flutter_cast_framework/flutter_cast_framework.dart';
|
||||
|
||||
void main() {
|
||||
const MethodChannel channel = MethodChannel('flutter_cast_framework');
|
||||
|
|
@ -16,6 +15,6 @@ void main() {
|
|||
});
|
||||
|
||||
test('getPlatformVersion', () async {
|
||||
expect(await FlutterCastFramework.platformVersion, '42');
|
||||
// expect(await FlutterCastFramework.platformVersion, '42');
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue