All files / src/providers/radio-service radio-service.ts

100% Statements 53/53
95% Branches 19/20
100% Functions 16/16
100% Lines 48/48
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                    1x 1x                                   22x         22x   1x 5x 5x 1x   5x         3x 3x 3x 1x   2x         1x 2x 2x 1x 10x             1x 1x   2x     1x 3x   3x               1x 1x   10x   10x 6x           10x         1x                       1x   14x 4x     10x   10x   10x 1x   10x 1x   10x 1x     10x                   1x 10x         73x               1x  
import { Injectable } from '@angular/core';
import { HttpClient, HttpRequest } from '@angular/common/http';
import { GlobalService } from '../global-service/global-service';
import { Events } from 'ionic-angular';
/* tslint:disable:no-import-side-effect */
import 'rxjs/add/operator/map';
/* tslint:enable:no-import-side-effect */
import { ICoverSong, ISong } from '../../interfaces/index';
 
@Injectable()
export class RadioService {
    private static TAGS = {
        DEFAULT: [
            'sample',
            'jingle',
            'faubourg simone',
            'fabourg simone', // lol
            'flash calepin',
        ],
        FRIDAY_WEAR: [
            'Friday Wear',
        ],
        NOUVEAUTE: [
            'nouveauté',
            'nothing',
            'nouveaute'
        ]
    };
 
    private loopInterval = 3000;
    private timer: any;
    private currentSong: ISong;
    private lastSongs: ISong[];
 
    constructor ( public http: HttpClient, private events: Events ) { }
 
    public initLoop ( interval?: number ) {
        this.loopInterval = interval ? interval : this.loopInterval;
        if ( this.timer ) {
            clearTimeout( this.timer );
        }
        this.getApiSongs()
            .then( this.onGetApiSongsSuccess.bind( this ) )
            .catch( this.onGetApiSongsError.bind( this ) );
    }
 
    public getApiSongs (): Promise<any> {
        return new Promise( ( resolve, reject ) => {
            this.http.get( GlobalService.URL_API_COVERS ).subscribe( data => {
                resolve( this.filterDefaultCovers( data ) );
            }, err => {
                reject( new Error( 'This request has failed ' + err ) );
            } );
        } );
    }
 
    private onGetApiSongsSuccess( data: any ) {
        const hasChanged = !this.currentSong || ( this.currentSong.title !== data.songs[0].title );
        if ( hasChanged ) {
            this.lastSongs = data.songs.map( song => {
                return {
                    artist: song.title.split( ' - ' )[0],
                    cover: song.cover,
                    title: song.title || '',
                    track: song.title.split( ' - ' )[1]
                };
            } );
            this.currentSong = this.lastSongs.shift();
            this.events.publish( '[RadioService]now-playing-change', this.currentSong, this.lastSongs );
        }
        this.timer = setTimeout( this.initLoop.bind( this ), this.loopInterval );
    }
 
    private onGetApiSongsError( error ) {
        console.log( error );
        // TODO : verifier que c'est entendu
        this.events.publish( '[RadioService]error', error );
    }
 
    /**
     * We may have to change the cover returned by theAPI
     * @param data
     * @returns {any}
     */
    private filterDefaultCovers ( data ): any {
        data.songs = data.songs.map( song => {
 
            let coverToGet: ICoverSong = this.getMatchedCover( song );
 
            if ( coverToGet === null ) {
                coverToGet = {
                    jpg: song.album_cover,
                    svg: song.album_cover
                };
            }
 
            return {
                cover: coverToGet,
                title: song.title
            };
        } );
        return data;
    }
 
    /**
     * Return a special cover if title of the song contains any
     * specific tags in RadioService.TAGS.DEFAULT,
     * RadioService.TAGS.NOUVEAUTE or in RadioService.TAGS.FRIDAY_WEAR.
     * Depending in which array the tag is matching,
     * returns a default Cover from GlobalService
     * @param song
     * @returns {ICoverSong}
     */
    private getMatchedCover( song: any ): ICoverSong {
 
        if ( song.album_cover.indexOf( 'pochette-default' ) > -1 ) {
            return GlobalService.COVER_DEFAULT;
        }
 
        let cover = null;
 
        const tagInIt = this.lookForMatch( song.title );
 
        if ( tagInIt === RadioService.TAGS.FRIDAY_WEAR ) {
            cover = GlobalService.COVER_DEFAULT_FRIDAY_WEAR;
        }
        if ( tagInIt === RadioService.TAGS.NOUVEAUTE ) {
            cover = GlobalService.COVER_DEFAULT_NOUVEAUTE;
        }
        if ( tagInIt === RadioService.TAGS.DEFAULT ) {
            cover = GlobalService.COVER_DEFAULT;
        }
 
        return cover;
    }
 
    /**
     * Checks if title contains any tag of the arrays (default friday
     * wear tags, default nouveaute tags, or simply default tags
     * If an array of tags matches, returns this array
     * @param {string} title
     * @returns {string[]}
     */
    private lookForMatch( title: string ): string[] {
        return [
            RadioService.TAGS.FRIDAY_WEAR,
            RadioService.TAGS.NOUVEAUTE,
            RadioService.TAGS.DEFAULT
        ]
            .find( tagArray => !!( tagArray.find( tag => title.toLowerCase().indexOf( tag.toLowerCase() ) > -1 ) ) );
    }
 
    // private someAsyncFunction() {
    //     return new Promise( resolve => {
    //         setTimeout( resolve, 1000, 'coucou' );
    //     } );
    // }
}