-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
128 lines (126 loc) · 2.95 KB
/
App.tsx
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* eslint-disable @typescript-eslint/no-unused-vars */
import React, {
ReactNode,
Ref,
useEffect,
useMemo,
useRef,
useState,
} from 'react';
import {
Alert,
BackHandler,
StatusBar,
StyleSheet,
NativeEventEmitter,
NativeModules,
} from 'react-native';
import MinBar, {BarRef} from '@/components/minBar';
import {SafeAreaProvider} from 'react-native-safe-area-context';
import MusicStore, {useMusic} from '@/store/music';
import Router from '@/routes';
import ThemeStore from '@/store/theme';
import {
useCreation,
useMemoizedFn,
useMount,
useSafeState,
useUnmount,
} from 'ahooks';
import musicTools from '@/utils/musicTools';
import {debounce} from 'lodash';
import {OnProgressData} from 'react-native-video';
const styles = StyleSheet.create({
app: {
flex: 1,
position: 'relative',
},
});
interface MusicNotifyProps {
children: ReactNode;
}
const MusicNotify = (props: MusicNotifyProps) => {
const eventEmitter = new NativeEventEmitter(NativeModules.ToastExample);
const {playOrPause, nextSong, lastSong} = useMusic();
const onNotifyCallback = useMemoizedFn(event => {
switch (event.status) {
case 'playOrPause':
playOrPause();
break;
case 'nextSong':
nextSong();
break;
case 'lastSong':
lastSong();
break;
default:
break;
}
});
const notifyCallback = useRef(
debounce(q => onNotifyCallback(q), 500),
).current;
eventEmitter.addListener('notifyCallback', notifyCallback);
return <>{props.children}</>;
};
export default function App() {
const {nextSong} = useMusic();
const [isMounted, setMounted] = useSafeState<boolean>(false);
const barRef = useRef<BarRef>(null);
useMount(() => {
setTimeout(() => {
setMounted(true);
}, 50);
});
useEffect(() => {
const backAction = () => {
Alert.alert('提示', '是否退出app', [
{
text: '否',
onPress: () => null,
style: 'cancel',
},
{
text: '是',
onPress: () => {
musicTools.closeNotify();
BackHandler.exitApp();
},
},
]);
return true;
};
const backHandler = BackHandler.addEventListener(
'hardwareBackPress',
backAction,
);
return () => backHandler.remove();
}, []);
useUnmount(() => {
setMounted(false);
});
return (
<SafeAreaProvider style={styles.app}>
{/* 沉浸式状态栏 */}
<StatusBar
translucent={isMounted}
backgroundColor="transparent"
barStyle="dark-content"
/>
<ThemeStore>
<MusicStore
onEnd={() => {
nextSong();
}}
onProgress={data => {
barRef.current && barRef.current.setProgress?.(data);
}}>
<MusicNotify>
<Router />
<MinBar cRef={barRef} />
</MusicNotify>
</MusicStore>
</ThemeStore>
</SafeAreaProvider>
);
}