import { OnDestroy } from '@angular/core' ; import { Store } from '@ngrx/store' ; import { ModalDialogParams } from "nativescript-angular" ; import { BehaviorSubject } from 'rxjs/BehaviorSubject' ; import 'rxjs/add/operator/take' ; import { LogService , BaseComponent } from '../../shared/core/index' ; import { TrackModel , PlaylistModel , IFirebaseState } from '../../shared/shoutoutplay/index' ; @ BaseComponent ( { selector : 'track-chooser' , templateUrl : './components/record/track-chooser.component.html' } ) export class TrackChooserComponent implements OnDestroy { public tracks$ : BehaviorSubject < Array < TrackModel > > = new BehaviorSubject ( [ ] ) ; public noTracks : boolean = false ; private _tracks : Array < TrackModel > = [ ] ; constructor ( private store , private logger , private params ) { store . take ( 1 ) . subscribe ( ( state ) => { this . initTracks ( state . firebase . playlists ) ; } ) ; } public close ( track ? ) { this . params . closeCallback ( track ) ; } public choose ( e ) { this . logger . debug ( e ) ; this . logger . debug ( e . name ) ; this . close ( e ) ; } public search ( e ) { if ( e && e . object ) { this . logger . debug ( e . object . text ) ; let filteredTracks = this . _tracks . filter ( ( t ) => { return t . name . indexOf ( e . object . text ) > - 1 || t . artist . name . indexOf ( e . object . text ) > - 1 ; } ) ; this . tracks$ . next ( filteredTracks ) ; } } public clear ( ) { this . logger . debug ( 'clear' ) ; this . tracks$ . next ( this . _tracks ) ; } private initTracks ( playlists ) { for ( let playlist of playlists ) { let availableTracks = playlist . tracks . filter ( ( track ) => { return ! track . shoutoutId ; } ) ; for ( let track of availableTracks ) { track . playlistId = playlist . id ; } this . _tracks = [ ... this . _tracks , ... availableTracks ] ; } if ( this . _tracks . length === 0 ) { this . noTracks = true ; } else { this . tracks$ . next ( this . _tracks ) ; } } ngOnDestroy ( ) { } }