/// <summary> /// Passes in a song /// checks file for it song id mapped to song title and artist /// returns the songid /// more unique selection poor performance /// </summary> /// public static async Task <int> GetSongIDFromFileAsync(Model.Song song) { int songID = -1; StorageFolder sFolder = ApplicationData.Current.LocalFolder; StorageFile sFile = await sFolder.CreateFileAsync(FILE_NAME2, CreationCollisionOption.OpenIfExists); //checks if song title and artist is already in SongStorage.txt file var slines = await FileIO.ReadLinesAsync(sFile); //searches for songs, if it find it add it into a dictionary foreach (var sline in slines) { if (sline != "") { var check = sline.Split(','); if (check[0] == song.Title) { songID = Convert.ToInt32(check[1]); } } } //when called in main view model must check if it is -1, means song does not have id return(songID); }
/// <summary> /// Gets all songs from a text file in local storage called SongStorage.txt /// </summary> /// <returns>collection of Songs</returns> public static async Task <ICollection <Model.Song> > GetSongsAsync() { List <Model.Song> songs = new List <Model.Song>(); StorageFolder folder = ApplicationData.Current.LocalFolder; StorageFile songFile = await folder.GetFileAsync(FILE_NAME); var lines = await FileIO.ReadLinesAsync(songFile); foreach (var line in lines) { var songData = line.Split(','); var song = new Model.Song(); song.Title = songData[0]; song.Artist = songData[1]; song.Album = songData[2]; song.CoverImagePath = songData[3]; song.AudioFilePath = songData[4]; song.Genre = (Model.Genre)Enum.Parse(typeof(Model.Genre), songData[5]); songs.Add(song); } return(songs); }
/// <summary> /// Write a song with its song title , artist and assigned ID into txt file in local storage /// checks to see if song exist before adding in this collection /// </summary> /// <param name="song">the song you want to save</param> public static async void WriteSongToFileAsync(Model.Song song) { StorageFolder sFolder = ApplicationData.Current.LocalFolder; StorageFile sFile = await sFolder.CreateFileAsync(FILE_NAME2, CreationCollisionOption.OpenIfExists); //checks if song title and artist is already in SongStorage.txt file var slines = await FileIO.ReadLinesAsync(sFile); bool exist = false; foreach (var sline in slines) { if (sline != "") { var check = sline.Split(','); if (check[0] == song.Title) { exist = true; } } } if (exist == false) { // create song data and add into SongStorage.txt file var sData = $"{song.Title},"; sData = sData + $"{song.ID.ToString()},"; sData = sData + Environment.NewLine; await FileIO.AppendTextAsync(sFile, sData); } }
/// <summary> /// Appends a song to the end of a txt file in local storage storing all /// songs in the collection /// </summary> /// <param name="song">the song you want to save</param> public static async void WriteSongToFileAsync(Model.Song song) { StorageFolder localFolder = ApplicationData.Current.LocalFolder; StorageFile songFile = await localFolder.CreateFileAsync(FILE_NAME, CreationCollisionOption.OpenIfExists); var songData = $"{song.Title},{song.Artist},{song.Album},{song.CoverImagePath}{song.AudioFilePath},{song.Genre}" + Environment.NewLine; await FileIO.AppendTextAsync(songFile, songData); }