Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add network plugin for issue #89 #90

Merged
merged 2 commits into from
Mar 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {InAppBrowser} from './plugins/inappbrowser';
import {Keyboard} from './plugins/keyboard';
import {LaunchNavigator} from './plugins/launchnavigator';
import {LocalNotifications} from './plugins/localnotifications';
import {Network, Connection} from './plugins/network';
import {Push} from './plugins/push';
import {SMS} from './plugins/sms';
import {SocialSharing} from './plugins/socialsharing';
Expand All @@ -58,6 +59,7 @@ export {
Calendar,
Camera,
Clipboard,
Connection,
Contacts,
DatePicker,
DBMeter,
Expand All @@ -76,6 +78,7 @@ export {
Keyboard,
LaunchNavigator,
LocalNotifications,
Network,
Push,
SMS,
SocialSharing,
Expand Down Expand Up @@ -103,6 +106,7 @@ window['IonicNative'] = {
Calendar: Calendar,
Camera: Camera,
Clipboard: Clipboard,
Connection: Connection,
Contacts: Contacts,
DatePicker: DatePicker,
DBMeter: DBMeter,
Expand All @@ -121,6 +125,7 @@ window['IonicNative'] = {
Keyboard: Keyboard,
LaunchNavigator: LaunchNavigator,
LocalNotifications: LocalNotifications,
Network: Network,
Push: Push,
SMS: SMS,
SocialSharing: SocialSharing,
Expand Down
87 changes: 87 additions & 0 deletions src/plugins/network.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import {Plugin, Cordova, CordovaProperty} from './plugin';
import {Observable} from "rxjs/Observable";

declare var navigator;

/**
* @name Network
* @description
* Requires Cordova plugin: cordova-plugin-network-information. For more info, please see the [Network plugin docs](https://github.com/apache/cordova-plugin-network-information).
*
* @usage
* ```js
* import {Network, Connection} from 'ionic-native';
*
* // watch network for a disconnect
* let disconnectSubscription = Network.onDisconnect().subscribe(() => {
* console.log('network was disconnected :-( ')
* });
*
* // stop disconnect watch
* disconnectSubscription.unsubscribe();
*
*
* // watch network for a connection
* let connectSubscription = Network.onConnect().subscribe(() => {
* console.log('network connected!');
* // use a timeout so that we can determine what type of connection we have
* setTimeout(() => {
* console.log(Network.connection);
* if (Network.connection === Connection.WIFI) {
* console.log('we got a wifi connection, woohoo!');
* }
* });
* });
*
* // stop connect watch
* connectSubscription.unsubscribe();
*
* ```
*/
@Plugin({
plugin: 'cordova-plugin-network-information',
repo: 'https://github.com/apache/cordova-plugin-network-information',
platforms: ['Amazon Fire OS', 'iOS', 'Android', 'BlackBerry 10', 'Windows Phone 7', 'Windows Phone 8', 'Windows', 'Firefox OS', 'Browser'],
pluginRef: 'navigator.connection'
})
export class Network {

/**
* Return the network connection type
*/
@CordovaProperty
static get connection() : Connection { return navigator.connection.type; }
// static get preferences() { return window.AppRate.preferences; }

/**
* Watch the network for a disconnect (i.e. network goes offline)
* @returns {Observable<any>} Returns an observable.
*/
@Cordova({
eventObservable: true,
event: 'offline'
})
static onDisconnect() : Observable<any> { return }

/**
* Watch the network for a connection (i.e. network goes online)
* @returns {Observable<any>} Returns an observable.
*/
@Cordova({
eventObservable: true,
event: 'online'
})
static onConnect() : Observable<any> { return; }

}

export class Connection {
static get UNKNOWN() { return "unknown"; }
static get ETHERNET() { return "ethernet"; }
static get WIFI() { return "wifi"; }
static get CELL_2G() { return "2g"; }
static get CELL_3G() { return "3g"; }
static get CELL_4G() { return "4g"; }
static get CELL() { return "cellular"; }
static get NONE() { return "none"; }
}