-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
66 lines (59 loc) · 2.11 KB
/
App.tsx
File metadata and controls
66 lines (59 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import React, { useEffect } from 'react';
import { StatusBar } from 'expo-status-bar';
import { SafeAreaProvider } from 'react-native-safe-area-context';
import { AppState, AppStateStatus } from 'react-native';
import { SleepProvider } from './src/context/SleepContext';
import { ThemeProvider, useTheme } from './src/context/ThemeContext';
import AppNavigator from './src/navigation/AppNavigator';
import BackgroundActivityService from './src/services/BackgroundActivityService';
import ErrorBoundary from './src/components/ErrorBoundary';
const AppContent = () => {
const { isDarkMode } = useTheme();
return (
<>
<AppNavigator />
<StatusBar style={isDarkMode ? 'light' : 'dark'} />
</>
);
};
export default function App() {
useEffect(() => {
// Handle app state changes to prevent crashes on force close
const handleAppStateChange = async (nextAppState: AppStateStatus) => {
console.log(`🔄 App state changed to: ${nextAppState}`);
if (nextAppState === 'background' || nextAppState === 'inactive') {
try {
// Gracefully handle app going to background
console.log('📱 App going to background - preparing cleanup');
} catch (error) {
console.error('❌ Error handling app background state:', error);
}
}
};
// Subscribe to app state changes
const subscription = AppState.addEventListener('change', handleAppStateChange);
// Cleanup function to prevent memory leaks
return () => {
console.log('🧹 Cleaning up app state subscription');
subscription?.remove();
// Emergency cleanup for background service
try {
const backgroundService = BackgroundActivityService.getInstance();
backgroundService.stopMonitoring().catch(console.error);
} catch (error) {
console.error('❌ Emergency cleanup error:', error);
}
};
}, []);
return (
<ErrorBoundary>
<SafeAreaProvider>
<ThemeProvider>
<SleepProvider>
<AppContent />
</SleepProvider>
</ThemeProvider>
</SafeAreaProvider>
</ErrorBoundary>
);
}