All files / src/pages/radio radio.ts

100% Statements 39/39
75% Branches 3/4
100% Functions 11/11
100% Lines 37/37
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                                        1x       1x             9x 9x 9x 9x 9x 9x 9x   9x 9x   9x     1x   1x     1x     1x 1x     1x 1x 1x     12x   12x 12x       12x     8x       3x     3x               1x 4x 4x 4x     1x 1x 1x     1x 1x   1x  
import { Component, ViewChild } from '@angular/core';
import { Events, NavController, Platform, ViewController } from 'ionic-angular';
import { GlobalService } from '../../providers/global-service/global-service';
import { GoogleAnalytics } from '@ionic-native/google-analytics';
import { InitService } from '../../providers/init-service/init-service';
import { PromptService } from '../../providers/prompt-service/prompt-service';
import { RadioService } from '../../providers/radio-service/radio-service';
import { PlayerComponent } from '../../components/player/player';
import { ISong } from '../../interfaces';
import { HttpEvent, HttpEventType } from '@angular/common/http';
 
/* tslint:disable:no-unused-variable */
declare let cordova: any;
declare let FB: any;
/* tslint:enable:no-unused-variable */
 
@Component( {
    selector: 'page-radio',
    templateUrl: 'radio.html'
} )
export class RadioPage {
 
    // private apiData:any;
 
    @ViewChild( 'player' ) private player: PlayerComponent;
 
    private streamingUrl: string;
    private configReady: boolean;
    private lastSongs: ISong[];
    private hasLeft: boolean;
 
    constructor ( private plt: Platform,
                  private prompt: PromptService,
                  private ga: GoogleAnalytics,
                  private viewCtrl: ViewController,
                  private initService: InitService,
                  private radioService: RadioService,
                  private events: Events,
    ) {
        this.configReady = false;
        this.hasLeft = false;
 
        this.plt.ready().then( this.onPlatformReady.bind( this ) );
    }
 
    protected ionViewDidLoad () {
        // Event from RadioService
        this.events.subscribe( '[RadioService]now-playing-change', this.onNowPlayingChanged.bind( this ) );
        // Event from RadioService
        // TODO: verifier
        this.events.subscribe( '[RadioService]error', this.onRadioServiceError.bind( this ) );
    }
 
    protected ionViewDidEnter () {
        this.hasLeft = false;
    }
 
    protected ionViewDidLeave () {
        this.hasLeft = true;
        this.prompt.dismissLoading();
    }
 
    private onPlatformReady () {
        // console.log( 'Platform ready from', readySource );
        Eif ( this.plt.is( 'cordova' ) ) {
            this.ga.trackView( this.viewCtrl.name );
        }
 
        // Look for streaming address in a json file (remote or local)
        this.initService.getInitData()
            .then( this.onData.bind( this ) )
            .catch( () => {
                this.prompt.presentMessage( {
                    classNameCss: 'error',
                    message: `⚠ Error when loading prod config => Try to resolved by loading local config`
                } );
                this.initService.getInitData( true )
                    .then( this.onData.bind( this ) )
                    .catch( errors => {
                        this.prompt.presentMessage( {
                            classNameCss: 'error',
                            message: `⚠ ${ errors.join( ' ⚠ ' ) }`
                        } );
                    } );
            } );
    }
 
    private onData( data: any ) {
        this.streamingUrl = data.streamingUrl ? data.streamingUrl : GlobalService.DEFAULT_URL_STREAMING;
        this.radioService.initLoop( data.loop_interval );
        this.configReady = true;
    }
 
    private onNowPlayingChanged ( currentSong, lastSongs ) {
        this.lastSongs = lastSongs;
        this.player.updateMeta( currentSong );
    }
 
    private onRadioServiceError ( error ) {
        this.prompt.presentMessage( { message: error.toString(), classNameCss: 'error' } );
    }
}