Skip to content

Commit fd0ab60

Browse files
committed
Method Channel Improvements Done
1 parent 87f2baf commit fd0ab60

File tree

11 files changed

+167
-28
lines changed

11 files changed

+167
-28
lines changed
Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,30 @@
11
package com.webreinvent.vaahflutter
22

3+
import android.os.Bundle
4+
import android.widget.Toast
35
import io.flutter.embedding.android.FlutterActivity
6+
import io.flutter.embedding.engine.FlutterEngine
7+
import io.flutter.plugin.common.MethodChannel
48

5-
class MainActivity: FlutterActivity() {
9+
class MainActivity : FlutterActivity() {
10+
private val CHANNEL = "snackbar_channel"
11+
12+
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
13+
super.configureFlutterEngine(flutterEngine)
14+
15+
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result ->
16+
if (call.method == "showSnackbar") {
17+
showSnackbar()
18+
result.success("Snackbar displayed")
19+
} else {
20+
result.notImplemented()
21+
}
22+
}
23+
}
24+
25+
private fun showSnackbar() {
26+
runOnUiThread {
27+
Toast.makeText(this, "This is a test", Toast.LENGTH_SHORT).show()
28+
}
29+
}
630
}

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_map": {},
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
@@ -62,6 +62,7 @@ class EnvironmentConfig {
6262
this.oneSignalConfig,
6363
this.pusherConfig,
6464
required this.showDebugPanel,
65+
required this.methodChannelMap,
6566
required this.debugPanelColor,
6667
});
6768

@@ -82,6 +83,7 @@ class EnvironmentConfig {
8283
final OneSignalConfig? oneSignalConfig;
8384
final PusherConfig? pusherConfig;
8485
final bool showDebugPanel;
86+
final Map<String, dynamic> methodChannelMap;
8587
@JsonKey(fromJson: _colorFromJson, toJson: _colorToJson)
8688
final Color debugPanelColor;
8789

@@ -123,6 +125,7 @@ class EnvironmentConfig {
123125
pushNotificationsServiceType: PushNotificationsServiceType.none,
124126
internalNotificationsServiceType: InternalNotificationsServiceType.none,
125127
showDebugPanel: true,
128+
methodChannelMap: {},
126129
debugPanelColor: Colors.black.withOpacity(0.8),
127130
);
128131
}

lib/vaahextendflutter/env/env.g.dart

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 51 additions & 0 deletions
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+
// }
Lines changed: 4 additions & 0 deletions
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+
}
Lines changed: 35 additions & 0 deletions
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+
static final Map<String, EventChannel> _eventChannels = {};
8+
9+
@override
10+
Future<T?> invokeMethod<T>(String method, [dynamic arguments]) async {
11+
final String methodChannelName = EnvironmentConfig.getConfig.methodChannelMap[method];
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+
}

lib/views/pages/home.dart

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:flutter/material.dart';
2+
import 'package:flutter/services.dart';
23

34
import '../../vaahextendflutter/services/notification/internal/notification_view.dart';
45
import 'ui/index.dart';
@@ -20,16 +21,32 @@ class HomePage extends StatefulWidget {
2021
}
2122

2223
class _HomePageState extends State<HomePage> {
24+
static const MethodChannel _platform = MethodChannel('snackbar_channel');
25+
26+
Future<void> _showNativeSnackbar() async {
27+
try {
28+
final result = await _platform.invokeMethod<String>('showSnackbar');
29+
debugPrint("Native Snackbar Response: $result");
30+
} on PlatformException catch (e) {
31+
debugPrint("Failed to show snackbar: ${e.message}");
32+
} on MissingPluginException catch (_) {
33+
debugPrint("MethodChannel not implemented on this platform");
34+
}
35+
}
36+
2337
@override
2438
void initState() {
2539
super.initState();
26-
// TODO:
27-
// increaseOpenCount();
40+
// TODO: increaseOpenCount();
2841
}
2942

3043
@override
3144
Widget build(BuildContext context) {
3245
return Scaffold(
46+
floatingActionButton: FloatingActionButton(
47+
onPressed: _showNativeSnackbar,
48+
child: const Icon(Icons.message),
49+
),
3350
appBar: AppBar(
3451
actions: const [
3552
InternalNotificationsBadge(),

0 commit comments

Comments
 (0)