Line data Source code
1 : part of './services.nativechannels.dart'; 2 : 3 : /// An implementation of [IDevicePlatform] that uses method channels. 4 : class DevicePlatform implements IDevicePlatform { 5 : late MethodChannel _mainChannel; 6 : 7 : late Map<String, bool> _entrypointsHistory; 8 : 9 : IPlatformEntrypoint? _actualEntrypoint; 10 : 11 2 : DevicePlatform({ 12 : required MethodChannel channel, 13 : }) { 14 6 : log('DevicePlatform -> constructor: ${channel.name}'); 15 2 : _mainChannel = channel; 16 4 : _entrypointsHistory = <String, bool>{}; 17 : } 18 : 19 : /// Instantiates a new channel on host. 20 : /// Parameters: 21 : /// [instanceFactory] - Gives us the opportunitty to intercept and overrides some properties. 22 : /// Throws: 23 : /// [ChannelInvokeException] - When there's some problem with native communication. 24 : @override 25 2 : Future<IPlatformEntrypoint> init({ 26 : required InitRequest request, 27 : required CallBacksController controller, 28 : required IPlatformEntrypoint Function({ 29 : required String id, 30 : required String mainChannelId, 31 : required MethodChannel methodChannel, 32 : required EventChannel eventChannel, 33 : required CallBacksController callBacksController, 34 : }) 35 : instanceFactory, 36 : }) async { 37 6 : log('DevicePlatform -> init: ${request.id}'); 38 : 39 6 : final initChannel = await _mainChannel.invokeMethod<dynamic>( 40 : 'init', 41 2 : request.toMap(), 42 : ); 43 : 44 : if (initChannel == null) { 45 1 : throw ChannelInvokeException(); 46 : } 47 : 48 2 : final response = InitResponse.fromMap(initChannel); 49 : 50 2 : final id = response.id; 51 : 52 4 : _actualEntrypoint = instanceFactory( 53 : id: id, 54 4 : mainChannelId: _mainChannel.name, 55 2 : methodChannel: MethodChannel( 56 4 : '${NativeCommunicationMetadata.platformMethodChannel}.$id'), 57 : eventChannel: 58 6 : EventChannel('${NativeCommunicationMetadata.hostEventChannel}.$id'), 59 : callBacksController: controller, 60 : ); 61 : 62 8 : _entrypointsHistory.putIfAbsent(response.id, () => true); 63 : 64 8 : _entrypointsHistory.update(response.id, (_) => true); 65 : 66 2 : return _actualEntrypoint!; 67 : } 68 : 69 1 : @override 70 1 : Map<String, bool> get entrypoints => _entrypointsHistory; 71 : 72 : @override 73 1 : Future<void> dispose(DisposeRequest request) async { 74 3 : log('DevicePlatform -> dispose: ${request.id}'); 75 : 76 3 : final answer = await _mainChannel.invokeMethod<Map<dynamic, dynamic>>( 77 1 : 'dispose', request.toMap()); 78 : 79 : if (answer == null) { 80 1 : throw ChannelInvalidAnswerException(); 81 : } 82 : 83 2 : _actualEntrypoint?.dispose(); 84 : 85 1 : final _ = DisposeResponse.fromMap(answer); 86 : 87 4 : _entrypointsHistory.update(request.id, (_) => false); 88 : 89 : return; 90 : } 91 : }