BlueWallet/class/on-app-launch.ts
Marcos Rodriguez Velez 50847fc78a
REF: Quick launch
2023-11-11 13:17:20 -04:00

49 lines
1.7 KiB
TypeScript

import AsyncStorage from '@react-native-async-storage/async-storage';
import { AbstractWallet } from './wallets/abstract-wallet';
const BlueApp = require('../BlueApp');
export default class OnAppLaunch {
static STORAGE_KEY = 'ONAPP_LAUNCH_SELECTED_DEFAULT_WALLET_KEY';
static async isViewAllWalletsEnabled(): Promise<boolean> {
try {
const selectedDefaultWallet = await AsyncStorage.getItem(OnAppLaunch.STORAGE_KEY);
return selectedDefaultWallet === '' || selectedDefaultWallet === null;
} catch (_e) {
return true;
}
}
static async setViewAllWalletsEnabled(value: boolean) {
if (!value) {
const selectedDefaultWallet = await OnAppLaunch.getSelectedDefaultWallet();
if (!selectedDefaultWallet) {
const firstWallet = BlueApp.getWallets()[0];
await OnAppLaunch.setSelectedDefaultWallet(firstWallet.getID());
}
} else {
await AsyncStorage.setItem(OnAppLaunch.STORAGE_KEY, '');
}
}
static async getSelectedDefaultWallet(): Promise<AbstractWallet | boolean> {
let selectedWallet: AbstractWallet | false = false;
try {
const selectedWalletID = JSON.parse((await AsyncStorage.getItem(OnAppLaunch.STORAGE_KEY)) || 'null');
if (selectedWalletID) {
selectedWallet = BlueApp.getWallets().find((wallet: AbstractWallet) => wallet.getID() === selectedWalletID);
if (!selectedWallet) {
await AsyncStorage.setItem(OnAppLaunch.STORAGE_KEY, '');
}
}
} catch (_e) {
return false;
}
return selectedWallet;
}
static async setSelectedDefaultWallet(value: string) {
await AsyncStorage.setItem(OnAppLaunch.STORAGE_KEY, JSON.stringify(value));
}
}