-
Notifications
You must be signed in to change notification settings - Fork 2
/
AppContextProvider.js
350 lines (318 loc) · 11.1 KB
/
AppContextProvider.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import React, { Component } from "react";
import { Dark, Light } from "./styles/theme-context";
import { AsyncStorage, StatusBar } from "react-native";
const Context = React.createContext();
import SoundPlayer from "react-native-sound-player";
import MusicControl from "react-native-music-control";
import changeNavigationBarColor from 'react-native-navigation-bar-color';
import libraryData from "./assets/data/libraryData"
export class AppContextProvider extends Component {
state = {
theme: Light,
setTheme: theme => {
if (theme == 0) {
this.setState({ theme: Light });
StatusBar.setBarStyle("dark-content");
} else if (theme == 1) {
this.setState({ theme: Dark });
StatusBar.setBarStyle("light-content");
} else {
let date = new Date();
let hrs = date.getHours();
const themeByHour = hrs >= 19 || hrs <= 6 ? Dark : Light;
const barColor = hrs >= 19 || hrs <= 6 ? "light-content" : "dark-content";
this.setState({ themePanel: false, theme: themeByHour });
StatusBar.setBarStyle(barColor);
}
},
loadedMusic: false,
notifShowing: false,
playerState: 0,
playing: false,
loading: true,
duration: 0,
startValue: 0,
presentPosition: 0,
searchValue: "",
repeat: true,
songState: {
title: "Em gái mưa",
artist: {
name: "Hương Tràm",
id: 1
},
songImage: require("./assets/huongtram.jpg"),
},
changeSongState: (title, artist, songImage) => {
this.setState({
songState: {
title: title,
artist: {
name: artist
},
songImage: songImage || require("./assets/huongtram.jpg")
}
});
this.state.showControlNotif();
},
albumName: "Album Hồng Nhung",
albumState: {
albumName: "Nguoi hay quen em di",
albumActor: "abc",
songImage: require("./assets/huongtram.jpg"),
subplaylists: [],
},
changeAlbumState: (albumName, albumActor, songImage, subplaylists) => {
this.setState({
albumState: {
albumName: albumName,
albumActor: albumActor,
songImage: songImage || require("./assets/huongtram.jpg"),
subplaylists: subplaylists
}
})
this.state.showControlNotif();
},
artistState: {
artistName: "Huong Tram",
artistNumberLike: "1.2tr",
songImage: require("./assets/huongtram.jpg"),
playlists: [],
},
changeArtistState: async (image, numberLike, artistName, playlists) => {
await this.setState({ artistState: { songImage: image , artistNumberLike: numberLike, artistName: artistName, playlists: playlists } });
this.state.showControlNotif();
},
libraryState: {
playlist: libraryData.data,
subplaylist: [],
},
changeLibraryState: (subplaylist) => {
// this.state.libraryState.subplaylist = subplaylist;
this.setState({ libraryState: { subplaylist: subplaylist } });
},
addMusicToLibrary: (key) => {
// console.log(this.state.libraryState)
let newLib = this.state.libraryState.playlist
newLib[key].playlist.push({ name: this.state.songState.title, actorName: this.state.songState.artist.name })
// this.setState({libraryState: { playlist : newLib}})
// console.log(this.state.libraryState)
},
icons: [
require("./assets/icon-album/0-03.png"),
require("./assets/icon-album/1-03.png"),
require("./assets/icon-album/2-03.png"),
require("./assets/icon-album/3-03.png"),
require("./assets/icon-album/4-03.png"),
require("./assets/icon-album/5-03.png"),
require("./assets/icon-album/6-03.png"),
require("./assets/icon-album/7-03.png"),
require("./assets/icon-album/8-03.png"),
require("./assets/icon-album/9-03.png"),
],
createNewPlaylist: (name) => {
// this.state.libraryState.playlist.push({playlistName: name, playlist: []});
let newListState = this.state.libraryState.playlist;
newListState.push({ playlistName: name, playlist: [], image: this.state.icons[Math.floor(Math.random() * 10)] });
// console.log(name)
// this.setState({libraryState: {playlist: newListState}});
},
updateState: someDict => {
this.setState(someDict);
},
loadMusic: () => {
try {
console.log("USE CONTEXT");
console.log("USE CONTEXT" + this.state);
// play the file mp3 located at /android/app/src/main/res/raw/
SoundPlayer.loadSoundFile("a", "mp3");
// play from mp3. IT'S WORKING
// SoundPlayer.playUrl('https://data25.chiasenhac.com/downloads/2039/6/2038231-e4db0911/128/Het%20Thuong%20Can%20Nho%20-%20Duc%20Phuc.mp3')
this.state.getInfo();
} catch (e) {
console.log("Task failed successfully", e);
}
},
getInfo: async () => {
try {
const info = await SoundPlayer.getInfo();
// this.setState({ duration: info.duration });
this.setState({ duration: info.duration });
console.log("getInfo: ", info); // {duration: 12.416, currentTime: 7.691}
} catch (e) {
console.log("There is no song playing", e);
}
},
play: () => {
//Playing on notif
MusicControl.updatePlayback({
state: MusicControl.STATE_PLAYING
});
this.setState({ playing: true });
// if (!this.state.notifShowing) {
this.state.showControlNotif();
// }
console.log("play pressed");
console.log("state playing now is: " + this.state.playing);
SoundPlayer.play();
},
pause: () => {
MusicControl.updatePlayback({
state: MusicControl.STATE_PAUSED
});
this.setState({ playing: false });
SoundPlayer.pause();
},
onPlayBtn: () => {
console.log("on play button working");
let xplay = !this.state.playing;
if (xplay) {
this.state.play();
} else {
this.state.pause();
}
console.log("playing: " + this.state.playing);
},
getCurrentTime: async () => {
try {
const info2 = await SoundPlayer.getInfo();
console.log(info2);
this.setState({ presentPosition: info2.currentTime });
// console.log("currrentTime:" + info2.currentTime)
} catch (e) {
console.log("Can not get current time", e);
}
},
showControlNotif: () => {
this.state.configNotification()
//change state in context
this.setState({
notifShowing: true
});
//show notification
MusicControl.setNowPlaying({
title: this.state.songState.title,
artwork: this.state.songState.songImage, // URL or RN's image require()
artist: this.state.songState.artist["name"],
album: this.state.albumName,
genre: "Post-disco, Rhythm and Blues, Funk, Dance-pop",
duration: this.state.duration, // (Seconds)
description: "Một vài mô tả về bài hát", // Android Only
color: 0xffffff, // Notification Color - Android Only
date: "1983-01-02T00:00:00Z", // Release Date (RFC 3339) - Android Only
rating: 84, // Android Only (Boolean or Number depending on the type)
notificationIcon: "grade" // Android Only (String), Android Drawable resource name for a custom notification icon
});
},
onFinishPlay: () => {
if (this.state.repeat) {
console.log("rePLay");
//replay
this.state.replay();
MusicControl.updatePlayback({
state: MusicControl.STATE_PLAYING // (STATE_ERROR, STATE_STOPPED, STATE_PLAYING, STATE_PAUSED, STATE_BUFFERING)
});
} else {
//stop
console.log("Stop");
SoundPlayer.seek(0);
SoundPlayer.pause();
MusicControl.updatePlayback({
state: MusicControl.STATE_STOPPED // (STATE_ERROR, STATE_STOPPED, STATE_PLAYING, STATE_PAUSED, STATE_BUFFERING)
});
this.setState({
playing: false
});
}
},
replay: () => {
this.setState({ presentPosition: 0 });
SoundPlayer.seek(0);
SoundPlayer.play();
},
onNextTrack: () => {
console.log("NEXT TRACK");
},
onPreviousTrack: () => {
console.log("PREVIOUS TRACK");
},
onCloseNotification: () => {
console.log("CLOSE NOTIFICATION");
this.state.pause();
MusicControl.stopControl();
this.setState({ notifShowing: false })
},
configNotification: () => {
MusicControl.enableBackgroundMode(true);
MusicControl.on("play", () => {
this.state.play();
});
// on iOS this event will also be triggered by audio router change events
// happening when headphones are unplugged or a bluetooth audio peripheral disconnects from the device
MusicControl.on("pause", () => {
this.state.pause();
});
MusicControl.on("stop", () => {
this.state.onFinishPlay();
});
MusicControl.on("nextTrack", () => {
this.state.onNextTrack();
});
MusicControl.on("previousTrack", () => {
this.state.onPreviousTrack();
});
MusicControl.on("skipForward", () => {
this.state.onCloseNotification();
});
// Basic Controls
MusicControl.enableControl("play", true);
MusicControl.enableControl("pause", true);
MusicControl.enableControl("stop", false);
MusicControl.enableControl("nextTrack", true);
MusicControl.enableControl("previousTrack", true);
// Changing track position on lockscreen
MusicControl.enableControl("changePlaybackPosition", true);
// Seeking
MusicControl.enableControl("seekForward", false); // iOS only
MusicControl.enableControl("seekBackward", false); // iOS only
MusicControl.enableControl("seek", true); // Android only
MusicControl.enableControl("skipForward", true);
MusicControl.enableControl("skipBackward", false);
// Default - Allow user to close notification on swipe when audio is paused
MusicControl.enableControl("closeNotification", true, { when: "always" });
// MusicControl.enableControl('closeNotification', true, {when: 'always'})
}
};
componentDidMount = async () => {
this.state.setTheme(await AsyncStorage.getItem("theme"));
console.log("MOUNT APP CONTEXT");
this.state.loadMusic();
setInterval(() => {
this.state.getCurrentTime();
}, 1000);
//onFinishPlay
_onFinishedPlayingSubscription = SoundPlayer.addEventListener(
"FinishedPlaying",
({ success }) => {
console.log("finished playing", success);
this.state.onFinishPlay();
}
);
_onFinishedLoadingSubscription = SoundPlayer.addEventListener(
"FinishedLoading",
({ success }) => {
this.setState({ loadedMusic: success });
}
);
};
render() {
const { theme, artistState, albumState, libraryState } = this.state;
return (
<Context.Provider value={this.state} theme={theme} artistState={artistState} albumState={albumState} libraryState={libraryState}>
{this.props.children}
</Context.Provider>
);
}
}
export const ThemeContext = Context;
export const AppConsumer = Context.Consumer;