-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainScreen.js
More file actions
343 lines (275 loc) · 11.3 KB
/
MainScreen.js
File metadata and controls
343 lines (275 loc) · 11.3 KB
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
import React, { Component } from 'react';
import {
StyleSheet,
View,
Text,
TouchableHighlight
} from "react-native";
import Spotify from 'rn-spotify-sdk';
import { StackActions, NavigationActions } from 'react-navigation';
import Header from './comps/header.js';
import Art from './comps/art.js';
import Control from './comps/control.js';
import Track from './comps/track.js';
import PainSlider from './comps/slider.js';
import SessionControl from './comps/sessionControl.js';
import firebase from './comps/firebase.js';
import prefs from 'react-native-shared-preferences'
const PAIN_RECORD_THRESHOLD = .5;
export class MainScreen extends Component {
state = {
pain: 5,
lastRecordedPain: -1,
spotifyUserName: null,
playing: false,
shuffling: false,
repeating: false,
initialSession: true,
beforeSession: false,
inSession: false,
afterSession: false,
events : [],
songStates : [],
songAttributes : [],
//-1 before any song states occour, first song state is 0
curSongState : -1,
songIDs : []
// track: {
// id: "3FCto7hnn1shUyZL42YgfO",
// album: {images: [{url: "https://i.scdn.co/image/05adfbc8914bec4983675dec65c514dcab13beb6"}]},
// name: "Piano Man",
// artists: [{name: "Billy Joel"}]
// }
};
constructor()
{
super();
this.spotifyLogoutButtonWasPressed = this.spotifyLogoutButtonWasPressed.bind(this);
this.searchSong = this.searchSong.bind(this);
this.songEventHandler = this.songEventHandler.bind(this);
this.painEventHandler = this.painEventHandler.bind(this);
this.medEventHandler = this.medEventHandler.bind(this);
this.onSliderChange = this.onSliderChange.bind(this);
this.sessionStart = this.sessionStart.bind(this);
this.sessionEnd = this.sessionEnd.bind(this);
this.onMedYes = this.onMedYes.bind(this);
this.onMedNo = this.onMedNo.bind(this);
}
painEventHandler(type){
var newPainEvent = {eventType:type,date:new Date().getTime(),pain:this.state.pain, songState: this.state.curSongState}
var newEvents = this.state.events.concat(newPainEvent);
//Update state
this.setState({
events: newEvents,
lastRecordedPain: this.state.pain
});
}
songEventHandler(event, type){
//create new playbackEvent
var newPlaybackEvent = {eventType: type, date:new Date().getTime(), pain:this.state.pain, songState: this.state.curSongState};
//add to events
var newEvents = this.state.events.concat(newPlaybackEvent);
//create new songState
this.setState({curSongState:this.state.curSongState+=1})
var newSongState = {id: this.state.curSongState, state: event.state, songID: (event.metadata.currentTrack != null ? event.metadata.currentTrack.uri : -1)}
//add to SongStates
var newSongStates = this.state.songStates.concat(newSongState)
//Update state
this.setState({
playing: event.state.playing,
shuffling: event.state.shuffling,
repeating: event.state.repeating,
events: newEvents,
songStates: newSongStates
});
//If there is a track
if(event.metadata.currentTrack!=null) {
//if song uri is unique, add it to songIDs
var songAlreadyExists = this.state.songIDs.some(function (o) {
return o["songID"] == event.metadata.currentTrack.uri
})
if (!songAlreadyExists) {
//add to songIDs
var curSongIDs = this.state.songIDs
curSongIDs[event.metadata.currentTrack.uri] = event.metadata.currentTrack
this.setState({
songIDs: curSongIDs
});
//add to song attributes
//if uri exists, get id from URI
if(event.metadata.currentTrack.uri!=null) {
let songID = event.metadata.currentTrack.uri.substring(14)
var attributePromise = Spotify.getTrackAudioFeatures(songID)
attributePromise.then( (attributes)=> {
var curSongAttributes = this.state.songAttributes
curSongAttributes[event.metadata.currentTrack.uri] = attributes
console.log(curSongAttributes)
this.setState({
songAttributes: curSongAttributes
});
})
}
}
}
}
medEventHandler(tookMed){
var newMedEvent = {eventType: 'medEvent', data:new Date().getTime(), tookMed:tookMed, pain:this.state.pain, songState:this.state.curSongState}
var newEvents = this.state.events.concat(newMedEvent);
console.log(newEvents)
//Update state
this.setState({
events: newEvents
}, prefs.getItem("key",(val)=>{this.sendDataToFirebase(val)}));
}
componentDidMount()
{
//Listeners for spotify events
Spotify.addListener('metadataChange', (event)=>{ this.songEventHandler(event,'metadataChange')});
Spotify.addListener('play', (event)=>{ this.songEventHandler(event,'play')});
Spotify.addListener('pause', (event)=>{ this.songEventHandler(event,'pause')});
Spotify.addListener('shuffleStatusChange', (event)=>{ this.songEventHandler(event,'shuffleStatusChange')});
Spotify.addListener('repeatStatusChange', (event)=>{ this.songEventHandler(event,'repeatStatusChange')});
//Send api request to get user info
Spotify.getMe().then((result) => {
// update state with user info
this.setState({ spotifyUserName: result.display_name });
// play song
// return Spotify.playURI("spotify:track:"+this.state.track.id, 0, 0);
}).then((ret) => {
// success
}).catch((error) => {
// error
Alert.alert("Error", error.message);
});
}
goToInitialScreen()
{
const navAction = StackActions.reset({
index: 0,
actions: [
NavigationActions.navigate({ routeName: 'initial'})
]
});
this.props.navigation.dispatch(navAction);
}
spotifyLogoutButtonWasPressed()
{
Spotify.logout().finally(() => {
this.goToInitialScreen();
});
}
searchSong(event){
Spotify.search(event.nativeEvent.text, ['album', 'artist', 'playlist', 'track']).then((ret) => {
// success
this.setState({track: ret.tracks.items[0]}); //Most relevant song
return Spotify.playURI("spotify:track:" + ret.tracks.items[0].id, 0, 0); //Play the song
}).catch((error) => {
// error
console.log("Error: " + error);
});
}
onSliderChange(pain){
if(this.state.initialSession){
this.setState({initialSession: false, beforeSession: true})
}
this.setState({pain: pain})
//only record pain if in session
if(this.state.inSession) {
//if there is a significant difference between currect apin and last recorded pain, record pain
if (Math.abs(this.state.pain - this.state.lastRecordedPain) >= PAIN_RECORD_THRESHOLD) {
//record pain
this.painEventHandler('painChange');
}
}
}
sessionStart(){
this.setState({beforeSession: false, inSession: true});
this.painEventHandler('sessionStart')
}
sessionEnd(){
this.setState({inSession: false, afterSession: true});
this.painEventHandler('sessionEnd');
}
sendDataToFirebase(pKey){
const sessionsRef = firebase.database().ref('sessions');
const session = {
events: this.state.events,
songStates: this.state.songStates,
songIDs : this.state.songIDs,
songAttributes : this.state.songAttributes,
participant : pKey
};
let sessionKey = sessionsRef.push(session).key;
//add session key to participant
const participantSessions = firebase.database().ref('staticParticipantInfo').child(pKey).child("sessions");
// participantSessions.append(sessionKey)
//
// firebase.database().ref('staticParticipantInfo').child(pKey).child("sessions").set(participantSessions,()=>{console.log("Done Updating")})
var attrRef = firebase.database().ref('songAttributes');
for(var a in this.state.songAttributes){
attrRef.child(a).set(this.state.songAttributes[a])
}
this.setState({
events: [],
songStates :[],
songIDs: this.state.songIDs,
songAttributes :[]
});
}
onMedYes(){
this.setState({afterSession: false, initialSession: false})
this.medEventHandler(true)
}
onMedNo(){
this.setState({afterSession: false, initialSession: false})
this.medEventHandler(false)
}
render() {
return (
<View style={styles.container}>
<Header onLogoutPress={this.spotifyLogoutButtonWasPressed}
onSearch={this.searchSong}/>
<Art url={this.state.track ? this.state.track.album.images[0].url : null} />
<Track title={this.state.track ? this.state.track.name : null}
artist={this.state.track ? this.state.track.artists[0].name : null}/>
<Control paused={!this.state.playing}
shuffleOn={this.state.shuffling}
repeatOn={this.state.repeating}
onForward={()=> Spotify.skipToNext()}
onBack={()=> Spotify.skipToPrevious()}
setShuffling={() => Spotify.setShuffling(!this.state.shuffling)}
onPressRepeat={() => Spotify.setPlaying(!this.state.repeating)}
onPressPause={() => Spotify.setPlaying(!this.state.playing)}
/>
<SessionControl
initialSession={this.state.initialSession}
beforeSession={this.state.beforeSession}
inSession={this.state.inSession}
afterSession={this.state.afterSession}
onBegin= {this.sessionStart}
onEnd={this.sessionEnd}
onYes={this.onMedYes}
onNo={this.onMedNo}
onCancel={()=> this.setState({afterSession: false, inSession: true})}
/>
<PainSlider pain={this.state.pain}
onValueChange={this.onSliderChange} />
</View>
);
}
}
const styles = StyleSheet.create({
container: {
backgroundColor: 'rgb(4,4,4)',
flex: 1,
flexDirection: 'column',
textAlign: 'center',
color: 'rgba(255, 255, 255, 0.72)',
fontWeight: 'bold',
fontSize: 10
},
text: {
color: 'rgba(255, 255, 255, 0.72)',
fontWeight: 'bold',
}
});