Merge pull request #97 from driftyco/instance-wrapper

feat(ionicnative): add instance wrapper
This commit is contained in:
Ibrahim Hadeed 2016-04-29 22:49:07 -04:00
commit 8b6d9f33f6
5 changed files with 389 additions and 19 deletions

View File

@ -34,6 +34,7 @@ import {File} from './plugins/file';
import {Flashlight} from './plugins/flashlight'; import {Flashlight} from './plugins/flashlight';
import {Geolocation} from './plugins/geolocation'; import {Geolocation} from './plugins/geolocation';
import {Globalization} from './plugins/globalization'; import {Globalization} from './plugins/globalization';
import {GoogleMaps} from './plugins/googlemaps';
import {GoogleAnalytics} from './plugins/googleanalytics'; import {GoogleAnalytics} from './plugins/googleanalytics';
import {Hotspot} from './plugins/hotspot'; import {Hotspot} from './plugins/hotspot';
import {ImagePicker} from './plugins/imagepicker'; import {ImagePicker} from './plugins/imagepicker';
@ -41,6 +42,7 @@ import {InAppBrowser} from './plugins/inappbrowser';
import {Keyboard} from './plugins/keyboard'; import {Keyboard} from './plugins/keyboard';
import {LaunchNavigator} from './plugins/launchnavigator'; import {LaunchNavigator} from './plugins/launchnavigator';
import {LocalNotifications} from './plugins/localnotifications'; import {LocalNotifications} from './plugins/localnotifications';
import {MediaPlugin} from './plugins/media';
import {Network, Connection} from './plugins/network'; import {Network, Connection} from './plugins/network';
import {Push} from './plugins/push'; import {Push} from './plugins/push';
import {Screenshot} from './plugins/screenshot'; import {Screenshot} from './plugins/screenshot';
@ -48,6 +50,7 @@ import {SMS} from './plugins/sms';
import {SocialSharing} from './plugins/socialsharing'; import {SocialSharing} from './plugins/socialsharing';
import {SpinnerDialog} from './plugins/spinnerdialog'; import {SpinnerDialog} from './plugins/spinnerdialog';
import {Splashscreen} from './plugins/splashscreen'; import {Splashscreen} from './plugins/splashscreen';
import {SQLite} from './plugins/sqlite';
import {StatusBar} from './plugins/statusbar'; import {StatusBar} from './plugins/statusbar';
import {Toast} from './plugins/toast'; import {Toast} from './plugins/toast';
import {TouchID} from './plugins/touchid'; import {TouchID} from './plugins/touchid';
@ -85,6 +88,7 @@ export {
Flashlight, Flashlight,
Geolocation, Geolocation,
Globalization, Globalization,
GoogleMaps,
GoogleAnalytics, GoogleAnalytics,
Hotspot, Hotspot,
ImagePicker, ImagePicker,
@ -92,6 +96,7 @@ export {
Keyboard, Keyboard,
LaunchNavigator, LaunchNavigator,
LocalNotifications, LocalNotifications,
MediaPlugin,
Network, Network,
Push, Push,
Screenshot, Screenshot,
@ -99,6 +104,7 @@ export {
SocialSharing, SocialSharing,
SpinnerDialog, SpinnerDialog,
Splashscreen, Splashscreen,
SQLite,
StatusBar, StatusBar,
Toast, Toast,
TouchID, TouchID,
@ -140,6 +146,7 @@ window['IonicNative'] = {
Flashlight: Flashlight, Flashlight: Flashlight,
Geolocation: Geolocation, Geolocation: Geolocation,
Globalization: Globalization, Globalization: Globalization,
GoogleMaps : GoogleMaps,
GoogleAnalytics: GoogleAnalytics, GoogleAnalytics: GoogleAnalytics,
Hotspot: Hotspot, Hotspot: Hotspot,
ImagePicker: ImagePicker, ImagePicker: ImagePicker,
@ -147,6 +154,7 @@ window['IonicNative'] = {
Keyboard: Keyboard, Keyboard: Keyboard,
LaunchNavigator: LaunchNavigator, LaunchNavigator: LaunchNavigator,
LocalNotifications: LocalNotifications, LocalNotifications: LocalNotifications,
MediaPlugin: MediaPlugin,
Network: Network, Network: Network,
Push: Push, Push: Push,
Screenshot: Screenshot, Screenshot: Screenshot,
@ -154,6 +162,7 @@ window['IonicNative'] = {
SocialSharing: SocialSharing, SocialSharing: SocialSharing,
SpinnerDialog: SpinnerDialog, SpinnerDialog: SpinnerDialog,
Splashscreen: Splashscreen, Splashscreen: Splashscreen,
SQLite: SQLite,
StatusBar: StatusBar, StatusBar: StatusBar,
Toast: Toast, Toast: Toast,
TouchID: TouchID, TouchID: TouchID,

37
src/plugins/googlemaps.ts Normal file
View File

@ -0,0 +1,37 @@
import {Cordova, Plugin} from "./plugin";
import {Observable} from "rxjs/Observable";
import {CordovaInstance} from "./plugin";
/**
* Created by Ibrahim on 3/29/2016.
*/
declare var plugin : any;
/**
* @name Google Maps
*/
@Plugin({
pluginRef: 'plugin.google.maps',
plugin: 'cordova-plugin-googlemaps',
repo: 'https://github.com/mapsplugin/cordova-plugin-googlemaps'
})
export class GoogleMaps {
private _objectInstance : any;
constructor (elementId : string) {
this._objectInstance = plugin.google.maps.Map.getMap(document.getElementById(elementId));
}
@Cordova({
eventObservable: true,
event: 'plugin.google.maps.event.MAP_READY'
})
static onInit () : Observable<GoogleMaps> {return}
@CordovaInstance({
sync: true
})
setDebuggable (isDebuggable : boolean) : void {}
setClickable (isClickable : boolean) : void {}
}

168
src/plugins/media.ts Normal file
View File

@ -0,0 +1,168 @@
import {CordovaInstance, Plugin} from './plugin';
declare var Media:any;
/**
* @name MediaPlugin
* @description
* @usage
* ```ts
* import {MediaPlugin} from 'ionic-native';
*
*
* ...
*
*
* // Playing a file
* var file = new MediaPlugin("path/to/file.mp3");
*
* // play the file
* file.play();
*
* // skip to 10 seconds
* file.seekTo(10000);
*
* // stop plying the file
* file.stop();
*
*
* ...
*
* // Recording to a file
* var newFile = new MediaPlugin("path/to/file.mp3");
* newFile.startRecord();
*
* newFile.stopRecord();
*
*
*
* ```
*/
@Plugin({
repo: 'https://github.com/apache/cordova-plugin-media',
plugin: 'cordova-plugin-media',
pluginRef: 'Media'
})
export class MediaPlugin {
// Constants
static MEDIA_NONE : number = 0;
static MEDIA_STARTING : number = 1;
static MEDIA_RUNNING : number = 2;
static MEDIA_PAUSED : number = 3;
static MEDIA_STOPPED : number = 4;
// Properties
private _objectInstance : any;
// Methods
/**
* Open a media file
* @param src {string} A URI containing the audio content.
*/
constructor (src : string) {
// TODO handle success, error, and status
this._objectInstance = new Media(src);
}
/**
* Returns the current amplitude of the current recording.
*/
@CordovaInstance()
getCurrentAmplitude () : Promise<any> {return}
/**
* Returns the current position within an audio file. Also updates the Media object's position parameter.
*/
@CordovaInstance()
getCurrentPosition () : Promise<any> {return}
/**
* Returns the duration of an audio file in seconds. If the duration is unknown, it returns a value of -1.
*/
@CordovaInstance({
sync: true
})
getDuration () : number {return}
/**
* Starts or resumes playing an audio file.
*/
@CordovaInstance({
sync: true
})
play (iosOptions? : {
numberOfLoops? : number,
playAudioWhenScreenIsLocked? : boolean
}) : void {}
/**
* Pauses playing an audio file.
*/
@CordovaInstance({
sync: true
})
pause () : void {}
/**
* Releases the underlying operating system's audio resources. This is particularly important for Android, since there are a finite amount of OpenCore instances for media playback. Applications should call the release function for any Media resource that is no longer needed.
*/
@CordovaInstance({
sync: true
})
release () : void {}
/**
* Sets the current position within an audio file.
* @param milliseconds
*/
@CordovaInstance({
sync: true
})
seekTo (milliseconds : number) : void {}
/**
* Set the volume for an audio file.
* @param volume The volume to set for playback. The value must be within the range of 0.0 to 1.0.
*/
@CordovaInstance({
sync: true
})
setVolume (volume : number) : void {}
/**
* Starts recording an audio file.
*/
@CordovaInstance({
sync: true
})
startRecord () : void {}
/**
* Stops recording
*/
@CordovaInstance({
sync: true
})
stopRecord () : void {}
/**
* Stops playing an audio file.
*/
@CordovaInstance({
sync: true
})
stop () : void {}
}
export class MediaError {
static get MEDIA_ERR_ABORTED () {return 1;}
static get MEDIA_ERR_NETWORK () {return 2;}
static get MEDIA_ERR_DECODE () {return 3;}
static get MEDIA_ERR_NONE_SUPPORTED () {return 4;}
code : number;
message : string;
}

View File

@ -54,11 +54,7 @@ export const cordovaWarn = function(pluginName: string, method: string) {
console.warn('Native: tried accessing the ' + pluginName + ' plugin but Cordova is not available. Make sure to include cordova.js or run in a device/simulator'); console.warn('Native: tried accessing the ' + pluginName + ' plugin but Cordova is not available. Make sure to include cordova.js or run in a device/simulator');
} }
}; };
function setIndex (args:any[], opts:any={}, resolve? : Function, reject?: Function) : any {
function callCordovaPlugin(pluginObj:any, methodName:string, args:any[], opts:any={}, resolve?: Function, reject?: Function) {
// Try to figure out where the success/error callbacks need to be bound
// to our promise resolve/reject handlers.
// If the plugin method expects myMethod(success, err, options) // If the plugin method expects myMethod(success, err, options)
if (opts.callbackOrder == 'reverse') { if (opts.callbackOrder == 'reverse') {
// Get those arguments in the order [resolve, reject, ...restOfArgs] // Get those arguments in the order [resolve, reject, ...restOfArgs]
@ -75,6 +71,15 @@ function callCordovaPlugin(pluginObj:any, methodName:string, args:any[], opts:an
args.push(reject); args.push(reject);
} }
return args;
}
function callCordovaPlugin(pluginObj:any, methodName:string, args:any[], opts:any={}, resolve?: Function, reject?: Function) {
// Try to figure out where the success/error callbacks need to be bound
// to our promise resolve/reject handlers.
args = setIndex (args, opts, resolve, reject);
let pluginInstance = getPlugin(pluginObj.pluginRef); let pluginInstance = getPlugin(pluginObj.pluginRef);
if(!pluginInstance) { if(!pluginInstance) {
@ -92,8 +97,6 @@ function callCordovaPlugin(pluginObj:any, methodName:string, args:any[], opts:an
}; };
} }
// console.log('Cordova calling', pluginObj.name, methodName, args);
// TODO: Illegal invocation needs window context // TODO: Illegal invocation needs window context
return get(window, pluginObj.pluginRef)[methodName].apply(pluginInstance, args); return get(window, pluginObj.pluginRef)[methodName].apply(pluginInstance, args);
} }
@ -152,6 +155,38 @@ function wrapObservable(pluginObj:any, methodName:string, args:any[], opts:any =
}); });
} }
function callInstance(pluginObj:any, methodName : string, args:any[], opts:any = {}, resolve? : Function, reject? : Function){
args = setIndex(args, opts, resolve, reject);
return pluginObj._objectInstance[methodName].apply(pluginObj._objectInstance, args);
}
function wrapInstance (pluginObj:any, methodName:string, args:any[], opts:any = {}){
return (...args) => {
if (opts.sync) {
return callInstance(pluginObj, methodName, args, opts);
} else if (opts.observable) {
return new Observable(observer => {
let pluginResult = callInstance(pluginObj, methodName,args, opts, observer.next.bind(observer), observer.error.bind(observer));
return () => {
try {
if (opts.clearWithArgs){
return pluginObj._objectInstance[opts.clearFunction].apply(pluginObj._objectInstance, args);
}
return pluginObj._objectInstance[opts.clearFunction].call(pluginObj, pluginResult);
} catch(e) {
console.warn('Unable to clear the previous observable watch for', pluginObj.name, methodName);
console.error(e);
}
}
});
} else {
return getPromise((resolve, reject) => {
callInstance(pluginObj, methodName, args, opts, resolve, reject);
});
}
}
}
/** /**
* Wrap the event with an observable * Wrap the event with an observable
* @param event * @param event
@ -242,6 +277,21 @@ export function Cordova(opts:any = {}) {
} }
} }
/**
* @private
*
* Wrap an instance method
*/
export function CordovaInstance(opts:any = {}) {
return (target: Object, methodName: string) => {
return {
value: function(...args: any[]) {
return wrapInstance(this, methodName, opts).apply(this, args);
}
}
}
}
/** /**
* @private * @private
* *
@ -252,7 +302,6 @@ export function CordovaProperty(target: Function, key: string, descriptor: Typed
let originalMethod = descriptor.get; let originalMethod = descriptor.get;
descriptor.get = function(...args: any[]) { descriptor.get = function(...args: any[]) {
// console.log('Calling', this);
if(!window.cordova) { if(!window.cordova) {
cordovaWarn(this.name, null); cordovaWarn(this.name, null);
return {}; return {};

107
src/plugins/sqlite.ts Normal file
View File

@ -0,0 +1,107 @@
import {CordovaInstance, Plugin, Cordova} from './plugin';
declare var sqlitePlugin;
/**
* @name SQLite
*/
@Plugin({
pluginRef: 'sqlitePlugin',
plugin: 'cordova-sqlite-storage',
repo: 'https://github.com/litehelpers/Cordova-sqlite-storage'
})
export class SQLite {
private _objectInstance : any;
get databaseFeatures() : any {
return this._objectInstance.databaseFeatures;
}
constructor (config : any) {
new Promise((resolve, reject) => {
sqlitePlugin.openDatabase(config, resolve, reject);
}).then(
db => this._objectInstance = db,
error => console.warn(error)
);
}
@CordovaInstance({
sync: true
})
addTransaction (transaction : any) : void {}
@CordovaInstance()
transaction (fn : any) : Promise<any> {return}
@CordovaInstance()
readTransaction (fn : any) : Promise<any> {return}
@CordovaInstance({
sync: true
})
startNextTransaction () : void {}
@CordovaInstance()
close () : Promise<any> {return}
@CordovaInstance({
sync: true
})
start () : void {}
@CordovaInstance()
executeSql (statement : string, params : any) : Promise<any> {return}
@CordovaInstance()
addSatement (sql, values) : Promise<any> {return}
@CordovaInstance()
sqlBatch (sqlStatements : any) : Promise<any> {return}
@CordovaInstance({
sync: true
})
abortallPendingTransactions () : void {}
@CordovaInstance({
sync: true
})
handleStatementSuccess (handler, response) : void {}
@CordovaInstance({
sync: true
})
handleStatementFailure (handler, response) : void {}
@CordovaInstance({
sync: true
})
run () : void {}
@CordovaInstance({
sync: true
})
abort (txFailure) : void {}
@CordovaInstance({
sync: true
})
finish () : void {}
@CordovaInstance({
sync: true
})
abortFromQ (sqlerror) : void {}
@Cordova()
static echoTest () : Promise<any> {return}
@Cordova()
static deleteDatabase (first) : Promise<any> {return}
}