Teach hello_services to provide a platform service (#4277)
Currently this service just returns mock data, but it exercises the plumbing.
This commit is contained in:
parent
54716cdaca
commit
8c805e234d
@ -13,6 +13,8 @@
|
||||
9740EEB41CF90195004384FC /* Flutter.xcconfig in Resources */ = {isa = PBXBuildFile; fileRef = 9740EEB21CF90195004384FC /* Flutter.xcconfig */; };
|
||||
9740EEB51CF90195004384FC /* Generated.xcconfig in Resources */ = {isa = PBXBuildFile; fileRef = 9740EEB31CF90195004384FC /* Generated.xcconfig */; };
|
||||
9740EEBB1CF902C7004384FC /* app.flx in Resources */ = {isa = PBXBuildFile; fileRef = 9740EEB71CF902C7004384FC /* app.flx */; };
|
||||
977505191CFDF23500BC28DA /* LocationProvider.m in Sources */ = {isa = PBXBuildFile; fileRef = 977505181CFDF23500BC28DA /* LocationProvider.m */; };
|
||||
97A38A351CFDEC880099F1B4 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 97A38A341CFDEC880099F1B4 /* AppDelegate.m */; };
|
||||
97C146F31CF9000F007C117D /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 97C146F21CF9000F007C117D /* main.m */; };
|
||||
97C146FC1CF9000F007C117D /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FA1CF9000F007C117D /* Main.storyboard */; };
|
||||
97C146FE1CF9000F007C117D /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FD1CF9000F007C117D /* Assets.xcassets */; };
|
||||
@ -40,6 +42,10 @@
|
||||
9740EEB71CF902C7004384FC /* app.flx */ = {isa = PBXFileReference; lastKnownFileType = file; name = app.flx; path = Flutter/app.flx; sourceTree = "<group>"; };
|
||||
9740EEB81CF902C7004384FC /* app.so */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = app.so; path = Flutter/app.so; sourceTree = "<group>"; };
|
||||
9740EEBA1CF902C7004384FC /* Flutter.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Flutter.framework; path = Flutter/Flutter.framework; sourceTree = "<group>"; };
|
||||
977505171CFDF21E00BC28DA /* LocationProvider.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = LocationProvider.h; sourceTree = "<group>"; };
|
||||
977505181CFDF23500BC28DA /* LocationProvider.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LocationProvider.m; sourceTree = "<group>"; };
|
||||
97A38A331CFDEC680099F1B4 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
|
||||
97A38A341CFDEC880099F1B4 /* AppDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
|
||||
97C146EE1CF9000F007C117D /* HelloServices.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = HelloServices.app; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
97C146F21CF9000F007C117D /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
|
||||
97C146FB1CF9000F007C117D /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
|
||||
@ -97,6 +103,10 @@
|
||||
97C146FF1CF9000F007C117D /* LaunchScreen.storyboard */,
|
||||
97C147021CF9000F007C117D /* Info.plist */,
|
||||
97C146F11CF9000F007C117D /* Supporting Files */,
|
||||
97A38A331CFDEC680099F1B4 /* AppDelegate.h */,
|
||||
97A38A341CFDEC880099F1B4 /* AppDelegate.m */,
|
||||
977505171CFDF21E00BC28DA /* LocationProvider.h */,
|
||||
977505181CFDF23500BC28DA /* LocationProvider.m */,
|
||||
);
|
||||
path = HelloServices;
|
||||
sourceTree = "<group>";
|
||||
@ -200,7 +210,9 @@
|
||||
isa = PBXSourcesBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
97A38A351CFDEC880099F1B4 /* AppDelegate.m in Sources */,
|
||||
97C146F31CF9000F007C117D /* main.m in Sources */,
|
||||
977505191CFDF23500BC28DA /* LocationProvider.m in Sources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
|
12
examples/hello_services/ios/HelloServices/AppDelegate.h
Normal file
12
examples/hello_services/ios/HelloServices/AppDelegate.h
Normal file
@ -0,0 +1,12 @@
|
||||
// Copyright 2016 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
@interface AppDelegate : UIResponder <UIApplicationDelegate>
|
||||
|
||||
@property (strong, nonatomic) UIWindow *window;
|
||||
|
||||
@end
|
||||
|
28
examples/hello_services/ios/HelloServices/AppDelegate.m
Normal file
28
examples/hello_services/ios/HelloServices/AppDelegate.m
Normal file
@ -0,0 +1,28 @@
|
||||
// Copyright 2016 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#import "AppDelegate.h"
|
||||
|
||||
#import <Flutter/Flutter.h>
|
||||
#import "LocationProvider.h"
|
||||
|
||||
@implementation AppDelegate {
|
||||
LocationProvider* _locationProvider;
|
||||
}
|
||||
|
||||
- (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];
|
||||
_locationProvider = [[LocationProvider alloc] init];
|
||||
[controller addMessageListener: _locationProvider];
|
||||
|
||||
self.window.rootViewController = controller;
|
||||
[self.window makeKeyAndVisible];
|
||||
return YES;
|
||||
}
|
||||
|
||||
@end
|
@ -1,7 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="8150" systemVersion="15A204g" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" initialViewController="01J-lp-oVM">
|
||||
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="10117" systemVersion="15F34" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" initialViewController="01J-lp-oVM">
|
||||
<dependencies>
|
||||
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="8122"/>
|
||||
<deployment identifier="iOS"/>
|
||||
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="10085"/>
|
||||
</dependencies>
|
||||
<scenes>
|
||||
<!--View Controller-->
|
||||
@ -15,7 +16,6 @@
|
||||
<view key="view" contentMode="scaleToFill" id="Ze5-6b-2t3">
|
||||
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
|
||||
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
|
||||
<animations/>
|
||||
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
|
||||
</view>
|
||||
</viewController>
|
||||
|
@ -0,0 +1,9 @@
|
||||
// Copyright 2016 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#import <Flutter/Flutter.h>
|
||||
|
||||
@interface LocationProvider : NSObject <FlutterMessageListener>
|
||||
|
||||
@end
|
28
examples/hello_services/ios/HelloServices/LocationProvider.m
Normal file
28
examples/hello_services/ios/HelloServices/LocationProvider.m
Normal file
@ -0,0 +1,28 @@
|
||||
// Copyright 2016 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#import "LocationProvider.h"
|
||||
|
||||
@implementation LocationProvider
|
||||
|
||||
@synthesize messageName = _messageName;
|
||||
|
||||
- (instancetype) init {
|
||||
self = [super init];
|
||||
if (self)
|
||||
self->_messageName = @"getLocation";
|
||||
return self;
|
||||
}
|
||||
|
||||
- (NSString*)didReceiveString:(NSString*)message {
|
||||
NSDictionary* response = @{
|
||||
@"latitude": @3.29334,
|
||||
@"longitude": @8.2492492
|
||||
};
|
||||
|
||||
NSData* data = [NSJSONSerialization dataWithJSONObject:response options:0 error:nil];
|
||||
return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
|
||||
}
|
||||
|
||||
@end
|
@ -4,11 +4,12 @@
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
#import <Flutter/Flutter.h>
|
||||
#import "AppDelegate.h"
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
FlutterInit(argc, (const char**)argv);
|
||||
@autoreleasepool {
|
||||
return UIApplicationMain(argc, argv, nil,
|
||||
NSStringFromClass([FlutterAppDelegate class]));
|
||||
NSStringFromClass([AppDelegate class]));
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,72 @@
|
||||
// Copyright 2015 The Chromium Authors. All rights reserved.
|
||||
// Copyright 2016 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:math';
|
||||
|
||||
void main() => runApp(new Center(child: new Text('Hello, world!')));
|
||||
import 'package:flutter/material.dart';
|
||||
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();
|
||||
|
||||
double value = (random.nextDouble() * (max - min)) + min;
|
||||
|
||||
Map<String, double> reply = <String, double>{'value': value};
|
||||
return JSON.encode(reply);
|
||||
}
|
||||
|
||||
class HelloServices extends StatefulWidget {
|
||||
@override
|
||||
_HelloServicesState createState() => new _HelloServicesState();
|
||||
}
|
||||
|
||||
class _HelloServicesState extends State<HelloServices> {
|
||||
double _latitude;
|
||||
double _longitude;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return new Material(
|
||||
child: new Center(
|
||||
child: new Column(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
||||
children: <Widget>[
|
||||
new Text('Hello from Flutter!'),
|
||||
new RaisedButton(
|
||||
child: new Text('Get Location'),
|
||||
onPressed: _getLocation
|
||||
),
|
||||
new Text('Latitude: $_latitude, Longitude: $_longitude'),
|
||||
]
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
void _getLocation() {
|
||||
Map<String, String> message = <String, String>{'provider': 'network'};
|
||||
HostMessages.sendToHost('getLocation', JSON.encode(message))
|
||||
.then(_onReceivedLocation);
|
||||
}
|
||||
|
||||
void _onReceivedLocation(String json) {
|
||||
Map<String, double> reply = JSON.decode(json);
|
||||
setState(() {
|
||||
_latitude = reply['latitude'];
|
||||
_longitude = reply['longitude'];
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
runApp(new HelloServices());
|
||||
|
||||
HostMessages.addMessageHandler('getRandom', handleGetRandom);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user