Skip to content

Commit 03b191d

Browse files
committed
Refactor ionic app
1 parent 508de25 commit 03b191d

File tree

3 files changed

+79
-88
lines changed

3 files changed

+79
-88
lines changed

app/apps/ionic/index.js

Lines changed: 4 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,11 @@
1-
import { Camera, CameraResultType, CameraSource } from "@capacitor/camera";
21
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);
95

106
angular
117
.module("app", [])
128
.config(($sceProvider) => {
139
$sceProvider.enabled(false);
1410
})
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);

app/apps/ionic/ionic.html

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,17 @@
1212

1313
<script
1414
type="module"
15-
src="https://cdn.jsdelivr.net/npm/@ionic/core/dist/ionic/ionic.esm.js"
15+
src="https://cdn.jsdelivr.net/npm/@ionic/core@6.0.0/dist/ionic/ionic.esm.js"
1616
></script>
1717
<script
1818
nomodule
19-
src="https://cdn.jsdelivr.net/npm/@ionic/core/dist/ionic/ionic.js"
19+
src="https://cdn.jsdelivr.net/npm/@ionic/core@6.0.0/dist/ionic/ionic.js"
2020
></script>
2121
<link
2222
rel="stylesheet"
23-
href="https://cdn.jsdelivr.net/npm/@ionic/core/css/ionic.bundle.css"
23+
href="https://cdn.jsdelivr.net/npm/@ionic/core@6.0.0/css/ionic.bundle.css"
2424
/>
2525

26-
<script
27-
type="module"
28-
src="https://unpkg.com/@ionic/pwa-elements@latest/dist/ionicpwaelements/ionicpwaelements.esm.js"
29-
></script>
30-
<script
31-
nomodule
32-
src="https://unpkg.com/@ionic/pwa-elements@latest/dist/ionicpwaelements/ionicpwaelements.js"
33-
></script>
34-
3526
<link rel="icon" type="image/x-icon" href="./assets/icon/favicon.ico" />
3627
<link rel="manifest" href="./manifest.json" />
3728
<link rel="stylesheet" href="./css/style.css" />

app/apps/ionic/photo-controller.js

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

0 commit comments

Comments
 (0)