Skip to content

Commit 47b8026

Browse files
committed
feat: add new method to Performance to check for fresh install
1 parent e44940c commit 47b8026

File tree

4 files changed

+53
-10
lines changed

4 files changed

+53
-10
lines changed

app/components/Nav/App/App.tsx

+1-10
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import {
3434
EXISTING_USER,
3535
LAST_APP_VERSION,
3636
} from '../../../constants/storage';
37-
import { getVersion, getFirstInstallTime, getLastUpdateTime } from 'react-native-device-info';
37+
import { getVersion } from 'react-native-device-info';
3838
import { Authentication } from '../../../core/';
3939
import Device from '../../../util/device';
4040
import SDKConnect from '../../../core/SDKConnect/SDKConnect';
@@ -922,15 +922,6 @@ const App: React.FC = () => {
922922

923923
useEffect(() => {
924924
async function startApp() {
925-
const firstInstallTime = await getFirstInstallTime();
926-
const lastUpdateTime = await getLastUpdateTime();
927-
const isFreshInstall = firstInstallTime === lastUpdateTime;
928-
929-
if (isFreshInstall) {
930-
// Clear all MMKV storage on fresh install to avoid leftovers of EXISTING_USER in case of phone migration
931-
await StorageWrapper.clearAll();
932-
}
933-
934925
const existingUser = await StorageWrapper.getItem(EXISTING_USER);
935926
if (!existingUser) {
936927
// List of chainIds to add (as hex strings)

app/constants/storage.ts

+2
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,5 @@ export const themeAppearanceLight = 'light';
6363
export const USE_TERMS = `${prefix}UserTermsAccepted${USE_TERMS_VERSION}`;
6464

6565
export const SOLANA_FEATURE_MODAL_SHOWN = `${prefix}solanaFeatureModalShown`;
66+
67+
export const FRESH_INSTALL_CHECK_DONE = 'FreshInstallCheckDone';

app/core/Performance/Performance.ts

+49
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
/* eslint-disable no-console */
22
import performance, { PerformanceObserver } from 'react-native-performance';
3+
import {
4+
getFirstInstallTime,
5+
getLastUpdateTime,
6+
} from 'react-native-device-info';
7+
import {
8+
EXISTING_USER,
9+
FRESH_INSTALL_CHECK_DONE,
10+
} from '../../constants/storage';
11+
import Logger from '../../util/Logger';
12+
import AsyncStorage from '@react-native-async-storage/async-storage';
313
import StorageWrapper from '../../store/storage-wrapper';
414
import { TraceName, TraceOperation, endTrace, trace } from '../../util/trace';
515
import getUIStartupSpan from './UIStartup';
@@ -15,6 +25,45 @@ async function setPerformanceValues(appStartTime: number) {
1525

1626
class Performance {
1727
static appLaunchTime: number;
28+
29+
/**
30+
* Checks if this is a fresh install with restored MMKV data from a backup.
31+
* If so, clears the MMKV storage to ensure a clean state.
32+
*/
33+
static handleFreshInstallWithRestoredData = () => {
34+
(async () => {
35+
try {
36+
// Check if we've already performed this check
37+
const freshInstallCheckDone = await AsyncStorage.getItem(
38+
FRESH_INSTALL_CHECK_DONE,
39+
);
40+
41+
if (!freshInstallCheckDone) {
42+
// Check if this is a fresh install
43+
const firstInstallTime = await getFirstInstallTime();
44+
const lastUpdateTime = await getLastUpdateTime();
45+
const isFreshInstall = firstInstallTime === lastUpdateTime;
46+
47+
if (isFreshInstall) {
48+
const existingUser = await StorageWrapper.getItem(EXISTING_USER);
49+
50+
if (existingUser) {
51+
// This is a fresh install but we have an existing user flag. This indicates restored data from a backup
52+
await StorageWrapper.clearAll();
53+
}
54+
}
55+
56+
await AsyncStorage.setItem('FRESH_INSTALL_CHECK_DONE', 'true');
57+
}
58+
} catch (error) {
59+
Logger.error(
60+
error as Error,
61+
'Error checking for fresh install with restored data',
62+
);
63+
}
64+
})();
65+
};
66+
1867
/**
1968
* Measures app start and JS bundle loading times
2069
*/

index.js

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { isE2E } from './app/util/test/utils.js';
2222
import { Performance } from './app/core/Performance';
2323
import { handleCustomError, setReactNativeDefaultHandler } from './app/core/ErrorHandler';
2424
Performance.setupPerformanceObservers();
25+
Performance.handleFreshInstallWithRestoredData();
2526

2627
LogBox.ignoreAllLogs();
2728
// List of warnings that we're ignoring

0 commit comments

Comments
 (0)