From 64fce3850190e7cd8a4339b97d43b463ee00aee1 Mon Sep 17 00:00:00 2001 From: Ayush Bherwani Date: Tue, 15 Dec 2020 11:45:35 +0530 Subject: [PATCH] [platform_channels] adds implementation of BasicMessageChannel for iOS (#610) --- platform_channels/README.md | 2 - .../ios/Runner/AppDelegate.swift | 38 +++++++++++++++++++ platform_channels/lib/main.dart | 5 +++ .../lib/src/pet_list_screen.dart | 17 +++++++-- 4 files changed, 56 insertions(+), 6 deletions(-) diff --git a/platform_channels/README.md b/platform_channels/README.md index 64f5b4931..62defce45 100644 --- a/platform_channels/README.md +++ b/platform_channels/README.md @@ -2,8 +2,6 @@ A sample app which demonstrates how to use `MethodChannel`, `EventChannel`, `BasicMessageChannel` and `MessageCodec` in Flutter. -This sample is currently being built. Not all platforms and functionality are in place. - ## Goals * Demonstrate how to use `MethodChannel` to invoke platform methods. diff --git a/platform_channels/ios/Runner/AppDelegate.swift b/platform_channels/ios/Runner/AppDelegate.swift index 71b430d64..131fd18b7 100644 --- a/platform_channels/ios/Runner/AppDelegate.swift +++ b/platform_channels/ios/Runner/AppDelegate.swift @@ -45,7 +45,45 @@ import Flutter FlutterEventChannel(name: "eventChannelDemo", binaryMessenger: flutterViewController.binaryMessenger).setStreamHandler(AccelerometerStreamHandler()) + var petList : [[String:String]] = [] + + // A FlutterBasicMessageChannel for sending petList to Dart. + let stringCodecChannel = FlutterBasicMessageChannel(name: "stringCodecDemo", binaryMessenger: flutterViewController.binaryMessenger, codec: FlutterStringCodec.sharedInstance()) + + // Registers a MessageHandler for FlutterBasicMessageChannel to receive pet details. + FlutterBasicMessageChannel(name: "jsonMessageCodecDemo", binaryMessenger: flutterViewController.binaryMessenger, codec: FlutterJSONMessageCodec.sharedInstance()) + .setMessageHandler{(message: Any?, reply: FlutterReply) -> Void in + petList.insert(message! as! [String: String], at: 0) + stringCodecChannel.sendMessage(self.convertPetListToJson(petList: petList)) + } + + // Registers a MessageHandler for FlutterBasicMessageHandler to receive indices of detail records to remove from the petList. + FlutterBasicMessageChannel(name: "binaryCodecDemo", binaryMessenger: flutterViewController.binaryMessenger, codec: FlutterBinaryCodec.sharedInstance()).setMessageHandler{ + (message: Any?, reply: FlutterReply) -> Void in + + guard let index = Int.init(String.init(data: message! as! Data, encoding: String.Encoding.utf8)!) else { + reply(nil) + return + } + + if (index >= 0 && index < petList.count) { + petList.remove(at: index) + reply("Removed Successfully".data(using: .utf8)!) + stringCodecChannel.sendMessage(self.convertPetListToJson(petList: petList)) + } else { + reply(nil) + } + } + GeneratedPluginRegistrant.register(with: self) return super.application(application, didFinishLaunchingWithOptions: launchOptions) } + + // Function to convert petList to json string. + func convertPetListToJson(petList: [[String: String]]) -> String? { + guard let data = try? JSONSerialization.data(withJSONObject: ["petList": petList], options: .prettyPrinted) else { + return nil + } + return String(data: data, encoding: String.Encoding.utf8) + } } diff --git a/platform_channels/lib/main.dart b/platform_channels/lib/main.dart index b6bf1624d..153e4c5a0 100644 --- a/platform_channels/lib/main.dart +++ b/platform_channels/lib/main.dart @@ -25,6 +25,11 @@ class PlatformChannelSample extends StatelessWidget { '/addPetDetails': (context) => AddPetDetails(), }, title: 'Platform Channel Sample', + theme: ThemeData( + snackBarTheme: SnackBarThemeData( + backgroundColor: Colors.blue[500], + ), + ), home: HomePage(), ); } diff --git a/platform_channels/lib/src/pet_list_screen.dart b/platform_channels/lib/src/pet_list_screen.dart index f86fc2cac..b2f4b3cc0 100644 --- a/platform_channels/lib/src/pet_list_screen.dart +++ b/platform_channels/lib/src/pet_list_screen.dart @@ -15,6 +15,7 @@ class PetListScreen extends StatefulWidget { class _PetListScreenState extends State { PetListModel petListModel; + final scaffoldKey = GlobalKey(); @override void initState() { @@ -23,9 +24,17 @@ class _PetListScreenState extends State { // to PetModel. BasicMessageChannel('stringCodecDemo', StringCodec()) .setMessageHandler((message) async { - setState(() { - petListModel = PetListModel.fromJson(message); - }); + if (message == null) { + scaffoldKey.currentState.showSnackBar( + SnackBar( + content: + const Text('An error occurred while adding pet details.')), + ); + } else { + setState(() { + petListModel = PetListModel.fromJson(message); + }); + } return; }); } @@ -33,6 +42,7 @@ class _PetListScreenState extends State { @override Widget build(BuildContext context) { return Scaffold( + key: scaffoldKey, appBar: AppBar( title: Text('Pet List'), ), @@ -84,7 +94,6 @@ class BuildPetList extends StatelessWidget { void showSnackBar(String message, BuildContext context) { Scaffold.of(context).showSnackBar(SnackBar( - backgroundColor: Theme.of(context).primaryColor, content: Text(message), )); }