utils.factory.js 11 KB

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