utils.factory.js 10 KB

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