-
Notifications
You must be signed in to change notification settings - Fork 1
Feature -> Develop | Added Platform Channel Service #44
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
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,30 @@ | ||
package com.webreinvent.vaahflutter | ||
|
||
import android.os.Bundle | ||
import android.widget.Toast | ||
import io.flutter.embedding.android.FlutterActivity | ||
import io.flutter.embedding.engine.FlutterEngine | ||
import io.flutter.plugin.common.MethodChannel | ||
|
||
class MainActivity: FlutterActivity() { | ||
class MainActivity : FlutterActivity() { | ||
private val CHANNEL = "snackbar_channel" | ||
|
||
override fun configureFlutterEngine(flutterEngine: FlutterEngine) { | ||
super.configureFlutterEngine(flutterEngine) | ||
|
||
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result -> | ||
if (call.method == "showSnackbar") { | ||
showSnackbar() | ||
result.success("Snackbar displayed") | ||
} else { | ||
result.notImplemented() | ||
} | ||
} | ||
} | ||
|
||
private fun showSnackbar() { | ||
runOnUiThread { | ||
Toast.makeText(this, "This is a test", Toast.LENGTH_SHORT).show() | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,4 +18,4 @@ | |
"pusher_config": null, | ||
"show_debug_panel": true, | ||
"debug_panel_color": 3422552064 | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,4 +18,4 @@ | |
"pusher_config": null, | ||
"show_debug_panel": false, | ||
"debug_panel_color": 3422552064 | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,4 +18,4 @@ | |
"pusher_config": null, | ||
"show_debug_panel": true, | ||
"debug_panel_color": 3422552064 | ||
} | ||
} |
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,36 @@ | ||
import 'package:flutter/services.dart'; | ||
|
||
import 'base_service.dart'; | ||
|
||
class PlatformChannelService implements BasePlatformService { | ||
static final Map<String, EventChannel> _eventChannels = {}; | ||
static final Map<String, String> _methodChannels = {}; | ||
|
||
@override | ||
Future<T?> invokeMethod<T>(String method, [dynamic arguments]) async { | ||
final String methodChannelName = _methodChannels.putIfAbsent(method, () => method); | ||
|
||
if (methodChannelName.isEmpty) { | ||
throw 'Method channel name not found for method: $method'; | ||
} | ||
|
||
final MethodChannel channel = MethodChannel(methodChannelName); | ||
|
||
try { | ||
return await channel.invokeMethod<T>(methodChannelName, arguments); | ||
} on MissingPluginException catch (e) { | ||
throw 'No plugin handler for the method call: ${e.message} (${channel.name})'; | ||
} on PlatformException catch (e) { | ||
throw 'Failed to invoke method: ${e.message}'; | ||
} catch (e) { | ||
throw 'Unexpected error invoking method: $e'; | ||
} | ||
} | ||
|
||
@override | ||
Stream<dynamic> getEventStream(String eventChannelName, [dynamic arguments]) { | ||
return _eventChannels | ||
.putIfAbsent(eventChannelName, () => EventChannel(eventChannelName)) | ||
.receiveBroadcastStream(arguments); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter/services.dart'; | ||
|
||
import '../../vaahextendflutter/services/notification/internal/notification_view.dart'; | ||
import 'ui/index.dart'; | ||
|
@@ -20,16 +21,41 @@ class HomePage extends StatefulWidget { | |
} | ||
|
||
class _HomePageState extends State<HomePage> { | ||
static const MethodChannel _platform = MethodChannel('snackbar_channel'); | ||
|
||
/// Calls the native platform method to show a snackbar. | ||
Future<void> _showNativeSnackbar() async { | ||
try { | ||
final String? result = await _platform.invokeMethod<String>('showSnackbar'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we could use a file to keep track of methods like - PlatformMethods.showSnackbar for known methods while allowing users to call any method name as a string, instead of hard-coding it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sure I will find a better way instead of harcoding it. |
||
if (result != null) { | ||
debugPrint("Native Snackbar Response: $result"); | ||
} else { | ||
debugPrint("Native Snackbar returned null response"); | ||
} | ||
} on PlatformException catch (e, stackTrace) { | ||
debugPrint("Failed to show snackbar: ${e.message}, Code: ${e.code}"); | ||
debugPrint(stackTrace.toString()); | ||
} on MissingPluginException { | ||
debugPrint("MethodChannel 'showSnackbar' not implemented on this platform."); | ||
} catch (e, stackTrace) { | ||
debugPrint("Unexpected error while showing snackbar: $e"); | ||
debugPrint(stackTrace.toString()); | ||
} | ||
} | ||
|
||
@override | ||
void initState() { | ||
super.initState(); | ||
// TODO: | ||
// increaseOpenCount(); | ||
// TODO: increaseOpenCount(); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
floatingActionButton: FloatingActionButton( | ||
onPressed: _showNativeSnackbar, | ||
child: const Icon(Icons.message), | ||
), | ||
appBar: AppBar( | ||
actions: const [ | ||
InternalNotificationsBadge(), | ||
|
Uh oh!
There was an error while loading. Please reload this page.