From 57a2596cad704fb9f65943e2496a169549627bcc Mon Sep 17 00:00:00 2001 From: Md Shoyab Date: Fri, 21 Mar 2025 11:17:27 +0530 Subject: [PATCH] Method Channel Improvements Done --- .../webreinvent/vaahflutter/MainActivity.kt | 26 ++++++++++- assets/env/develop.json | 2 +- assets/env/production.json | 2 +- assets/env/staging.json | 2 +- .../services/base_service.dart | 4 ++ .../services/platform_channel_service.dart | 36 +++++++++++++++ lib/views/pages/home.dart | 30 ++++++++++++- pubspec.lock | 44 +++++++++---------- 8 files changed, 118 insertions(+), 28 deletions(-) create mode 100644 lib/vaahextendflutter/services/platform_service/services/base_service.dart create mode 100644 lib/vaahextendflutter/services/platform_service/services/platform_channel_service.dart diff --git a/android/app/src/main/kotlin/com/webreinvent/vaahflutter/MainActivity.kt b/android/app/src/main/kotlin/com/webreinvent/vaahflutter/MainActivity.kt index 7dc467a5..c7230ff8 100644 --- a/android/app/src/main/kotlin/com/webreinvent/vaahflutter/MainActivity.kt +++ b/android/app/src/main/kotlin/com/webreinvent/vaahflutter/MainActivity.kt @@ -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() + } + } } diff --git a/assets/env/develop.json b/assets/env/develop.json index 9fb49289..e5f0b7d5 100644 --- a/assets/env/develop.json +++ b/assets/env/develop.json @@ -18,4 +18,4 @@ "pusher_config": null, "show_debug_panel": true, "debug_panel_color": 3422552064 -} +} \ No newline at end of file diff --git a/assets/env/production.json b/assets/env/production.json index bd073639..c47866f0 100644 --- a/assets/env/production.json +++ b/assets/env/production.json @@ -18,4 +18,4 @@ "pusher_config": null, "show_debug_panel": false, "debug_panel_color": 3422552064 -} +} \ No newline at end of file diff --git a/assets/env/staging.json b/assets/env/staging.json index a5303820..9d1d529b 100644 --- a/assets/env/staging.json +++ b/assets/env/staging.json @@ -18,4 +18,4 @@ "pusher_config": null, "show_debug_panel": true, "debug_panel_color": 3422552064 -} +} \ No newline at end of file diff --git a/lib/vaahextendflutter/services/platform_service/services/base_service.dart b/lib/vaahextendflutter/services/platform_service/services/base_service.dart new file mode 100644 index 00000000..dad77683 --- /dev/null +++ b/lib/vaahextendflutter/services/platform_service/services/base_service.dart @@ -0,0 +1,4 @@ +abstract class BasePlatformService { + Future invokeMethod(String method, [dynamic arguments]); + Stream getEventStream(String eventChannelName, [dynamic arguments]); +} diff --git a/lib/vaahextendflutter/services/platform_service/services/platform_channel_service.dart b/lib/vaahextendflutter/services/platform_service/services/platform_channel_service.dart new file mode 100644 index 00000000..5d11f8a3 --- /dev/null +++ b/lib/vaahextendflutter/services/platform_service/services/platform_channel_service.dart @@ -0,0 +1,36 @@ +import 'package:flutter/services.dart'; + +import 'base_service.dart'; + +class PlatformChannelService implements BasePlatformService { + static final Map _eventChannels = {}; + static final Map _methodChannels = {}; + + @override + Future invokeMethod(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(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 getEventStream(String eventChannelName, [dynamic arguments]) { + return _eventChannels + .putIfAbsent(eventChannelName, () => EventChannel(eventChannelName)) + .receiveBroadcastStream(arguments); + } +} diff --git a/lib/views/pages/home.dart b/lib/views/pages/home.dart index b45165fb..fdd0dd12 100644 --- a/lib/views/pages/home.dart +++ b/lib/views/pages/home.dart @@ -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 { + static const MethodChannel _platform = MethodChannel('snackbar_channel'); + + /// Calls the native platform method to show a snackbar. + Future _showNativeSnackbar() async { + try { + final String? result = await _platform.invokeMethod('showSnackbar'); + 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(), diff --git a/pubspec.lock b/pubspec.lock index 3d36131e..7e1c5f47 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -5,10 +5,10 @@ packages: dependency: transitive description: name: _fe_analyzer_shared - sha256: f256b0c0ba6c7577c15e2e4e114755640a875e885099367bf6e012b19314c834 + sha256: "16e298750b6d0af7ce8a3ba7c18c69c3785d11b15ec83f6dcd0ad2a0009b3cab" url: "https://pub.dev" source: hosted - version: "72.0.0" + version: "76.0.0" _flutterfire_internals: dependency: transitive description: @@ -21,15 +21,15 @@ packages: dependency: transitive description: dart source: sdk - version: "0.3.2" + version: "0.3.3" analyzer: dependency: transitive description: name: analyzer - sha256: b652861553cd3990d8ed361f7979dc6d7053a9ac8843fa73820ab68ce5410139 + sha256: "1f14db053a8c23e260789e9b0980fa27f2680dd640932cae5e1137cce0e46e1e" url: "https://pub.dev" source: hosted - version: "6.7.0" + version: "6.11.0" args: dependency: transitive description: @@ -178,10 +178,10 @@ packages: dependency: transitive description: name: collection - sha256: ee67cb0715911d28db6bf4af1026078bd6f0128b07a5f66fb2ed94ec6783c09a + sha256: a1ace0a119f20aabc852d165077c036cd864315bd99b7eaa10a60100341941bf url: "https://pub.dev" source: hosted - version: "1.18.0" + version: "1.19.0" colorize: dependency: "direct main" description: @@ -529,18 +529,18 @@ packages: dependency: transitive description: name: leak_tracker - sha256: "3f87a60e8c63aecc975dda1ceedbc8f24de75f09e4856ea27daf8958f2f0ce05" + sha256: "7bb2830ebd849694d1ec25bf1f44582d6ac531a57a365a803a6034ff751d2d06" url: "https://pub.dev" source: hosted - version: "10.0.5" + version: "10.0.7" leak_tracker_flutter_testing: dependency: transitive description: name: leak_tracker_flutter_testing - sha256: "932549fb305594d82d7183ecd9fa93463e9914e1b67cacc34bc40906594a1806" + sha256: "9491a714cca3667b60b5c420da8217e6de0d1ba7a5ec322fab01758f6998f379" url: "https://pub.dev" source: hosted - version: "3.0.5" + version: "3.0.8" leak_tracker_testing: dependency: transitive description: @@ -569,10 +569,10 @@ packages: dependency: transitive description: name: macros - sha256: "0acaed5d6b7eab89f63350bccd82119e6c602df0f391260d0e32b5e23db79536" + sha256: "1d9e801cd66f7ea3663c45fc708450db1fa57f988142c64289142c9b7ee80656" url: "https://pub.dev" source: hosted - version: "0.1.2-main.4" + version: "0.1.3-main.0" matcher: dependency: transitive description: @@ -786,7 +786,7 @@ packages: dependency: transitive description: flutter source: sdk - version: "0.0.99" + version: "0.0.0" source_gen: dependency: transitive description: @@ -823,10 +823,10 @@ packages: dependency: transitive description: name: stack_trace - sha256: "73713990125a6d93122541237550ee3352a2d84baad52d375a4cad2eb9b7ce0b" + sha256: "9f47fd3630d76be3ab26f0ee06d213679aa425996925ff3feffdec504931c377" url: "https://pub.dev" source: hosted - version: "1.11.1" + version: "1.12.0" stream_channel: dependency: transitive description: @@ -847,10 +847,10 @@ packages: dependency: transitive description: name: string_scanner - sha256: "556692adab6cfa87322a115640c11f13cb77b3f076ddcc5d6ae3c20242bedcde" + sha256: "688af5ed3402a4bde5b3a6c15fd768dbf2621a614950b17f04626c431ab3c4c3" url: "https://pub.dev" source: hosted - version: "1.2.0" + version: "1.3.0" term_glyph: dependency: transitive description: @@ -863,10 +863,10 @@ packages: dependency: transitive description: name: test_api - sha256: "5b8a98dafc4d5c4c9c72d8b31ab2b23fc13422348d2997120294d3bac86b4ddb" + sha256: "664d3a9a64782fcdeb83ce9c6b39e78fd2971d4e37827b9b06c3aa1edc5e760c" url: "https://pub.dev" source: hosted - version: "0.7.2" + version: "0.7.3" timezone: dependency: "direct main" description: @@ -911,10 +911,10 @@ packages: dependency: transitive description: name: vm_service - sha256: "5c5f338a667b4c644744b661f309fb8080bb94b18a7e91ef1dbd343bed00ed6d" + sha256: f6be3ed8bd01289b34d679c2b62226f63c0e69f9fd2e50a6b3c1c729a961041b url: "https://pub.dev" source: hosted - version: "14.2.5" + version: "14.3.0" watcher: dependency: transitive description: