-
-
Notifications
You must be signed in to change notification settings - Fork 228
Expand file tree
/
Copy pathApp.tsx
More file actions
275 lines (245 loc) · 9.82 KB
/
Copy pathApp.tsx
File metadata and controls
275 lines (245 loc) · 9.82 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/**
* Off Grid - On-Device AI Chat Application
* Private AI assistant that runs entirely on your device
*/
import 'react-native-gesture-handler';
import React, { useEffect, useState, useCallback } from 'react';
import { StatusBar, ActivityIndicator, View, StyleSheet, LogBox } from 'react-native';
import { GestureHandlerRootView } from 'react-native-gesture-handler';
import { SafeAreaProvider } from 'react-native-safe-area-context';
import { NavigationContainer } from '@react-navigation/native';
import { AppNavigator } from './src/navigation';
import { useTheme } from './src/theme';
import { hardwareService, modelManager, authService, ragService, remoteServerManager } from './src/services';
import logger from './src/utils/logger';
import { useAppStore, useAuthStore, useRemoteServerStore } from './src/stores';
import { hydrateDownloadStore } from './src/services/downloadHydration';
import { useDownloadListeners } from './src/hooks/useDownloads';
import { LockScreen } from './src/screens';
import { useAppState } from './src/hooks/useAppState';
import { useDownloadStore } from './src/stores/downloadStore';
LogBox.ignoreAllLogs(); // Suppress all logs
const ensureRemoteServerStoreHydrated = async () => {
const persistApi = useRemoteServerStore.persist;
if (!persistApi?.hasHydrated || !persistApi.rehydrate) return;
if (!persistApi.hasHydrated()) {
await persistApi.rehydrate();
}
};
function App() {
useDownloadListeners();
const [isInitializing, setIsInitializing] = useState(true);
const setDeviceInfo = useAppStore((s) => s.setDeviceInfo);
const setModelRecommendation = useAppStore((s) => s.setModelRecommendation);
const setDownloadedModels = useAppStore((s) => s.setDownloadedModels);
const setDownloadedImageModels = useAppStore((s) => s.setDownloadedImageModels);
const { colors, isDark } = useTheme();
const {
isEnabled: authEnabled,
isLocked,
setLocked,
setLastBackgroundTime,
} = useAuthStore();
const reattachTextDownloadRecovery = useCallback(async () => {
const restoredIds = await modelManager.restoreInProgressDownloads();
modelManager.startBackgroundDownloadPolling();
restoredIds.forEach((downloadId) => {
modelManager.watchDownload(
downloadId,
async () => {
const models = await modelManager.getDownloadedModels();
setDownloadedModels(models);
useDownloadStore.getState().remove(
useDownloadStore.getState().downloadIdIndex[downloadId] ?? '',
);
},
(error: Error) => {
logger.error('[App] Restored text download failed:', error);
useDownloadStore.getState().setStatus(downloadId, 'failed', { message: error.message });
},
);
});
}, [setDownloadedModels]);
// Handle app state changes for auto-lock
useAppState({
onBackground: useCallback(() => {
if (authEnabled) {
setLastBackgroundTime(Date.now());
setLocked(true);
}
}, [authEnabled, setLastBackgroundTime, setLocked]),
onForeground: useCallback(() => {
// Rebuild the unified store before reattaching JS listeners so restored
// progress events map onto current download entries instead of racing hydration.
hydrateDownloadStore()
.catch((error) => {
logger.error('[App] Failed to hydrate download store on foreground:', error);
})
.finally(() => {
reattachTextDownloadRecovery().catch((error) => {
logger.error('[App] Failed to restore text downloads on foreground:', error);
});
});
}, [reattachTextDownloadRecovery]),
});
const ensureAppStoreHydrated = useCallback(async () => {
const persistApi = useAppStore.persist;
if (!persistApi?.hasHydrated || !persistApi.rehydrate) return;
if (!persistApi.hasHydrated()) {
await persistApi.rehydrate();
}
}, []);
const initializeApp = useCallback(async () => {
try {
// Ensure persisted download metadata is loaded before restore logic reads it.
await ensureAppStoreHydrated();
// Hydrate download store from SQLite before any screen mounts.
await hydrateDownloadStore().catch((error) => {
logger.error('[App] Failed to hydrate download store during startup:', error);
});
await reattachTextDownloadRecovery();
// Phase 1: Quick initialization - get app ready to show UI
// Initialize hardware detection
const deviceInfo = await hardwareService.getDeviceInfo();
setDeviceInfo(deviceInfo);
const recommendation = hardwareService.getModelRecommendation();
setModelRecommendation(recommendation);
// Initialize model manager and load downloaded models list
await modelManager.initialize();
// Clean up any mmproj files that were incorrectly added as standalone models
await modelManager.cleanupMMProjEntries();
// Reconcile image model directories that finished extracting on disk but
// whose AsyncStorage registration was lost to an app kill. Runs before
// refreshModelLists so the recovered models are included in the initial
// setDownloadedImageModels call. activeModelIds guards against touching
// directories that are currently being downloaded/extracted.
const activeImageModelIds = new Set(
Object.values(useDownloadStore.getState().downloads)
.filter(e => e.modelType === 'image')
.map(e => e.modelId.replace('image:', '')),
);
await modelManager.reconcileFinishedImageDownloads(activeImageModelIds).catch((error) => {
logger.error('[App] Image model reconciliation failed:', error);
});
// Scan for any models that may have been downloaded externally or
// while the app was killed. hydrateDownloadStore (called on cold start
// and foreground resume) repopulates in-flight downloads directly
// from the native Room DB, replacing the old metadata-callback +
// syncBackgroundDownloads recovery path.
const { textModels, imageModels } = await modelManager.refreshModelLists();
setDownloadedModels(textModels);
setDownloadedImageModels(imageModels);
// Ensure remote server store is hydrated before initializing providers,
// so getServers() / activeServerId reads see persisted data.
await ensureRemoteServerStoreHydrated();
// Initialize remote server providers in the background — don't block
// the home screen while fetching models from potentially unreachable servers.
remoteServerManager.initializeProviders().catch((err) => {
logger.error('[App] Failed to initialize remote server providers:', err);
});
// Check if passphrase is set and lock app if needed
const hasPassphrase = await authService.hasPassphrase();
if (hasPassphrase && authEnabled) {
setLocked(true);
}
// Initialize RAG database tables
ragService.ensureReady().catch((err) => logger.error('Failed to initialize RAG service on startup', err));
// Show the UI immediately
setIsInitializing(false);
// Models are loaded on-demand when the user opens a chat,
// not eagerly on startup, to avoid freezing the UI.
} catch (error) {
logger.error('[App] Error initializing app:', error);
setIsInitializing(false);
}
}, [
authEnabled,
ensureAppStoreHydrated,
reattachTextDownloadRecovery,
setDeviceInfo,
setDownloadedImageModels,
setDownloadedModels,
setLocked,
setModelRecommendation,
]);
useEffect(() => {
initializeApp();
}, [initializeApp]);
const handleUnlock = useCallback(() => {
setLocked(false);
}, [setLocked]);
if (isInitializing) {
return (
<GestureHandlerRootView style={styles.flex}>
<SafeAreaProvider>
<View style={[styles.loadingContainer, { backgroundColor: colors.background }]} testID="app-loading">
<StatusBar barStyle={isDark ? 'light-content' : 'dark-content'} backgroundColor={colors.background} />
<ActivityIndicator size="large" color={colors.primary} />
</View>
</SafeAreaProvider>
</GestureHandlerRootView>
);
}
// Show lock screen if auth is enabled and app is locked
if (authEnabled && isLocked) {
return (
<GestureHandlerRootView style={styles.flex} testID="app-locked">
<SafeAreaProvider>
<StatusBar barStyle={isDark ? 'light-content' : 'dark-content'} backgroundColor={colors.background} />
<LockScreen onUnlock={handleUnlock} />
</SafeAreaProvider>
</GestureHandlerRootView>
);
}
return (
<GestureHandlerRootView style={styles.flex}>
<SafeAreaProvider>
<StatusBar barStyle={isDark ? 'light-content' : 'dark-content'} backgroundColor={colors.background} />
<NavigationContainer
theme={{
dark: isDark,
colors: {
primary: colors.primary,
background: colors.background,
card: colors.surface,
text: colors.text,
border: colors.border,
notification: colors.primary,
},
fonts: {
regular: {
fontFamily: 'System',
fontWeight: '400',
},
medium: {
fontFamily: 'System',
fontWeight: '500',
},
bold: {
fontFamily: 'System',
fontWeight: '700',
},
heavy: {
fontFamily: 'System',
fontWeight: '900',
},
},
}}
>
<AppNavigator />
</NavigationContainer>
</SafeAreaProvider>
</GestureHandlerRootView>
);
}
const styles = StyleSheet.create({
flex: {
flex: 1,
},
loadingContainer: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
});
export default App;