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.
This commit is contained in:
Adam Barth 2016-06-10 15:04:01 -07:00 committed by GitHub
parent 2315432d2f
commit 0b6df634af
5 changed files with 41 additions and 29 deletions

View file

@ -6,7 +6,6 @@
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property(strong, nonatomic) UIWindow *window;
@end

View file

@ -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;
}

View file

@ -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<String> handleGetRandom(String json) async {
Map<String, dynamic> message = JSON.decode(json);
double min = message['min'].toDouble();
double max = message['max'].toDouble();
Future<dynamic> handleGetRandom(Map<String, dynamic> message) async {
final double min = message['min'].toDouble();
final double max = message['max'].toDouble();
double value = (random.nextDouble() * (max - min)) + min;
Map<String, double> reply = <String, double>{'value': value};
return JSON.encode(reply);
return <String, double>{
'value': (random.nextDouble() * (max - min)) + min
};
}
class HelloServices extends StatefulWidget {
@ -50,14 +47,14 @@ class _HelloServicesState extends State<HelloServices> {
);
}
void _getLocation() {
Map<String, String> message = <String, String>{'provider': 'network'};
HostMessages.sendToHost('getLocation', JSON.encode(message))
.then(_onReceivedLocation);
}
void _onReceivedLocation(String json) {
Map<String, num> reply = JSON.decode(json);
Future<Null> _getLocation() async {
final Map<String, String> message = <String, String>{'provider': 'network'};
final Map<String, dynamic> 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<HelloServices> {
void main() {
runApp(new HelloServices());
HostMessages.addMessageHandler('getRandom', handleGetRandom);
HostMessages.addJSONMessageHandler('getRandom', handleGetRandom);
}

View file

@ -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';

View file

@ -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<String> sendToHost(String messageName, String message) async {
static Future<String> 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<String>, or null.
/// Sends a JSON-encoded message to the host application and JSON-decodes the response.
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) {
_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)));
};
}
}