<commit_before>angular.module("mopify.services.playlistmanager", [
    'mopify.services.mopidy',
    'mopify.services.servicemanager',
    'mopify.services.settings',
    "spotify"
])

.factory("PlaylistManager", function($rootScope, $q, $interval, ServiceManager, Spotify, mopidyservice, Settings){
    "use strict";

    function PlaylistManager(){
        var that = this;

        this.source = "";
        this.playlists = [];
        this.orderedPlaylists = {};
        this.loading = true;
        that.spotifyuserid = null;

        // Load playlists via Spotify or Mopidy depending on the settings
        var loadspotifyplaylists = false;
        
        if(Settings.get("spotify") !== undefined)
            loadspotifyplaylists = Settings.get("spotify").loadspotifyplaylists;

        if(ServiceManager.isEnabled("spotify") && loadspotifyplaylists === true){
            // Load when mopidy is online
            $rootScope.$on("mopify:spotify:connected", function(){
                Spotify.getCurrentUser().then(function(user){
                    that.spotifyuserid = user.id;
                    that.loadPlaylists();
                });
            });
        }
        else{
            $rootScope.$on("mopidy:state:online", function(){
                that.loadPlaylists();
            });

            if(mopidyservice.isConnected){
                that.loadPlaylists();
            }
        }
    }

    /**
     * Return the previously loaded playlists
     * @param  {object} options extra options for the returned object
     * @return {array}         the playlists
     */
    PlaylistManager.prototype.getPlaylists = function(options){
        var deferred = $q.defer();
        var that = this;

        options = options || {};
        
        if(!that.loading){
            var playlists = that.playlists;

            if(options.ordered === true)
                playlists = that.orderedPlaylists;

            if(options.useronly === true && options.ordered !== true){
                playlists = _.filter(that.playlists, function(playlist){
                    return (playlist.uri.indexOf(that.spotifyuserid) > 0);
                });
            }
            
            deferred.resolve(playlists);
        }
        else{
            var loadinginterval = $interval(function(){
                if(!that.loading){
                    $interval.cancel(loadinginterval);

                    var playlists = that.playlists;

                    if(options.ordered === true)
                        playlists = that.orderedPlaylists;

                    if(options.useronly === true && options.ordered !== true){
                        playlists = _.filter(that.playlists, function(playlist){
                            return (playlist.uri.indexOf(that.spotifyuserid) > 0);
                        });
                    }

                    deferred.resolve(playlists);
                }
            }, 300);
        }

        return deferred.promise;
    };

    /**
     * Load the playlists from Spotify or Mopidy
     */
    PlaylistManager.prototype.loadPlaylists = function() {
        var that = this;
        
        // Set loading
        this.loading = true;

        // Get the spotify loadplaylists setting
        var loadspotifyplaylists = false;

        if(Settings.get("spotify") !== undefined)
            loadspotifyplaylists = Settings.get("spotify").loadspotifyplaylists;

        // Load the playlists from Spotify is the user is connected, otherwise load them from Mopidy
        if(ServiceManager.isEnabled("spotify") && loadspotifyplaylists === true){
            // Set source to spotify
            this.source = "spotify";

            // Get user's playlists
            Spotify.getUserPlaylists(that.spotifyuserid, { limit: 50 }).then(function(data){
                that.playlists = data.items;

                // Starts loading more playlists if needed
                if(data.next !== null){
                    that.loadMorePlaylists(data.next);
                }
                else{
                    that.playlists = sortPlaylists(that.playlists);
                    that.loading = false;
                }
            });
        }
        else{
            // Set source to mopidy
            this.source = "mopidy";

            mopidyservice.getPlaylists().then(function(playlists){
                that.playlists = sortPlaylists(playlists);
                that.orderedPlaylists = orderPlaylists(playlists);
                that.loading = false;
            });
        }
    };

    /**
     * Load more playlists 
     * This is used when spotify playlists are loaded and the next attribute is present
     * @param {string} next The url of the next page
     */
    PlaylistManager.prototype.loadMorePlaylists = function(next){
        var that = this; 

        Spotify.api(next.replace("https://api.spotify.com/v1", ""), 'GET', null, {}, {
            'Authorization': 'Bearer ' + Spotify.authToken,
            'Content-Type': 'application/json'
        }).then(function(data){
            // Starts loading more playlists if needed
            if(data.next !== null){
                loadMorePlaylists(data.next);
            }
            else{
                that.playlists = sortPlaylists(that.playlists.concat(data.items));
                that.loading = false;
            }
        });<commit_msg>Added playlist reload event<commit_after>angular.module("mopify.services.playlistmanager", [
    'mopify.services.mopidy',
    'mopify.services.servicemanager',
    'mopify.services.settings',
    "spotify"
])

.factory("PlaylistManager", function($rootScope, $q, $interval, ServiceManager, Spotify, mopidyservice, Settings){
    "use strict";

    function PlaylistManager(){
        var that = this;

        this.source = "";
        this.playlists = [];
        this.orderedPlaylists = {};
        this.loading = true;
        that.spotifyuserid = null;

        // Load playlists via Spotify or Mopidy depending on the settings
        var loadspotifyplaylists = false;
        
        if(Settings.get("spotify") !== undefined)
            loadspotifyplaylists = Settings.get("spotify").loadspotifyplaylists;

        if(ServiceManager.isEnabled("spotify") && loadspotifyplaylists === true){
            // Load when mopidy is online
            $rootScope.$on("mopify:spotify:connected", function(){
                Spotify.getCurrentUser().then(function(user){
                    that.spotifyuserid = user.id;
                    that.loadPlaylists();
                });
            });
        }
        else{
            $rootScope.$on("mopidy:state:online", function(){
                that.loadPlaylists();
            });

            if(mopidyservice.isConnected){
                that.loadPlaylists();
            }
        }

        // Load playlists on playlists:change event
        $rootScope.$on("mopidy:event:playlistsLoaded", function(){
            // Load playlists
            that.loadPlaylists();
        });
    }

    /**
     * Load the playlists from Spotify or Mopidy
     */
    PlaylistManager.prototype.loadPlaylists = function() {
        var that = this;
        
        // Set loading
        this.loading = true;

        // Clear current playlists array
        this.playlists = [];
        this.orderedPlaylists = {};

        // Get the spotify loadplaylists setting
        var loadspotifyplaylists = false;

        if(Settings.get("spotify") !== undefined)
            loadspotifyplaylists = Settings.get("spotify").loadspotifyplaylists;

        // Load the playlists from Spotify is the user is connected, otherwise load them from Mopidy
        if(ServiceManager.isEnabled("spotify") && loadspotifyplaylists === true){
            // Set source to spotify
            this.source = "spotify";

            // Get user's playlists
            Spotify.getUserPlaylists(that.spotifyuserid, { limit: 50 }).then(function(data){
                that.playlists = data.items;

                // Starts loading more playlists if needed
                if(data.next !== null){
                    that.loadMorePlaylists(data.next);
                }
                else{
                    that.playlists = sortPlaylists(that.playlists);
                    that.loading = false;
                }
            });
        }
        else{
            // Set source to mopidy
            this.source = "mopidy";

            mopidyservice.getPlaylists().then(function(playlists){
                that.playlists = sortPlaylists(playlists);
                that.orderedPlaylists = orderPlaylists(playlists);
                that.loading = false;
            });
        }
    };

    /**
     * Return the previously loaded playlists
     * @param  {object} options extra options for the returned object
     * @return {array}         the playlists
     */
    PlaylistManager.prototype.getPlaylists = function(options){
        var deferred = $q.defer();
        var that = this;

        options = options || {};
        
        if(!that.loading){
            var playlists = that.playlists;

            if(options.ordered === true)
                playlists = that.orderedPlaylists;

            if(options.useronly === true && options.ordered !== true){
                playlists = _.filter(that.playlists, function(playlist){
                    return (playlist.uri.indexOf(that.spotifyuserid) > 0);
                });
            }
            
            deferred.resolve(playlists);
        }
        else{
            var loadinginterval = $interval(function(){
                if(!that.loading){
                    $interval.cancel(loadinginterval);

                    var playlists = that.playlists;

                    if(options.ordered === true)
                        playlists = that.orderedPlaylists;

                    if(options.useronly === true && options.ordered !== true){
                        playlists = _.filter(that.playlists, function(playlist){
                            return (playlist.uri.indexOf(that.spotifyuserid) > 0);
                        });
                    }

                    deferred.resolve(playlists);
                }
            }, 300);
        }

        return deferred.promise;
    };

    /**
     * Load more playlists 
     * This is used when spotify playlists are loaded and the next attribute is present
     * @param {string} next The url of the next page
     */
    PlaylistManager.prototype.loadMorePlaylists = function(next){
        var that = this; 

        Spotify.api(next.replace("https://api.spotify.com/v1", ""), 'GET', null, {}, {
            'Authorization': 'Bearer ' + Spotify.authToken,
            'Content-Type': 'application/json'
        }).then(function(data){
            // Starts loading more playlists if needed
            if(data.next !== null){
                loadMorePlaylists(data.next);
            }
            else{
                that.playlists = sortPlaylists(that.playlists.concat(data.items));
                that.loading = false;
            }
        });
{'fraction_non_alphanumeric': 0.10980321682091831, 'fraction_numerical': 0.001437685326624135, 'mean_word_length': 1.6299621928166352, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '7381202', 'n_tokens_mistral': 2928, 'n_tokens_neox': 2784, 'n_words': 581}