import { OnDestroy , NgZone , ViewContainerRef } from '@angular/core' ; import { ModalDialogService , ModalDialogOptions } from "nativescript-angular" ; import { isIOS } from 'platform' ; import * as utils from 'utils/utils' ; import { File } from 'file-system' ; var TNSAudioPlayer ; if ( isIOS ) { let ezAudio = require ( 'nativescript-ezaudio' ) ; TNSAudioPlayer = ezAudio . TNSEZAudioPlayer ; } else { let audio = require ( 'nativescript-audio' ) ; TNSAudioPlayer = audio . TNSPlayer ; } import { Store } from '@ngrx/store' ; import { BehaviorSubject } from 'rxjs/BehaviorSubject' ; import { Subscription } from 'rxjs/Subscription' ; import 'rxjs/add/operator/take' ; import { LogService , BaseComponent , FancyAlertService , TextService , Utils } from '../../shared/core/index' ; import { ShoutoutModel , FIREBASE_ACTIONS , IFirebaseState , ShoutoutService , FirebaseService , TrackControlService } from '../../shared/shoutoutplay/index' ; import { TrackChooserComponent } from '../record/track-chooser.component' ; declare var zonedCallback ; @ BaseComponent ( { selector : 'shoutout-list' , templateUrl : './components/shoutout/shoutout-list.component.html' } ) export class ShoutOutListComponent implements OnDestroy { public shoutouts$ : BehaviorSubject < Array < any > > = new BehaviorSubject ( [ ] ) ; private _currentIndex : number ; private _shoutOutPlayer : any ; private _currentShoutOut : any ; private _sub : Subscription ; constructor ( private store , private logger , private shoutoutService , public firebaseService , public trackControl , private fancyalert , private ngZone , private modal , private vcRef ) { if ( isIOS ) { this . _shoutOutPlayer = new TNSAudioPlayer ( true ) ; this . _shoutOutPlayer . delegate ( ) . audioEvents . on ( 'reachedEnd' , zonedCallback ( ( eventData ) => { this . logger . debug ( `ShoutOutListComponent: audioEvents.on('reachedEnd')` ) ; this . toggleShoutOutPlay ( false , false ) ; } ) ) ; } else { this . _shoutOutPlayer = new TNSAudioPlayer ( ) ; } } public togglePlay ( shoutout ) { this . toggleShoutOutPlay ( shoutout , ( this . _currentShoutOut ? shoutout . id !== this . _currentShoutOut . id : true ) ) ; } private toggleShoutOutPlay ( shoutout ? , reload ? ) { if ( shoutout ) { this . _currentShoutOut = Object . assign ( { } , shoutout ) ; } if ( this . _currentShoutOut && this . _currentShoutOut . filename ) { this . logger . debug ( `_shoutOutPlayer.togglePlay` ) ; let fullPath = Utils . documentsPath ( this . _currentShoutOut . filename ) ; this . logger . debug ( fullPath ) ; if ( File . exists ( fullPath ) ) { if ( isIOS ) { this . _shoutOutPlayer . togglePlay ( fullPath , reload ) ; } else { if ( reload ) { let playerOptions = { audioFile : fullPath , completeCallback : ( ) => { this . logger . debug ( 'completeCallback..' ) ; this . toggleShoutOutPlay ( false , false ) ; } , errorCallback : ( ) => { } , infoCallback : ( ) => { } } ; this . _shoutOutPlayer . playFromFile ( playerOptions ) . then ( ( ) => { } , ( err ) => { console . log ( err ) ; } ) ; } else { if ( this . _currentShoutOut . playing ) { this . _shoutOutPlayer . pause ( ) ; } else { this . _shoutOutPlayer . start ( ) ; } } } this . _currentShoutOut . playing = ! this . _currentShoutOut . playing ; let shoutouts = [ ... this . shoutouts$ . getValue ( ) ] ; for ( let s of shoutouts ) { if ( s . id === this . _currentShoutOut . id ) { s . playing = this . _currentShoutOut . playing ; this . logger . debug ( `set playing: ${ s . playing }` ) ; } else { s . playing = false ; } } this . ngZone . run ( ( ) => { this . shoutouts$ . next ( [ ... shoutouts ] ) ; } ) ; } else { this . fancyalert . show ( TextService . SHOUTOUT_NOT_FOUND ) ; } } } public addToTrack ( shoutout ) { } public remove ( e ) { this . fancyalert . confirm ( 'Are you sure you want to delete this ShoutOut?' , 'warning' , ( ) => { let shoutouts = [ ... this . shoutouts$ . getValue ( ) ] ; this . shoutoutService . removeShoutout ( shoutouts [ this . _currentIndex ] ) ; } ) ; } public onSwipeCellStarted ( args ) { let density = utils . layout . getDisplayDensity ( ) ; let delta = Math . floor ( density ) !== density ? 1.1 : .1 ; var swipeLimits = args . data . swipeLimits ; swipeLimits . top = 0 ; swipeLimits . bottom = 0 ; swipeLimits . left = 0 ; swipeLimits . right = Math . round ( density * 100 ) ; swipeLimits . threshold = Math . round ( density * 50 ) ; } public onSwipeCellFinished ( args ) { this . _currentIndex = args . itemIndex ; } ngOnInit ( ) { this . _sub = this . store . select ( 'firebase' ) . subscribe ( ( s ) => { let playlists = [ ... s . playlists ] ; let shoutouts = [ ... s . shoutouts ] ; for ( let shoutout of shoutouts ) { for ( let p of playlists ) { for ( let t of p . tracks ) { if ( t . shoutoutId === shoutout . id ) { shoutout . track = t . name ; break ; } } } } this . shoutouts$ . next ( shoutouts ) ; } ) ; } ngOnDestroy ( ) { if ( this . _shoutOutPlayer ) { if ( isIOS ) { this . _shoutOutPlayer . delegate ( ) . audioEvents . off ( 'reachedEnd' ) ; this . _shoutOutPlayer = undefined ; } else { this . _shoutOutPlayer . dispose ( ) . then ( ( ) => { this . logger . debug ( 'player disposed.' ) ; this . _shoutOutPlayer = undefined ; } , ( err ) => { this . logger . debug ( err ) ; } ) ; } } if ( this . _sub ) this . _sub . unsubscribe ( ) ; } }