utils.factory.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. angular.module('odoo')
  2. /**
  3. * -----------------------------------------------------------------------------
  4. * Description: Native SQL util instructions
  5. * -----------------------------------------------------------------------------
  6. */
  7. .factory('sqlFactory', function () {
  8. /**
  9. *
  10. */
  11. var count = function (tableName, success, error) {
  12. var sql = 'SELECT COUNT(*) AS total FROM ' + tableName;
  13. db.executeSql(sql, [], function (result) {
  14. success(result.rows.item(0).total);
  15. }, function (err) {
  16. error(err);
  17. });
  18. };
  19. // Execute native SQL SELECT instruction
  20. var select = function (tableName, success, error) {
  21. var sql = 'SELECT * FROM ' + tableName;
  22. db.executeSql(sql, [], function (result) {
  23. success(result.rows);
  24. }, function (err) {
  25. error(err);
  26. });
  27. };
  28. // Execute native SQL SELECT instruction with a constraint
  29. var selectByConstraint = function (tableName, constraint, success, error) {
  30. var sql = 'SELECT * FROM ' + tableName + ' WHERE ' + constraint;
  31. db.executeSql(sql, [], function (result) {
  32. success(result.rows);
  33. }, function (err) {
  34. error(err);
  35. });
  36. };
  37. // Execute native SQL SELECT instruction with a constraint
  38. var selectById = function (tableName, id, success, error) {
  39. var sql = 'SELECT * FROM ' + tableName + ' WHERE id = ?';
  40. db.executeSql(sql, [id], function (result) {
  41. success(result.rows);
  42. }, function (err) {
  43. error(err);
  44. });
  45. };
  46. // Execute native SQL SELECT instruction with count instruction
  47. var count = function (tableName, success, error) {
  48. var sql = 'SELECT COUNT(*) AS total FROM ' + tableName;
  49. db.executeSql(sql, [], function (result) {
  50. success(result.rows.item(0).total);
  51. }, function (err) {
  52. error(err);
  53. });
  54. };
  55. return {
  56. select: select,
  57. selectByConstraint: selectByConstraint,
  58. selectById: selectById,
  59. count: count
  60. }
  61. })
  62. /**
  63. * -----------------------------------------------------------------------------
  64. * Description: Get user configuration from local database
  65. * -----------------------------------------------------------------------------
  66. */
  67. .factory('configFactory', function (sqlFactory) {
  68. return function (success, error) {
  69. sqlFactory.select('user', function (users) {
  70. if (users.length == 0) {
  71. success(0);
  72. } else if (users.length == 1) {
  73. success(users.item(0));
  74. } else {
  75. var configs = [];
  76. for (var i = 0; i < users.length; i++) {
  77. configs.push(users.item(i))
  78. }
  79. success(configs);
  80. }
  81. }, function (err) {
  82. error(err);
  83. });
  84. };
  85. })
  86. /**
  87. * -----------------------------------------------------------------------------
  88. * Description: Async loop util v2
  89. * -----------------------------------------------------------------------------
  90. */
  91. .factory('asyncLoopFactory', function () {
  92. return function (iterations, func, callback) {
  93. var index = 0;
  94. var done = false;
  95. var loop = {
  96. next: function () {
  97. if (done) {
  98. return;
  99. }
  100. if (index < iterations) {
  101. index++;
  102. func(loop);
  103. } else {
  104. done = true;
  105. callback();
  106. }
  107. },
  108. iteration: function () {
  109. return index - 1;
  110. },
  111. break: function () {
  112. done = true;
  113. callback();
  114. }
  115. };
  116. loop.next();
  117. return loop;
  118. }
  119. })
  120. /**
  121. * -----------------------------------------------------------------------------
  122. * Description: Native device functions manager
  123. * -----------------------------------------------------------------------------
  124. */
  125. .factory('deviceFactory', function (
  126. $timeout,
  127. $rootScope,
  128. $cordovaToast,
  129. $cordovaCamera,
  130. $cordovaDialogs,
  131. $cordovaContacts,
  132. $cordovaVibration,
  133. $cordovaGeolocation,
  134. $cordovaDeviceMotion,
  135. $cordovaLaunchNavigator
  136. ) {
  137. var vibrateDuration = 50;
  138. /**
  139. *
  140. */
  141. var takePicture = function (success, error) {
  142. var options = {
  143. quality: 75,
  144. destinationType: Camera.DestinationType.DATA_URL,
  145. sourceType: Camera.PictureSourceType.CAMERA,
  146. allowEdit: true,
  147. encodingType: Camera.EncodingType.JPEG,
  148. targetWidth: 300,
  149. targetHeight: 300,
  150. popoverOptions: CameraPopoverOptions,
  151. saveToPhotoAlbum: false,
  152. correctOrientation: true
  153. };
  154. $cordovaCamera.getPicture(options).then(function (imageData) {
  155. success(imageData);
  156. }, function (err) {
  157. error(err);
  158. });
  159. }
  160. /**
  161. *
  162. */
  163. var saveContact = function (contact, success, error) {
  164. if (!contact.mobile && !contact.phone && !contact.email) {
  165. return error('No hay nada que guardar');
  166. }
  167. var info = {
  168. name: {
  169. givenName: contact.name,
  170. familyName: '',
  171. formatted: ''
  172. },
  173. nickname: '',
  174. phoneNumbers: [
  175. {
  176. value: contact.phone,
  177. type: 'phone'
  178. },
  179. {
  180. value: contact.mobile,
  181. type: 'mobile'
  182. }
  183. ],
  184. emails: [
  185. {
  186. value: contact.email,
  187. type: 'home'
  188. }
  189. ],
  190. addresses: [
  191. {
  192. type: 'home',
  193. formatted: '',
  194. streetAddress: contact.street,
  195. locality: contact.city,
  196. region: '',
  197. postalCode: '',
  198. country: 'Paraguay'
  199. }
  200. ],
  201. ims: null,
  202. organizations: null,
  203. birthday: null,
  204. note: null,
  205. photos: null,
  206. categories: null,
  207. urls: null
  208. };
  209. $cordovaContacts.save(info).then(function (result) {
  210. $cordovaVibration.vibrate(vibrateDuration);
  211. success(result);
  212. }, function (err) {
  213. $cordovaVibration.vibrate(vibrateDuration);
  214. error(err);
  215. });
  216. }
  217. /**
  218. *
  219. */
  220. var getCurrentPosition = function (success, error) {
  221. var options = {
  222. timeout: 10000,
  223. enableHighAccuracy: false
  224. }
  225. notify('Obteniendo localización');
  226. $cordovaGeolocation.getCurrentPosition(options).then(function (position) {
  227. notify('Localización obtenida con éxito');
  228. success(position);
  229. }, function (err) {
  230. notify('No se pudo obtener la localización, revise si su GPS está activo', true);
  231. error(err);
  232. });
  233. }
  234. /**
  235. *
  236. */
  237. var navigate = function (destination, success, error) {
  238. if (!destination.partner_latitude || !destination.partner_longitude) {
  239. return error('No hay destino');
  240. }
  241. $cordovaLaunchNavigator.navigate(destination).then(function () {
  242. success('Navigator launched');
  243. }, function (err) {
  244. error(err);
  245. });
  246. }
  247. /**
  248. *
  249. */
  250. var vibrate = function () {
  251. $cordovaVibration.vibrate(vibrateDuration);
  252. }
  253. /**
  254. *
  255. */
  256. var toast = function (message, long) {
  257. long = long || false;
  258. $cordovaToast.show(message, long ? 'long' : 'short', 'bottom');
  259. }
  260. /**
  261. *
  262. */
  263. var confirm = function (message, title, success) {
  264. $cordovaDialogs.confirm(message, title, ['Aceptar', 'Cancelar']).then(function (index) {
  265. success(index);
  266. });
  267. }
  268. /**
  269. *
  270. */
  271. var notify = function (message, long) {
  272. vibrate();
  273. toast(message, long);
  274. }
  275. /**
  276. *
  277. */
  278. var detectShake = function () {
  279. var previousMeasurements = {};
  280. var options = {
  281. frequency: 100,
  282. deviation: 25
  283. }
  284. var watcher = null;
  285. var startWatching = function () {
  286. watcher = $cordovaDeviceMotion.watchAcceleration(options);
  287. watcher.then(null, function (err) {
  288. console.log(err);
  289. }, function (currentMeasurements) {
  290. evaluateShake(currentMeasurements);
  291. });
  292. }
  293. var evaluateShake = function (currentMeasurements) {
  294. var measurements = {};
  295. if (previousMeasurements.x) {
  296. measurements.x = Math.abs(previousMeasurements.x, currentMeasurements.x);
  297. measurements.y = Math.abs(previousMeasurements.y, currentMeasurements.y);
  298. measurements.z = Math.abs(previousMeasurements.x, currentMeasurements.z);
  299. }
  300. if (measurements.x + measurements.y + measurements.z > options.deviation) {
  301. stopWatching();
  302. previousMeasurements = {};
  303. } else {
  304. previousMeasurements = currentMeasurements;
  305. }
  306. }
  307. var stopWatching = function () {
  308. watcher.clearWatch();
  309. $timeout(function () {
  310. startWatching();
  311. }, 1500);
  312. $rootScope.$broadcast('device.shaked');
  313. }
  314. startWatching();
  315. }
  316. return {
  317. takePicture: takePicture,
  318. saveContact: saveContact,
  319. getCurrentPosition: getCurrentPosition,
  320. navigate: navigate,
  321. vibrate: vibrate,
  322. toast: toast,
  323. confirm: confirm,
  324. notify: notify,
  325. detectShake: detectShake
  326. }
  327. });