Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.68 KB | None | 0 0
  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. 'address': this.geoAddress,
  97. 'date': this.repDate,
  98. 'time': this.repTime,
  99. 'desc': this.repDesc
  100. })
  101.  
  102. this.reportService.createReport({
  103. 'image': path,
  104. 'category': this.repCategory,
  105. 'location': this.repLocation,
  106. 'address': this.geoAddress,
  107. 'date': this.repDate,
  108. 'time': this.repTime,
  109. 'desc': this.repDesc
  110. }).then(resp => {
  111. this.repCategory = '';
  112. this.croppedImagepath = null;
  113. this.repLocation = undefined;
  114. this.geoAddress = undefined;
  115. this.repDate = undefined;
  116. this.repTime = undefined;
  117. this.repDesc = '';
  118. });
  119. });
  120. }
  121.  
  122. pickImage(sourceType) {
  123. const options: CameraOptions = {
  124. quality: 100,
  125. sourceType: sourceType,
  126. destinationType: this.camera.DestinationType.FILE_URI,
  127. encodingType: this.camera.EncodingType.JPEG,
  128. mediaType: this.camera.MediaType.PICTURE
  129. };
  130. this.camera.getPicture(options).then((imageData) => {
  131. this.cropImage(imageData);
  132. }, (err) => {
  133. // Handle error
  134. });
  135. }
  136.  
  137. cropImage(fileUrl) {
  138. this.crop.crop(fileUrl, { quality: 50 })
  139. .then(
  140. newPath => {
  141. this.showCroppedImage(newPath.split('?')[0]);
  142. },
  143. error => {
  144. alert('Error cropping image' + error);
  145. }
  146. );
  147. }
  148.  
  149. showCroppedImage(ImagePath) {
  150. this.isLoading = true;
  151. var copyPath = ImagePath;
  152. var splitPath = copyPath.split('/');
  153. var imageName = splitPath[splitPath.length - 1];
  154. var filePath = ImagePath.split(imageName)[0];
  155.  
  156. this.image = {
  157. 'name': imageName,
  158. 'path': filePath
  159. }
  160.  
  161. this.file.
  162. readAsDataURL(filePath, imageName).then(base64 => {
  163. this.croppedImagepath = base64;
  164. this.isLoading = false;
  165. }, error => {
  166. alert('Error in showing image' + error);
  167. this.isLoading = false;
  168. });
  169. console.log(ImagePath);
  170. }
  171.  
  172. // Get current coordinates of device
  173. getGeolocation() {
  174. this.geolocation.getCurrentPosition().then((resp) => {
  175. this.repLocation = `${resp.coords.latitude},${resp.coords.longitude}`;
  176. this.geoLatitude = resp.coords.latitude;
  177. this.geoLongitude = resp.coords.longitude;
  178. this.geoAccuracy = resp.coords.accuracy;
  179. console.log(resp);
  180. this.getGeoencoder(this.geoLatitude, this.geoLongitude);
  181. }).catch((error) => {
  182. alert('Error getting location' + JSON.stringify(error));
  183. });
  184. }
  185.  
  186. // geocoder method to fetch address from coordinates passed as arguments
  187. getGeoencoder(latitude, longitude) {
  188. this.nativeGeocoder.reverseGeocode(latitude, longitude, this.geoencoderOptions)
  189. .then((result: NativeGeocoderResult[]) => {
  190. this.geoAddress = this.generateAddress(result[0]);
  191. alert(this.geoAddress);
  192. })
  193. .catch((error: any) => {
  194. alert('Error getting location' + JSON.stringify(error));
  195. });
  196. }
  197.  
  198. // Return Comma saperated address
  199. generateAddress(addressObj) {
  200. let obj = [];
  201. let address = '';
  202. for (let key in addressObj) {
  203. obj.push(addressObj[key]);
  204. }
  205. obj.reverse();
  206. for (let val in obj) {
  207. if (obj[val].length){
  208. address += obj[val] + ' , ';
  209. }
  210. }
  211. return address.slice(0, -2);
  212. }
  213.  
  214. presentActionSheet() {
  215.  
  216. const actionSheet = document.createElement('ion-action-sheet');
  217.  
  218. // actionSheet.header = "Albums";
  219. actionSheet.buttons = [{
  220. text: 'Camera',
  221. // icon: 'heart',
  222. handler: () => {
  223. this.pickImage(this.camera.PictureSourceType.CAMERA);
  224. }
  225. }, {
  226. text: 'Album',
  227. // icon: 'share',
  228. handler: () => {
  229. this.pickImage(this.camera.PictureSourceType.PHOTOLIBRARY);
  230. }
  231. }, {
  232. text: 'Delete',
  233. role: 'destructive',
  234. // icon: 'trash',
  235. handler: () => {
  236. console.log('Delete clicked');
  237. }
  238. }, {
  239. text: 'Cancel',
  240. // icon: 'close',
  241. role: 'cancel',
  242. handler: () => {
  243. console.log('Cancel clicked');
  244. }
  245. }];
  246. document.body.appendChild(actionSheet);
  247. return actionSheet.present();
  248. }
  249.  
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement