forked from alichherawalla/off-grid-mobile-ai
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
287 lines (254 loc) · 9.99 KB
/
App.tsx
File metadata and controls
287 lines (254 loc) · 9.99 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
276
277
278
279
280
281
282
283
284
285
286
287
/**
* 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 { LockScreen } from './src/screens';
import { useAppState } from './src/hooks/useAppState';
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() {
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 clearImageModelDownloading = useAppStore((s) => s.clearImageModelDownloading);
const { colors, isDark } = useTheme();
const {
isEnabled: authEnabled,
isLocked,
setLocked,
setLastBackgroundTime,
} = useAuthStore();
// Handle app state changes for auto-lock
useAppState({
onBackground: useCallback(() => {
if (authEnabled) {
setLastBackgroundTime(Date.now());
setLocked(true);
}
}, [authEnabled, setLastBackgroundTime, setLocked]),
onForeground: useCallback(() => {
// Lock is already set when going to background
// Nothing additional needed here
}, []),
});
useEffect(() => {
initializeApp();
}, []);
const ensureAppStoreHydrated = async () => {
const persistApi = useAppStore.persist;
if (!persistApi?.hasHydrated || !persistApi.rehydrate) return;
if (!persistApi.hasHydrated()) {
await persistApi.rehydrate();
}
};
const initializeApp = async () => {
try {
// Ensure persisted download metadata is loaded before restore logic reads it.
await ensureAppStoreHydrated();
// 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();
// Wire up background download metadata persistence
const {
setBackgroundDownload,
activeBackgroundDownloads,
addDownloadedModel,
setDownloadProgress,
} = useAppStore.getState();
modelManager.setBackgroundDownloadMetadataCallback((downloadId, info) => {
setBackgroundDownload(downloadId, info);
});
// Recover any background downloads that completed while app was dead
try {
const recoveredModels = await modelManager.syncBackgroundDownloads(
activeBackgroundDownloads,
(downloadId) => setBackgroundDownload(downloadId, null)
);
for (const model of recoveredModels) {
addDownloadedModel(model);
logger.log('[App] Recovered background download:', model.name);
}
} catch (err) {
logger.error('[App] Failed to sync background downloads:', err);
}
// Recover completed image downloads (zip unzip / multifile finalization)
try {
const recoveredImageModels = await modelManager.syncCompletedImageDownloads(
activeBackgroundDownloads,
(downloadId) => setBackgroundDownload(downloadId, null),
);
for (const model of recoveredImageModels) {
logger.log('[App] Recovered image download:', model.name);
}
} catch (err) {
logger.error('[App] Failed to sync completed image downloads:', err);
}
// Re-wire event listeners for downloads that were still running when the
// app was killed (running/pending status in Android DownloadManager).
try {
const restoredDownloadIds = await modelManager.restoreInProgressDownloads(
activeBackgroundDownloads,
(progress) => {
const key = `${progress.modelId}/${progress.fileName}`;
setDownloadProgress(key, {
progress: progress.progress,
bytesDownloaded: progress.bytesDownloaded,
totalBytes: progress.totalBytes,
});
},
);
for (const downloadId of restoredDownloadIds) {
const metadata = activeBackgroundDownloads[downloadId];
const progressKey = metadata ? `${metadata.modelId}/${metadata.fileName}` : null;
modelManager.watchDownload(
downloadId,
(model) => {
if (progressKey) setDownloadProgress(progressKey, null);
addDownloadedModel(model);
logger.log('[App] Restored in-progress download completed:', model.name);
},
(error) => {
if (progressKey) setDownloadProgress(progressKey, null);
logger.error('[App] Restored in-progress download failed:', error);
},
);
}
} catch (err) {
logger.error('[App] Failed to restore in-progress downloads:', err);
}
// Clear any stale imageModelDownloading entries — if the app was killed
// mid-download these would be persisted as "downloading" forever.
clearImageModelDownloading();
// Scan for any models that may have been downloaded externally or
// when app was killed before JS callback fired
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);
}
};
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;