Beispiel #1
0
 public bool RemoveSong(Song song)
 {
     return(SongDao.RemoveSong(song));
 }
        /// <summary>
        /// Searches for differences between the database and the music collection. Then, if needed, it adds or removes the songs that doesn't exist on both lists
        /// </summary>
        public static async void LoadCollectionChanges()
        {
            if (LoadCollectionCompleted == false)
            {
                return;
            }

            Debug.WriteLine("INICIANDO BUSCA");

            LoadCollectionCompleted = false;

            bool changed = false;

            if (ApplicationSettings.IsCollectionLoaded)
            {
                List <Song>   currentSongs = SongDao.GetSongs(false);
                List <string> folderSongs  = new List <string>();
                if (currentSongs != null)
                {
                    List <Song> listOfSongs = currentSongs;
                    Song        aux         = null;

                    StorageFolder coversFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("Covers", CreationCollisionOption.OpenIfExists);

                    var songs = await StorageHelper.ScanFolder(KnownFolders.MusicLibrary);


                    int totalCount = songs.Count;

                    try
                    {
                        foreach (StorageFile file in songs)
                        {
                            if (Stop)
                            {
                                Stop = false;
                                break;
                            }

                            folderSongs.Add(file.Path);
                        }
                    }
                    catch
                    {
                    }

                    try
                    {
                        // Searches for new songs in the music library
                        foreach (StorageFile file in songs)
                        {
                            if (Stop)
                            {
                                Stop = false;
                                break;
                            }

                            if (currentSongs.Any(s => s.SongURI == file.Path) == false)
                            {
                                if (Stop)
                                {
                                    Stop = false;
                                    break;
                                }

                                aux = await CreateSongObjectByFile(file, listOfSongs);

                                changed = true;
                                listOfSongs.Add(aux);
                            }
                        }
                    }
                    catch
                    {
                    }

                    try
                    {
                        // Removes any registry for a song that doesn't exist anymore in music library
                        foreach (Song song in currentSongs)
                        {
                            if (Stop)
                            {
                                Stop = false;
                                break;
                            }

                            if (folderSongs.Contains(song.SongURI) == false)
                            {
                                SongDao.RemoveSong(song);
                                changed = true;
                            }
                        }
                    }
                    catch
                    {
                    }
                }
            }

            Debug.WriteLine("BUSCA CONCLUÍDA");

            LoadCollectionCompleted = true;
        }