feat(native): starting native plugins

This commit is contained in:
Max Lynch
2015-09-08 10:50:23 -05:00
parent e875fe500f
commit f35646e63e
6 changed files with 146 additions and 0 deletions

View File

@ -0,0 +1,48 @@
import {Component} from 'angular2/angular2';
import {Control, ControlGroup} from 'angular2/forms';
import {App, Http, Camera, Geolocation} from 'ionic/ionic';
let testUrl = 'https://ionic-api-tester.herokuapp.com/json';
let testUrl404 = 'https://ionic-api-tester.herokuapp.com/404';
@App({
templateUrl: 'main.html'
})
class IonicApp {
constructor() {
}
doGetLocation() {
console.log('Getting location');
this.gettingLocation = true;
Geolocation.getCurrentPosition().then((pos) => {
this.gettingLocation = false;
console.log('Got location', pos);
this.location = pos;
}, (err) => {
this.gettingLocation = false;
console.error('Unable to get location', err);
});
}
doTrackLocation() {
this.gettingTrackLocation = true;
Geolocation.watchPosition().source.subscribe((pos) => {
this.gettingTrackLocation = false;
console.log('Got location', pos);
this.trackLocation = pos;
}, (err) => {
this.gettingTrackLocation = false;
console.error('Unable to get location', pos);
});
}
getPicture() {
Camera.getPicture({
}).then(data => {
console.log('Data', data);
}, err => {
alert('Unable to take picture')
})
}
}

View File

@ -0,0 +1,17 @@
<ion-view>
<ion-content padding>
<h2>Camera</h2>
<button primary (click)="getPicture()">Get Picture</button>
<h2>Geolocation</h2>
<button primary (click)="doGetLocation()">Get Location</button>
<div>
<b *ng-if="gettingLocation">Fetching location...</b>
<b *ng-if="location">{{location.coords.latitude}}, {{location.coords.longitude}}</b>
</div>
<button primary (click)="doTrackLocation()">Track Location</button>
<div>
<b *ng-if="gettingTrackLocation">Fetching location...</b>
<b *ng-if="trackLocation">{{trackLocation.coords.latitude}}, {{trackLocation.coords.longitude}}</b>
</div>
</ion-content>
</ion-view>

View File

@ -23,3 +23,5 @@ export * from './animations/builtins'
export * from './transitions/transition'
export * from './transitions/ios-transition'
export * from './transitions/md-transition'
export * from './native/plugins'

View File

@ -0,0 +1,38 @@
import * as util from 'ionic/util';
export class Camera {
static getPicture(options) {
return new Promise((resolve, reject) => {
if (!navigator.camera) {
console.warn('Camera: no camera plugin installed. Pictures will not work.')
resolve(null);
return;
}
var options = util.defaults({
quality: 80,
destinationType: window.Camera.DestinationType.DATA_URL,
sourceType: window.Camera.PictureSourceType.CAMERA,
allowEdit: true,
encodingType: window.Camera.EncodingType.JPEG,
popoverOptions: window.CameraPopoverOptions,
saveToPhotoAlbum: false
}, options);
navigator.camera.getPicture(function (imageData) {
resolve(imageData);
}, function (err) {
reject(err);
}, options);
});
}
static cleanup() {
return new Promise((resolve, reject) => {
navigator.camera.cleanup(function () {
resolve();
}, function (err) {
reject(err);
});
});
}
}

View File

@ -0,0 +1,39 @@
import * as util from 'ionic/util';
import * as Rx from 'rx';
export class Geolocation {
static getCurrentPosition(options) {
return new Promise((resolve, reject) => {
navigator.geolocation.getCurrentPosition(function (result) {
resolve(result);
}, function (err) {
reject(err);
}, options);
});
}
static watchPosition(options) {
let watchID;
let source = Rx.Observable.create((observer) => {
watchID = navigator.geolocation.watchPosition(function (result) {
observer.onNext(result)
}, function(err) {
observer.onError(err, observer);
}, options);
})
return {
source: source,
watchID: watchID,
clear: () => {
navigator.geolocation.clearWatch(watchID);
}
}
}
static clearWatch(watchID) {
return navigator.geolocation.clearWatch(watchID);
}
}

2
ionic/native/plugins.ts Normal file
View File

@ -0,0 +1,2 @@
export * from './camera/camera'
export * from './geolocation/geolocation'