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
@@ -13,57 +13,282 @@ Fortunately, this is easy: we’ll leverage the Capacitor [Preferences API](http
13
13
Begin by defining a constant variable that will act as the key for the store at the top of the `usePhotoGallery` function in `src/composables/usePhotoGallery.ts`:
Next, use the Vue [watch function](https://v3.vuejs.org/guide/composition-api-introduction.html#reacting-to-changes-with-watch) to watch the `photos` array. Whenever the array is modified (in this case, taking or deleting photos), trigger the `cachePhotos` function. Not only do we get to reuse code, but it also doesn’t matter when the app user closes or switches to a different app - photo data is always saved.
74
+
Next, use the Vue [watch function](https://vuejs.org/api/reactivity-core.html#watch) to watch the `photos` array. Whenever the array is modified (in this case, taking or deleting photos), trigger the `cachePhotos` method. Not only do we get to reuse code, but it also doesn’t matter when the app user closes or switches to a different app - photo data is always saved.
75
+
76
+
Add the call to the `watch` function above the return statement in `usePhotoGallery`:
// CHANGE: Add call to `watch` with `photos` array and `cachePhotos` method.
100
+
watch(photos, cachePhotos);
101
+
102
+
return {
103
+
photos,
104
+
takePhoto
105
+
};
106
+
};
34
107
```
35
108
36
-
Now that the photo array data is saved, create a function to retrieve the data when Tab2 loads. First, retrieve photo data from Preferences, then each photo's data into base64 format:
109
+
Now that the photo array data is saved, we need a way to retrieve the data when Tab2 loads. Create a new method in `usePhotoGallery` called `loadSaved` which will first retrieve photo data from Preferences, then convert each photo's data to base64 format:
On mobile (coming up next!), we can directly set the source of an image tag - `<img src="x" />` - to each photo file on the Filesystem, displaying them automatically. On the web, however, we must read each image from the Filesystem into base64 format, because the Filesystem API stores them in base64 within [IndexedDB](https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API) under the hood.
56
158
57
-
Finally, we need a way to call the `loadSaved`function when the Photo Gallery page is loaded. To do so, use the Vue [mounted lifecycle hook](https://v3.vuejs.org/guide/composition-api-introduction.html#lifecycle-hook-registration-inside-setup). Earlier we had already imported `onMounted`from Vue:
159
+
Finally, we need a way to call the `loadSaved`method when the Photo Gallery page is loaded. To do so, use the Vue [mounted lifecycle hook](https://vuejs.org/api/options-lifecycle.html#mounted). Above the `usePhotoGallery` return statement where we added the call to `watch` earlier, add a call to the `onMounted`function and pass in the `loadSaved` method created above:
If you're seeing broken image links or missing photos after following these steps, you may need to open your browser's dev tools and clear both [localStorage](https://developer.chrome.com/docs/devtools/storage/localstorage) and [IndexedDB](https://developer.chrome.com/docs/devtools/storage/indexeddb).
290
+
291
+
In localStorage, look for domain `http://localhost:8100` and key `CapacitorStorage.photos`. In IndexedDB, find a store called "FileStorage". Your photos will have a key like `/DATA/123456789012.jpeg`.
292
+
:::
293
+
69
294
That’s it! We’ve built a complete Photo Gallery feature in our Ionic app that works on the web. Next up, we’ll transform it into a mobile app for iOS and Android!
0 commit comments