Line data Source code
1 : import 'package:flutter/foundation.dart';
2 : import 'package:flutter/services.dart';
3 : import 'package:flutter_chrome_cast/entities/cast_device.dart';
4 : import 'package:flutter_chrome_cast/models/ios/ios_cast_device.dart';
5 : import 'package:rxdart/subjects.dart';
6 : import 'discovery_manager_platform_interface.dart';
7 :
8 : /// iOS-specific implementation of the Google Cast discovery manager.
9 : ///
10 : /// This class handles the discovery of Google Cast devices on iOS platform
11 : /// using method channels to communicate with the native iOS implementation.
12 : class GoogleCastDiscoveryManagerMethodChannelIOS
13 : implements GoogleCastDiscoveryManagerPlatformInterface {
14 : /// Creates a new instance of the iOS discovery manager.
15 : ///
16 : /// Sets up the method call handler to receive updates from the native side.
17 2 : GoogleCastDiscoveryManagerMethodChannelIOS() {
18 6 : _channel.setMethodCallHandler(_handleMethodCall);
19 : }
20 :
21 : final _channel = const MethodChannel('google_cast.discovery_manager');
22 :
23 : final _devicesStreamController = BehaviorSubject<List<GoogleCastDevice>>()
24 : ..add([]);
25 :
26 1 : @override
27 2 : List<GoogleCastDevice> get devices => _devicesStreamController.value;
28 :
29 1 : @override
30 : Stream<List<GoogleCastDevice>> get devicesStream =>
31 2 : _devicesStreamController.stream;
32 :
33 1 : @override
34 : Future<bool> isDiscoveryActiveForDeviceCategory(String deviceCategory) async {
35 3 : return await _channel.invokeMethod('isDiscoveryActiveForDeviceCategory', {
36 : 'deviceCategory': deviceCategory,
37 : });
38 : }
39 :
40 1 : @override
41 : Future<void> startDiscovery() {
42 2 : return _channel.invokeMethod('startDiscovery');
43 : }
44 :
45 1 : @override
46 : Future<void> stopDiscovery() {
47 2 : return _channel.invokeMethod('stopDiscovery');
48 : }
49 :
50 1 : @visibleForTesting
51 : void onDevicesChanged(List arguments) {
52 1 : _onDevicesChanged(arguments);
53 : }
54 :
55 1 : @visibleForTesting
56 : Future<void> handleMethodCall(MethodCall call) {
57 1 : return _handleMethodCall(call);
58 : }
59 :
60 1 : void _onDevicesChanged(List arguments) {
61 1 : final devices = List.from(arguments)
62 4 : .map((device) => GoogleCastIosDevice.fromMap(Map.from(device)))
63 1 : .toList();
64 :
65 2 : _devicesStreamController.add(devices);
66 : }
67 :
68 1 : Future _handleMethodCall(MethodCall call) async {
69 1 : switch (call.method) {
70 1 : case 'onDevicesChanged':
71 2 : _onDevicesChanged(call.arguments);
72 : break;
73 : default:
74 : if (kDebugMode) {
75 3 : print('No Handler for method ${call.method}');
76 : }
77 : }
78 : }
79 : }
|