-
-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
191 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,88 @@ | ||
import ReactAsyncStorage from '@react-native-async-storage/async-storage' | ||
import Toast from 'react-native-root-toast' | ||
import i18n from '../lang/i18n' | ||
|
||
export const error = (err: unknown) => { | ||
if (err) { | ||
console.log(err) | ||
} | ||
Toast.show(i18n.t('GENERIC_ERROR'), { | ||
duration: Toast.durations.LONG, | ||
}) | ||
} | ||
import * as ToastHelper from './ToastHelper' | ||
|
||
/** | ||
* Store a string in async-storage. | ||
* | ||
* @async | ||
* @param {string} key | ||
* @param {string} value | ||
* @returns {void} | ||
*/ | ||
export const storeString = async (key: string, value: string) => { | ||
try { | ||
await ReactAsyncStorage.setItem(key, value) | ||
} catch (err) { | ||
error(err) | ||
ToastHelper.error(err) | ||
} | ||
} | ||
|
||
/** | ||
* Get a string by key from async-storage. | ||
* | ||
* @async | ||
* @param {string} key | ||
* @returns {unknown} | ||
*/ | ||
export const getString = async (key: string) => { | ||
try { | ||
const value = await ReactAsyncStorage.getItem(key) | ||
return value | ||
} catch (err) { | ||
error(err) | ||
ToastHelper.error(err) | ||
} | ||
} | ||
|
||
/** | ||
* Store an object in async-storage. | ||
* | ||
* @export | ||
* @async | ||
* @template T | ||
* @param {string} key | ||
* @param {T} value | ||
* @returns {void} | ||
*/ | ||
export async function storeObject<T>(key: string, value: T) { | ||
try { | ||
const jsonValue = JSON.stringify(value) | ||
await ReactAsyncStorage.setItem(key, jsonValue) | ||
} catch (err) { | ||
error(err) | ||
ToastHelper.error(err) | ||
} | ||
} | ||
|
||
/** | ||
* Get an object by key from async-storage. | ||
* | ||
* @export | ||
* @async | ||
* @template T | ||
* @param {string} key | ||
* @returns {T|null} | ||
*/ | ||
export async function getObject<T>(key: string) { | ||
try { | ||
const value = await ReactAsyncStorage.getItem(key) | ||
const jsonValue = value != null ? JSON.parse(value) as T : null | ||
return jsonValue | ||
} catch (err) { | ||
error(err) | ||
ToastHelper.error(err) | ||
return null | ||
} | ||
} | ||
|
||
/** | ||
* Remove an item by key from async-storage. | ||
* | ||
* @async | ||
* @param {string} key | ||
* @returns {void} | ||
*/ | ||
export const removeItem = async (key: string) => { | ||
try { | ||
await ReactAsyncStorage.removeItem(key) | ||
} catch (err) { | ||
error(err) | ||
ToastHelper.error(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters