Skip to content

Commit 798ce11

Browse files
committed
Added: platform service
1 parent 9969374 commit 798ce11

File tree

8 files changed

+103
-5
lines changed

8 files changed

+103
-5
lines changed

assets/env/develop.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@
1717
"one_signal_config": null,
1818
"pusher_config": null,
1919
"show_debug_panel": true,
20+
"method_channel_name": "",
2021
"debug_panel_color": 3422552064
21-
}
22+
}

assets/env/production.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@
1717
"one_signal_config": null,
1818
"pusher_config": null,
1919
"show_debug_panel": false,
20+
"method_channel_name": "",
2021
"debug_panel_color": 3422552064
21-
}
22+
}

assets/env/staging.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@
1717
"one_signal_config": null,
1818
"pusher_config": null,
1919
"show_debug_panel": true,
20+
"method_channel_name": "",
2021
"debug_panel_color": 3422552064
21-
}
22+
}

lib/vaahextendflutter/env/env.dart

+3
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class EnvironmentConfig {
5858
this.oneSignalConfig,
5959
this.pusherConfig,
6060
required this.showDebugPanel,
61+
required this.methodChannelName,
6162
required this.debugPanelColor,
6263
});
6364

@@ -78,6 +79,7 @@ class EnvironmentConfig {
7879
final OneSignalConfig? oneSignalConfig;
7980
final PusherConfig? pusherConfig;
8081
final bool showDebugPanel;
82+
final String methodChannelName;
8183
@JsonKey(fromJson: _colorFromJson, toJson: _colorToJson)
8284
final Color debugPanelColor;
8385

@@ -119,6 +121,7 @@ class EnvironmentConfig {
119121
pushNotificationsServiceType: PushNotificationsServiceType.none,
120122
internalNotificationsServiceType: InternalNotificationsServiceType.none,
121123
showDebugPanel: true,
124+
methodChannelName: '',
122125
debugPanelColor: Colors.black.withOpacity(0.8),
123126
);
124127
}

lib/vaahextendflutter/env/env.g.dart

+4-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import 'package:flutter/foundation.dart';
2+
3+
import 'services/base_service.dart';
4+
import 'services/platform_channel_service.dart';
5+
6+
BasePlatformService get service {
7+
return PlatformChannelService();
8+
}
9+
10+
abstract class PlatformService {
11+
static final BasePlatformService _service = service;
12+
13+
/// Invokes a [method] with or without [arguments] present at the native side.
14+
///
15+
/// Returns a [Future] which completes to one of the following:
16+
///
17+
/// * on successful invocation, a result (possibly null),
18+
/// * if the invocation failed in the platform plugin, a [PlatformException],
19+
/// * if the method has not been implemented by a platform plugin, a [MissingPluginException].
20+
///
21+
/// Example:
22+
/// ```dart
23+
/// final int sum = await PlatformService.invokeMethod<int>('getSum', {'a': 10, 'b': 20});
24+
/// print(sum); // 30
25+
/// ```
26+
static Future<T?> invokeMethod<T>(String method, [dynamic arguments]) {
27+
return _service.invokeMethod<T>(method, arguments);
28+
}
29+
30+
/// Sets up a [Stream] using the [eventChannelName] with or without [argumetns].
31+
///
32+
/// Returns a broadcast [Stream] which emits events to listeners as follows:
33+
///
34+
/// * for every successfull event, a decoded data event (possibly null) is received from the
35+
/// platform plugin;
36+
/// * for every error event, an error event containing a [PlatformException]
37+
/// received from the platform plugin.
38+
///
39+
/// When a stream is activated or deactivated, errors that happen are reported using the
40+
/// [FlutterError] capability. Only when the number of stream listeners increases from 0 to 1 does
41+
/// the stream become active. Deactivation of the stream only occurs when the number of stream
42+
/// listeners drops to zero.
43+
///
44+
/// Example:
45+
/// ```dart
46+
/// final myStream = PlatformService.getEventStream('com.example.app/battery');
47+
/// ```
48+
static Stream<dynamic> getEventStream(String eventChannelName, [dynamic arguments]) {
49+
return _service.getEventStream(eventChannelName, arguments);
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
abstract class BasePlatformService {
2+
Future<T?> invokeMethod<T>(String method, [dynamic arguments]);
3+
Stream<dynamic> getEventStream(String eventChannelName, [dynamic arguments]);
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import 'package:flutter/services.dart';
2+
3+
import '../../../env/env.dart';
4+
import 'base_service.dart';
5+
6+
class PlatformChannelService implements BasePlatformService {
7+
final String _methodChannelName = EnvironmentConfig.getConfig.methodChannelName;
8+
static final Map<String, EventChannel> _eventChannels = {};
9+
10+
@override
11+
Future<T?> invokeMethod<T>(String method, [dynamic arguments]) async {
12+
final MethodChannel channel = MethodChannel(_methodChannelName);
13+
try {
14+
final result = await channel.invokeMethod<T>(method, arguments);
15+
return result;
16+
} on MissingPluginException catch (e) {
17+
if (channel.name.isEmpty) {
18+
throw 'Please provide correct method_channel_name in env config: ${e.message}';
19+
}
20+
throw 'No plugin handler for the method call was found: ${e.message}(${channel.name})';
21+
} on PlatformException catch (e) {
22+
throw 'Failed to invoke method: ${e.message}';
23+
} on Exception catch (_) {
24+
rethrow;
25+
}
26+
}
27+
28+
@override
29+
Stream<dynamic> getEventStream(String eventChannelName, [dynamic arguments]) {
30+
if (!_eventChannels.containsKey(eventChannelName)) {
31+
_eventChannels[eventChannelName] = EventChannel(eventChannelName);
32+
}
33+
return _eventChannels[eventChannelName]!.receiveBroadcastStream(arguments);
34+
}
35+
}

0 commit comments

Comments
 (0)