Browse Source

modularized camera and contact manager

robert2206 8 years ago
parent
commit
df6830c1be
2 changed files with 113 additions and 84 deletions
  1. 26 83
      www/js/controllers/customer.controller.js
  2. 87 1
      www/js/factories/utils.factory.js

+ 26 - 83
www/js/controllers/customer.controller.js

@@ -1,9 +1,7 @@
 angular.module(
     'odoo.customer.controller',
     [
-        'odoo.interoperability.factory',
-        'odoo.storage.factory',
-        'ngCordova'
+        'odoo.sync.factory'
     ]
 )
 
@@ -14,11 +12,11 @@ angular.module(
     $ionicModal,
     $ionicActionSheet,
     $ionicPopup,
-    $cordovaContacts,
-    $cordovaCamera,
     sync,
     odoo,
-    storage
+    storage,
+    camera,
+    contact
 ) {
 
     // Objects
@@ -44,7 +42,7 @@ angular.module(
         $scope.customer = {};
     });
 
-    // Fill view list
+    // Fill view list from data readed from local database
     $scope.fill = function () {
         $ionicLoading.show();
 
@@ -69,6 +67,7 @@ angular.module(
         });
     }
 
+    // Get filtered customers for correct data visualization
     $scope.getCustomers = function (success, error) {
         storage.getByConstraint('partner', 'customer = 1', function (customers) {
             console.log(customers);
@@ -85,6 +84,7 @@ angular.module(
         });
     }
 
+    // Show action sheet for customer options
     $scope.openOptions = function(index) {
         if (index == -1) {
             $scope.customer = {};
@@ -130,6 +130,7 @@ angular.module(
         });
     }
 
+    // Save a customer data on local database
     $scope.save = function() {
         storage.saveCustomer($scope.customer, function (customerId) {
             if (!$scope.customer.id) {
@@ -146,28 +147,7 @@ angular.module(
         });
     }
 
-    $scope.takePicture = function () {
-        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) {
-            $scope.customer.image_small = imageData;
-            $scope.customer.image_medium = imageData;
-        }, function(err) {
-            $log.error(JSON.stringify(err));
-        });
-    }
-
+    // Delete a customer from local database
     $scope.delete = function () {
         $ionicPopup.confirm({
             title: 'Confirmar',
@@ -190,64 +170,27 @@ angular.module(
         });
     }
 
+    // Show customer data in modal
     $scope.show = function () {
         $scope.customerModal.show();
     }
 
-    $scope.addContact = function () {
-        if(!$scope.customer.mobile && !$scope.customer.phone && !$scope.customer.email) {
-            $ionicPopup.alert({ title: 'No se puede guardar el contacto', template: 'El cliente no tiene registrado un teléfono, celular y email'});
-            return;
-        }
-
-        var contact = {
-            name: {
-                givenName: $scope.customer.name,
-                familyName: '',
-                formatted: ''
-            },
-            nickname: '',
-            phoneNumbers: [
-                {
-                    value: $scope.customer.phone,
-                    type: 'phone'
-                },
-                {
-                    value: $scope.customer.mobile,
-                    type: 'mobile'
-                }
-            ],
-            emails: [
-                {
-                    value: $scope.customer.email,
-                    type: 'home'
-                }
-            ],
-            addresses: [
-                {
-                    type: 'home',
-                    formatted: '',
-                    streetAddress: $scope.customer.street,
-                    locality: $scope.customer.city,
-                    region: '',
-                    postalCode: '',
-                    country: 'Paraguay'
-                }
-            ],
-            ims: null,
-            organizations: null,
-            birthday: null,
-		    note: null,
-            photos: null,
-            categories: null,
-            urls: null
-        };
-
-        $cordovaContacts.save(contact).then(function (result) {
-            $ionicPopup.alert({ title: 'Contacto creado con éxito' });
-        }, function (err) {
-            $ionicPopup.alert({ title: 'No se puedo eliminar el cliente', template: JSON.stringify(error) });
-            $log.error(JSON.stringify(err));
+    // Take a picture from camera for customer profile
+    $scope.takePicture = function () {
+        camera.takePicture(function (imageData) {
+            $scope.customer.image_small = imageData;
+            $scope.customer.image_medium = imageData;
+        }, function (error) {
+            console.log(error);
         });
     }
+
+    // Add customer data contact to device mobile contacts
+    $scope.addContact = function () {
+        contact.save($scope.customer, function (result) {
+            $ionicPopup.alert({ title: 'Contacto guardado' });
+        }, function (error) {
+            console.log(error);
+        })
+    }
 });

+ 87 - 1
www/js/factories/utils.factory.js

@@ -1,4 +1,4 @@
-angular.module('odoo.utils.factory', [])
+angular.module('odoo.utils.factory', ['ngCordova'])
 
 .factory('async', function () {
     return {
@@ -35,4 +35,90 @@ angular.module('odoo.utils.factory', [])
             return loop;
         }
     }
+})
+
+.factory('camera', function ($cordovaCamera) {
+    return {
+        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);
+            });
+        }
+    }
+})
+
+.factory('contact', function ($cordovaContacts) {
+
+    return {
+        save: function (customer, success, error) {
+            if(!customer.mobile && !customer.phone && !customer.email) {
+                error();
+                return;
+            }
+
+            var contact = {
+                name: {
+                    givenName: customer.name,
+                    familyName: '',
+                    formatted: ''
+                },
+                nickname: '',
+                phoneNumbers: [
+                    {
+                        value: customer.phone,
+                        type: 'phone'
+                    },
+                    {
+                        value: customer.mobile,
+                        type: 'mobile'
+                    }
+                ],
+                emails: [
+                    {
+                        value: customer.email,
+                        type: 'home'
+                    }
+                ],
+                addresses: [
+                    {
+                        type: 'home',
+                        formatted: '',
+                        streetAddress: customer.street,
+                        locality: customer.city,
+                        region: '',
+                        postalCode: '',
+                        country: 'Paraguay'
+                    }
+                ],
+                ims: null,
+                organizations: null,
+                birthday: null,
+                note: null,
+                photos: null,
+                categories: null,
+                urls: null
+            };
+
+            $cordovaContacts.save(contact).then(function (result) {
+                success(result);
+            }, function (err) {
+                error(err);
+            });
+        }
+    }
 });