-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
148 lines (138 loc) · 4.17 KB
/
App.js
File metadata and controls
148 lines (138 loc) · 4.17 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import React, { useState, useEffect, useCallback } from 'react';
import { View, Text, TouchableOpacity, StyleSheet } from 'react-native';
import { GestureHandlerRootView } from 'react-native-gesture-handler';
import { SafeAreaProvider } from 'react-native-safe-area-context';
import { enableFreeze, enableScreens } from 'react-native-screens';
import ThemedToast from './src/components/ui/ThemedToast';
import { KeyboardProvider } from 'react-native-keyboard-controller';
import { BottomSheetModalProvider } from '@gorhom/bottom-sheet';
import BootSplash from 'react-native-bootsplash';
import { AppContextProvider } from './src/context/AppContext';
import { ThemeProvider } from './src/context/ThemeContext';
import { ActiveWorkoutBannerProvider } from './src/context/ActiveWorkoutBannerContext';
import AppNavigator from './src/navigation/AppNavigator';
import { runMigrations } from './src/services/migrations';
import MigrationScreen from './src/screens/MigrationScreen';
import LibrarySetupScreen from './src/screens/LibrarySetupScreen';
import { ensureTrainingDatabase } from './src/domain/storage/trainingDatabase';
enableScreens(true);
enableFreeze(true);
export default function App() {
const [migrationStep, setMigrationStep] = useState(null);
const [ready, setReady] = useState(false);
const [startupError, setStartupError] = useState(null);
const [bootAttempt, setBootAttempt] = useState(0);
const bootstrap = useCallback(async () => {
setReady(false);
setStartupError(null);
setMigrationStep(null);
try {
await runMigrations((step, total) => setMigrationStep({ step, total }));
setMigrationStep(null);
await ensureTrainingDatabase();
} catch (e) {
console.warn('Startup error:', e);
setStartupError(e);
} finally {
setReady(true);
}
}, []);
useEffect(() => {
(async () => {
await bootstrap();
})();
}, [bootAttempt, bootstrap]);
useEffect(() => {
if (ready) {
BootSplash.hide({ fade: true }).catch(() => {});
}
}, [ready]);
let content = null;
if (migrationStep) {
content = (
<MigrationScreen
step={migrationStep.step}
total={migrationStep.total}
message="Upgrading your data..."
/>
);
} else if (!ready) {
content = <LibrarySetupScreen status="setting_up" />;
} else if (startupError) {
content = (
<View style={s.errorScreen}>
<Text style={s.errorTitle}>IronlogDB</Text>
<Text style={s.errorHeading}>Startup failed</Text>
<Text style={s.errorMessage}>
{startupError?.message || 'Something went wrong while loading your data.'}
</Text>
<TouchableOpacity style={s.retryButton} onPress={() => setBootAttempt((attempt) => attempt + 1)}>
<Text style={s.retryButtonText}>TRY AGAIN</Text>
</TouchableOpacity>
</View>
);
} else {
content = (
<KeyboardProvider>
<BottomSheetModalProvider>
<AppContextProvider>
<ThemeProvider>
<ActiveWorkoutBannerProvider>
<AppNavigator />
<ThemedToast />
</ActiveWorkoutBannerProvider>
</ThemeProvider>
</AppContextProvider>
</BottomSheetModalProvider>
</KeyboardProvider>
);
}
return (
<GestureHandlerRootView style={{ flex: 1 }}>
<SafeAreaProvider>
{content}
</SafeAreaProvider>
</GestureHandlerRootView>
);
}
const s = StyleSheet.create({
errorScreen: {
flex: 1,
backgroundColor: '#080808',
alignItems: 'center',
justifyContent: 'center',
padding: 32,
},
errorTitle: {
fontSize: 32,
fontWeight: '900',
color: '#f0f0f0',
letterSpacing: -1,
marginBottom: 8,
},
errorHeading: {
fontSize: 16,
fontWeight: '800',
color: '#FF4500',
letterSpacing: 2,
marginBottom: 12,
},
errorMessage: {
fontSize: 13,
color: '#999',
textAlign: 'center',
lineHeight: 20,
marginBottom: 24,
},
retryButton: {
paddingHorizontal: 24,
paddingVertical: 14,
backgroundColor: '#FF4500',
},
retryButtonText: {
color: '#fff',
fontSize: 12,
fontWeight: '800',
letterSpacing: 2,
},
});