forked from tanishkothari9/quickloop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
376 lines (333 loc) · 10.3 KB
/
App.tsx
File metadata and controls
376 lines (333 loc) · 10.3 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
import React, { useState, useEffect } from 'react';
import { StatusBar } from 'expo-status-bar';
import { StyleSheet, View, ActivityIndicator, Alert, Platform } from 'react-native';
import * as ImagePicker from 'expo-image-picker';
import * as MediaLibrary from 'expo-media-library';
import PhoneLoginScreen from './screens/PhoneLoginScreen';
import ChatRoomScreen from './screens/ChatRoomScreen';
import RoomListScreen from './screens/RoomListScreen';
import ProfileScreen from './screens/ProfileScreen';
import ActivityScreen from './screens/ActivityScreen';
import { Room, User, createUser, getUserByUsername, leaveRoom } from './lib/supabase';
import {
getUserName,
saveUserName,
getCurrentRoom,
saveCurrentRoom,
clearCurrentRoom,
getUserData,
saveUserData,
getPermissionsRequested,
savePermissionsRequested,
clearUserData
} from './lib/storage';
import { initImageCache, clearExpiredImageCaches } from './lib/imageUtils';
// Define app colors - based on deep navy blue #1A2C50
const COLORS = {
primary: '#1A2C50', // Deep navy blue
secondary: '#4A6FA5', // Medium blue
accent: '#6B98D4', // Light blue
highlight: '#F0B429', // Gold accent
lightBg: '#E6EBF5', // Light background
white: '#FFFFFF',
black: '#000000',
gray: '#6B7280',
lightGray: '#E5E7EB',
border: '#D1D5DB',
background: '#F9FAFB',
text: '#1F2937', // Dark text color
}
export default function App() {
const [user, setUser] = useState<User | null>(null);
const [currentRoom, setCurrentRoom] = useState<Room | null>(null);
const [loading, setLoading] = useState(true);
const [showProfile, setShowProfile] = useState(false);
const [showActivity, setShowActivity] = useState(false);
const [currentTab, setCurrentTab] = useState('home');
const [activities, setActivities] = useState<any[]>([]);
// Request all necessary permissions and initialize systems
const initializeApp = async () => {
try {
// Initialize image caching system
await initImageCache();
// Clear expired image caches
await clearExpiredImageCaches();
// Check if we've already requested permissions
const permissionsRequested = await getPermissionsRequested();
if (permissionsRequested) {
console.log('Permissions were previously requested');
return; // Don't ask again if we've already requested
}
console.log('Requesting app permissions');
// Request all permissions at once to avoid multiple prompts later
// Camera permissions
await ImagePicker.requestCameraPermissionsAsync();
// Media library permissions
await ImagePicker.requestMediaLibraryPermissionsAsync();
// Media library write permissions (needed for saving images)
await MediaLibrary.requestPermissionsAsync();
// Mark permissions as requested so we don't ask again
await savePermissionsRequested(true);
} catch (error) {
console.error('Error initializing app:', error);
}
};
// Load user data from AsyncStorage when app starts
useEffect(() => {
const loadStoredData = async () => {
try {
// Initialize app systems and request permissions
await initializeApp();
// First try to load full user data
const storedUserData = await getUserData();
if (storedUserData && storedUserData.id) {
setUser(storedUserData);
// If we have full user data, check if they were in a room
const storedRoom = await getCurrentRoom();
if (storedRoom) {
setCurrentRoom(storedRoom);
}
} else {
// Fall back to just the username for backward compatibility
const storedUserName = await getUserName();
if (storedUserName) {
// Try to get full user data from the database
const dbUser = await getUserByUsername(storedUserName);
if (dbUser) {
// Save the full user data for future use
await saveUserData(dbUser);
setUser(dbUser);
// Check if they were in a room
const storedRoom = await getCurrentRoom();
if (storedRoom) {
setCurrentRoom(storedRoom);
}
} else {
// Just create a user object with the username
setUser({ username: storedUserName });
}
}
}
} catch (error) {
console.error('Error loading stored data:', error);
} finally {
setLoading(false);
}
};
loadStoredData();
}, []);
// Load mock activity data
useEffect(() => {
// In a real app, this would fetch from an API
// For now, we'll use mock data
const mockActivities = [
{
id: '1',
type: 'join',
username: 'Liam',
timestamp: new Date().toISOString(),
timeDisplay: '10:30 AM',
},
{
id: '2',
type: 'upload',
username: 'Sophia',
timestamp: new Date().toISOString(),
timeDisplay: '11:15 AM',
photoCount: 3,
},
{
id: '3',
type: 'join',
username: 'Ethan',
timestamp: new Date().toISOString(),
timeDisplay: '12:00 PM',
},
{
id: '4',
type: 'upload',
username: 'Olivia',
timestamp: new Date().toISOString(),
timeDisplay: '12:45 PM',
photoCount: 2,
},
{
id: '5',
type: 'create',
username: 'Noah',
timestamp: new Date(Date.now() - 86400000).toISOString(), // Yesterday
timeDisplay: '9:00 AM',
},
{
id: '6',
type: 'join',
username: 'Ava',
timestamp: new Date(Date.now() - 86400000).toISOString(), // Yesterday
timeDisplay: '9:30 AM',
},
{
id: '7',
type: 'upload',
username: 'Lucas',
timestamp: new Date(Date.now() - 86400000).toISOString(), // Yesterday
timeDisplay: '10:00 AM',
photoCount: 5,
},
{
id: '8',
type: 'join',
username: 'Isabella',
timestamp: new Date(Date.now() - 86400000).toISOString(), // Yesterday
timeDisplay: '11:00 AM',
},
];
setActivities(mockActivities);
}, []);
const handleUserCreated = async (name: string) => {
try {
// First check if user exists
let dbUser = await getUserByUsername(name);
// If not, create the user
if (!dbUser) {
dbUser = await createUser(name);
if (!dbUser) {
throw new Error('Failed to create user');
}
}
// Save full user data
await saveUserData(dbUser);
setUser(dbUser);
// Request permissions when user logs in (if not already requested)
initializeApp();
} catch (error) {
console.error('Error handling user creation:', error);
// Fallback to just username
setUser({ username: name });
await saveUserName(name);
}
};
const handleRoomJoined = async (room: Room) => {
setCurrentRoom(room);
await saveCurrentRoom(room);
};
const handleExitRoom = async (shouldLeaveRoom = false) => {
if (shouldLeaveRoom && user && user.id && currentRoom) {
try {
// If we're actually leaving the room (not just navigating back), remove membership
await leaveRoom(user.id, currentRoom.id);
console.log(`User ${user.username} left room ${currentRoom.id}`);
} catch (error) {
console.error('Error leaving room:', error);
// Continue even if there's an error
}
}
// Always clear current room and go back to room list
setCurrentRoom(null);
setCurrentTab('home'); // Ensure we're on the home tab
await clearCurrentRoom();
};
const handleLogout = async () => {
try {
// Clear all user data from storage
await clearUserData();
// Reset app state
setUser(null);
setCurrentRoom(null);
} catch (error) {
console.error('Error during logout:', error);
}
};
// Handle navigation between tabs
const handleNavigateToTab = (tab: string) => {
setCurrentTab(tab);
if (tab === 'profile') {
setShowProfile(true);
setShowActivity(false);
} else if (tab === 'notifications') {
setShowActivity(true);
setShowProfile(false);
} else if (tab === 'home') {
setShowProfile(false);
setShowActivity(false);
}
};
// Handle back navigation from profile
const handleBackFromProfile = () => {
setShowProfile(false);
};
// Handle back navigation from activity
const handleBackFromActivity = () => {
setShowActivity(false);
};
// Show loading indicator while checking for stored user data
if (loading) {
return (
<View style={[styles.container, styles.loadingContainer]}>
<ActivityIndicator size="large" color={COLORS.primary} />
</View>
);
}
// Determine which screen to show
const renderScreen = () => {
if (!user) {
return (
<PhoneLoginScreen
onUserCreated={handleUserCreated}
/>
);
}
if (currentRoom) {
return (
<ChatRoomScreen
room={currentRoom}
userName={user.username}
userId={user.id}
onExit={handleExitRoom}
/>
);
}
if (showProfile) {
return (
<ProfileScreen
user={user}
onBack={handleBackFromProfile}
onLogout={handleLogout}
onNavigateToTab={handleNavigateToTab}
/>
);
}
if (showActivity) {
return (
<ActivityScreen
onBack={handleBackFromActivity}
activities={activities}
/>
);
}
return (
<RoomListScreen
user={user}
onSelectRoom={handleRoomJoined}
onCreateRoom={() => {}} // Empty function since we handle creation in RoomListScreen
onLogout={handleLogout}
onNavigateToTab={handleNavigateToTab}
/>
);
};
return (
<View style={styles.container}>
{renderScreen()}
<StatusBar style="auto" />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
},
loadingContainer: {
justifyContent: 'center',
alignItems: 'center',
},
});