2019-11-03 01:25:55 +01:00
|
|
|
import QuickActions from 'react-native-quick-actions';
|
|
|
|
import { Platform } from 'react-native';
|
2020-07-20 15:38:46 +02:00
|
|
|
import { formatBalance } from '../loc';
|
2020-12-16 04:15:57 +01:00
|
|
|
import AsyncStorage from '@react-native-async-storage/async-storage';
|
2020-10-24 19:20:59 +02:00
|
|
|
import { useContext, useEffect } from 'react';
|
|
|
|
import { BlueStorageContext } from '../blue_modules/storage-context';
|
2019-11-03 01:25:55 +01:00
|
|
|
|
2020-10-24 19:20:59 +02:00
|
|
|
function DeviceQuickActions() {
|
|
|
|
DeviceQuickActions.STORAGE_KEY = 'DeviceQuickActionsEnabled';
|
2021-01-03 16:22:41 +01:00
|
|
|
const { wallets, walletsInitialized, isStorageEncrypted } = useContext(BlueStorageContext);
|
|
|
|
|
2020-10-24 19:20:59 +02:00
|
|
|
useEffect(() => {
|
|
|
|
if (walletsInitialized) {
|
2021-01-03 16:22:41 +01:00
|
|
|
isStorageEncrypted()
|
|
|
|
.then(value => {
|
|
|
|
if (value) {
|
|
|
|
QuickActions.clearShortcutItems();
|
|
|
|
} else {
|
|
|
|
setQuickActions();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(() => QuickActions.clearShortcutItems());
|
2020-10-24 19:20:59 +02:00
|
|
|
}
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2021-01-03 16:22:41 +01:00
|
|
|
}, [wallets, walletsInitialized]);
|
2019-11-03 01:25:55 +01:00
|
|
|
|
2020-10-24 19:20:59 +02:00
|
|
|
DeviceQuickActions.setEnabled = (enabled = true) => {
|
2020-10-10 21:19:42 +02:00
|
|
|
return AsyncStorage.setItem(DeviceQuickActions.STORAGE_KEY, JSON.stringify(enabled)).then(() => {
|
|
|
|
if (!enabled) {
|
2020-10-24 19:20:59 +02:00
|
|
|
QuickActions.clearShortcutItems();
|
2020-10-10 21:19:42 +02:00
|
|
|
} else {
|
2020-10-24 19:20:59 +02:00
|
|
|
setQuickActions();
|
2020-10-10 21:19:42 +02:00
|
|
|
}
|
|
|
|
});
|
2020-10-24 19:20:59 +02:00
|
|
|
};
|
2020-10-10 21:19:42 +02:00
|
|
|
|
2021-05-15 18:45:02 +02:00
|
|
|
DeviceQuickActions.popInitialAction = async () => {
|
|
|
|
const data = await QuickActions.popInitialAction();
|
|
|
|
return data;
|
|
|
|
};
|
|
|
|
|
2020-10-24 19:20:59 +02:00
|
|
|
DeviceQuickActions.getEnabled = async () => {
|
2020-10-10 21:19:42 +02:00
|
|
|
try {
|
|
|
|
const isEnabled = await AsyncStorage.getItem(DeviceQuickActions.STORAGE_KEY);
|
|
|
|
if (isEnabled === null) {
|
|
|
|
await DeviceQuickActions.setEnabled(JSON.stringify(true));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return !!JSON.parse(isEnabled);
|
|
|
|
} catch {
|
|
|
|
return true;
|
|
|
|
}
|
2020-10-24 19:20:59 +02:00
|
|
|
};
|
2020-10-10 21:19:42 +02:00
|
|
|
|
2020-10-24 19:20:59 +02:00
|
|
|
const setQuickActions = async () => {
|
2020-10-10 21:19:42 +02:00
|
|
|
if (await DeviceQuickActions.getEnabled()) {
|
|
|
|
QuickActions.isSupported((error, _supported) => {
|
|
|
|
if (error === null) {
|
|
|
|
const shortcutItems = [];
|
2020-10-24 19:20:59 +02:00
|
|
|
for (const wallet of wallets.slice(0, 4)) {
|
2020-10-10 21:19:42 +02:00
|
|
|
shortcutItems.push({
|
|
|
|
type: 'Wallets', // Required
|
|
|
|
title: wallet.getLabel(), // Optional, if empty, `type` will be used instead
|
|
|
|
subtitle:
|
|
|
|
wallet.hideBalance || wallet.getBalance() <= 0
|
|
|
|
? ''
|
|
|
|
: formatBalance(Number(wallet.getBalance()), wallet.getPreferredBalanceUnit(), true),
|
|
|
|
userInfo: {
|
|
|
|
url: `bluewallet://wallet/${wallet.getID()}`, // Provide any custom data like deep linking URL
|
|
|
|
},
|
|
|
|
icon: Platform.select({ android: 'quickactions', ios: 'bookmark' }),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
QuickActions.setShortcutItems(shortcutItems);
|
2019-11-03 01:25:55 +01:00
|
|
|
}
|
2020-10-10 21:19:42 +02:00
|
|
|
});
|
|
|
|
} else {
|
2020-10-24 19:20:59 +02:00
|
|
|
QuickActions.clearShortcutItems();
|
2020-10-10 21:19:42 +02:00
|
|
|
}
|
2020-10-24 19:20:59 +02:00
|
|
|
};
|
2019-11-03 01:25:55 +01:00
|
|
|
|
2020-10-24 19:20:59 +02:00
|
|
|
return null;
|
2019-11-03 01:25:55 +01:00
|
|
|
}
|
2020-10-24 19:20:59 +02:00
|
|
|
|
|
|
|
export default DeviceQuickActions;
|