|
1 | | -import { Camera, CameraResultType, CameraSource } from "@capacitor/camera"; |
2 | 1 | import { angular } from "@angular-wave/angular.ts"; |
3 | | - |
4 | | -/** |
5 | | - * @typedef {Object} UserPhoto |
6 | | - * @property {string} filepath - The file path to the user's photo. |
7 | | - * @property {string} base64Data - Base64 representation of the photo. |
8 | | - */ |
| 2 | +import { PhotoController } from "./photo-controller.js"; |
| 3 | +import { defineCustomElements } from "@ionic/pwa-elements/loader"; |
| 4 | +defineCustomElements(window); |
9 | 5 |
|
10 | 6 | angular |
11 | 7 | .module("app", []) |
12 | 8 | .config(($sceProvider) => { |
13 | 9 | $sceProvider.enabled(false); |
14 | 10 | }) |
15 | | - .controller( |
16 | | - "PhotoController", |
17 | | - class PhotoController { |
18 | | - static $inject = ["$scope"]; |
19 | | - |
20 | | - constructor($scope) { |
21 | | - /** @type {UserPhoto[]} */ |
22 | | - this.photos = []; |
23 | | - this.$scope = $scope; |
24 | | - } |
25 | | - |
26 | | - async addPhotoToGallery() { |
27 | | - const capturedPhoto = await Camera.getPhoto({ |
28 | | - resultType: CameraResultType.Uri, |
29 | | - source: CameraSource.Camera, |
30 | | - quality: 100, |
31 | | - }); |
32 | | - const savedImageFile = await this.savePicture(capturedPhoto); |
33 | | - this.photos.unshift(savedImageFile); |
34 | | - this.$scope.$digest(); |
35 | | - } |
36 | | - |
37 | | - /** |
38 | | - * @param {import('@capacitor/camera').Photo} photo |
39 | | - * @returns {Promise<UserPhoto>} |
40 | | - */ |
41 | | - async savePicture(photo) { |
42 | | - // Convert photo to base64 format, required by Filesystem API to save |
43 | | - const base64Data = await this.readAsBase64(photo); |
44 | | - const fileName = Date.now() + ".jpeg"; |
45 | | - |
46 | | - // Use webPath to display the new image instead of base64 since it's |
47 | | - // already loaded into memory |
48 | | - return { |
49 | | - filepath: fileName, |
50 | | - base64Data: base64Data, |
51 | | - }; |
52 | | - } |
53 | | - |
54 | | - /** |
55 | | - * Convert a Blob to a base64 string. |
56 | | - *@param {import('@capacitor/camera').Photo} photo |
57 | | - * @returns {string} |
58 | | - */ |
59 | | - async readAsBase64(photo) { |
60 | | - // Fetch the photo, read as a blob, then convert to base64 format |
61 | | - const response = await fetch(photo.webPath); |
62 | | - const blob = await response.blob(); |
63 | | - |
64 | | - return await this.convertBlobToBase64(blob); |
65 | | - } |
66 | | - |
67 | | - /** |
68 | | - * @private |
69 | | - * @param {Blob} blob |
70 | | - * @returns {Promise<string>} |
71 | | - */ |
72 | | - convertBlobToBase64(blob) { |
73 | | - return new Promise((resolve, reject) => { |
74 | | - const reader = new FileReader(); |
75 | | - reader.onerror = reject; |
76 | | - reader.onload = () => { |
77 | | - resolve(reader.result); |
78 | | - }; |
79 | | - reader.readAsDataURL(blob); |
80 | | - }); |
81 | | - } |
82 | | - }, |
83 | | - ); |
| 11 | + .controller("PhotoController", PhotoController); |
0 commit comments