1
1
/* eslint-disable no-console */
2
2
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' ;
3
13
import StorageWrapper from '../../store/storage-wrapper' ;
4
14
import { TraceName , TraceOperation , endTrace , trace } from '../../util/trace' ;
5
15
import getUIStartupSpan from './UIStartup' ;
@@ -15,6 +25,45 @@ async function setPerformanceValues(appStartTime: number) {
15
25
16
26
class Performance {
17
27
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
+
18
67
/**
19
68
* Measures app start and JS bundle loading times
20
69
*/
0 commit comments