Last active
August 29, 2015 14:20
-
-
Save montrealist/9c40c2fc6e1dab3fd0f9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
/** | |
* services.cordova Module | |
* | |
* General Cordova services module | |
*/ | |
angular.module('bili.services.cordova') | |
/** | |
* CordovaDialog AngularJS service | |
* | |
* com.apache.cordova.dialogs wrapper for AngularJS with fallback for testing without Cordova | |
*/ | |
.service('CordovaDialog', ['$window', '$q', function($window, $q) { | |
var notification = navigator.notification || { | |
alert: function (message, alertCallback, title, buttonLabel) { | |
return alertCallback($window.alert(title + '\n\n' + message + '\n\nButton: ' + buttonLabel)); | |
}, | |
confirm: function (message, confirmCallback, title, buttonLabels) { | |
buttonLabels = buttonLabels || ['OK', 'Cancel']; | |
return $window.confirm(message); | |
}, | |
prompt: function (message, confirmCallback, title, buttonLabels, defaultValue) { | |
return $window.prompt(title + '\n\n' + message + '\n\nButtons:\n' + buttonLabels.join('\n'), defaultValue); | |
}, | |
beep: function (times) { | |
var def = $q.defer(); | |
for (var i = 0; i <= times; i++) { | |
notification.alert('Beep ' + (i + 1)); | |
if (i === times) { | |
def.resolve(times); | |
} | |
} | |
return def.promise; | |
} | |
}; | |
return { | |
alert: function (message, title, buttonName) { | |
var def = $q.defer(); | |
console.info('Alert triggered'); | |
var cb = function () { | |
console.info('Alert callback triggered'); | |
def.resolve(); | |
}; | |
notification.alert(message, cb, title, buttonName); | |
return def.promise; | |
}, | |
confirm: function (message, title, buttonName) { | |
var def = $q.defer(); | |
var cb = function (buttonIndex) { | |
switch (buttonIndex) { | |
case 1: | |
// First button is the confirm button | |
def.resolve(true); | |
break; | |
default: | |
def.reject(false); | |
} | |
}; | |
var confirmed = notification.confirm(message, cb, title, buttonName); | |
if (confirmed) { | |
def.resolve(true); | |
} else { | |
def.resolve(false); | |
} | |
return def.promise; | |
}, | |
prompt: function (message, title, buttonLabels, defaultValue) { | |
var def = $q.defer(); | |
var cb = function (results) { | |
switch (results.buttonIndex) { | |
case 1: | |
// First button is the confirm button | |
def.resolve(results); | |
break; | |
default: | |
def.reject(false); | |
} | |
}; | |
var confirmed = notification.prompt(message, cb, title, buttonLabels, defaultValue); | |
if (confirmed && confirmed !== null) { | |
def.resolve(true); | |
} else { | |
def.resolve(false); | |
} | |
return def.promise; | |
}, | |
beep: function (times) { | |
return notification.beep(times); | |
} | |
}; | |
}]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
/** | |
* services.cordova Module | |
* | |
* General Cordova services wrapper module | |
*/ | |
angular.module('services.cordova') | |
/** | |
* CordovaNetwork AngularJS service | |
* | |
* com.apache.cordova.network-information wrapper for AngularJS with fallback for testing without Cordova | |
*/ | |
.service('CordovaNetwork', ['$rootScope', '$ionicPlatform', function($rootScope, $ionicPlatform) { | |
// Get Cordova's global Connection object or emulate a smilar one | |
var Connection = window.Connection || { | |
'ETHERNET': 'ETHERNET', | |
'WIFI': 'WIFI', | |
'CELL_2G': 'CELL_2G', | |
'CELL_3G': 'CELL_3G', | |
'CELL_4G': 'CELL_4G', | |
'CELL': 'CELL', | |
'EDGE': 'EDGE', | |
'UNKNOWN': 'unknown' | |
}; | |
// Get Cordova's global navigator.connection object or emulate one | |
var networkConnection = navigator.connection || { | |
type: 'UNKNOWN' | |
}; | |
// These services are written with full Ionic Framework integration. | |
$ionicPlatform.ready(function () { | |
document.addEventListener('offline', function () { | |
$rootScope.$broadcast('Cordova.NetworkStatus.Offline'); | |
}, false); | |
document.addEventListener('online', function () { | |
$rootScope.$broadcast('Cordova.NetworkStatus.Online'); | |
}, false); | |
}); | |
return { | |
isOnline: function () { | |
var blnReturn = false; | |
switch (this.getStatus()) { | |
case Connection.ETHERNET: | |
case Connection.WIFI: | |
case Connection.CELL_2G: | |
case Connection.CELL_3G: | |
case Connection.CELL_4G: | |
case Connection.CELL: | |
blnReturn = true; | |
break; | |
} | |
return blnReturn; | |
}, | |
getStatus: function () { | |
return networkConnection.type; | |
} | |
}; | |
}]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment