Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature -> Develop | Added Platform Channel Service #44

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion assets/env/develop.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
"one_signal_config": null,
"pusher_config": null,
"show_debug_panel": true,
"method_channel_name": "",
"debug_panel_color": 3422552064
}
}
3 changes: 2 additions & 1 deletion assets/env/production.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
"one_signal_config": null,
"pusher_config": null,
"show_debug_panel": false,
"method_channel_name": "",
"debug_panel_color": 3422552064
}
}
3 changes: 2 additions & 1 deletion assets/env/staging.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
"one_signal_config": null,
"pusher_config": null,
"show_debug_panel": true,
"method_channel_name": "",
"debug_panel_color": 3422552064
}
}
3 changes: 3 additions & 0 deletions lib/vaahextendflutter/env/env.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class EnvironmentConfig {
this.oneSignalConfig,
this.pusherConfig,
required this.showDebugPanel,
required this.methodChannelName,
required this.debugPanelColor,
});

Expand All @@ -78,6 +79,7 @@ class EnvironmentConfig {
final OneSignalConfig? oneSignalConfig;
final PusherConfig? pusherConfig;
final bool showDebugPanel;
final String methodChannelName;
@JsonKey(fromJson: _colorFromJson, toJson: _colorToJson)
final Color debugPanelColor;

Expand Down Expand Up @@ -119,6 +121,7 @@ class EnvironmentConfig {
pushNotificationsServiceType: PushNotificationsServiceType.none,
internalNotificationsServiceType: InternalNotificationsServiceType.none,
showDebugPanel: true,
methodChannelName: '',
debugPanelColor: Colors.black.withOpacity(0.8),
);
}
Expand Down
2 changes: 2 additions & 0 deletions lib/vaahextendflutter/env/env.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -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<int>('getSum', {'a': 10, 'b': 20});
/// print(sum); // 30
/// ```
static Future<T?> invokeMethod<T>(String method, [dynamic arguments]) {
return _service.invokeMethod<T>(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<dynamic> getEventStream(String eventChannelName, [dynamic arguments]) {
return _service.getEventStream(eventChannelName, arguments);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
abstract class BasePlatformService {
Future<T?> invokeMethod<T>(String method, [dynamic arguments]);
Stream<dynamic> getEventStream(String eventChannelName, [dynamic arguments]);
}
Original file line number Diff line number Diff line change
@@ -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<String, EventChannel> _eventChannels = {};

@override
Future<T?> invokeMethod<T>(String method, [dynamic arguments]) async {
final MethodChannel channel = MethodChannel(_methodChannelName);
try {
final result = await channel.invokeMethod<T>(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<dynamic> getEventStream(String eventChannelName, [dynamic arguments]) {
if (!_eventChannels.containsKey(eventChannelName)) {
_eventChannels[eventChannelName] = EventChannel(eventChannelName);
}
return _eventChannels[eventChannelName]!.receiveBroadcastStream(arguments);
}
}