|
1 |
| -import React, { useEffect, useState } from "react"; |
| 1 | +import React, { useState } from "react"; |
2 | 2 |
|
3 | 3 | import { StyleSheet, View } from "react-native";
|
4 | 4 |
|
5 |
| -import Animated, { |
6 |
| - FadeIn, |
7 |
| - FadeInUp, |
8 |
| - FadeOut, |
9 |
| - useAnimatedStyle, |
10 |
| - useSharedValue, |
11 |
| - withRepeat, |
12 |
| - withTiming, |
13 |
| -} from "react-native-reanimated"; |
| 5 | +import Animated, { FadeIn, FadeOut } from "react-native-reanimated"; |
14 | 6 |
|
15 |
| -import { Feather } from "@expo/vector-icons"; |
| 7 | +import { BreatheState } from "./Breathe.interface"; |
| 8 | +import { BreatheCountdown } from "./BreatheCountdown"; |
| 9 | +import { BreatheSuccess } from "./BreatheSuccess"; |
| 10 | +import BreatheTimer from "./BreatheTimer"; |
| 11 | +import { commonStyles } from "./common.styles"; |
| 12 | +import { BreatheProvider } from "./context/BreatheProvider"; |
16 | 13 |
|
17 | 14 | import { useColor } from "~/color";
|
18 |
| -import { BackButton } from "~/components/BackButton"; |
19 |
| - |
20 | 15 | import { Box } from "~/components/Box";
|
21 | 16 | import { Breather } from "~/components/Breather";
|
22 |
| -import { Bold } from "~/components/Text"; |
23 |
| -import { Palette } from "~/styles"; |
| 17 | +import { Spacer } from "~/components/Spacer"; |
24 | 18 |
|
25 | 19 | export function Breathe() {
|
26 | 20 | const { color } = useColor();
|
27 |
| - const [counter, setCounter] = useState(5); |
28 |
| - const [timer, setTimer] = useState(30); |
29 |
| - const [animate, setAnimate] = useState(false); |
30 |
| - |
31 |
| - const countdown = useSharedValue<number>(0); |
32 |
| - |
33 |
| - useEffect(() => { |
34 |
| - const interval = setInterval(() => { |
35 |
| - setCounter((prev) => { |
36 |
| - if (prev) { |
37 |
| - return prev - 1; |
38 |
| - } else { |
39 |
| - clearInterval(interval); |
40 |
| - setAnimate(true); |
41 |
| - } |
42 |
| - |
43 |
| - return prev; |
44 |
| - }); |
45 |
| - }, 1000); |
46 |
| - |
47 |
| - return () => { |
48 |
| - clearInterval(interval); |
49 |
| - }; |
50 |
| - }, []); |
51 |
| - |
52 |
| - useEffect(() => { |
53 |
| - let interval: NodeJS.Timeout; |
| 21 | + const store = useState(BreatheState.COUNTDOWN); |
| 22 | + const [timer, setTimer] = useState(0); |
54 | 23 |
|
55 |
| - if (!counter) { |
56 |
| - interval = setInterval(() => { |
57 |
| - setTimer((prev) => { |
58 |
| - if (prev) { |
59 |
| - return prev - 1; |
60 |
| - } else { |
61 |
| - clearInterval(interval); |
62 |
| - } |
63 |
| - |
64 |
| - return prev; |
65 |
| - }); |
66 |
| - }, 1000); |
67 |
| - } |
68 |
| - |
69 |
| - return () => { |
70 |
| - clearInterval(interval); |
71 |
| - }; |
72 |
| - }, [counter]); |
73 |
| - |
74 |
| - useEffect(() => { |
75 |
| - countdown.value = withRepeat( |
76 |
| - withTiming(1, { |
77 |
| - duration: 500, |
78 |
| - }), |
79 |
| - 10, |
80 |
| - true |
81 |
| - ); |
82 |
| - }, []); |
| 24 | + const [state] = store; |
83 | 25 |
|
84 | 26 | return (
|
85 |
| - <Box |
86 |
| - style={[ |
87 |
| - styles.wrapper, |
88 |
| - { |
89 |
| - backgroundColor: color, |
90 |
| - }, |
91 |
| - ]} |
92 |
| - > |
93 |
| - <Breather animate={animate} /> |
94 |
| - <View style={styles.content}> |
95 |
| - {/* {!!counter && ( |
96 |
| - <Animated.Text |
97 |
| - entering={FadeIn} |
98 |
| - layout={FadeInUp} |
99 |
| - exiting={FadeOut} |
100 |
| - > |
101 |
| - <Bold style={styles.countdown}>{counter}</Bold> |
102 |
| - </Animated.Text> |
103 |
| - )} |
104 |
| - {!!timer && !counter && ( |
105 |
| - <Animated.Text |
106 |
| - entering={FadeIn} |
107 |
| - layout={FadeInUp} |
108 |
| - exiting={FadeOut} |
109 |
| - > |
110 |
| - <Bold style={styles.countdown}>{timer}</Bold> |
111 |
| - </Animated.Text> |
112 |
| - )} |
113 |
| - {!timer && !countdown && ( */} |
114 |
| - <Animated.View entering={FadeInUp}></Animated.View> |
115 |
| - {/* )} */} |
116 |
| - </View> |
117 |
| - <View style={styles.infoBox}> |
118 |
| - <BackButton /> |
119 |
| - </View> |
120 |
| - </Box> |
| 27 | + <BreatheProvider value={store}> |
| 28 | + <Box |
| 29 | + style={[ |
| 30 | + commonStyles.wrapper, |
| 31 | + { |
| 32 | + backgroundColor: color, |
| 33 | + }, |
| 34 | + ]} |
| 35 | + > |
| 36 | + <Breather animate={state === BreatheState.TIMER} /> |
| 37 | + <View style={[commonStyles.content, styles.container]}> |
| 38 | + <Spacer header /> |
| 39 | + {state === BreatheState.COUNTDOWN && ( |
| 40 | + <Animated.View |
| 41 | + exiting={FadeOut.duration(500)} |
| 42 | + style={commonStyles.content} |
| 43 | + > |
| 44 | + <BreatheCountdown /> |
| 45 | + </Animated.View> |
| 46 | + )} |
| 47 | + |
| 48 | + {state === BreatheState.TIMER && ( |
| 49 | + <Animated.View |
| 50 | + entering={FadeIn.delay(500).duration(500)} |
| 51 | + exiting={FadeOut.duration(500)} |
| 52 | + style={commonStyles.content} |
| 53 | + > |
| 54 | + <BreatheTimer setTimer={setTimer} /> |
| 55 | + </Animated.View> |
| 56 | + )} |
| 57 | + |
| 58 | + {state === BreatheState.SUCCESS && ( |
| 59 | + <Animated.View |
| 60 | + entering={FadeIn.delay(500).duration(500)} |
| 61 | + exiting={FadeOut.duration(1000)} |
| 62 | + style={commonStyles.content} |
| 63 | + > |
| 64 | + <BreatheSuccess time={timer} /> |
| 65 | + </Animated.View> |
| 66 | + )} |
| 67 | + </View> |
| 68 | + </Box> |
| 69 | + </BreatheProvider> |
121 | 70 | );
|
122 | 71 | }
|
123 | 72 |
|
124 | 73 | const styles = StyleSheet.create({
|
125 |
| - content: { |
126 |
| - ...StyleSheet.absoluteFillObject, |
127 |
| - alignItems: "center", |
128 |
| - justifyContent: "center", |
129 |
| - }, |
130 |
| - countdown: { |
131 |
| - color: Palette.FOREGROUND, |
132 |
| - fontSize: 72, |
133 |
| - }, |
134 |
| - infoBox: { |
135 |
| - alignItems: "center", |
136 |
| - bottom: 50, |
137 |
| - gap: 10, |
138 |
| - justifyContent: "center", |
139 |
| - left: 0, |
140 |
| - position: "absolute", |
141 |
| - right: 0, |
142 |
| - zIndex: 999, |
143 |
| - }, |
144 |
| - wrapper: { |
145 |
| - position: "relative", |
| 74 | + container: { |
| 75 | + marginHorizontal: 20, |
146 | 76 | },
|
147 | 77 | });
|
0 commit comments