diff --git a/examples/hello_services/ios/HelloServices/AppDelegate.h b/examples/hello_services/ios/HelloServices/AppDelegate.h index 5dae783b0a..42062723fb 100644 --- a/examples/hello_services/ios/HelloServices/AppDelegate.h +++ b/examples/hello_services/ios/HelloServices/AppDelegate.h @@ -6,7 +6,6 @@ @interface AppDelegate : UIResponder -@property (strong, nonatomic) UIWindow *window; +@property(strong, nonatomic) UIWindow *window; @end - diff --git a/examples/hello_services/ios/HelloServices/AppDelegate.m b/examples/hello_services/ios/HelloServices/AppDelegate.m index d42c82e29a..bf8e1915c5 100644 --- a/examples/hello_services/ios/HelloServices/AppDelegate.m +++ b/examples/hello_services/ios/HelloServices/AppDelegate.m @@ -14,13 +14,13 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { FlutterDartProject* project = [[FlutterDartProject alloc] initFromDefaultSourceForConfiguration]; self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; - FlutterViewController* controller = [[FlutterViewController alloc] initWithProject:project - nibName:nil - bundle:nil]; + FlutterViewController* flutterController = [[FlutterViewController alloc] initWithProject:project + nibName:nil + bundle:nil]; _locationProvider = [[LocationProvider alloc] init]; - [controller addMessageListener: _locationProvider]; + [flutterController addMessageListener:_locationProvider]; - self.window.rootViewController = controller; + self.window.rootViewController = flutterController; [self.window makeKeyAndVisible]; return YES; } diff --git a/examples/hello_services/lib/main.dart b/examples/hello_services/lib/main.dart index f40cb99de5..ea20c6bef0 100644 --- a/examples/hello_services/lib/main.dart +++ b/examples/hello_services/lib/main.dart @@ -3,7 +3,6 @@ // found in the LICENSE file. import 'dart:async'; -import 'dart:convert'; import 'dart:math'; import 'package:flutter/material.dart'; @@ -11,15 +10,13 @@ import 'package:flutter/services.dart'; final Random random = new Random(); -Future handleGetRandom(String json) async { - Map message = JSON.decode(json); - double min = message['min'].toDouble(); - double max = message['max'].toDouble(); +Future handleGetRandom(Map message) async { + final double min = message['min'].toDouble(); + final double max = message['max'].toDouble(); - double value = (random.nextDouble() * (max - min)) + min; - - Map reply = {'value': value}; - return JSON.encode(reply); + return { + 'value': (random.nextDouble() * (max - min)) + min + }; } class HelloServices extends StatefulWidget { @@ -50,14 +47,14 @@ class _HelloServicesState extends State { ); } - void _getLocation() { - Map message = {'provider': 'network'}; - HostMessages.sendToHost('getLocation', JSON.encode(message)) - .then(_onReceivedLocation); - } - - void _onReceivedLocation(String json) { - Map reply = JSON.decode(json); + Future _getLocation() async { + final Map message = {'provider': 'network'}; + final Map reply = await HostMessages.sendJSON('getLocation', message); + // If the widget was removed from the tree while the message was in flight, + // we want to discard the reply rather than calling setState to update our + // non-existant appearance. + if (!mounted) + return; setState(() { _latitude = reply['latitude'].toDouble(); _longitude = reply['longitude'].toDouble(); @@ -68,5 +65,5 @@ class _HelloServicesState extends State { void main() { runApp(new HelloServices()); - HostMessages.addMessageHandler('getRandom', handleGetRandom); + HostMessages.addJSONMessageHandler('getRandom', handleGetRandom); } diff --git a/packages/flutter/lib/services.dart b/packages/flutter/lib/services.dart index b5faf4163c..de857c074d 100644 --- a/packages/flutter/lib/services.dart +++ b/packages/flutter/lib/services.dart @@ -15,11 +15,11 @@ library services; export 'src/services/activity.dart'; -export 'src/services/app_messages.dart'; export 'src/services/asset_bundle.dart'; export 'src/services/binding.dart'; export 'src/services/clipboard.dart'; export 'src/services/haptic_feedback.dart'; +export 'src/services/host_messages.dart'; export 'src/services/image_cache.dart'; export 'src/services/image_decoder.dart'; export 'src/services/image_resource.dart'; diff --git a/packages/flutter/lib/src/services/app_messages.dart b/packages/flutter/lib/src/services/host_messages.dart similarity index 66% rename from packages/flutter/lib/src/services/app_messages.dart rename to packages/flutter/lib/src/services/host_messages.dart index e437e80168..ecba71c5e5 100644 --- a/packages/flutter/lib/src/services/app_messages.dart +++ b/packages/flutter/lib/src/services/host_messages.dart @@ -3,6 +3,7 @@ // found in the LICENSE file. import 'dart:async'; +import 'dart:convert'; import 'package:mojo/core.dart' as core; import 'package:sky_services/flutter/platform/app_messages.mojom.dart' as mojom; @@ -48,13 +49,28 @@ final _ApplicationMessagesImpl _appMessages = new _ApplicationMessagesImpl(); /// Flutter framework to exchange application-specific messages. class HostMessages { /// Send a message to the host application. - static Future sendToHost(String messageName, String message) async { + static Future sendToHost(String messageName, [String message = '']) async { return (await _hostAppMessagesProxy.sendString(messageName, message)).reply; } - /// Register a callback for messages received from the host application. - /// The callback function must return a String, Future, or null. + /// Sends a JSON-encoded message to the host application and JSON-decodes the response. + static Future sendJSON(String messageName, [dynamic json]) async { + return JSON.decode((await _hostAppMessagesProxy.sendString(messageName, JSON.encode(json))).reply); + } + + /// Register a callback for receiving messages from the host application. static void addMessageHandler(String messageName, HostMessageCallback callback) { _appMessages.handlers[messageName] = callback; } + + /// Register a callback for receiving JSON messages from the host application. + /// + /// Messages received from the host application are decoded as JSON before + /// being passed to `callback`. The result of the callback is encoded as JSON + /// before being returned to the host application. + static void addJSONMessageHandler(String messageName, Future callback(dynamic json)) { + _appMessages.handlers[messageName] = (String message) async { + return JSON.encode(await callback(JSON.decode(message))); + }; + } }