Skip to content

Commit

Permalink
When dropping a song from a song source into the playlist, move it to…
Browse files Browse the repository at this point in the history
… the correct position.

Previously the song was added to the end of the playlist, even when the drop indicator suggested that it's dropped at a specific position.
  • Loading branch information
flagbug committed Jun 18, 2014
1 parent 47c447b commit 0b2a087
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
4 changes: 2 additions & 2 deletions Espera/Espera.View/ViewModels/DirectYoutubeViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public override IReactiveCommand PlayNowCommand
/// <summary>
/// Resolves the given YouTube URL and adds the song to the playlist.
/// </summary>
public async Task AddDirectYoutubeUrlToPlaylist(Uri url)
public async Task AddDirectYoutubeUrlToPlaylist(Uri url, int targetIndex)
{
if (url == null)
Throw.ArgumentNullException(() => url);
Expand All @@ -43,7 +43,7 @@ public async Task AddDirectYoutubeUrlToPlaylist(Uri url)

this.SelectedSongs = new[] { new YoutubeSongViewModel(song, () => { throw new NotImplementedException(); }) };

this.AddToPlaylistCommand.Execute(null);
this.AddToPlaylistCommand.Execute(targetIndex);
}
}
}
3 changes: 2 additions & 1 deletion Espera/Espera.View/ViewModels/SongSourceViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ protected SongSourceViewModel(Library library, Guid accessToken)
this.timeoutWarning = new Subject<Unit>();

this.AddToPlaylistCommand = this.WhenAnyValue(x => x.SelectedSongs, x => x != null && x.Any()).ToCommand();
this.AddToPlaylistCommand.Subscribe(p =>
this.AddToPlaylistCommand.Subscribe(x =>
{
if (this.library.RemainingPlaylistTimeout > TimeSpan.Zero)
{
Expand All @@ -42,6 +42,7 @@ protected SongSourceViewModel(Library library, Guid accessToken)
if (this.IsAdmin)
{
this.library.AddSongsToPlaylist(this.SelectedSongs.Select(song => song.Model), accessToken);
this.library.MovePlaylistSong(this.library.CurrentPlaylist.Last().Index, (int)x, accessToken);
}
else
Expand Down
43 changes: 29 additions & 14 deletions Espera/Espera.View/Views/ShellView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -328,28 +328,43 @@ private void WireDragAndDrop()
.Subscribe(x => DragDrop.DoDragDrop((DependencyObject)x.Source, songSourceFormat, DragDropEffects.Link));

// Local songs and YouTube songs
this.PlaylistListBox.Events().Drop
.Where(x => x.Data.GetDataPresent(DataFormats.StringFormat) && (string)x.Data.GetData(DataFormats.StringFormat) == songSourceFormat)
.Select(_ => this.shellViewModel.WhenAnyValue(x => x.CurrentSongSource).Select(x => x.AddToPlaylistCommand))
.Switch()
.Where(x => x.CanExecute(null))
.Subscribe(x => x.Execute(null));
this.PlaylistListBox.ItemContainerStyle.RegisterEventSetter<DragEventArgs>(DropEvent, x => new DragEventHandler(x))
.Where(x => x.Item2.Data.GetDataPresent(DataFormats.StringFormat) && (string)x.Item2.Data.GetData(DataFormats.StringFormat) == songSourceFormat)
.Select(x =>
{
var target = (PlaylistEntryViewModel)((ListBoxItem)(x.Item1)).DataContext;
return target.Index;
})
.Subscribe(targetIndex =>
{
var addCommand = this.shellViewModel.CurrentSongSource.AddToPlaylistCommand;
if (addCommand.CanExecute(null))
{
addCommand.Execute(targetIndex);
}
});

// YouTube links (e.g from the browser)
this.PlaylistListBox.Events().Drop
.Where(x => x.Data.GetDataPresent(DataFormats.StringFormat))
.Select(x => (string)x.Data.GetData(DataFormats.StringFormat))
this.PlaylistListBox.ItemContainerStyle.RegisterEventSetter<DragEventArgs>(DropEvent, x => new DragEventHandler(x))
.Where(x => x.Item2.Data.GetDataPresent(DataFormats.StringFormat))
.Where(x =>
{
var url = (string)x.Item2.Data.GetData(DataFormats.StringFormat);
Uri uriResult;
bool result = Uri.TryCreate(x, UriKind.Absolute, out uriResult) && (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);
bool result = Uri.TryCreate(url, UriKind.Absolute, out uriResult) && (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);
string urlDontCare;
return result && DownloadUrlResolver.TryNormalizeYoutubeUrl(x, out urlDontCare);
return result && DownloadUrlResolver.TryNormalizeYoutubeUrl(url, out urlDontCare);
})
.Select(x => this.shellViewModel.DirectYoutubeViewModel.AddDirectYoutubeUrlToPlaylist(new Uri(x)).ToObservable())
.Concat()
.Subscribe();
.Subscribe(async x =>
{
var url = (string)x.Item2.Data.GetData(DataFormats.StringFormat);
var target = (PlaylistEntryViewModel)((ListBoxItem)(x.Item1)).DataContext;
await this.shellViewModel.DirectYoutubeViewModel.AddDirectYoutubeUrlToPlaylist(new Uri(url), target.Index);
});

// Moving items inside the playlist
const string movePlaylistSongFormat = "MovePlaylistSong";
Expand Down

0 comments on commit 0b2a087

Please sign in to comment.