Skip to content

Commit 5fd7ce4

Browse files
authored
docs(your-first-app): Replace Storage plugin with Preferences (#2499)
1 parent d415947 commit 5fd7ce4

30 files changed

+90
-90
lines changed

docs/angular/your-first-app.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Highlights include:
3636

3737
- One Angular-based codebase that runs on the web, iOS, and Android using Ionic Framework [UI components](https://ionicframework.com/docs/components).
3838
- Deployed as a native iOS and Android mobile app using [Capacitor](https://capacitor.ionicframework.com), Ionic's official native app runtime.
39-
- Photo Gallery functionality powered by the Capacitor [Camera](https://capacitor.ionicframework.com/docs/apis/camera), [Filesystem](https://capacitor.ionicframework.com/docs/apis/filesystem), and [Storage](https://capacitor.ionicframework.com/docs/apis/storage) APIs.
39+
- Photo Gallery functionality powered by the Capacitor [Camera](https://capacitor.ionicframework.com/docs/apis/camera), [Filesystem](https://capacitor.ionicframework.com/docs/apis/filesystem), and [Preferences](https://capacitor.ionicframework.com/docs/apis/preferences) APIs.
4040

4141
Find the complete app code referenced in this guide [on GitHub](https://github.com/ionic-team/photo-gallery-capacitor-ng).
4242

@@ -88,7 +88,7 @@ cd photo-gallery
8888
Next we'll need to install the necessary Capacitor plugins to make the app's native functionality work:
8989

9090
```shell
91-
npm install @capacitor/camera @capacitor/storage @capacitor/filesystem
91+
npm install @capacitor/camera @capacitor/preferences @capacitor/filesystem
9292
```
9393

9494
### PWA Elements

docs/angular/your-first-app/2-taking-photos.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Open the new `services/photo.service.ts` file, and let’s add the logic that wi
2626
```tsx
2727
import { Camera, CameraResultType, CameraSource, Photo } from '@capacitor/camera';
2828
import { Filesystem, Directory } from '@capacitor/filesystem';
29-
import { Storage } from '@capacitor/storage';
29+
import { Preferences } from '@capacitor/preferences';
3030
```
3131

3232
Next, define a new class method, `addNewToGallery`, that will contain the core logic to take a device photo and save it to the filesystem. Let’s start by opening the device camera:

docs/angular/your-first-app/4-loading-photos.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ sidebar_label: Loading Photos
66

77
We’ve implemented photo taking and saving to the filesystem. There’s one last piece of functionality missing: the photos are stored in the filesystem, but we need a way to save pointers to each file so that they can be displayed again in the photo gallery.
88

9-
Fortunately, this is easy: we’ll leverage the Capacitor [Storage API](https://capacitor.ionicframework.com/docs/apis/storage) to store our array of Photos in a key-value store.
9+
Fortunately, this is easy: we’ll leverage the Capacitor [Preferences API](https://capacitor.ionicframework.com/docs/apis/preferences) to store our array of Photos in a key-value store.
1010

11-
## Storage API
11+
## Preferences API
1212

1313
Begin by defining a constant variable that will act as the key for the store:
1414

@@ -21,10 +21,10 @@ export class PhotoService {
2121
}
2222
```
2323

24-
Next, at the end of the `addNewToGallery` function, add a call to `Storage.set()` to save the Photos array. By adding it here, the Photos array is stored each time a new photo is taken. This way, it doesn’t matter when the app user closes or switches to a different app - all photo data is saved.
24+
Next, at the end of the `addNewToGallery` function, add a call to `Preferences.set()` to save the Photos array. By adding it here, the Photos array is stored each time a new photo is taken. This way, it doesn’t matter when the app user closes or switches to a different app - all photo data is saved.
2525

2626
```tsx
27-
Storage.set({
27+
Preferences.set({
2828
key: this.PHOTO_STORAGE,
2929
value: JSON.stringify(this.photos),
3030
});
@@ -35,7 +35,7 @@ With the photo array data saved, create a function called `loadSaved()` that can
3535
```tsx
3636
public async loadSaved() {
3737
// Retrieve cached photo array data
38-
const photoList = await Storage.get({ key: this.PHOTO_STORAGE });
38+
const photoList = await Preferences.get({ key: this.PHOTO_STORAGE });
3939
this.photos = JSON.parse(photoList.value) || [];
4040

4141
// more to come...

docs/angular/your-first-app/5-adding-mobile.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ Next, head back over to the `loadSaved()` function we implemented for the web ea
9393
```tsx
9494
public async loadSaved() {
9595
// Retrieve cached photo array data
96-
const photoList = await Storage.get({ key: this.PHOTO_STORAGE });
96+
const photoList = await Preferences.get({ key: this.PHOTO_STORAGE });
9797
this.photos = JSON.parse(photoList.value) || [];
9898

9999
// Easiest way to detect when running on the web:

docs/angular/your-first-app/7-live-reload.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public async deletePicture(photo: UserPhoto, position: number) {
9191
this.photos.splice(position, 1);
9292

9393
// Update photos array cache by overwriting the existing photo array
94-
Storage.set({
94+
Preferences.set({
9595
key: this.PHOTO_STORAGE,
9696
value: JSON.stringify(this.photos)
9797
});
@@ -107,7 +107,7 @@ public async deletePicture(photo: UserPhoto, position: number) {
107107
}
108108
```
109109

110-
The selected photo is removed from the Photos array first. Then, we use the Capacitor Storage API to update the cached version of the Photos array. Finally, we delete the actual photo file itself using the Filesystem API.
110+
The selected photo is removed from the Photos array first. Then, we use the Capacitor Preferences API to update the cached version of the Photos array. Finally, we delete the actual photo file itself using the Filesystem API.
111111

112112
Save this file, then tap on a photo again and choose the “Delete” option. This time, the photo is deleted! Implemented much faster using Live Reload. 💪
113113

docs/react/your-first-app.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Highlights include:
3232

3333
- One React-based codebase that runs on the web, iOS, and Android using Ionic Framework [UI components](https://ionicframework.com/docs/components).
3434
- Deployed as a native iOS and Android mobile app using [Capacitor](https://capacitor.ionicframework.com), Ionic's official native app runtime.
35-
- Photo Gallery functionality powered by the Capacitor [Camera](https://capacitor.ionicframework.com/docs/apis/camera), [Filesystem](https://capacitor.ionicframework.com/docs/apis/filesystem), and [Storage](https://capacitor.ionicframework.com/docs/apis/storage) APIs.
35+
- Photo Gallery functionality powered by the Capacitor [Camera](https://capacitor.ionicframework.com/docs/apis/camera), [Filesystem](https://capacitor.ionicframework.com/docs/apis/filesystem), and [Preferences](https://capacitor.ionicframework.com/docs/apis/preferences) APIs.
3636

3737
Find the complete app code referenced in this guide [on GitHub](https://github.com/ionic-team/photo-gallery-capacitor-react).
3838

@@ -84,7 +84,7 @@ cd photo-gallery
8484
Next we'll need to install the necessary Capacitor plugins to make the app's native functionality work:
8585

8686
```shell
87-
npm install @capacitor/camera @capacitor/storage @capacitor/filesystem
87+
npm install @capacitor/camera @capacitor/preferences @capacitor/filesystem
8888
```
8989

9090
### PWA Elements

docs/react/your-first-app/2-taking-photos.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import { isPlatform } from '@ionic/react';
2929

3030
import { Camera, CameraResultType, CameraSource, Photo } from '@capacitor/camera';
3131
import { Filesystem, Directory } from '@capacitor/filesystem';
32-
import { Storage } from '@capacitor/storage';
32+
import { Preferences } from '@capacitor/preferences';
3333
import { Capacitor } from '@capacitor/core';
3434
```
3535

docs/react/your-first-app/4-loading-photos.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ sidebar_label: Loading Photos
77
<title>Loading Photos from the Filesystem Using A Key-Value Store</title>
88
<meta
99
name="description"
10-
content="We’ve implemented photo taking and saving to the filesystem, now learn how Ionic leverages Capacitor Storage API for loading our photos in a key-value store."
10+
content="We’ve implemented photo taking and saving to the filesystem, now learn how Ionic leverages Capacitor Preferences API for loading our photos in a key-value store."
1111
/>
1212
</head>
1313

1414
We’ve implemented photo taking and saving to the filesystem. There’s one last piece of functionality missing: the photos are stored in the filesystem, but we need a way to save pointers to each file so that they can be displayed again in the photo gallery.
1515

16-
Fortunately, this is easy: we’ll leverage the Capacitor [Storage API](https://capacitor.ionicframework.com/docs/apis/storage) to store our array of Photos in a key-value store.
16+
Fortunately, this is easy: we’ll leverage the Capacitor [Preferences API](https://capacitor.ionicframework.com/docs/apis/preferences) to store our array of Photos in a key-value store.
1717

18-
## Storage API
18+
## Preferences API
1919

2020
Begin by defining a constant variable that will act as the key for the store before the `usePhotoGallery` function definition in `src/hooks/usePhotoGallery.ts`:
2121

@@ -26,29 +26,29 @@ export function usePhotoGallery() {}
2626

2727
Then, use the `Storage` class to get access to the get and set methods for reading and writing to device storage:
2828

29-
At the end of the `takePhoto` function, add a call to `Storage.set()` to save the Photos array. By adding it here, the Photos array is stored each time a new photo is taken. This way, it doesn’t matter when the app user closes or switches to a different app - all photo data is saved.
29+
At the end of the `takePhoto` function, add a call to `Preferences.set()` to save the Photos array. By adding it here, the Photos array is stored each time a new photo is taken. This way, it doesn’t matter when the app user closes or switches to a different app - all photo data is saved.
3030

3131
```tsx
32-
Storage.set({ key: PHOTO_STORAGE, value: JSON.stringify(newPhotos) });
32+
Preferences.set({ key: PHOTO_STORAGE, value: JSON.stringify(newPhotos) });
3333
```
3434

3535
With the photo array data saved, we will create a method that will retrieve the data when the hook loads. We will do so by using React's `useEffect` hook. Insert this above the `takePhoto` declaration. Here is the code, and we will break it down:
3636

3737
```tsx
3838
useEffect(() => {
3939
const loadSaved = async () => {
40-
const { value } = await Storage.get({ key: PHOTO_STORAGE });
41-
const photosInStorage = (value ? JSON.parse(value) : []) as UserPhoto[];
40+
const { value } = await Preferences.get({ key: PHOTO_STORAGE });
41+
const photosInPreferences = (value ? JSON.parse(value) : []) as UserPhoto[];
4242

43-
for (let photo of photosInStorage) {
43+
for (let photo of photosInPreferences) {
4444
const file = await Filesystem.readFile({
4545
path: photo.filepath,
4646
directory: Directory.Data,
4747
});
4848
// Web platform only: Load the photo as base64 data
4949
photo.webviewPath = `data:image/jpeg;base64,${file.data}`;
5050
}
51-
setPhotos(photosInStorage);
51+
setPhotos(photosInPreferences);
5252
};
5353
loadSaved();
5454
}, []);

docs/react/your-first-app/5-adding-mobile.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ Next, add a new bit of logic in the `loadSaved` function. On mobile, we can dire
5252

5353
```tsx
5454
const loadSaved = async () => {
55-
const { value } = await Storage.get({ key: PHOTO_STORAGE });
55+
const { value } = await Preferences.get({ key: PHOTO_STORAGE });
5656

57-
const photosInStorage = (value ? JSON.parse(value) : []) as UserPhoto[];
57+
const photosInPreferences = (value ? JSON.parse(value) : []) as UserPhoto[];
5858
// If running on the web...
5959
if (!isPlatform('hybrid')) {
60-
for (let photo of photosInStorage) {
60+
for (let photo of photosInPreferences) {
6161
const file = await Filesystem.readFile({
6262
path: photo.filepath,
6363
directory: Directory.Data,
@@ -66,7 +66,7 @@ const loadSaved = async () => {
6666
photo.webviewPath = `data:image/jpeg;base64,${file.data}`;
6767
}
6868
}
69-
setPhotos(photosInStorage);
69+
setPhotos(photosInPreferences);
7070
};
7171
```
7272

docs/react/your-first-app/7-live-reload.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ const deletePhoto = async (photo: UserPhoto) => {
9191
const newPhotos = photos.filter((p) => p.filepath !== photo.filepath);
9292

9393
// Update photos array cache by overwriting the existing photo array
94-
Storage.set({ key: PHOTO_STORAGE, value: JSON.stringify(newPhotos) });
94+
Preferences.set({ key: PHOTO_STORAGE, value: JSON.stringify(newPhotos) });
9595

9696
// delete photo file from filesystem
9797
const filename = photo.filepath.substr(photo.filepath.lastIndexOf('/') + 1);
@@ -103,7 +103,7 @@ const deletePhoto = async (photo: UserPhoto) => {
103103
};
104104
```
105105

106-
The selected photo is removed from the Photos array first. Then, we use the Capacitor Storage API to update the cached version of the Photos array. Finally, we delete the actual photo file itself using the Filesystem API.
106+
The selected photo is removed from the Photos array first. Then, we use the Capacitor Preferences API to update the cached version of the Photos array. Finally, we delete the actual photo file itself using the Filesystem API.
107107

108108
Make sure to return the `deletePhoto` function so it is as a part of the hook API that we expose:
109109

0 commit comments

Comments
 (0)