2022-07-03 01:50:01 +00:00
|
|
|
import AsyncStorage from '@react-native-async-storage/async-storage';
|
|
|
|
import React, {useEffect, useState} from 'react';
|
2022-07-04 04:03:48 +00:00
|
|
|
import {StyleSheet, Text} from 'react-native';
|
|
|
|
import {Button, Modal, Portal} from 'react-native-paper';
|
2022-07-03 01:50:01 +00:00
|
|
|
|
2022-07-04 04:03:48 +00:00
|
|
|
export default function Alarm() {
|
|
|
|
const [show, setShow] = useState(false);
|
2022-07-03 01:50:01 +00:00
|
|
|
const [seconds, setSeconds] = useState(0);
|
|
|
|
const [minutes, setMinutes] = useState(0);
|
|
|
|
let intervalId: number;
|
|
|
|
|
|
|
|
useEffect(() => {
|
2022-07-04 04:03:48 +00:00
|
|
|
intervalId = setInterval(async () => {
|
|
|
|
const next = await AsyncStorage.getItem('nextAlarm');
|
2022-07-03 01:50:01 +00:00
|
|
|
if (!next) return;
|
|
|
|
const ms = new Date(next).getTime() - new Date().getTime();
|
|
|
|
if (ms <= 0) return;
|
|
|
|
let secondsLeft = ms / 1000;
|
2022-07-04 04:03:48 +00:00
|
|
|
secondsLeft--;
|
|
|
|
if (secondsLeft <= 0) return clearInterval(intervalId);
|
2022-07-03 06:25:21 +00:00
|
|
|
setSeconds(Math.floor(secondsLeft % 60));
|
2022-07-03 01:50:01 +00:00
|
|
|
setMinutes(Math.floor(secondsLeft / 60));
|
2022-07-04 04:03:48 +00:00
|
|
|
}, 1000);
|
2022-07-03 01:50:01 +00:00
|
|
|
return () => clearInterval(intervalId);
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return (
|
2022-07-04 04:03:48 +00:00
|
|
|
<>
|
|
|
|
<Portal>
|
|
|
|
<Modal
|
|
|
|
visible={show}
|
|
|
|
style={styles.center}
|
|
|
|
onDismiss={() => setShow(false)}>
|
|
|
|
<Text style={[styles.center, styles.title]}>Resting</Text>
|
|
|
|
<Text style={styles.center}>
|
|
|
|
{minutes}:{seconds}
|
|
|
|
</Text>
|
|
|
|
<Button mode="contained" onPress={() => setShow(false)}>
|
|
|
|
Close
|
|
|
|
</Button>
|
|
|
|
</Modal>
|
|
|
|
</Portal>
|
|
|
|
<Button icon="time" onPress={() => setShow(true)}>
|
|
|
|
Time left
|
|
|
|
</Button>
|
|
|
|
</>
|
2022-07-03 01:50:01 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
2022-07-04 04:03:48 +00:00
|
|
|
center: {
|
2022-07-03 01:50:01 +00:00
|
|
|
alignItems: 'center',
|
2022-07-04 04:03:48 +00:00
|
|
|
alignSelf: 'center',
|
|
|
|
marginBottom: 10,
|
2022-07-03 01:50:01 +00:00
|
|
|
},
|
2022-07-04 04:03:48 +00:00
|
|
|
title: {
|
|
|
|
fontSize: 18,
|
2022-07-03 01:50:01 +00:00
|
|
|
},
|
|
|
|
});
|