Commit 0b6df634 authored by Adam Barth's avatar Adam Barth Committed by GitHub

Polish HostMessages (#4515)

Cleans up a few style nits in hello_services and adds support for automatic
JSON encoding and decoding to the HostMessages interface.
parent 2315432d
...@@ -6,7 +6,6 @@ ...@@ -6,7 +6,6 @@
@interface AppDelegate : UIResponder <UIApplicationDelegate> @interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window; @property(strong, nonatomic) UIWindow *window;
@end @end
...@@ -14,13 +14,13 @@ ...@@ -14,13 +14,13 @@
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
FlutterDartProject* project = [[FlutterDartProject alloc] initFromDefaultSourceForConfiguration]; FlutterDartProject* project = [[FlutterDartProject alloc] initFromDefaultSourceForConfiguration];
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
FlutterViewController* controller = [[FlutterViewController alloc] initWithProject:project FlutterViewController* flutterController = [[FlutterViewController alloc] initWithProject:project
nibName:nil nibName:nil
bundle:nil]; bundle:nil];
_locationProvider = [[LocationProvider alloc] init]; _locationProvider = [[LocationProvider alloc] init];
[controller addMessageListener: _locationProvider]; [flutterController addMessageListener:_locationProvider];
self.window.rootViewController = controller; self.window.rootViewController = flutterController;
[self.window makeKeyAndVisible]; [self.window makeKeyAndVisible];
return YES; return YES;
} }
......
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
// found in the LICENSE file. // found in the LICENSE file.
import 'dart:async'; import 'dart:async';
import 'dart:convert';
import 'dart:math'; import 'dart:math';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
...@@ -11,15 +10,13 @@ import 'package:flutter/services.dart'; ...@@ -11,15 +10,13 @@ import 'package:flutter/services.dart';
final Random random = new Random(); final Random random = new Random();
Future<String> handleGetRandom(String json) async { Future<dynamic> handleGetRandom(Map<String, dynamic> message) async {
Map<String, dynamic> message = JSON.decode(json); final double min = message['min'].toDouble();
double min = message['min'].toDouble(); final double max = message['max'].toDouble();
double max = message['max'].toDouble();
double value = (random.nextDouble() * (max - min)) + min; return <String, double>{
'value': (random.nextDouble() * (max - min)) + min
Map<String, double> reply = <String, double>{'value': value}; };
return JSON.encode(reply);
} }
class HelloServices extends StatefulWidget { class HelloServices extends StatefulWidget {
...@@ -50,14 +47,14 @@ class _HelloServicesState extends State<HelloServices> { ...@@ -50,14 +47,14 @@ class _HelloServicesState extends State<HelloServices> {
); );
} }
void _getLocation() { Future<Null> _getLocation() async {
Map<String, String> message = <String, String>{'provider': 'network'}; final Map<String, String> message = <String, String>{'provider': 'network'};
HostMessages.sendToHost('getLocation', JSON.encode(message)) final Map<String, dynamic> reply = await HostMessages.sendJSON('getLocation', message);
.then(_onReceivedLocation); // 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.
void _onReceivedLocation(String json) { if (!mounted)
Map<String, num> reply = JSON.decode(json); return;
setState(() { setState(() {
_latitude = reply['latitude'].toDouble(); _latitude = reply['latitude'].toDouble();
_longitude = reply['longitude'].toDouble(); _longitude = reply['longitude'].toDouble();
...@@ -68,5 +65,5 @@ class _HelloServicesState extends State<HelloServices> { ...@@ -68,5 +65,5 @@ class _HelloServicesState extends State<HelloServices> {
void main() { void main() {
runApp(new HelloServices()); runApp(new HelloServices());
HostMessages.addMessageHandler('getRandom', handleGetRandom); HostMessages.addJSONMessageHandler('getRandom', handleGetRandom);
} }
...@@ -15,11 +15,11 @@ ...@@ -15,11 +15,11 @@
library services; library services;
export 'src/services/activity.dart'; export 'src/services/activity.dart';
export 'src/services/app_messages.dart';
export 'src/services/asset_bundle.dart'; export 'src/services/asset_bundle.dart';
export 'src/services/binding.dart'; export 'src/services/binding.dart';
export 'src/services/clipboard.dart'; export 'src/services/clipboard.dart';
export 'src/services/haptic_feedback.dart'; export 'src/services/haptic_feedback.dart';
export 'src/services/host_messages.dart';
export 'src/services/image_cache.dart'; export 'src/services/image_cache.dart';
export 'src/services/image_decoder.dart'; export 'src/services/image_decoder.dart';
export 'src/services/image_resource.dart'; export 'src/services/image_resource.dart';
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
// found in the LICENSE file. // found in the LICENSE file.
import 'dart:async'; import 'dart:async';
import 'dart:convert';
import 'package:mojo/core.dart' as core; import 'package:mojo/core.dart' as core;
import 'package:sky_services/flutter/platform/app_messages.mojom.dart' as mojom; import 'package:sky_services/flutter/platform/app_messages.mojom.dart' as mojom;
...@@ -48,13 +49,28 @@ final _ApplicationMessagesImpl _appMessages = new _ApplicationMessagesImpl(); ...@@ -48,13 +49,28 @@ final _ApplicationMessagesImpl _appMessages = new _ApplicationMessagesImpl();
/// Flutter framework to exchange application-specific messages. /// Flutter framework to exchange application-specific messages.
class HostMessages { class HostMessages {
/// Send a message to the host application. /// Send a message to the host application.
static Future<String> sendToHost(String messageName, String message) async { static Future<String> sendToHost(String messageName, [String message = '']) async {
return (await _hostAppMessagesProxy.sendString(messageName, message)).reply; return (await _hostAppMessagesProxy.sendString(messageName, message)).reply;
} }
/// Register a callback for messages received from the host application. /// Sends a JSON-encoded message to the host application and JSON-decodes the response.
/// The callback function must return a String, Future<String>, or null. static Future<dynamic> 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) { static void addMessageHandler(String messageName, HostMessageCallback callback) {
_appMessages.handlers[messageName] = 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<dynamic> callback(dynamic json)) {
_appMessages.handlers[messageName] = (String message) async {
return JSON.encode(await callback(JSON.decode(message)));
};
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment