public async Task<bool> WritePlaylistFile(Playlist play) { try { StorageFolder storageFolder = ApplicationData.Current.LocalFolder; StorageFolder playlistsFolder = await storageFolder.CreateFolderAsync("Playlists", CreationCollisionOption.OpenIfExists); StorageFile playlist = await playlistsFolder.CreateFileAsync(play.PlaylistName + ".m3u", CreationCollisionOption.ReplaceExisting); var stream = await playlist.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite); using (var outputStream = stream.GetOutputStreamAt(0)) { using (var dataWriter = new Windows.Storage.Streams.DataWriter(outputStream)) { dataWriter.WriteString(await PrepareM3uFile(play)); await dataWriter.StoreAsync(); await outputStream.FlushAsync(); } } stream.Dispose(); return true; } catch { return false; } }
//show delete dialog and if the user confirms, delete the playlist private async void DeletePlaylist(Playlist play) { bool confirmation = await contentDialogService.ShowPlaylistDeletionConfirmation(play); if (confirmation) { await fileIOService.DeletePlaylistFile(play); //remove the playlist from the list and if it was selected, select the first int index = Playlists.IndexOf(play); if (SelectedPlaylist == Playlists.ElementAt(index)) SelectedPlaylist = Playlists.FirstOrDefault(); Playlists.Remove(play); } }
//create a dialog that accepts a playlist name and returns the new playlist public async Task<Playlist> CreateNewPlaylist() { Playlist play = new Playlist { PlaylistId = Guid.NewGuid() }; var dialog = new ContentDialog() { Title = "New Playlist", }; var panel = new StackPanel(); var tb = new TextBox { PlaceholderText = "Enter the new playlist's name", Margin = new Windows.UI.Xaml.Thickness { Top = 10 } }; panel.Children.Add(tb); dialog.Content = panel; dialog.PrimaryButtonText = "Save"; var cmd = new RelayCommand(() => { play.PlaylistName = tb.Text; }, () => CanSave(tb.Text)); dialog.IsPrimaryButtonEnabled = false; dialog.PrimaryButtonCommand = cmd; dialog.SecondaryButtonText = "Cancel"; dialog.RequestedTheme = (ElementTheme)new Converters.BooleanToThemeConverter().Convert(Settings.UseDarkTheme, null, null, null); tb.TextChanged += delegate { cmd.RaiseCanExecuteChanged(); if (tb.Text.Trim().Length > 0) { dialog.IsPrimaryButtonEnabled = true; } else { dialog.IsPrimaryButtonEnabled = false; } }; var result = await dialog.ShowAsync(); if (result == ContentDialogResult.Primary) { return play; } return null; }
public async Task<bool> DeletePlaylistFile(Playlist play) { try { StorageFolder storageFolder = ApplicationData.Current.LocalFolder; StorageFolder playlistsFolder = await storageFolder.CreateFolderAsync("Playlists", CreationCollisionOption.OpenIfExists); StorageFile playlistFile = await playlistsFolder.GetFileAsync(play.PlaylistName + ".m3u"); await playlistFile.DeleteAsync(StorageDeleteOption.PermanentDelete); return true; } catch { } return false; }
//edit is just deleting the old playlist and making a new one private async void EditPlaylist(Playlist oldPlay) { var newPlay = await contentDialogService.ShowEditPlaylistDialog(oldPlay); if (newPlay != null) { bool playlistWasSelected = false; int index = Playlists.IndexOf(oldPlay); if (SelectedPlaylist == Playlists.ElementAt(index)) playlistWasSelected = true; Playlists.RemoveAt(index); Playlists.Insert(index, newPlay); if(playlistWasSelected) SelectedPlaylist = newPlay; await fileIOService.WritePlaylistFile(newPlay); await fileIOService.DeletePlaylistFile(oldPlay); } }
//make sure the user wants to delete the playlist public async Task<bool> ShowPlaylistDeletionConfirmation(Playlist play) { bool disable = false; var dialog = new ContentDialog() { Title = "Confirm Deletion", }; var panel = new StackPanel(); var tb = new TextBlock { Text = "Deleting a playlist cannot be undone. Are you sure you want to proceed?", }; var cb = new CheckBox { Content = "Disable confirmations for this session", Margin = new Windows.UI.Xaml.Thickness { Top = 10 } }; panel.Children.Add(tb); panel.Children.Add(cb); dialog.Content = panel; dialog.PrimaryButtonText = "Delete"; var cmd = new RelayCommand(() => { disable = cb.IsChecked ?? false; }); dialog.PrimaryButtonCommand = cmd; dialog.SecondaryButtonText = "Cancel"; dialog.RequestedTheme = (ElementTheme) new Converters.BooleanToThemeConverter().Convert(Settings.UseDarkTheme,null,null,null); var result = await dialog.ShowAsync(); if (result == ContentDialogResult.Primary) { if (disable) ApplicationSettingsHelper.SaveSettingsValue("disableConfirmSession", true); return true; } return false; }
public async Task<List<StorageFile>> ReadPlaylistFile(Playlist play) { StorageFolder storageFolder = ApplicationData.Current.LocalFolder; StorageFolder playlistsFolder = await storageFolder.CreateFolderAsync("Playlists", CreationCollisionOption.OpenIfExists); StorageFile playlistFile = await playlistsFolder.GetFileAsync(play.PlaylistName + ".m3u"); string contents = await Windows.Storage.FileIO.ReadTextAsync(playlistFile); string[] paths = Regex.Split(contents, "\r\n|\r|\n"); List<StorageFile> items = new List<StorageFile>(); foreach (var path in paths) { if (path.Contains("#") || path.Length == 0) { //do nothing } else { items.Add(await StorageFile.GetFileFromPathAsync(path)); } } return items; // PopulatePlayist(items, false, false, true); }
//when the track changes from opening a playlist item, change the current queue to the displayed playlist private void TransferPlaylist(Playlist play) { if (CurrentPlaylist.Items != null && CurrentPlaylist.Items.Count > 0) { CurrentPlaylist.PlaylistId = play.PlaylistId; CurrentPlaylist.PlaylistName = play.PlaylistName; CurrentPlaylist.Items = new ObservableCollection<PlaylistItem>(play.Items); } }
//listen when the selected playist changes and change the displayed playlist //only read the playlist when it has been clicked for the first time, otherwise reference it //consider cleaning up the playlists when they have not been used for a while. private void SelectedPlaylistChanged(Playlist play) { queue.Add(async () => { if (play != null) { Messenger.Default.Send(new NotificationMessage<string>("ShowPlaylistLoadingBar", "ShowPlaylistLoadingBar")); await Task.Delay(5); var currentPlaylistId = play.PlaylistId; if (play.PlaylistName != "") { if (play.PlaylistId == Guid.Empty) { try { play.PlaylistId = Guid.NewGuid(); var files = await fileIOService.ReadPlaylistFile(play); play.Items = new ObservableCollection<PlaylistItem>(await AcceptFiles(files)); } catch { Debug.WriteLine("oops!"); } } } Messenger.Default.Send(new NotificationMessage<Playlist>(play, "SelectedPlaylistChanged")); int index = Playlists.IndexOf(Playlists.Where(x => x.PlaylistName == play.PlaylistName).FirstOrDefault()); if (index > -1) { Playlists[index] = play; SelectedPlaylist = Playlists[index]; } } }); }
private async Task<string> PrepareM3uFile(Playlist play) { string contents = ""; contents += "#EXTM3U"; contents += "\r\n"; foreach (var item in play.Items.OrderBy(x => x.PlaylistTrackNo)) { Windows.Storage.FileProperties.BasicProperties basicProperties = await item.PlaylistFile.GetBasicPropertiesAsync(); string filePath = item.PlaylistFile.Path; contents += "#EXTINF:" + item.Properties.Duration.TotalSeconds + ", " + item.Properties.Artist + " - " + item.Properties.Title; contents += "\r\n"; contents += filePath; contents += "\r\n"; } return contents; }
//save a playlist in thebackground private async void SavePlaylist(Playlist play) { bool success = await fileIOService.WritePlaylistFile(play); }
private void PlayItems(ObservableCollection<PlaylistItem> play) { Playlist playlist = new Playlist { Items = play, PlaylistId = SelectedPlaylist.PlaylistId, PlaylistName = SelectedPlaylist.PlaylistName }; Messenger.Default.Send(new NotificationMessage<Playlist>(playlist, "PlayFirst")); }