2022-07-03 01:50:01 +00:00
|
|
|
import AsyncStorage from '@react-native-async-storage/async-storage';
|
2022-07-06 04:27:36 +00:00
|
|
|
import {useNavigation} from '@react-navigation/native';
|
2022-07-03 01:50:01 +00:00
|
|
|
import React, {useEffect, useState} from 'react';
|
|
|
|
import {
|
|
|
|
FlatList,
|
2022-07-03 06:25:21 +00:00
|
|
|
NativeModules,
|
2022-07-03 01:50:01 +00:00
|
|
|
SafeAreaView,
|
|
|
|
StyleSheet,
|
|
|
|
View,
|
|
|
|
} from 'react-native';
|
2022-07-05 03:33:42 +00:00
|
|
|
import {AnimatedFAB, Searchbar} from 'react-native-paper';
|
2022-07-06 04:27:36 +00:00
|
|
|
import {getSets} from './db';
|
2022-07-03 01:50:01 +00:00
|
|
|
import EditSet from './EditSet';
|
|
|
|
|
2022-07-04 04:03:48 +00:00
|
|
|
import Set from './set';
|
2022-07-04 11:35:15 +00:00
|
|
|
import SetItem from './SetItem';
|
2022-07-03 01:50:01 +00:00
|
|
|
|
|
|
|
const limit = 20;
|
|
|
|
|
2022-07-04 04:03:48 +00:00
|
|
|
export default function Home() {
|
2022-07-03 01:50:01 +00:00
|
|
|
const [sets, setSets] = useState<Set[]>();
|
|
|
|
const [id, setId] = useState<number>();
|
|
|
|
const [offset, setOffset] = useState(0);
|
|
|
|
const [showEdit, setShowEdit] = useState(false);
|
|
|
|
const [search, setSearch] = useState('');
|
2022-07-06 04:27:36 +00:00
|
|
|
const [refreshing, setRefresing] = useState(false);
|
|
|
|
const navigation = useNavigation();
|
2022-07-03 01:50:01 +00:00
|
|
|
|
|
|
|
const refresh = async () => {
|
2022-07-06 04:27:36 +00:00
|
|
|
setRefresing(true);
|
|
|
|
const [result] = await getSets({search, limit, offset: 0}).finally(() =>
|
|
|
|
setRefresing(false),
|
2022-07-03 01:50:01 +00:00
|
|
|
);
|
|
|
|
if (!result) return setSets([]);
|
|
|
|
setSets(result.rows.raw());
|
|
|
|
setOffset(0);
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
refresh();
|
|
|
|
}, [search]);
|
|
|
|
|
2022-07-06 04:27:36 +00:00
|
|
|
useEffect(() => navigation.addListener('focus', refresh), [navigation]);
|
|
|
|
|
2022-07-03 01:50:01 +00:00
|
|
|
const renderItem = ({item}: {item: Set}) => (
|
2022-07-04 11:35:15 +00:00
|
|
|
<SetItem
|
|
|
|
item={item}
|
2022-07-03 01:50:01 +00:00
|
|
|
key={item.id}
|
2022-07-04 11:35:15 +00:00
|
|
|
setShowEdit={setShowEdit}
|
|
|
|
setId={setId}
|
|
|
|
onRemove={refresh}
|
2022-07-03 01:50:01 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
const save = async () => {
|
|
|
|
refresh();
|
|
|
|
const enabled = await AsyncStorage.getItem('alarmEnabled');
|
|
|
|
if (enabled !== 'true') return;
|
|
|
|
const minutes = await AsyncStorage.getItem('minutes');
|
|
|
|
const seconds = await AsyncStorage.getItem('seconds');
|
|
|
|
const milliseconds = Number(minutes) * 60 * 1000 + Number(seconds) * 1000;
|
2022-07-03 06:25:21 +00:00
|
|
|
NativeModules.AlarmModule.timer(milliseconds);
|
2022-07-03 01:50:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const next = async () => {
|
2022-07-06 04:27:36 +00:00
|
|
|
setRefresing(true);
|
2022-07-03 01:50:01 +00:00
|
|
|
const newOffset = offset + limit;
|
2022-07-06 04:27:36 +00:00
|
|
|
const [result] = await getSets({search, limit, offset: newOffset}).finally(
|
|
|
|
() => setRefresing(false),
|
2022-07-03 01:50:01 +00:00
|
|
|
);
|
2022-07-04 11:35:15 +00:00
|
|
|
if (result.rows.length === 0) return;
|
2022-07-03 01:50:01 +00:00
|
|
|
if (!sets) return;
|
|
|
|
setSets([...sets, ...result.rows.raw()]);
|
|
|
|
setOffset(newOffset);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<SafeAreaView style={styles.container}>
|
2022-07-04 04:17:29 +00:00
|
|
|
<Searchbar placeholder="Search" value={search} onChangeText={setSearch} />
|
2022-07-03 01:50:01 +00:00
|
|
|
<FlatList
|
2022-07-06 04:27:36 +00:00
|
|
|
style={{height: '90%'}}
|
2022-07-03 01:50:01 +00:00
|
|
|
data={sets}
|
|
|
|
renderItem={renderItem}
|
|
|
|
keyExtractor={set => set.id.toString()}
|
2022-07-06 04:27:36 +00:00
|
|
|
onEndReached={next}
|
|
|
|
refreshing={refreshing}
|
|
|
|
onRefresh={refresh}
|
2022-07-03 01:50:01 +00:00
|
|
|
/>
|
2022-07-06 09:03:56 +00:00
|
|
|
<EditSet
|
|
|
|
clearId={() => setId(undefined)}
|
|
|
|
id={id}
|
|
|
|
show={showEdit}
|
|
|
|
setShow={setShowEdit}
|
|
|
|
onSave={save}
|
|
|
|
/>
|
2022-07-05 00:43:23 +00:00
|
|
|
|
|
|
|
<AnimatedFAB
|
|
|
|
extended={false}
|
|
|
|
label="Add"
|
|
|
|
icon="add"
|
|
|
|
style={{position: 'absolute', right: 20, bottom: 20}}
|
|
|
|
onPress={() => {
|
|
|
|
setId(undefined);
|
|
|
|
setShowEdit(true);
|
|
|
|
}}
|
|
|
|
/>
|
2022-07-03 01:50:01 +00:00
|
|
|
</SafeAreaView>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
name: {
|
|
|
|
fontSize: 18,
|
|
|
|
},
|
|
|
|
container: {
|
|
|
|
flex: 1,
|
2022-07-04 04:03:48 +00:00
|
|
|
padding: 10,
|
2022-07-03 01:50:01 +00:00
|
|
|
},
|
|
|
|
bottom: {
|
|
|
|
alignSelf: 'center',
|
|
|
|
flexDirection: 'row',
|
|
|
|
},
|
|
|
|
});
|