diff --git a/assets/env/develop.json b/assets/env/develop.json index 9fb4928..9021373 100644 --- a/assets/env/develop.json +++ b/assets/env/develop.json @@ -17,5 +17,6 @@ "one_signal_config": null, "pusher_config": null, "show_debug_panel": true, + "method_channel_name": "", "debug_panel_color": 3422552064 -} +} \ No newline at end of file diff --git a/assets/env/production.json b/assets/env/production.json index bd07363..b1812c6 100644 --- a/assets/env/production.json +++ b/assets/env/production.json @@ -17,5 +17,6 @@ "one_signal_config": null, "pusher_config": null, "show_debug_panel": false, + "method_channel_name": "", "debug_panel_color": 3422552064 -} +} \ No newline at end of file diff --git a/assets/env/staging.json b/assets/env/staging.json index a530382..4d31f12 100644 --- a/assets/env/staging.json +++ b/assets/env/staging.json @@ -17,5 +17,6 @@ "one_signal_config": null, "pusher_config": null, "show_debug_panel": true, + "method_channel_name": "", "debug_panel_color": 3422552064 -} +} \ No newline at end of file diff --git a/lib/vaahextendflutter/env/env.dart b/lib/vaahextendflutter/env/env.dart index eb09b9c..ebb49fe 100644 --- a/lib/vaahextendflutter/env/env.dart +++ b/lib/vaahextendflutter/env/env.dart @@ -62,6 +62,7 @@ class EnvironmentConfig { this.oneSignalConfig, this.pusherConfig, required this.showDebugPanel, + required this.methodChannelName, required this.debugPanelColor, }); @@ -82,6 +83,7 @@ class EnvironmentConfig { final OneSignalConfig? oneSignalConfig; final PusherConfig? pusherConfig; final bool showDebugPanel; + final String methodChannelName; @JsonKey(fromJson: _colorFromJson, toJson: _colorToJson) final Color debugPanelColor; @@ -123,6 +125,7 @@ class EnvironmentConfig { pushNotificationsServiceType: PushNotificationsServiceType.none, internalNotificationsServiceType: InternalNotificationsServiceType.none, showDebugPanel: true, + methodChannelName: '', debugPanelColor: Colors.black.withOpacity(0.8), ); } diff --git a/lib/vaahextendflutter/env/env.g.dart b/lib/vaahextendflutter/env/env.g.dart index 740c761..b994818 100644 --- a/lib/vaahextendflutter/env/env.g.dart +++ b/lib/vaahextendflutter/env/env.g.dart @@ -38,8 +38,9 @@ EnvironmentConfig _$EnvironmentConfigFromJson(Map json) => : PusherConfig.fromJson( json['pusher_config'] as Map), showDebugPanel: json['show_debug_panel'] as bool, - debugPanelColor: EnvironmentConfig._colorFromJson( - (json['debug_panel_color'] as num).toInt()), + methodChannelName: json['method_channel_name'] as String, + debugPanelColor: + EnvironmentConfig._colorFromJson(json['debug_panel_color'] as int), ); Map _$EnvironmentConfigToJson(EnvironmentConfig instance) => @@ -64,6 +65,7 @@ Map _$EnvironmentConfigToJson(EnvironmentConfig instance) => 'one_signal_config': instance.oneSignalConfig, 'pusher_config': instance.pusherConfig, 'show_debug_panel': instance.showDebugPanel, + 'method_channel_name': instance.methodChannelName, 'debug_panel_color': EnvironmentConfig._colorToJson(instance.debugPanelColor), }; diff --git a/lib/vaahextendflutter/services/platform_service/platform_service.dart b/lib/vaahextendflutter/services/platform_service/platform_service.dart new file mode 100644 index 0000000..4fb0933 --- /dev/null +++ b/lib/vaahextendflutter/services/platform_service/platform_service.dart @@ -0,0 +1,51 @@ +import 'package:flutter/foundation.dart'; + +import 'services/base_service.dart'; +import 'services/platform_channel_service.dart'; + +BasePlatformService get service { + return PlatformChannelService(); +} + +abstract class PlatformService { + static final BasePlatformService _service = service; + + /// Invokes a [method] with or without [arguments] present at the native side. + /// + /// Returns a [Future] which completes to one of the following: + /// + /// * on successful invocation, a result (possibly null), + /// * if the invocation failed in the platform plugin, a [PlatformException], + /// * if the method has not been implemented by a platform plugin, a [MissingPluginException]. + /// + /// Example: + /// ```dart + /// final int sum = await PlatformService.invokeMethod('getSum', {'a': 10, 'b': 20}); + /// print(sum); // 30 + /// ``` + static Future invokeMethod(String method, [dynamic arguments]) { + return _service.invokeMethod(method, arguments); + } + + /// Sets up a [Stream] using the [eventChannelName] with or without [argumetns]. + /// + /// Returns a broadcast [Stream] which emits events to listeners as follows: + /// + /// * for every successfull event, a decoded data event (possibly null) is received from the + /// platform plugin; + /// * for every error event, an error event containing a [PlatformException] + /// received from the platform plugin. + /// + /// When a stream is activated or deactivated, errors that happen are reported using the + /// [FlutterError] capability. Only when the number of stream listeners increases from 0 to 1 does + /// the stream become active. Deactivation of the stream only occurs when the number of stream + /// listeners drops to zero. + /// + /// Example: + /// ```dart + /// final myStream = PlatformService.getEventStream('com.example.app/battery'); + /// ``` + static Stream getEventStream(String eventChannelName, [dynamic arguments]) { + return _service.getEventStream(eventChannelName, arguments); + } +} diff --git a/lib/vaahextendflutter/services/platform_service/services/base_service.dart b/lib/vaahextendflutter/services/platform_service/services/base_service.dart new file mode 100644 index 0000000..dad7768 --- /dev/null +++ b/lib/vaahextendflutter/services/platform_service/services/base_service.dart @@ -0,0 +1,4 @@ +abstract class BasePlatformService { + Future invokeMethod(String method, [dynamic arguments]); + Stream getEventStream(String eventChannelName, [dynamic arguments]); +} diff --git a/lib/vaahextendflutter/services/platform_service/services/platform_channel_service.dart b/lib/vaahextendflutter/services/platform_service/services/platform_channel_service.dart new file mode 100644 index 0000000..89059c7 --- /dev/null +++ b/lib/vaahextendflutter/services/platform_service/services/platform_channel_service.dart @@ -0,0 +1,35 @@ +import 'package:flutter/services.dart'; + +import '../../../env/env.dart'; +import 'base_service.dart'; + +class PlatformChannelService implements BasePlatformService { + final String _methodChannelName = EnvironmentConfig.getConfig.methodChannelName; + static final Map _eventChannels = {}; + + @override + Future invokeMethod(String method, [dynamic arguments]) async { + final MethodChannel channel = MethodChannel(_methodChannelName); + try { + final result = await channel.invokeMethod(method, arguments); + return result; + } on MissingPluginException catch (e) { + if (channel.name.isEmpty) { + throw 'Please provide correct method_channel_name in env config: ${e.message}'; + } + throw 'No plugin handler for the method call was found: ${e.message}(${channel.name})'; + } on PlatformException catch (e) { + throw 'Failed to invoke method: ${e.message}'; + } on Exception catch (_) { + rethrow; + } + } + + @override + Stream getEventStream(String eventChannelName, [dynamic arguments]) { + if (!_eventChannels.containsKey(eventChannelName)) { + _eventChannels[eventChannelName] = EventChannel(eventChannelName); + } + return _eventChannels[eventChannelName]!.receiveBroadcastStream(arguments); + } +}