-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9969374
commit 798ce11
Showing
8 changed files
with
103 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
51 changes: 51 additions & 0 deletions
51
lib/vaahextendflutter/services/platform_service/platform_service.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
lib/vaahextendflutter/services/platform_service/services/base_service.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} |
35 changes: 35 additions & 0 deletions
35
lib/vaahextendflutter/services/platform_service/services/platform_channel_service.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |