-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
201 lines (179 loc) · 5.77 KB
/
App.js
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import React, { useEffect, createContext, } from 'react';
import SplashScreen from 'react-native-splash-screen';
import { DefaultTheme, DarkTheme, NavigationContainer } from '@react-navigation/native';
import { createStackNavigator, TransitionPresets } from '@react-navigation/stack';
import TabNavigator from './src/components/tab';
import AsyncStorage from '@react-native-async-storage/async-storage';
import {
PermissionsAndroid,
StatusBar,
View,
useColorScheme,
} from 'react-native';
import TrackPlayer from 'react-native-track-player';
import { setAllSongs } from './src/redux/action';
import { getAll } from 'react-native-get-music-files';
import { useDispatch } from 'react-redux'
import FavouritesSongs from './src/components/Tab_screens/favsongs';
import AddToFavourites from './src/components/Audio_screens/AddToFavourite';
import SearchMusic from './src/components/Audio_screens/AudioSearch';
import AudioPlayer from './src/components/Player/AudioPlayer';
import ArtisBasedSongs from './src/components/Audio_screens/artistBasedSongs';
import AlbumSongs from './src/components/Audio_screens/albumSong';
const Stack = createStackNavigator();
export const AppContext = createContext();
const App = () => {
const isDarkMode = useColorScheme() === 'dark';
const theme = isDarkMode ? DarkTheme : DefaultTheme;
const dispatch = useDispatch()
useEffect(() => {
setTimeout(() => {
SplashScreen.hide();
}, 500);
})
useEffect(() => {
const initializePlayer = async () => {
try {
await TrackPlayer.setupPlayer();
} catch (error) {
console.error('Failed to initialize TrackPlayer', error);
}
};
const initializeFavSongs = async () => {
try {
const existingSongs = await AsyncStorage.getItem('favSongs');
if (!existingSongs) {
await AsyncStorage.setItem('favSongs', JSON.stringify([]));
}
} catch (e) {
console.error('Failed to initialize favSongs:', e);
}
};
initializeFavSongs()
initializePlayer();
return () => {
TrackPlayer.stop();
};
}, []);
useEffect(() => {
const requestPermissions = async () => {
try {
const grantedStorage = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE,
{
title: 'Storage Permission',
message: 'Your app needs access to storage to store audio and video files.',
buttonNeutral: 'Ask Me Later',
buttonNegative: 'Cancel',
buttonPositive: 'OK',
},
);
const grantedWriteStorage = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
{
title: 'Storage Permission',
message: 'Your app needs access to storage to store audio and video files.',
buttonNeutral: 'Ask Me Later',
buttonNegative: 'Cancel',
buttonPositive: 'OK',
},
);
if (
grantedStorage === PermissionsAndroid.RESULTS.GRANTED &&
grantedWriteStorage === PermissionsAndroid.RESULTS.GRANTED
) {
fetechAllSongs();
} else {
requestPermissions();
}
} catch (err) {
console.warn(err);
}
};
requestPermissions();
}, []);
const fetechAllSongs = async () => {
await getAll({
})
.then((filesOrError) => {
if (typeof filesOrError === 'string') {
console.error(filesOrError);
return;
}
const reversedFiles = filesOrError.reverse();
dispatch(setAllSongs(reversedFiles));
})
.catch((error) => {
console.error(error);
});
}
return (
<AppContext.Provider value={{ fetechAllSongs }}>
<View style={{ flex: 1, }}>
<StatusBar translucent backgroundColor="transparent"
barStyle={isDarkMode ? 'light-content' : 'dark-content'}
/>
<NavigationContainer theme={theme}>
<Stack.Navigator>
<Stack.Screen
name="Tabs"
component={TabNavigator}
options={{
headerShown: false,
}}
/>
<Stack.Screen
name="Favourites"
component={FavouritesSongs}
options={{
headerShown: false,
...TransitionPresets.SlideFromRightIOS,
}}
/>
<Stack.Screen
name="AddToFavourites"
component={AddToFavourites}
options={{
headerShown: false,
...TransitionPresets.SlideFromRightIOS,
}}
/>
<Stack.Screen
name="SearchMusic"
component={SearchMusic}
options={{
headerShown: false,
...TransitionPresets.SlideFromRightIOS,
}}
/>
<Stack.Screen
name="AudioPlayer"
component={AudioPlayer}
options={{
headerShown: false,
...TransitionPresets.ModalSlideFromBottomIOS,
}}
/>
<Stack.Screen
name="artistBasedSongs"
component={ArtisBasedSongs}
options={{
headerShown: false,
...TransitionPresets.SlideFromRightIOS,
}}
/>
<Stack.Screen
name="albumbasesongs"
component={AlbumSongs}
options={{
headerShown: false,
...TransitionPresets.SlideFromRightIOS,
}}
/>
</Stack.Navigator>
</NavigationContainer>
</View>
</AppContext.Provider>
);
};
export default App;