import { PlaylistModel } from '../models/playlist.model' ; import { SharedModel } from '../models/shared.model' ; import { SOPUtils } from './sop-utils' ; interface IListViewState { playlists : Array < PlaylistModel > ; sharedlist : Array < SharedModel > ; } export interface IListViewResult { ready : boolean ; playing ? : boolean ; trackId ? : string ; activeShoutOutPath ? : string ; state ? : IListViewState ; msg ? : string ; } /**
 * List view helper to aid in state updates throughout all list views
 */ export class ListViewHelper { public static update ( state , trackId , activeList , activeShoutOutPath ? , playlistId ? ) { switch ( activeList ) { case 'playlists' : return ListViewHelper . updatePlaylists ( trackId , state , activeShoutOutPath , playlistId ) ; case 'shared' : return ListViewHelper . updateShared ( trackId , state , activeShoutOutPath ) ; default : return { ready : false } ; } } public static updatePlaylists ( trackId , state , activeShoutOutPath ? , playlistId ? ) { let playlists = [ ... state . firebase . playlists ] ; let shoutouts = [ ... state . firebase . shoutouts ] ; let sharedlist = ListViewHelper . reset ( [ ... state . firebase . sharedlist ] ) ; let currentTrackId = state . player . currentTrackId ; let playing = ! state . player . playing ; if ( trackId ) { if ( trackId !== currentTrackId ) { playing = true ; } currentTrackId = trackId ; } console . log ( `playlistId: ${ playlistId }` ) ; let playlistIndex = - 1 ; for ( let i = 0 ; i < playlists . length ; i ++ ) { if ( playlistId ) { if ( playlistId === playlists [ i ] . id ) { playlistIndex = i ; break ; } } else if ( trackId ) { for ( let t of playlists [ i ] . tracks ) { if ( t . id === trackId ) { playlistId = playlists [ i ] . id ; playlistIndex = i ; break ; } } } } console . log ( `playlistIndex: ${ playlistIndex }` ) ; if ( playlistIndex > - 1 ) { if ( playlists [ playlistIndex ] . tracks . length === 0 ) { return { ready : false , msg : `Playlist is empty! Add some tracks to play.` } ; } else { let playFirst = true ; for ( let t of playlists [ playlistIndex ] . tracks ) { if ( trackId ) { playFirst = false ; if ( trackId === t . id ) { t . playing = playing ; activeShoutOutPath = SOPUtils . getShoutOutPath ( t , shoutouts ) ; playlists [ playlistIndex ] . playing = playing ; console . log ( `setting track playing: ${ t . playing }` ) ; } else { t . playing = false ; } } else { if ( t . playing ) { currentTrackId = t . id ; activeShoutOutPath = SOPUtils . getShoutOutPath ( t , shoutouts ) ; playing = false ; t . playing = playing ; playlists [ playlistIndex ] . playing = playing ; playFirst = false ; } } } if ( playFirst ) { if ( playlists [ playlistIndex ] . tracks [ 0 ] . id !== currentTrackId ) { playing = true ; } playlists [ playlistIndex ] . tracks [ 0 ] . playing = playing ; playlists [ playlistIndex ] . playing = playing ; currentTrackId = playlists [ playlistIndex ] . tracks [ 0 ] . id ; activeShoutOutPath = SOPUtils . getShoutOutPath ( playlists [ playlistIndex ] . tracks [ 0 ] , shoutouts ) ; } for ( let p of playlists ) { if ( p . id !== playlistId ) { p . playing = false ; for ( let t of p . tracks ) { t . playing = false ; } } } console . log ( `playlists[playlistIndex].playing: ${ playlists [ playlistIndex ] . playing }` ) ; return { ready : true , trackId : currentTrackId , activeShoutOutPath , playing , state : { playlists , sharedlist } } ; } } } public static updateShared ( trackId , state , activeShoutOutPath ) { let playlists = ListViewHelper . reset ( [ ... state . firebase . playlists ] , true ) ; let sharedlist = [ ... state . firebase . sharedlist ] ; let playing = ! state . player . playing ; if ( trackId ) { if ( state . player . activeShoutOutPath !== activeShoutOutPath ) { playing = true ; } } for ( let shared of sharedlist ) { if ( shared . trackId === trackId ) { shared . playing = playing ; } else { shared . playing = false ; } } return { ready : true , trackId , activeShoutOutPath , playing , state : { playlists , sharedlist } } ; } /**
     * Reset playing: false
     **/ public static reset ( list , isPlaylists ? ) { for ( let item of list ) { item . playing = false ; if ( isPlaylists && item . tracks ) { for ( let t of item . tracks ) { t . playing = false ; } } } return list ; } }