-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
334 lines (307 loc) · 9.09 KB
/
Copy pathApp.tsx
File metadata and controls
334 lines (307 loc) · 9.09 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
/**
* VibeDownloader Mobile
* Android-only React Native app for downloading media from multiple platforms
*
* @format
*/
import React, { useState, useRef, useEffect } from 'react';
import {
StatusBar,
StyleSheet,
View,
TouchableOpacity,
Text,
Animated,
Dimensions,
PanResponder,
Easing,
} from 'react-native';
import { SafeAreaProvider, SafeAreaView } from 'react-native-safe-area-context';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { HomeScreen, LibraryScreen, SplashScreen, OnboardingScreen } from './src/screens';
import { Colors, BorderRadius, Spacing, Typography, Shadows } from './src/theme';
import { HomeIcon, LibraryIcon, DownloadIcon } from './src/components/Icons';
import { UpdateLog } from './src/components/UpdateLog';
import { Haptics } from './src/utils/haptics';
// Storage key constant
const ONBOARDING_COMPLETE_KEY = 'hasLaunched';
const { width } = Dimensions.get('window');
type TabType = 'home' | 'library';
interface TabButtonProps {
id: TabType;
label: string;
icon: React.JSX.Element;
activeIcon: React.JSX.Element;
isActive: boolean;
onPress: () => void;
}
const TabButton: React.FC<TabButtonProps> = ({ id, label, icon, activeIcon, isActive, onPress }) => {
const scaleAnim = useRef(new Animated.Value(1)).current;
const indicatorAnim = useRef(new Animated.Value(isActive ? 1 : 0)).current;
useEffect(() => {
Animated.parallel([
Animated.spring(scaleAnim, {
toValue: isActive ? 1.1 : 1,
tension: 300,
friction: 15,
useNativeDriver: true,
}),
Animated.timing(indicatorAnim, {
toValue: isActive ? 1 : 0,
duration: 200,
useNativeDriver: true,
}),
]).start();
}, [isActive]);
return (
<TouchableOpacity
style={styles.tabButton}
onPress={() => {
Haptics.selection();
onPress();
}}
activeOpacity={1} // Physical buttons don't fade, they react
>
{/* Top indicator bar */}
<Animated.View
style={[
styles.indicator,
{
opacity: indicatorAnim,
backgroundColor: Colors.primary,
transform: [{ scaleX: indicatorAnim }]
}
]}
/>
<Animated.View
style={[
styles.tabIconContainer,
{
transform: [{ scale: scaleAnim }],
}
]}
>
{isActive ? activeIcon : icon}
</Animated.View>
<Text
style={[
styles.tabLabel,
isActive && styles.tabLabelActive,
]}
>
{label}
</Text>
</TouchableOpacity>
);
};
function App(): React.JSX.Element {
const [appState, setAppState] = useState<'splash' | 'onboarding' | 'main'>('splash');
const [activeTab, setActiveTab] = useState<TabType>('home');
const [isFirstLaunch, setIsFirstLaunch] = useState<boolean | null>(null);
const isFirstLaunchRef = useRef<boolean | null>(null);
const slideAnim = useRef(new Animated.Value(0)).current;
const storageChecked = useRef(false);
// Ref to track active tab for PanResponder
const activeTabRef = useRef(activeTab);
// Check storage on mount - only once
useEffect(() => {
if (!storageChecked.current) {
storageChecked.current = true;
AsyncStorage.getItem(ONBOARDING_COMPLETE_KEY)
.then((value: string | null) => {
setIsFirstLaunch(value === null);
isFirstLaunchRef.current = value === null;
})
.catch(() => {
// If storage fails, assume not first launch to avoid annoying users
setIsFirstLaunch(false);
isFirstLaunchRef.current = false;
});
}
}, []);
// Sync activeTabRef
useEffect(() => {
activeTabRef.current = activeTab;
}, [activeTab]);
// PanResponder for swiping
const panResponder = useRef(
PanResponder.create({
onMoveShouldSetPanResponder: (_, gestureState) => {
// Capture horizontal swipes
const isHorizontal = Math.abs(gestureState.dx) > Math.abs(gestureState.dy);
return isHorizontal && Math.abs(gestureState.dx) > 20;
},
onPanResponderRelease: (_, gestureState) => {
const threshold = 50;
if (gestureState.dx < -threshold && activeTabRef.current === 'home') {
setActiveTab('library');
} else if (gestureState.dx > threshold && activeTabRef.current === 'library') {
setActiveTab('home');
}
},
})
).current;
// Tab animation
useEffect(() => {
Animated.spring(slideAnim, {
toValue: activeTab === 'home' ? 0 : -width,
tension: 50,
friction: 10,
useNativeDriver: true,
}).start();
}, [activeTab]);
const handleSplashFinish = () => {
// If storage hasn't been checked yet, wait a bit more
if (isFirstLaunchRef.current === null) {
// Retry after a short delay
const retryTimer = setInterval(() => {
if (isFirstLaunchRef.current !== null) {
clearInterval(retryTimer);
setAppState(isFirstLaunchRef.current ? 'onboarding' : 'main');
}
}, 100);
// Fallback after 2 seconds
setTimeout(() => {
clearInterval(retryTimer);
if (isFirstLaunchRef.current === null) {
setAppState('main'); // Default to main if storage check fails
}
}, 2000);
return;
}
setAppState(isFirstLaunchRef.current ? 'onboarding' : 'main');
};
const handleOnboardingDone = async () => {
try {
await AsyncStorage.setItem(ONBOARDING_COMPLETE_KEY, 'true');
await AsyncStorage.setItem('last_seen_version', '1.2.0');
} catch (e) {
console.warn('Failed to save launch state:', e);
}
setIsFirstLaunch(false);
isFirstLaunchRef.current = false;
setAppState('main');
};
return (
<SafeAreaProvider>
<StatusBar
barStyle="light-content"
backgroundColor={Colors.background}
translucent={false}
/>
{appState === 'splash' && <SplashScreen onFinish={handleSplashFinish} />}
{appState === 'onboarding' && <OnboardingScreen onDone={handleOnboardingDone} />}
{appState === 'main' && (
<View style={styles.container}>
<View style={styles.screenContainer} {...panResponder.panHandlers}>
<Animated.View
style={[
styles.screenWrapper,
{ transform: [{ translateX: slideAnim }] }
]}
>
<View style={styles.screen}>
<HomeScreen onNavigateToLibrary={() => setActiveTab('library')} />
</View>
<View style={styles.screen}>
<LibraryScreen isFocused={activeTab === 'library'} />
</View>
</Animated.View>
</View>
<SafeAreaView edges={['bottom']} style={styles.bottomNavSafeArea}>
<View style={styles.bottomNav}>
<View style={styles.navContent}>
<TabButton
id="home"
label="Download"
icon={<DownloadIcon size={22} color={Colors.textMuted} />}
activeIcon={<DownloadIcon size={22} color={Colors.primary} />}
isActive={activeTab === 'home'}
onPress={() => setActiveTab('home')}
/>
<TabButton
id="library"
label="Library"
icon={<LibraryIcon size={22} color={Colors.textMuted} />}
activeIcon={<LibraryIcon size={22} color={Colors.primary} />}
isActive={activeTab === 'library'}
onPress={() => setActiveTab('library')}
/>
</View>
</View>
</SafeAreaView>
{/* Update Log Modal */}
<UpdateLog />
</View>
)}
</SafeAreaProvider>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: Colors.background,
},
screenContainer: {
flex: 1,
overflow: 'hidden',
},
screenWrapper: {
flexDirection: 'row',
width: width * 2,
flex: 1,
},
screen: {
width,
flex: 1,
},
bottomNavSafeArea: {
backgroundColor: Colors.surfaceHigh,
},
bottomNav: {
backgroundColor: Colors.surfaceHigh,
height: 65,
flexDirection: 'row',
alignItems: 'center',
borderTopWidth: 1,
borderTopColor: Colors.innerBorder,
},
navContent: {
flex: 1,
flexDirection: 'row',
justifyContent: 'space-evenly',
alignItems: 'center',
},
tabButton: {
alignItems: 'center',
justifyContent: 'center',
flex: 1,
height: '100%',
},
indicator: {
position: 'absolute',
top: 0,
width: 40,
height: 3,
borderBottomLeftRadius: 3,
borderBottomRightRadius: 3,
},
tabIconContainer: {
marginBottom: 4,
alignItems: 'center',
justifyContent: 'center',
},
tabLabel: {
fontSize: 10,
color: Colors.textMuted,
fontWeight: Typography.weights.medium,
letterSpacing: 0.5,
textTransform: 'uppercase',
},
tabLabelActive: {
color: Colors.primary,
fontWeight: Typography.weights.bold,
},
// removed activeDot styles
});
export default App;