angular.module('odoo') /** * ----------------------------------------------------------------------------- * Description: Native SQL util instructions * ----------------------------------------------------------------------------- */ .factory('sqlFactory', function () { /** * */ var count = function (tableName, success, error) { var sql = 'SELECT COUNT(*) AS total FROM ' + tableName; db.executeSql(sql, [], function (result) { success(result.rows.item(0).total); }, function (err) { error(err); }); }; // Execute native SQL SELECT instruction var select = function (tableName, success, error) { var sql = 'SELECT * FROM ' + tableName; db.executeSql(sql, [], function (result) { success(result.rows); }, function (err) { error(err); }); }; // Execute native SQL SELECT instruction with a constraint var selectByConstraint = function (tableName, constraint, success, error) { var sql = 'SELECT * FROM ' + tableName + ' WHERE ' + constraint; db.executeSql(sql, [], function (result) { success(result.rows); }, function (err) { error(err); }); }; // Execute native SQL SELECT instruction with a constraint var selectById = function (tableName, id, success, error) { var sql = 'SELECT * FROM ' + tableName + ' WHERE id = ?'; db.executeSql(sql, [id], function (result) { success(result.rows); }, function (err) { error(err); }); }; // Execute native SQL SELECT instruction with count instruction var count = function (tableName, success, error) { var sql = 'SELECT COUNT(*) AS total FROM ' + tableName; db.executeSql(sql, [], function (result) { success(result.rows.item(0).total); }, function (err) { error(err); }); }; return { select: select, selectByConstraint: selectByConstraint, selectById: selectById, count: count } }) /** * ----------------------------------------------------------------------------- * Description: Get user configuration from local database * ----------------------------------------------------------------------------- */ .factory('configFactory', function (sqlFactory) { return function (success, error) { sqlFactory.select('user', function (users) { if (users.length == 0) { success(0); } else if (users.length == 1) { success(users.item(0)); } else { var configs = []; for (var i = 0; i < users.length; i++) { configs.push(users.item(i)) } success(configs); } }, function (err) { error(err); }); }; }) /** * ----------------------------------------------------------------------------- * Description: Async loop util v2 * ----------------------------------------------------------------------------- */ .factory('asyncLoopFactory', function () { return function (iterations, func, callback) { var index = 0; var done = false; var loop = { next: function () { if (done) { return; } if (index < iterations) { index++; func(loop); } else { done = true; callback(); } }, iteration: function () { return index - 1; }, break: function () { done = true; callback(); } }; loop.next(); return loop; } }) /** * ----------------------------------------------------------------------------- * Description: Native device functions manager * ----------------------------------------------------------------------------- */ .factory('deviceFactory', function ( $timeout, $rootScope, $cordovaToast, $cordovaCamera, $cordovaDialogs, $cordovaContacts, $cordovaVibration, $cordovaGeolocation, $cordovaDeviceMotion, $cordovaLaunchNavigator ) { var vibrateDuration = 50; /** * */ var takePicture = function (success, error) { var options = { quality: 75, destinationType: Camera.DestinationType.DATA_URL, sourceType: Camera.PictureSourceType.CAMERA, allowEdit: true, encodingType: Camera.EncodingType.JPEG, targetWidth: 300, targetHeight: 300, popoverOptions: CameraPopoverOptions, saveToPhotoAlbum: false, correctOrientation: true }; $cordovaCamera.getPicture(options).then(function (imageData) { success(imageData); }, function (err) { error(err); }); } /** * */ var saveContact = function (contact, success, error) { if (!contact.mobile && !contact.phone && !contact.email) { return error('No hay nada que guardar'); } var info = { name: { givenName: contact.name, familyName: '', formatted: '' }, nickname: '', phoneNumbers: [ { value: contact.phone, type: 'phone' }, { value: contact.mobile, type: 'mobile' } ], emails: [ { value: contact.email, type: 'home' } ], addresses: [ { type: 'home', formatted: '', streetAddress: contact.street, locality: contact.city, region: '', postalCode: '', country: 'Paraguay' } ], ims: null, organizations: null, birthday: null, note: null, photos: null, categories: null, urls: null }; $cordovaContacts.save(info).then(function (result) { $cordovaVibration.vibrate(vibrateDuration); success(result); }, function (err) { $cordovaVibration.vibrate(vibrateDuration); error(err); }); } /** * */ var getCurrentPosition = function (success, error) { var options = { timeout: 10000, enableHighAccuracy: false } notify('Obteniendo localización'); $cordovaGeolocation.getCurrentPosition(options).then(function (position) { notify('Localización obtenida con éxito'); success(position); }, function (err) { notify('No se pudo obtener la localización, revise si su GPS está activo', true); error(err); }); } /** * */ var navigate = function (destination, success, error) { if (!destination.partner_latitude || !destination.partner_longitude) { return error('No hay destino'); } $cordovaLaunchNavigator.navigate(destination).then(function () { success('Navigator launched'); }, function (err) { error(err); }); } /** * */ var vibrate = function () { $cordovaVibration.vibrate(vibrateDuration); } /** * */ var toast = function (message, long) { long = long || false; $cordovaToast.show(message, long ? 'long' : 'short', 'bottom'); } /** * */ var confirm = function (message, title, success) { $cordovaDialogs.confirm(message, title, ['Aceptar', 'Cancelar']).then(function (index) { success(index); }); } /** * */ var notify = function (message, long) { vibrate(); toast(message, long); } /** * */ var detectShake = function () { var previousMeasurements = {}; var options = { frequency: 100, deviation: 25 } var watcher = null; var startWatching = function () { watcher = $cordovaDeviceMotion.watchAcceleration(options); watcher.then(null, function (err) { console.log(err); }, function (currentMeasurements) { evaluateShake(currentMeasurements); }); } var evaluateShake = function (currentMeasurements) { var measurements = {}; if (previousMeasurements.x) { measurements.x = Math.abs(previousMeasurements.x, currentMeasurements.x); measurements.y = Math.abs(previousMeasurements.y, currentMeasurements.y); measurements.z = Math.abs(previousMeasurements.x, currentMeasurements.z); } if (measurements.x + measurements.y + measurements.z > options.deviation) { stopWatching(); previousMeasurements = {}; } else { previousMeasurements = currentMeasurements; } } var stopWatching = function () { watcher.clearWatch(); $timeout(function () { startWatching(); }, 1500); $rootScope.$broadcast('device.shaked'); } startWatching(); } return { takePicture: takePicture, saveContact: saveContact, getCurrentPosition: getCurrentPosition, navigate: navigate, vibrate: vibrate, toast: toast, confirm: confirm, notify: notify, detectShake: detectShake } });