1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- import 'dart:async';
- import 'dart:convert';
- import 'package:flutter/foundation.dart';
- import 'package:flutter/services.dart';
- import 'event_entrepot.dart';
- import 'vnote_device_plugin_platform_interface.dart';
- /// An implementation of [VnoteDevicePluginPlatform] that uses method channels.
- class MethodChannelVnoteDevicePlugin extends VnoteDevicePluginPlatform {
- /// The method channel used to interact with the native platform.
- @visibleForTesting
- final methodChannel = const MethodChannel('vnote_device_plugin');
- final eventChannel = const EventChannel("vnote_device_plugin_event");
- StreamSubscription<dynamic>? _eventChannelSubcription;
- @override
- Future<String?> getPlatformVersion() async {
- final version =
- await methodChannel.invokeMethod<String>('getPlatformVersion');
- return version;
- }
- @override
- Future<void> callAction(String action, Map<String, dynamic> data) async {
- data["ACTION"] = action;
- await methodChannel.invokeMethod(
- "callAction",
- {
- "request": jsonEncode(data),
- },
- );
- }
- @override
- Future<void> init() async {
- _eventChannelSubcription ??=
- eventChannel.receiveBroadcastStream().listen(_onEventReceived);
- return methodChannel.invokeMethod("init");
- }
- void _onEventReceived(dynamic event) {
- final String jsonText = event;
- final jsonMap = jsonDecode(jsonText);
- EventEntrepot.instance.receiveEvent(jsonMap);
- }
- }
|