utils.factory.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 camera manager
  113. * -----------------------------------------------------------------------------
  114. */
  115. .factory('cameraFactory', function ($cordovaCamera) {
  116. // Take a picture using native camera
  117. var takePicture = function (success, error) {
  118. var options = {
  119. quality: 75,
  120. destinationType: Camera.DestinationType.DATA_URL,
  121. sourceType: Camera.PictureSourceType.CAMERA,
  122. allowEdit: true,
  123. encodingType: Camera.EncodingType.JPEG,
  124. targetWidth: 300,
  125. targetHeight: 300,
  126. popoverOptions: CameraPopoverOptions,
  127. saveToPhotoAlbum: false,
  128. correctOrientation: true
  129. };
  130. $cordovaCamera.getPicture(options).then(function (imageData) {
  131. success(imageData);
  132. }, function (err) {
  133. error(err);
  134. });
  135. };
  136. return {
  137. takePicture: takePicture
  138. }
  139. })
  140. /**
  141. * -----------------------------------------------------------------------------
  142. * Description: Native device functions manager
  143. * -----------------------------------------------------------------------------
  144. */
  145. .factory('deviceFactory', function (
  146. $timeout,
  147. $rootScope,
  148. $cordovaGeolocation,
  149. $cordovaDeviceMotion,
  150. $cordovaToast,
  151. $cordovaVibration
  152. ) {
  153. var vibrateDuration = 100;
  154. /**
  155. *
  156. */
  157. var getCurrentPosition = function (success, error) {
  158. var options = {
  159. timeout: 10000,
  160. enableHighAccuracy: false
  161. }
  162. $cordovaToast.showShortBottom('Obteniendo localización');
  163. $cordovaVibration.vibrate(vibrateDuration);
  164. $cordovaGeolocation.getCurrentPosition(options).then(function (position) {
  165. $cordovaVibration.vibrate(vibrateDuration);
  166. $cordovaToast.showShortBottom('Localización obtenida con éxito');
  167. success(position);
  168. }, function (err) {
  169. $cordovaVibration.vibrate(vibrateDuration);
  170. $cordovaToast.showLongBottom('No se pudo obtener la localización, revise si su GPS está activo');
  171. error(err);
  172. });
  173. }
  174. /**
  175. *
  176. */
  177. var detectShake = function () {
  178. var previousMeasurements = {};
  179. var options = {
  180. frequency: 100,
  181. deviation: 25
  182. }
  183. var watcher = null;
  184. var startWatching = function () {
  185. watcher = $cordovaDeviceMotion.watchAcceleration(options);
  186. watcher.then(null, function (err) {
  187. console.log(err);
  188. }, function (currentMeasurements) {
  189. evaluateShake(currentMeasurements);
  190. });
  191. }
  192. var evaluateShake = function (currentMeasurements) {
  193. var measurements = {};
  194. if (previousMeasurements.x) {
  195. measurements.x = Math.abs(previousMeasurements.x, currentMeasurements.x);
  196. measurements.y = Math.abs(previousMeasurements.y, currentMeasurements.y);
  197. measurements.z = Math.abs(previousMeasurements.x, currentMeasurements.z);
  198. }
  199. if (measurements.x + measurements.y + measurements.z > options.deviation) {
  200. stopWatching();
  201. previousMeasurements = {};
  202. } else {
  203. previousMeasurements = currentMeasurements;
  204. }
  205. }
  206. var stopWatching = function () {
  207. watcher.clearWatch();
  208. $timeout(function () {
  209. startWatching();
  210. }, 1500);
  211. $rootScope.$broadcast('device.shaked');
  212. }
  213. startWatching();
  214. }
  215. return {
  216. getCurrentPosition: getCurrentPosition,
  217. detectShake: detectShake
  218. }
  219. })
  220. /**
  221. * -----------------------------------------------------------------------------
  222. * Description: Native contacts manager
  223. * -----------------------------------------------------------------------------
  224. */
  225. .factory('contactFactory', function ($cordovaContacts) {
  226. // Save customer information to device contacts
  227. var save = function (customer, success, error) {
  228. if (!customer.mobile && !customer.phone && !customer.email) {
  229. error();
  230. return;
  231. }
  232. var contact = {
  233. name: {
  234. givenName: customer.name,
  235. familyName: '',
  236. formatted: ''
  237. },
  238. nickname: '',
  239. phoneNumbers: [
  240. {
  241. value: customer.phone,
  242. type: 'phone'
  243. },
  244. {
  245. value: customer.mobile,
  246. type: 'mobile'
  247. }
  248. ],
  249. emails: [
  250. {
  251. value: customer.email,
  252. type: 'home'
  253. }
  254. ],
  255. addresses: [
  256. {
  257. type: 'home',
  258. formatted: '',
  259. streetAddress: customer.street,
  260. locality: customer.city,
  261. region: '',
  262. postalCode: '',
  263. country: 'Paraguay'
  264. }
  265. ],
  266. ims: null,
  267. organizations: null,
  268. birthday: null,
  269. note: null,
  270. photos: null,
  271. categories: null,
  272. urls: null
  273. };
  274. $cordovaContacts.save(contact).then(function (result) {
  275. success(result);
  276. }, function (err) {
  277. error(err);
  278. });
  279. };
  280. return {
  281. save: save
  282. }
  283. });