2023-11-11 18:17:20 +01:00
|
|
|
import React, { useContext, useEffect } from 'react';
|
2019-11-03 01:25:55 +01:00
|
|
|
import QuickActions from 'react-native-quick-actions';
|
2023-11-11 17:56:54 +01:00
|
|
|
import { DeviceEventEmitter, Linking, 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 { BlueStorageContext } from '../blue_modules/storage-context';
|
2023-11-11 17:56:54 +01:00
|
|
|
import DeeplinkSchemaMatch from './deeplink-schema-match';
|
|
|
|
import OnAppLaunch from './on-app-launch';
|
|
|
|
import * as NavigationService from '../NavigationService';
|
|
|
|
import { CommonActions } from '@react-navigation/native';
|
2023-11-11 18:17:20 +01:00
|
|
|
import { AbstractWallet } from './wallets/abstract-wallet';
|
2019-11-03 01:25:55 +01:00
|
|
|
|
2023-11-11 18:17:20 +01:00
|
|
|
const DeviceQuickActionsStorageKey = 'DeviceQuickActionsEnabled';
|
|
|
|
|
|
|
|
function DeviceQuickActions(): JSX.Element | null {
|
2023-11-11 17:56:54 +01:00
|
|
|
const { wallets, walletsInitialized, isStorageEncrypted, preferredFiatCurrency, addWallet, saveToDisk, setSharedCosigner } =
|
|
|
|
useContext(BlueStorageContext);
|
2021-01-03 16:22:41 +01:00
|
|
|
|
2020-10-24 19:20:59 +02:00
|
|
|
useEffect(() => {
|
|
|
|
if (walletsInitialized) {
|
2021-01-03 16:22:41 +01:00
|
|
|
isStorageEncrypted()
|
2023-11-11 18:17:20 +01:00
|
|
|
.then((value: boolean | undefined | null) => {
|
2021-01-03 16:22:41 +01:00
|
|
|
if (value) {
|
2023-10-25 00:27:17 +02:00
|
|
|
removeShortcuts();
|
2021-01-03 16:22:41 +01:00
|
|
|
} else {
|
|
|
|
setQuickActions();
|
|
|
|
}
|
|
|
|
})
|
2023-10-25 00:27:17 +02:00
|
|
|
.catch(() => removeShortcuts());
|
2020-10-24 19:20:59 +02:00
|
|
|
}
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2023-11-11 18:17:20 +01:00
|
|
|
}, [wallets, walletsInitialized, preferredFiatCurrency, isStorageEncrypted]);
|
2019-11-03 01:25:55 +01:00
|
|
|
|
2023-11-11 17:56:54 +01:00
|
|
|
useEffect(() => {
|
|
|
|
if (walletsInitialized) {
|
|
|
|
DeviceEventEmitter.addListener('quickActionShortcut', walletQuickActions);
|
2023-11-11 18:17:20 +01:00
|
|
|
popInitialShortcutAction().then(popInitialAction);
|
|
|
|
return () => DeviceEventEmitter.removeAllListeners('quickActionShortcut');
|
2023-11-11 17:56:54 +01:00
|
|
|
}
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
}, [walletsInitialized]);
|
|
|
|
|
2023-11-11 18:17:20 +01:00
|
|
|
// @ts-ignore: Fix later
|
|
|
|
DeviceQuickActions.setEnabled = async (enabled: boolean = true): Promise<void> => {
|
|
|
|
await AsyncStorage.setItem(DeviceQuickActionsStorageKey, JSON.stringify(enabled));
|
|
|
|
if (!enabled) {
|
|
|
|
removeShortcuts();
|
|
|
|
} else {
|
|
|
|
setQuickActions();
|
|
|
|
}
|
2020-10-24 19:20:59 +02:00
|
|
|
};
|
2020-10-10 21:19:42 +02:00
|
|
|
|
2023-11-11 18:17:20 +01:00
|
|
|
const popInitialShortcutAction = async (): Promise<any> => {
|
2021-05-15 18:45:02 +02:00
|
|
|
const data = await QuickActions.popInitialAction();
|
|
|
|
return data;
|
|
|
|
};
|
|
|
|
|
2023-11-11 18:17:20 +01:00
|
|
|
// @ts-ignore: Fix later
|
|
|
|
DeviceQuickActions.getEnabled = async (): Promise<boolean> => {
|
2020-10-10 21:19:42 +02:00
|
|
|
try {
|
2023-11-11 18:17:20 +01:00
|
|
|
const isEnabled = await AsyncStorage.getItem(DeviceQuickActionsStorageKey);
|
2020-10-10 21:19:42 +02:00
|
|
|
if (isEnabled === null) {
|
2023-11-11 18:17:20 +01:00
|
|
|
// @ts-ignore: Fix later
|
|
|
|
await DeviceQuickActions.setEnabled(true);
|
2020-10-10 21:19:42 +02:00
|
|
|
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
|
|
|
|
2023-11-11 18:17:20 +01:00
|
|
|
const popInitialAction = async (data: any): Promise<void> => {
|
2023-11-11 17:56:54 +01:00
|
|
|
if (data) {
|
2023-11-11 18:17:20 +01:00
|
|
|
const wallet = wallets.find((w: { getID: () => any }) => w.getID() === data.userInfo.url.split('wallet/')[1]);
|
|
|
|
if (wallet) {
|
|
|
|
NavigationService.dispatch(
|
|
|
|
CommonActions.navigate({
|
|
|
|
name: 'WalletTransactions',
|
|
|
|
key: `WalletTransactions-${wallet.getID()}`,
|
|
|
|
params: {
|
|
|
|
walletID: wallet.getID(),
|
|
|
|
walletType: wallet.type,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
2023-11-11 17:56:54 +01:00
|
|
|
} else {
|
|
|
|
const url = await Linking.getInitialURL();
|
|
|
|
if (url) {
|
|
|
|
if (DeeplinkSchemaMatch.hasSchema(url)) {
|
|
|
|
handleOpenURL({ url });
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const isViewAllWalletsEnabled = await OnAppLaunch.isViewAllWalletsEnabled();
|
|
|
|
if (!isViewAllWalletsEnabled) {
|
2023-11-11 18:17:20 +01:00
|
|
|
const selectedDefaultWallet: AbstractWallet = (await OnAppLaunch.getSelectedDefaultWallet()) as AbstractWallet;
|
|
|
|
if (selectedDefaultWallet) {
|
|
|
|
const wallet = wallets.find((w: AbstractWallet) => w.getID() === selectedDefaultWallet.getID());
|
|
|
|
if (wallet) {
|
|
|
|
NavigationService.dispatch(
|
|
|
|
CommonActions.navigate({
|
|
|
|
name: 'WalletTransactions',
|
|
|
|
key: `WalletTransactions-${wallet.getID()}`,
|
|
|
|
params: {
|
|
|
|
walletID: wallet.getID(),
|
|
|
|
walletType: wallet.type,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
2023-11-11 17:56:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-11-11 18:17:20 +01:00
|
|
|
const handleOpenURL = (event: { url: string }): void => {
|
|
|
|
DeeplinkSchemaMatch.navigationRouteFor(event, (value: any) => NavigationService.navigate(...value), {
|
2023-11-11 17:56:54 +01:00
|
|
|
wallets,
|
|
|
|
addWallet,
|
|
|
|
saveToDisk,
|
|
|
|
setSharedCosigner,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-11-11 18:17:20 +01:00
|
|
|
const walletQuickActions = (data: any): void => {
|
|
|
|
const wallet = wallets.find((w: { getID: () => any }) => w.getID() === data.userInfo.url.split('wallet/')[1]);
|
|
|
|
if (wallet) {
|
|
|
|
NavigationService.dispatch(
|
|
|
|
CommonActions.navigate({
|
|
|
|
name: 'WalletTransactions',
|
|
|
|
key: `WalletTransactions-${wallet.getID()}`,
|
|
|
|
params: {
|
|
|
|
walletID: wallet.getID(),
|
|
|
|
walletType: wallet.type,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
2023-11-11 17:56:54 +01:00
|
|
|
};
|
|
|
|
|
2023-11-11 18:17:20 +01:00
|
|
|
const removeShortcuts = async (): Promise<void> => {
|
2023-10-25 00:27:17 +02:00
|
|
|
if (Platform.OS === 'android') {
|
|
|
|
QuickActions.clearShortcutItems();
|
|
|
|
} else {
|
2023-11-11 18:17:20 +01:00
|
|
|
// @ts-ignore: Fix later
|
|
|
|
QuickActions.setShortcutItems([{ type: 'EmptyWallets', title: '' }]);
|
2023-10-25 00:27:17 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-11-11 18:17:20 +01:00
|
|
|
const setQuickActions = async (): Promise<void> => {
|
|
|
|
// @ts-ignore: Fix later
|
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' }),
|
|
|
|
});
|
|
|
|
}
|
2023-11-11 18:17:20 +01:00
|
|
|
// @ts-ignore: Fix later
|
2020-10-10 21:19:42 +02:00
|
|
|
QuickActions.setShortcutItems(shortcutItems);
|
2019-11-03 01:25:55 +01:00
|
|
|
}
|
2020-10-10 21:19:42 +02:00
|
|
|
});
|
|
|
|
} else {
|
2023-10-25 00:27:17 +02:00
|
|
|
removeShortcuts();
|
2020-10-10 21:19:42 +02:00
|
|
|
}
|
2020-10-24 19:20:59 +02:00
|
|
|
};
|
2019-11-03 01:25:55 +01:00
|
|
|
|
2023-11-11 18:17:20 +01:00
|
|
|
return <></>;
|
2019-11-03 01:25:55 +01:00
|
|
|
}
|
2020-10-24 19:20:59 +02:00
|
|
|
|
|
|
|
export default DeviceQuickActions;
|