<commit_before>import { Component, NgZone } from '@angular/core';
import { NavController, ViewController } from 'ionic-angular';
import { BackgroundMode } from '@ionic-native/background-mode';
import { RadioPlayer } from '../../providers/radioplayer';
// import { InformationService } from '../../services/information';
import { InitService } from '../../providers/init-service';
import { MusicControls } from '@ionic-native/music-controls';

declare let cordova: any;

@Component({
    selector: 'page-radio',
    templateUrl: 'radio.html',
    providers: [InitService, BackgroundMode, MusicControls]
})
export class RadioPage {

    private streaming_url:string = 'http://192.168.127.12:1337/faubourgsimone';
    private loop_interval:Number = 3000;
    private timer:any;
    private hasLeft:boolean = false;
    private isPlaying:boolean = false;
    private playPauseButton:string = 'play';
    private isButtonActive:boolean = true;
    // private volume:number = 50;

    private currentSong = {
        cover_url: 'assets/images/cover-default.jpg',
        title: '',
        artist: '',
        track: ''
    };

    constructor(public viewCtrl: ViewController,
                public navCtrl: NavController,
                private player: RadioPlayer,
                private initService: InitService,
                private backgroundMode: BackgroundMode,
                private zone: NgZone,
                private musicControls: MusicControls) {

        // Cherche l'adresse du streaming dans un fichier json sur nos serveurs
        this.initService.getInitData().subscribe(
            // Success
            data => {
                console.log('Data received ', data.loop_interval);
                this.streaming_url = data.streaming_url ? data.streaming_url : this.streaming_url;
                this.loop_interval = data.loop_interval ? data.loop_interval : this.loop_interval;
                // this.goOn();
            }
            // Failure
            // err => this.goOn()
        );
    }

    ngOnInit() {
        console.log('NG INIT');

        this.manageBackground();

        this.player.init(this.streaming_url);

    }

    manageBackground(){
        this.backgroundMode.enable();
        try {
            // ENTREE DU MODE BACKGROUND
            this.backgroundMode.on('activate').subscribe(()=> {
                // if(this.timer && !this.isPlaying) {
                //     clearTimeout(this.timer);
                // }
            });
            // SORTIE DU MODE BACKGROUND
            this.backgroundMode.on('deactivate').subscribe(()=> {
                this.zone.run(()=>{
                    // si on n'avait pas change de tab avant de rentrer en mode background
                    // if(!this.hasLeft) {
                        this.loopData();
                    // }
                });
            });
        }
        catch(e) {}
    }

    ionViewDidLoad() {
        console.log('ION VIEW DID LOAD');
    }

    ionViewDidEnter() {
        console.log('ionViewDidEnter');
        this.hasLeft = false;
        this.loopData();
    }
    ionViewDidLeave() {
        this.hasLeft = true;
        // if(this.timer && !this.isPlaying) {
        //     clearTimeout(this.timer);
        // }
    }

    // setVolume() {
    //     console.log('RadioPage.setVolume ', this.volume/100);
    //     // if (typeof cordova !== 'undefined') {
    //     //     // console.log(cordova.plugins.VolumeControl);
    //     //     console.log("volume : ", cordova.plugins.VolumeControl.getVolume());
    //     //     cordova.plugins.VolumeControl.setVolume(this.volume/100);
    //     // }
    //     // else{
    //     //     console.log("NOT FOUND");
    //     // }
    // }

    loopData() {
        if(this.timer) {
            // console.log('clear timer');
            clearTimeout(this.timer);
        }
        // console.log('loopdata ', this);
        // Cherche les informations sur la piste en cours de lecture
        this.initService.getCurrentSongs().subscribe(
            data => {
                console.log('############################################################');
                let hasChanged = (this.currentSong.title !== data.songs[0].title);

                this.currentSong = {
                    cover_url: data.songs[0].album_cover || '',
                    title: data.songs[0].title || '',
                    artist: data.songs[0].title.split(" - ")[0],
                    track: data.songs[0].title.split(" - ")[1]
                };
                if(hasChanged) {
                    this.destroyMusicControls();
                    this.createMusicControls();
                }
                this.timer = setTimeout(()=>this.loopData(), this.loop_interval);
            },
            err => this.handleCurrentError(err)
        );
    }

    togglePlayPause() {
        if(this.isPlaying) {
            this.pause();
        }
        else {
            this.play();
        }
    }

    play() {
        this.isButtonActive = false;
        console.log('Waiting For Streaming');
        if(this.player.isPlaying) {
            return false;
        }
        this.playPauseButton = 'pause';
        this.player.play()
            .catch(error => this.handlePlayError(error))
            .then(() => {
                console.log('Start Playing');
                this.isPlaying = true;
                this.isButtonActive = true;
                if (typeof cordova !== 'undefined') {
                    this.musicControls.updateIsPlaying(true);
                }
            });
    }

