Skip to content

Commit 30f87f1

Browse files
we-mohd-i001we-md-s001
authored andcommitted
Added: pararameter & exceptions
Added: methodChannelName parameter Updated: method channel name from env config Added: Exceptions Update: renamed MethodChannelService to PlatformChannelService Added: MethodChannel parameter
1 parent b864645 commit 30f87f1

File tree

9 files changed

+51
-36
lines changed

9 files changed

+51
-36
lines changed

assets/env/develop.json

Lines changed: 2 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import 'services/base_service.dart';
2-
import 'services/method_channel_service.dart';
2+
import 'services/platform_channel_service.dart';
33

44
BasePlatformService get service {
5-
return MethodChannelService();
5+
return PlatformChannelService();
66
}
77

88
abstract class PlatformService {
@@ -12,7 +12,7 @@ abstract class PlatformService {
1212
return _service.invokeMethod<T>(method, arguments);
1313
}
1414

15-
static Stream<dynamic> getEventStream(String eventChannelName) {
16-
return _service.getEventStream(eventChannelName);
15+
static Stream<dynamic> getEventStream(String eventChannelName, [dynamic arguments]) {
16+
return _service.getEventStream(eventChannelName, arguments);
1717
}
1818
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
abstract class BasePlatformService {
22
Future<T?> invokeMethod<T>(String method, [dynamic arguments]);
3-
Stream<dynamic> getEventStream(String eventChannelName);
3+
Stream<dynamic> getEventStream(String eventChannelName, [dynamic arguments]);
44
}

lib/vaahextendflutter/services/platform_service/services/method_channel_service.dart

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
}
24+
}
25+
26+
@override
27+
Stream<dynamic> getEventStream(String eventChannelName, [dynamic arguments]) {
28+
if (!_eventChannels.containsKey(eventChannelName)) {
29+
_eventChannels[eventChannelName] = EventChannel(eventChannelName);
30+
}
31+
return _eventChannels[eventChannelName]!.receiveBroadcastStream(arguments);
32+
}
33+
}

0 commit comments

Comments
 (0)