-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
FlashMessage.jsx
71 lines (59 loc) · 1.67 KB
/
FlashMessage.jsx
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
import * as React from 'react';
import { useEffect, useRef, useState } from 'react';
import {
Animated,
StyleSheet,
} from 'react-native';
import { Queue, wait } from './util/async';
import { useSelector } from './redux/hooks';
import Colors from './constants/Colors';
const FlashMessage = () => {
const { message, paused } = useSelector(({ ui }) => ({
message: ui.flashMessage,
paused: ui.paused
}))
const opacity = useRef(new Animated.Value(0)).current;
const queue = useRef(/** @type {Queue<{ text: string, stamp: number, options?: any }>} */(null));
const [text, setText] = useState(null);
useEffect(() => {
queue.current = new Queue();
const q = queue.current;
q.loop(async message => {
setText(message.text);
await new Promise(resolve => {
Animated.timing(opacity, { toValue: 1, useNativeDriver: true }).start(resolve);
});
await wait(3000);
await new Promise(resolve => {
Animated.timing(opacity, { toValue: 0, useNativeDriver: true }).start(resolve);
});
});
return () => q.done();
}, []);
useEffect(() => {
if (message && queue.current)
queue.current.push(message);
}, [message]);
if (paused)
return null;
return (
<Animated.Text style={[styles.flashMessage, { opacity }]}
allowFontScaling
adjustsFontSizeToFit>
{text}
</Animated.Text>
);
};
const styles = StyleSheet.create({
flashMessage: {
backgroundColor: Colors.activeColor,
color: 'white',
fontSize: 18,
textAlign: 'center',
position: 'absolute',
top: 50,
padding: 10,
width: '100%',
}
});
export default FlashMessage;