SHARE
TWEET

Untitled

a guest Oct 16th, 2019 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Component, OnInit } from '@angular/core';
  2. import { Time } from '@angular/common';
  3. import { ReportService } from 'src/app/services/report.service';
  4. import { NativeGeocoder, NativeGeocoderOptions, NativeGeocoderResult } from '@ionic-native/native-geocoder/ngx';
  5. import { Geolocation } from '@ionic-native/geolocation/ngx';
  6. import { ActionSheetController } from '@ionic/angular';
  7. import { Camera, CameraOptions } from '@ionic-native/camera/ngx';
  8. import { Crop } from '@ionic-native/crop/ngx';
  9. import { File } from '@ionic-native/file/ngx';
  10. import { AngularFireStorage, AngularFireUploadTask } from '@angular/fire/storage';
  11. import { AngularFirestore, AngularFirestoreCollection } from '@angular/fire/firestore';
  12.  
  13. @Component({
  14.   selector: 'app-tab-report',
  15.   templateUrl: './tab-report.page.html',
  16.   styleUrls: ['./tab-report.page.scss'],
  17. })
  18.  
  19. export class TabReportPage implements OnInit {
  20.  
  21.   reports: any;
  22.   repID: number;
  23.   repCategory: string;
  24.   repLocation: number;
  25.   repDate: Date;
  26.   repTime: Time;
  27.   repDesc: string;
  28.  
  29.   geoLatitude: number;
  30.   geoLongitude: number;
  31.   geoAccuracy: number;
  32.   geoAddress: string;
  33.  
  34.   actionSheet: any;
  35.  
  36.   // Geocoder configuration
  37.   geoencoderOptions: NativeGeocoderOptions = {
  38.     useLocale: true,
  39.     maxResults: 5
  40.   };
  41.  
  42.   croppedImagepath: any;
  43.   isLoading = false;
  44.  
  45.   imagePickerOptions = {
  46.     maximumImagesCount: 1,
  47.     quality: 50
  48.   };
  49.  
  50.   image: any;
  51.  
  52.   fileName: string;
  53.   fileSize: number;
  54.  
  55.   isUploaded: boolean;
  56.  
  57.  
  58.   constructor(private reportService: ReportService, private geolocation: Geolocation,
  59.     private nativeGeocoder: NativeGeocoder,
  60.     public actionSheetController: ActionSheetController,
  61.     private crop: Crop, private camera: Camera, private file: File,
  62.     private storage: AngularFireStorage) {
  63.      }
  64.  
  65.   ngOnInit() {
  66.  
  67.   }
  68.  
  69.   createReport() {
  70.  
  71.  
  72.       this.file.readAsArrayBuffer(this.image.path, this.image.name).then(data => {
  73.       this.isUploaded = false;
  74.  
  75.       this.fileName = this.image.name;
  76.  
  77.       // The storage path
  78.       const path = `images/${new Date().getTime()}_${this.fileName}`;
  79.  
  80.       // Totally optional metadata
  81.       const customMetadata = { app: 'Report Images' };
  82.  
  83.       // File reference
  84.       const fileRef = this.storage.ref(path);
  85.  
  86.       var blob = new Blob([data], {
  87.         type: "image/jpeg"
  88.       });
  89.  
  90.       fileRef.put(blob);
  91.  
  92.       console.log({
  93.         'image': path,
  94.         'category': this.repCategory,
  95.         'location': this.repLocation,
  96.         'date': this.repDate,
  97.         'time': this.repTime,
  98.         'desc': this.repDesc
  99.       })
  100.  
  101.       this.reportService.createReport({
  102.         'image': path,
  103.         'category': this.repCategory,
  104.         'location': this.repLocation,
  105.         'date': this.repDate,
  106.         'time': this.repTime,
  107.         'desc': this.repDesc
  108.       }).then(resp => {
  109.         this.repCategory = '';
  110.         this.repLocation = undefined;
  111.         this.repDate = undefined;
  112.         this.repTime = undefined;
  113.         this.repDesc = '';
  114.       });
  115.     });
  116.   }
  117.  
  118.   pickImage(sourceType) {
  119.     const options: CameraOptions = {
  120.       quality: 100,
  121.       sourceType: sourceType,
  122.       destinationType: this.camera.DestinationType.FILE_URI,
  123.       encodingType: this.camera.EncodingType.JPEG,
  124.       mediaType: this.camera.MediaType.PICTURE
  125.     };
  126.     this.camera.getPicture(options).then((imageData) => {
  127.       this.cropImage(imageData);
  128.     }, (err) => {
  129.       // Handle error
  130.     });
  131.   }
  132.  
  133.   cropImage(fileUrl) {
  134.     this.crop.crop(fileUrl, { quality: 50 })
  135.       .then(
  136.         newPath => {
  137.           this.showCroppedImage(newPath.split('?')[0]);
  138.         },
  139.         error => {
  140.           alert('Error cropping image' + error);
  141.         }
  142.       );
  143.   }
  144.  
  145.   showCroppedImage(ImagePath) {
  146.     this.isLoading = true;
  147.     var copyPath = ImagePath;
  148.     var splitPath = copyPath.split('/');
  149.     var imageName = splitPath[splitPath.length - 1];
  150.     var filePath = ImagePath.split(imageName)[0];
  151.  
  152.     this.image = {
  153.       'name': imageName,
  154.       'path': filePath
  155.     }
  156.  
  157.     this.file.
  158.     readAsDataURL(filePath, imageName).then(base64 => {
  159.       this.croppedImagepath = base64;
  160.       this.isLoading = false;
  161.     }, error => {
  162.       alert('Error in showing image' + error);
  163.       this.isLoading = false;
  164.     });
  165.     console.log(ImagePath);
  166.   }
  167.  
  168.   // Get current coordinates of device
  169.   getGeolocation() {
  170.     this.geolocation.getCurrentPosition().then((resp) => {
  171.       this.repLocation = resp.coords.latitude;
  172.       this.geoLatitude = resp.coords.latitude;
  173.       this.geoLongitude = resp.coords.longitude;
  174.       this.geoAccuracy = resp.coords.accuracy;
  175.       console.log(resp);
  176.       this.getGeoencoder(this.geoLatitude, this.geoLongitude);
  177.      }).catch((error) => {
  178.        alert('Error getting location' + JSON.stringify(error));
  179.      });
  180.   }
  181.  
  182.   // geocoder method to fetch address from coordinates passed as arguments
  183.   getGeoencoder(latitude, longitude) {
  184.     this.nativeGeocoder.reverseGeocode(latitude, longitude, this.geoencoderOptions)
  185.     .then((result: NativeGeocoderResult[]) => {
  186.       this.geoAddress = this.generateAddress(result[0]);
  187.       alert(this.geoAddress);
  188.     })
  189.     .catch((error: any) => {
  190.       alert('Error getting location' + JSON.stringify(error));
  191.     });
  192.   }
  193.  
  194.   // Return Comma saperated address
  195.   generateAddress(addressObj) {
  196.       let obj = [];
  197.       let address = '';
  198.       for (let key in addressObj) {
  199.         obj.push(addressObj[key]);
  200.       }
  201.       obj.reverse();
  202.       for (let val in obj) {
  203.         if (obj[val].length){
  204.           address += obj[val] + ' , ';
  205.         }
  206.       }
  207.       return address.slice(0, -2);
  208.   }
  209.  
  210.   presentActionSheet() {
  211.  
  212.     const actionSheet = document.createElement('ion-action-sheet');
  213.  
  214.     // actionSheet.header = "Albums";
  215.     actionSheet.buttons = [{
  216.       text: 'Camera',
  217.       // icon: 'heart',
  218.       handler: () => {
  219.         this.pickImage(this.camera.PictureSourceType.CAMERA);
  220.       }
  221.     }, {
  222.       text: 'Album',
  223.       // icon: 'share',
  224.       handler: () => {
  225.         this.pickImage(this.camera.PictureSourceType.PHOTOLIBRARY);
  226.       }
  227.     }, {
  228.       text: 'Delete',
  229.       role: 'destructive',
  230.       // icon: 'trash',
  231.       handler: () => {
  232.         console.log('Delete clicked');
  233.       }
  234.     }, {
  235.       text: 'Cancel',
  236.       // icon: 'close',
  237.       role: 'cancel',
  238.       handler: () => {
  239.         console.log('Cancel clicked');
  240.       }
  241.     }];
  242.     document.body.appendChild(actionSheet);
  243.     return actionSheet.present();
  244.   }
  245.  
  246. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top