Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding async storage helpers #127

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions apps/box/src/services/storage/helpers.storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import AsyncStorage from '@react-native-async-storage/async-storage';

export const clearStorage = async () => {
try {
await AsyncStorage.clear();
} catch (e) {
// TODO: add error logging
}
};

export const getStringData = async (key) => {
try {
return await AsyncStorage.getItem(key);
} catch (e) {
// error reading value
}
};
export const setStringData = async (key, value) => {
try {
await AsyncStorage.setItem(key, value);
} catch (e) {
// saving error
}
};

export const getBoolData = async (key) => {
try {
const jsonValue = await AsyncStorage.getItem(key);
return jsonValue != null ? JSON.parse(jsonValue) : null;
} catch (e) {
// error reading value
}
};
export const setBoolData = async (key, bool) => {
try {
const jsonValue = JSON.stringify(bool);
await AsyncStorage.setItem(key, jsonValue);
} catch (e) {
// saving error
}
};
export const getObjectData = async (key) => {
try {
const jsonValue = await AsyncStorage.getItem(key);
return jsonValue != null ? JSON.parse(jsonValue) : null;
} catch (e) {
// error reading value
}
};
export const setObjectData = async (key, value) => {
try {
const jsonValue = JSON.stringify(value);
await AsyncStorage.setItem(key, jsonValue);
} catch (e) {
// saving error
}
};

export const removeValue = async (key) => {
try {
await AsyncStorage.removeItem(key);
} catch (e) {
// remove error
}
};

export const getAllKeys = async () => {
try {
return await AsyncStorage.getAllKeys();
} catch (e) {
// read key error
}
};

const Storage = {
clearStorage,
getAllKeys,
getStringData,
setStringData,
getBoolData,
setBoolData,
getObjectData,
setObjectData,
removeValue,
};
export default Storage;
1 change: 1 addition & 0 deletions apps/box/src/services/storage/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './helpers.storage';