-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
130 lines (118 loc) · 3.35 KB
/
App.tsx
File metadata and controls
130 lines (118 loc) · 3.35 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
import React, { useCallback, useState, useRef } from "react";
import {
View,
ActivityIndicator,
Animated,
Dimensions,
} from "react-native";
import { GestureHandlerRootView } from "react-native-gesture-handler";
import {
useFonts,
JetBrainsMono_400Regular,
JetBrainsMono_600SemiBold,
JetBrainsMono_700Bold,
} from "@expo-google-fonts/jetbrains-mono";
import * as SplashScreen from "expo-splash-screen";
import HomeScreen from "./src/screens/HomeScreen";
import CreateHabitScreen from "./src/screens/CreateHabitScreen";
import { useHabits } from "./src/hooks/useHabits";
import { COLORS } from "./src/utils/theme";
import type { Habit, Screen } from "./src/types";
SplashScreen.preventAutoHideAsync();
const { width: SCREEN_WIDTH } = Dimensions.get("window");
export default function App() {
const [fontsLoaded] = useFonts({
JetBrainsMono_400Regular,
JetBrainsMono_600SemiBold,
JetBrainsMono_700Bold,
});
const {
habits,
loading,
addHabit,
updateHabit,
deleteHabit,
commitDay,
uncommitDay
} = useHabits();
const [screen, setScreen] = useState<Screen>("home");
const [editTarget, setEditTarget] = useState<Habit | null>(null);
const slideAnim = useRef(new Animated.Value(0)).current;
const navigateTo = (s: Screen, habit?: Habit | null) => {
setEditTarget(habit ?? null);
setScreen(s);
slideAnim.setValue(SCREEN_WIDTH);
Animated.spring(slideAnim, {
toValue: 0,
useNativeDriver: true,
tension: 80,
friction: 12,
}).start();
};
const navigateBack = () => {
Animated.timing(slideAnim, {
toValue: SCREEN_WIDTH,
duration: 220,
useNativeDriver: true,
}).start(() => {
setScreen("home");
setEditTarget(null);
});
};
const onLayoutRootView = useCallback(async () => {
if (fontsLoaded && !loading) {
await SplashScreen.hideAsync();
}
}, [fontsLoaded, loading]);
if (!fontsLoaded || loading) {
return (
<View
style={{
flex: 1,
backgroundColor: COLORS.bg,
justifyContent: "center",
alignItems: "center",
}}
>
<ActivityIndicator color={COLORS.textMuted} />
</View>
);
}
return (
<GestureHandlerRootView style={{ flex: 1 }} onLayout={onLayoutRootView}>
<View style={{ flex: 1, backgroundColor: COLORS.bg }}>
<View style={{ flex: 1 }}>
<HomeScreen
habits={habits}
commitDay={commitDay}
uncommitDay={uncommitDay}
deleteHabit={deleteHabit}
onNavigateCreate={() => navigateTo("create", null)}
onNavigateEdit={(habit) => navigateTo("edit", habit)}
/>
</View>
{(screen === "create" || screen === "edit") && (
<Animated.View
style={{
position: "absolute",
top: 0,
left: 0,
right: 0,
bottom: 0,
backgroundColor: COLORS.bg,
transform: [{ translateX: slideAnim }],
}}
>
<CreateHabitScreen
editHabit={editTarget}
addHabit={addHabit}
updateHabit={updateHabit}
deleteHabit={deleteHabit}
onBack={navigateBack}
/>
</Animated.View>
)}
</View>
</GestureHandlerRootView>
);
}