-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
67 lines (60 loc) · 1.98 KB
/
App.tsx
File metadata and controls
67 lines (60 loc) · 1.98 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
import { StatusBar } from "expo-status-bar";
import React, { useEffect, useState } from "react";
import { SafeAreaView, StyleSheet, Text, View } from "react-native";
import FocusPage from "./src/features/focus/FocusPage";
import CountDown from "./src/features/timer/CountDown";
import TimerPage from "./src/features/timer/TimerPage";
import { IFocusItem } from "./src/utils/types";
import AsyncStorage from "@react-native-async-storage/async-storage";
export default function App() {
const [isFocusing, setIsFocusing] = useState(false);
const [focusItem, setFocusItem] = useState({ subject: "", completed: false } as IFocusItem);
const [focusHistory, setFocusHistory] = useState([] as IFocusItem[]);
const retrieveHistory = async () => {
try {
const history = await AsyncStorage.getItem("focusHistory");
if (history !== null) {
setFocusHistory(JSON.parse(history));
} else {
await AsyncStorage.setItem("focusHistory", JSON.stringify(focusHistory));
}
} catch (e) {
// error reading value
console.log(e);
}
};
const updateHistory = async () => {
try {
await AsyncStorage.setItem("focusHistory", JSON.stringify(focusHistory));
} catch (e) {
console.log(e);
}
};
useEffect(() => {
retrieveHistory();
}, []);
useEffect(() => {
updateHistory();
}, [focusHistory]);
useEffect(() => {
if (!isFocusing && focusItem.subject.length !== 0) {
setFocusHistory((old) => [...old, focusItem]);
setFocusItem({ subject: "", completed: false });
}
}, [isFocusing]);
return (
<SafeAreaView style={{ flex: 1 }}>
<StatusBar style="auto" />
{isFocusing ? (
<TimerPage setIsFocusing={setIsFocusing} focusItem={focusItem} setFocusItem={setFocusItem} />
) : (
<FocusPage
setIsFocusing={setIsFocusing}
focusItem={focusItem}
setFocusItem={setFocusItem}
focusHistory={focusHistory}
/>
)}
</SafeAreaView>
);
}