// Generate a new library file using the selected directory. private async Task <string> DoGenerateLibrary(string selectedFolder) { await Task.Run(() => { ObservableCollection <Song> tempLibrary = GenerateLibrary.Create(selectedFolder); MusickLibrary.SongList = tempLibrary; string tempMusicLibraryFile = System.IO.Path.Combine(ConfigClass.appLibraryFolder, libraryName); JSON.SerializeLibrary(tempMusicLibraryFile, tempLibrary); MusickSettings.libList.Add(GenerateLibrary.CreateLibraryEntry(tempLibrary, tempMusicLibraryFile)); // Creates an entry for the local library file to be displayed and interracted with. } ); lblStatus.Content = "Library Generated..."; return(""); }
// Generate a new library file using the selected directory. private async Task <LibraryFile> DoGenerateLibrary(string selectedFolder, string libraryName) { LibraryFile tempLibraryFile = new LibraryFile(); await Task.Run(() => { ObservableCollection <Song> tempLibrary = GenerateLibrary.Create(selectedFolder); // Adds the songs contained within this new library to the library window. MusickLibrary.SongList.Union(tempLibrary).ToList(); // Create library entry for the application to use and see. tempLibraryFile = GenerateLibrary.CreateLibraryEntry(tempLibrary, libraryName); // Serialize Library. JSON.SerializeLibrary(libraryName, tempLibrary); return(tempLibraryFile); } ); return(tempLibraryFile); }
// If a local library (or libraries) exists, this will deserialise the file(s) into the songList object for the Library window to use. private async Task <string> DoLoadLibrary() { await Task.Run(() => { foreach (var file in Directory.GetFiles(ConfigClass.appLibraryFolder)) { ObservableCollection <Song> tempLibrary = JSON.DeserializeLibrary(file); LibraryFile tempLibFile = GenerateLibrary.CreateLibraryEntry(tempLibrary, file); foreach (var song in tempLibrary.ToList()) { if (!File.Exists(song.FileLocation)) { tempLibrary.Remove(song); } } foreach (var musicFile in Directory.GetFiles(tempLibFile.LibrarySource, "*", SearchOption.AllDirectories)) { if (!tempLibrary.Any(p => p.FileLocation == musicFile)) { if (file.Contains(".mp3") || file.Contains(".wma") || file.Contains(".wav")) { tempLibrary.Add(GenerateLibrary.GenerateSong(musicFile)); } } } JSON.SerializeLibrary(file, tempLibrary); try { foreach (var tempSong in tempLibrary) { tempSongList.Add(tempSong); } MusickSettings.libList.Add(tempLibFile); // Creates an entry for the local library file to be displayed and interracted with. } catch // If the object returned is unusable, it'll throw this error. { Dispatcher.Invoke(() => { MusickError errorWin = new MusickError(); errorWin.Owner = this; errorWin.lblError.Content = "One or more libraries are corrupt/missing - Restart to generate a new library"; if (errorWin.ShowDialog() == true) { foreach (var libFile in Directory.GetFiles(ConfigClass.appLibraryFolder)) { File.Delete(libFile); } this.Close(); } } ); } } } ); MusickLibrary.SongList = tempSongList; lblStatus.Content = "Library loaded..."; await Task.Delay(729); return(""); }