You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/vue/your-first-app/5-adding-mobile.md
+131-4Lines changed: 131 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,16 +6,24 @@ Let’s start with making some small code changes - then our app will "just work
6
6
7
7
## Platform-specific Logic
8
8
9
-
First, we’ll update the photo saving functionality to support mobile. We'll run slightly different code depending on the platform - mobile or web. Import the `Platform` API from Ionic Vue and `Capacitor` from Capacitor's `core` package:
9
+
First, we’ll update the photo saving functionality to support mobile. We'll run slightly different code depending on the platform - mobile or web. At the top of `usePhotoGallery.ts`, import `isPlatform` from Ionic Vue and `Capacitor` from Capacitor's `core` package:
// CHANGE: Add imports from `@ionic/vue` and `@capcitor/core`.
12
17
import { isPlatform } from'@ionic/vue';
13
18
import { Capacitor } from'@capacitor/core';
19
+
20
+
// Same old code from before.
14
21
```
15
22
16
-
In the `savePicture` function, check which platform the app is running on. If it’s "hybrid" (Capacitor, the native runtime), then read the photo file into base64 format using the `readFile` method. Also, return the complete file path to the photo using the Filesystem API. When setting the `webviewPath`, use the special `Capacitor.convertFileSrc` method ([details here](https://capacitorjs.com/docs/basics/utilities#convertfilesrc)). Otherwise, use the same logic as before when running the app on the web.
23
+
Next, update the `usePhotoGallery` function's `savePicture` method to check which platform the app is running on. If it’s "hybrid" (Capacitor, the native runtime), then read the photo file into base64 format using the `readFile` method. Also, return the complete file path to the photo using the Filesystem API. When setting the `webviewPath`, use the special `Capacitor.convertFileSrc` method ([details here](https://capacitorjs.com/docs/basics/utilities#convertfilesrc)). Otherwise, use the same logic as before when running the app on the web.
17
24
18
25
```tsx
26
+
// CHANGE: Update the `savePicture` method to include branches for web and native.
Next, add a new bit of logic in the `loadSaved`function. On mobile, we can directly point to each photo file on the Filesystem and display them automatically. On the web, however, we must read each image from the Filesystem into base64 format. This is because the Filesystem API uses [IndexedDB](https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API) under the hood. Update the `loadSaved` function:
66
+
Next, add a new bit of logic in the `loadSaved`method. On mobile, we can directly point to each photo file on the Filesystem and display them automatically. On the web, however, we must read each image from the Filesystem into base64 format. This is because the Filesystem API uses [IndexedDB](https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API) under the hood. Update the `loadSaved` function:
58
67
59
68
```tsx
69
+
// CHANGE: Update the `loadSaved` method to convert to base64 on web platform only.
Our Photo Gallery now consists of one codebase that runs on the web, Android, and iOS. Next up, the part you’ve been waiting for - deploying the app to a device.
90
+
Our Photo Gallery now consists of one codebase that runs on the web, Android, and iOS.
0 commit comments