    pause() {
        console.log('RadioPage.pause');<commit_msg>feat: Add spinner loader when click on play<commit_after>import { Component, NgZone } from '@angular/core';
import { NavController, ViewController } from 'ionic-angular';
import { BackgroundMode } from '@ionic-native/background-mode';
import { RadioPlayer } from '../../providers/radioplayer';
import { InitService } from '../../providers/init-service';
import { MusicControls } from '@ionic-native/music-controls';
import { LoadingController, Loading } from 'ionic-angular';

declare let cordova: any;

@Component({
    selector: 'page-radio',
    templateUrl: 'radio.html',
    providers: [InitService, BackgroundMode, MusicControls]
})
export class RadioPage {

    private streaming_url:string = 'http://192.168.127.12:1337/faubourgsimone';
    private loop_interval:Number = 3000;
    private timer:any;
    private hasLeft:boolean = false;
    private isPlaying:boolean = false;
    private playPauseButton:string = 'play';
    private isButtonActive:boolean = true;
    // private volume:number = 50;
    private loader:Loading;

    private currentSong = {
        cover_url: 'assets/images/cover-default.jpg',
        title: '',
        artist: '',
        track: ''
    };

    constructor(public viewCtrl: ViewController,
                public navCtrl: NavController,
                private player: RadioPlayer,
                private initService: InitService,
                private backgroundMode: BackgroundMode,
                private zone: NgZone,
                private musicControls: MusicControls,
                public loadingCtrl: LoadingController) {

        // Cherche l'adresse du streaming dans un fichier json sur nos serveurs
        this.initService.getInitData().subscribe(
            // Success
            data => {
                console.log('Data received ', data.loop_interval);
                this.streaming_url = data.streaming_url ? data.streaming_url : this.streaming_url;
                this.loop_interval = data.loop_interval ? data.loop_interval : this.loop_interval;
                // this.goOn();
            }
            // Failure
            // err => this.goOn()
        );
    }

    ngOnInit() {
        console.log('NG INIT');

        this.manageBackground();

        this.player.init(this.streaming_url);

    }

    manageBackground(){
        this.backgroundMode.enable();
        try {
            // ENTREE DU MODE BACKGROUND
            this.backgroundMode.on('activate').subscribe(()=> {
                // if(this.timer && !this.isPlaying) {
                //     clearTimeout(this.timer);
                // }
            });
            // SORTIE DU MODE BACKGROUND
            this.backgroundMode.on('deactivate').subscribe(()=> {
                this.zone.run(()=>{
                    // si on n'avait pas change de tab avant de rentrer en mode background
                    // if(!this.hasLeft) {
                        this.loopData();
                    // }
                });
            });
        }
        catch(e) {}
    }

    ionViewDidLoad() {
        console.log('ION VIEW DID LOAD');
    }

    ionViewDidEnter() {
        console.log('ionViewDidEnter');
        this.hasLeft = false;
        this.loopData();
    }
    ionViewDidLeave() {
        this.hasLeft = true;
        // if(this.timer && !this.isPlaying) {
        //     clearTimeout(this.timer);
        // }
    }

    // setVolume() {
    //     console.log('RadioPage.setVolume ', this.volume/100);
    //     // if (typeof cordova !== 'undefined') {
    //     //     // console.log(cordova.plugins.VolumeControl);
    //     //     console.log("volume : ", cordova.plugins.VolumeControl.getVolume());
    //     //     cordova.plugins.VolumeControl.setVolume(this.volume/100);
    //     // }
    //     // else{
    //     //     console.log("NOT FOUND");
    //     // }
    // }

    presentLoading() {
        this.loader = this.loadingCtrl.create({
            spinner: 'dots',
            content: 'Paris ne s\'est pas faite en un jour...'
        });
        this.loader.present();
    }

    dismissLoading() {
        if(this.loader) {
            this.loader.dismiss();
        }
    }

    loopData() {
        if(this.timer) {
            // console.log('clear timer');
            clearTimeout(this.timer);
        }
        // console.log('loopdata ', this);
        // Cherche les informations sur la piste en cours de lecture
        this.initService.getCurrentSongs().subscribe(
            data => {
                console.log('############################################################');
                let hasChanged = (this.currentSong.title !== data.songs[0].title);

                this.currentSong = {
                    cover_url: data.songs[0].album_cover || '',
                    title: data.songs[0].title || '',
                    artist: data.songs[0].title.split(" - ")[0],
                    track: data.songs[0].title.split(" - ")[1]
                };
                if(hasChanged) {
                    this.destroyMusicControls();
                    this.createMusicControls();
                }
                this.timer = setTimeout(()=>this.loopData(), this.loop_interval);
            },
            err => this.handleCurrentError(err)
        );
    }

    togglePlayPause() {
        if(this.isPlaying) {
            this.pause();
        }
        else {
            this.play();
        }
    }

    play() {
        this.isButtonActive = false;
        this.presentLoading();
        console.log('Waiting For Streaming');
        if(this.player.isPlaying) {
            return false;
        }
        this.playPauseButton = 'pause';
        this.player.play()
            .catch(error => this.handlePlayError(error))
            .then(() => {
                console.log('Start Playing');
                this.isPlaying = true;
                this.isButtonActive = true;
                this.dismissLoading();
                if (typeof cordova !== 'undefined') {
                    this.musicControls.updateIsPlaying(true);
                }
            });
    }

    pause() {
        console.log('RadioPage.pause');
{'fraction_non_alphanumeric': 0.1413200033889689, 'fraction_numerical': 0.0057612471405574855, 'mean_word_length': 1.6283678468047205, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2194038', 'n_tokens_mistral': 3287, 'n_tokens_neox': 3086, 'n_words': 572}