From 609bce0e4d37e1b364d448c16e58c9373d831c17 Mon Sep 17 00:00:00 2001 From: Anonymous Raccoon <32224410+AnonymusRaccoon@users.noreply.github.com> Date: Sat, 12 May 2018 18:46:49 +0200 Subject: [PATCH] Adding save option on youtube search. --- MusicApp/Resources/Portable Class/Playlist.cs | 41 +----------------- .../Resources/Portable Class/YoutubeEngine.cs | 42 +++++++++++++++++++ .../Resources/Portable Class/YtAdapter.cs | 6 ++- 3 files changed, 48 insertions(+), 41 deletions(-) diff --git a/MusicApp/Resources/Portable Class/Playlist.cs b/MusicApp/Resources/Portable Class/Playlist.cs index cab587f..a28a197 100644 --- a/MusicApp/Resources/Portable Class/Playlist.cs +++ b/MusicApp/Resources/Portable Class/Playlist.cs @@ -241,46 +241,7 @@ namespace MusicApp.Resources.Portable_Class { string url = view.FindViewById(Resource.Id.playlistURL).Text; string playlistID = url.Substring(url.IndexOf('=') + 1); - - ChannelSectionsResource.ListRequest forkedRequest = YoutubeEngine.youtubeService.ChannelSections.List("snippet,contentDetails"); - forkedRequest.Mine = true; - ChannelSectionListResponse forkedResponse = await forkedRequest.ExecuteAsync(); - - foreach (ChannelSection section in forkedResponse.Items) - { - if (section.Snippet.Title == "Saved Playlists") - { - //AddToSection - if (section.ContentDetails.Playlists.Contains(playlistID)) - { - Snackbar.Make(Activity.FindViewById(Resource.Id.snackBar), "You've already added this playlist.", 1).Show(); - return; - } - else - { - section.ContentDetails.Playlists.Add(playlistID); - ChannelSectionsResource.UpdateRequest request = YoutubeEngine.youtubeService.ChannelSections.Update(section, "snippet,contentDetails"); - ChannelSection response = await request.ExecuteAsync(); - Refresh(); - return; - } - } - } - //CreateSection and add to it - ChannelSection newSection = new ChannelSection(); - ChannelSectionContentDetails details = new ChannelSectionContentDetails(); - ChannelSectionSnippet snippet = new ChannelSectionSnippet(); - - details.Playlists = new List() { playlistID }; - snippet.Title = "Saved Playlists"; - snippet.Type = "multiplePlaylists"; - snippet.Style = "horizontalRow"; - - newSection.ContentDetails = details; - newSection.Snippet = snippet; - - ChannelSectionsResource.InsertRequest insert = YoutubeEngine.youtubeService.ChannelSections.Insert(newSection, "snippet,contentDetails"); - ChannelSection insertResponse = await insert.ExecuteAsync(); + await YoutubeEngine.ForkPlaylist(playlistID); Refresh(); }) .Show(); diff --git a/MusicApp/Resources/Portable Class/YoutubeEngine.cs b/MusicApp/Resources/Portable Class/YoutubeEngine.cs index 6d2c39a..561ced0 100644 --- a/MusicApp/Resources/Portable Class/YoutubeEngine.cs +++ b/MusicApp/Resources/Portable Class/YoutubeEngine.cs @@ -813,6 +813,48 @@ namespace MusicApp.Resources.Portable_Class insertRequest.Execute(); } + public static async Task ForkPlaylist(string playlistID) + { + ChannelSectionsResource.ListRequest forkedRequest = youtubeService.ChannelSections.List("snippet,contentDetails"); + forkedRequest.Mine = true; + ChannelSectionListResponse forkedResponse = await forkedRequest.ExecuteAsync(); + + foreach (ChannelSection section in forkedResponse.Items) + { + if (section.Snippet.Title == "Saved Playlists") + { + //AddToSection + if (section.ContentDetails.Playlists.Contains(playlistID)) + { + Snackbar.Make(MainActivity.instance.FindViewById(Resource.Id.snackBar), "You've already added this playlist.", 1).Show(); + return; + } + else + { + section.ContentDetails.Playlists.Add(playlistID); + ChannelSectionsResource.UpdateRequest request = youtubeService.ChannelSections.Update(section, "snippet,contentDetails"); + ChannelSection response = await request.ExecuteAsync(); + return; + } + } + } + //CreateSection and add to it + ChannelSection newSection = new ChannelSection(); + ChannelSectionContentDetails details = new ChannelSectionContentDetails(); + ChannelSectionSnippet snippet = new ChannelSectionSnippet(); + + details.Playlists = new List() { playlistID }; + snippet.Title = "Saved Playlists"; + snippet.Type = "multiplePlaylists"; + snippet.Style = "horizontalRow"; + + newSection.ContentDetails = details; + newSection.Snippet = snippet; + + ChannelSectionsResource.InsertRequest insert = youtubeService.ChannelSections.Insert(newSection, "snippet,contentDetails"); + ChannelSection insertResponse = await insert.ExecuteAsync(); + } + public static async void RandomPlay(string playlistID) { List tracks = new List(); diff --git a/MusicApp/Resources/Portable Class/YtAdapter.cs b/MusicApp/Resources/Portable Class/YtAdapter.cs index d6ef027..00b6da7 100644 --- a/MusicApp/Resources/Portable Class/YtAdapter.cs +++ b/MusicApp/Resources/Portable Class/YtAdapter.cs @@ -94,7 +94,7 @@ namespace MusicApp.Resources.Portable_Class AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.instance, MainActivity.dialogTheme); builder.SetTitle("Pick an action"); - builder.SetItems(new string[] { "Random play", "Download" }, (senderAlert, args) => + builder.SetItems(new string[] { "Random play", "Save To Youtube", "Download" }, (senderAlert, args) => { switch (args.Which) { @@ -102,6 +102,10 @@ namespace MusicApp.Resources.Portable_Class YoutubeEngine.RandomPlay(playlist.GetPath()); break; case 1: + #pragma warning disable CS4014 + YoutubeEngine.ForkPlaylist(playlist.GetPath()); + break; + case 2: YoutubeEngine.DownloadPlaylist(playlist.GetPath()); break; default: