Skip to content

Commit

Permalink
more android tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
walmat committed Nov 18, 2024
1 parent 466f3dc commit 461ee79
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/handlers/cloudBackup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export async function fetchAllBackups(): Promise<CloudBackups> {
});

return {
files: files?.files?.filter((file: BackupFile) => file.name !== USERDATA_FILE) || [],
files: files?.files?.filter((file: BackupFile) => normalizeAndroidBackupFilename(file.name) !== USERDATA_FILE) || [],
};
}

Expand Down
21 changes: 19 additions & 2 deletions src/hooks/useManageCloudBackups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@ import lang from 'i18n-js';
import { useDispatch } from 'react-redux';
import { cloudPlatform } from '../utils/platform';
import { WrappedAlert as Alert } from '@/helpers/alert';
import { GoogleDriveUserData, getGoogleAccountUserData, deleteAllBackups, logoutFromGoogleDrive } from '@/handlers/cloudBackup';
import {
GoogleDriveUserData,
getGoogleAccountUserData,
deleteAllBackups,
logoutFromGoogleDrive as logout,
login,
} from '@/handlers/cloudBackup';
import { clearAllWalletsBackupStatus } from '@/redux/wallets';
import { showActionSheetWithOptions } from '@/utils';
import { IS_ANDROID } from '@/env';
import { RainbowError, logger } from '@/logger';
import * as i18n from '@/languages';
import { backupsStore } from '@/state/backups/backups';
import { backupsStore, CloudBackupState } from '@/state/backups/backups';

export default function useManageCloudBackups() {
const dispatch = useDispatch();
Expand Down Expand Up @@ -49,8 +55,19 @@ export default function useManageCloudBackups() {
await dispatch(clearAllWalletsBackupStatus());
};

const logoutFromGoogleDrive = async () => {
await logout();
backupsStore.setState({
backupProvider: undefined,
backups: { files: [] },
mostRecentBackup: undefined,
status: CloudBackupState.NotAvailable,
});
};

const loginToGoogleDrive = async () => {
try {
await login();
const accountDetails = await getGoogleAccountUserData();
backupsStore.getState().syncAndFetchBackups();
setAccountDetails(accountDetails ?? undefined);
Expand Down
4 changes: 3 additions & 1 deletion src/model/backup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
getGoogleAccountUserData,
login,
logoutFromGoogleDrive,
normalizeAndroidBackupFilename,
} from '@/handlers/cloudBackup';
import { Alert as NativeAlert } from '@/components/alerts';
import WalletBackupTypes from '../helpers/walletBackupTypes';
Expand Down Expand Up @@ -53,8 +54,9 @@ export interface BackupFile {
}

export const parseTimestampFromFilename = (filename: string) => {
const name = normalizeAndroidBackupFilename(filename);
return Number(
filename
name
.replace('.backup_', '')
.replace('backup_', '')
.replace('.json', '')
Expand Down
3 changes: 1 addition & 2 deletions src/react-native-cool-modals/Portal.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { IS_IOS } from '@/env';
import { portalStore } from '@/state/portal/portal';
import { Platform, requireNativeComponent, StyleSheet, View } from 'react-native';
import { requireNativeComponent, StyleSheet, View } from 'react-native';

const NativePortal = IS_IOS ? requireNativeComponent('WindowPortal') : View;
const Wrapper = IS_IOS ? ({ children }: { children: React.ReactNode }) => children : View;
Expand Down Expand Up @@ -35,7 +35,6 @@ export function Portal() {

const sx = StyleSheet.create({
wrapper: {
zIndex: Number.MAX_SAFE_INTEGER,
...StyleSheet.absoluteFillObject,
},
});
27 changes: 21 additions & 6 deletions src/screens/SettingsSheet/components/Backups/WalletsAndBackup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import { backupsCard } from '@/components/cards/utils/constants';
import { WalletCountPerType, useVisibleWallets } from '../../useVisibleWallets';
import { SETTINGS_BACKUP_ROUTES } from './routes';
import { RainbowAccount, createWallet } from '@/model/wallet';
import { PROFILES, useExperimentalFlag } from '@/config';
import { useDispatch } from 'react-redux';
import { setIsWalletLoading, walletsLoadState } from '@/redux/wallets';
import { RainbowError, logger } from '@/logger';
Expand Down Expand Up @@ -193,6 +192,20 @@ export const WalletsAndBackup = () => {
);

const { status: iconStatusType, text } = useMemo<{ status: StatusType; text: string }>(() => {
if (!backupProvider) {
if (!allBackedUp) {
return {
status: 'out-of-date',
text: 'Out of Date',
};
}

return {
status: 'up-to-date',
text: 'Up to date',
};
}

if (status === CloudBackupState.FailedToInitialize || status === CloudBackupState.NotAvailable) {
return {
status: 'not-enabled',
Expand All @@ -218,7 +231,7 @@ export const WalletsAndBackup = () => {
status: 'up-to-date',
text: 'Up to date',
};
}, [status, allBackedUp]);
}, [backupProvider, status, allBackedUp]);

const renderView = useCallback(() => {
switch (backupProvider) {
Expand Down Expand Up @@ -256,14 +269,15 @@ export const WalletsAndBackup = () => {
<BackUpMenuItem
title={i18n.t(i18n.l.back_up.cloud.enable_cloud_backups)}
backupState={status}
disabled={status !== CloudBackupState.Ready}
onPress={backupAllNonBackedUpWalletsTocloud}
/>
</Menu>
</Box>

<Stack space={'24px'}>
{sortedWallets.map(({ id, name, backedUp, imported, addresses }) => {
{sortedWallets.map(({ id, name, backedUp, backupFile, backupType, imported, addresses }) => {
const isBackedUp = isWalletBackedUpForCurrentAccount({ backedUp, backupFile, backupType });

return (
<Menu key={`wallet-${id}`}>
<MenuItem
Expand All @@ -280,7 +294,7 @@ export const WalletsAndBackup = () => {
</Text>
}
>
{!backedUp && (
{!isBackedUp && (
<MenuItem.Label
testID={'not-backed-up'}
color={'#FF584D'}
Expand All @@ -301,7 +315,7 @@ export const WalletsAndBackup = () => {
/>
</Inline>
}
leftComponent={<MenuItem.TextIcon colorOverride={!backedUp ? '#FF584D' : ''} icon={backedUp ? '􀢶' : '􀡝'} />}
leftComponent={<MenuItem.TextIcon colorOverride={!isBackedUp ? '#FF584D' : ''} icon={isBackedUp ? '􀢶' : '􀡝'} />}
onPress={() => onNavigateToWalletView(id, name)}
size={60}
titleComponent={<MenuItem.Title text={name} />}
Expand Down Expand Up @@ -404,6 +418,7 @@ export const WalletsAndBackup = () => {
<Stack space={'24px'}>
{sortedWallets.map(({ id, name, backedUp, backupFile, backupType, imported, addresses }) => {
const isBackedUp = isWalletBackedUpForCurrentAccount({ backedUp, backupFile, backupType });
console.log('isBackedUp', isBackedUp);

return (
<Menu key={`wallet-${id}`}>
Expand Down
31 changes: 29 additions & 2 deletions src/screens/SettingsSheet/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as i18n from '@/languages';
import { cloudPlatform } from '@/utils/platform';
import { backupsStore, CloudBackupState } from '@/state/backups/backups';
import { RainbowWallet } from '@/model/wallet';
import { IS_IOS } from '@/env';
import { IS_ANDROID, IS_IOS } from '@/env';
import { normalizeAndroidBackupFilename } from '@/handlers/cloudBackup';

type WalletBackupStatus = {
Expand All @@ -30,6 +30,26 @@ export const checkLocalWalletsForBackupStatus = (wallets: ReturnType<typeof useW
};
}

// FOR ANDROID, we need to check if the current google account also has the backup file
if (IS_ANDROID) {
const backupFiles = backupsStore.getState().backups;
return Object.values(wallets).reduce<WalletBackupStatus>(
(acc, wallet) => {
const isBackupEligible = wallet.type !== WalletTypes.readOnly && wallet.type !== WalletTypes.bluetooth;
const hasBackupFile = backupFiles.files.some(
file => normalizeAndroidBackupFilename(file.name) === normalizeAndroidBackupFilename(wallet.backupFile ?? '')
);

return {
allBackedUp: acc.allBackedUp && hasBackupFile && (wallet.backedUp || !isBackupEligible),
areBackedUp: acc.areBackedUp && hasBackupFile && (wallet.backedUp || !isBackupEligible),
canBeBackedUp: acc.canBeBackedUp || isBackupEligible,
};
},
{ allBackedUp: true, areBackedUp: true, canBeBackedUp: false }
);
}

return Object.values(wallets).reduce<WalletBackupStatus>(
(acc, wallet) => {
const isBackupEligible = wallet.type !== WalletTypes.readOnly && wallet.type !== WalletTypes.bluetooth;
Expand Down Expand Up @@ -81,14 +101,21 @@ export const titleForBackupState: Partial<Record<CloudBackupState, string>> = {
};

export const isWalletBackedUpForCurrentAccount = ({ backupType, backedUp, backupFile }: Partial<RainbowWallet>) => {
if (!backupType || !backedUp || !backupFile) {
console.log({
backupType,
backedUp,
backupFile,
});
if (!backupType || !backupFile) {
return false;
}

if (IS_IOS || backupType === WalletBackupTypes.manual) {
return backedUp;
}

console.log('backupFile', backupFile);

// NOTE: For Android, we also need to check if the current google account has the matching backup file
if (!backupFile) {
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/state/backups/backups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export const backupsStore = createRainbowStore<BackupsStore>((set, get) => ({
const gdata = await getGoogleAccountUserData();
if (!gdata) {
logger.debug('[backupsStore]: Google account is not available');
set({ status: CloudBackupState.NotAvailable });
set({ status: CloudBackupState.NotAvailable, backups: { files: [] }, mostRecentBackup: undefined });
return {
success: false,
retry: false,
Expand Down

0 comments on commit 461ee79

Please sign in to comment.