From 09dc54062911a4f89d32dc01ee1ab274c6ac4c75 Mon Sep 17 00:00:00 2001 From: Tristan Roux Date: Tue, 26 Feb 2019 20:43:38 +0100 Subject: [PATCH] Reworking queue refresh. --- Opus/Resources/Portable Class/Home.cs | 12 ++++++++++ .../Portable Class/ItemTouchCallback.cs | 2 +- Opus/Resources/Portable Class/MusicPlayer.cs | 24 +++++++++++++------ Opus/Resources/Portable Class/Queue.cs | 12 +++++++--- Opus/Resources/Portable Class/QueueAdapter.cs | 4 ++-- 5 files changed, 41 insertions(+), 13 deletions(-) diff --git a/Opus/Resources/Portable Class/Home.cs b/Opus/Resources/Portable Class/Home.cs index 8b2a114..2a5ee08 100644 --- a/Opus/Resources/Portable Class/Home.cs +++ b/Opus/Resources/Portable Class/Home.cs @@ -456,6 +456,18 @@ namespace Opus.Resources.Portable_Class } } + public void NotifyQueueInserted(int position) + { + if (adapterItems.Count > 0) + QueueAdapter?.NotifyItemInserted(position); + } + + public void NotifyQueueRemoved(int position) + { + if (adapterItems.Count > 0) + QueueAdapter?.NotifyItemRemoved(position); + } + private void ListView_ItemClick(object sender, int position) { if(adapterItems[position].contentType == SectionType.Shuffle) diff --git a/Opus/Resources/Portable Class/ItemTouchCallback.cs b/Opus/Resources/Portable Class/ItemTouchCallback.cs index f04037a..67df8eb 100644 --- a/Opus/Resources/Portable Class/ItemTouchCallback.cs +++ b/Opus/Resources/Portable Class/ItemTouchCallback.cs @@ -51,7 +51,7 @@ namespace Opus.Resources.Portable_Class if (alwaysAllowSwap && (viewHolder.AdapterPosition + 1 == ((QueueAdapter)adapter).ItemCount || viewHolder.AdapterPosition == 0)) return MakeFlag(0, 0); - if (alwaysAllowSwap && MusicPlayer.CurrentID() == viewHolder.AdapterPosition) + if (alwaysAllowSwap && MusicPlayer.CurrentID() + 1 == viewHolder.AdapterPosition) return MakeMovementFlags(dragFlag, 0); return MakeMovementFlags(dragFlag, swipeFlag); diff --git a/Opus/Resources/Portable Class/MusicPlayer.cs b/Opus/Resources/Portable Class/MusicPlayer.cs index a83b424..c7bf125 100644 --- a/Opus/Resources/Portable Class/MusicPlayer.cs +++ b/Opus/Resources/Portable Class/MusicPlayer.cs @@ -773,7 +773,8 @@ namespace Opus.Resources.Portable_Class song.IsLiveStream = isLive; queue.Insert(CurrentID() + 1, song); - Home.instance?.RefreshQueue(); + Home.instance?.NotifyQueueInserted(CurrentID() + 1); + Queue.instance?.NotifyItemInserted(CurrentID() + 1); UpdateQueueDataBase(); if (UseCastPlayer) @@ -794,7 +795,8 @@ namespace Opus.Resources.Portable_Class public void AddToQueue(Song song) { queue.Insert(CurrentID() + 1, song); - Home.instance?.RefreshQueue(); + Home.instance?.NotifyQueueInserted(CurrentID() + 1); + Queue.instance?.NotifyItemInserted(CurrentID() + 1); UpdateQueueDataBase(); if(UseCastPlayer) @@ -815,6 +817,8 @@ namespace Opus.Resources.Portable_Class public async void AddToQueue(Song[] songs) { queue.AddRange(songs); + Home.instance?.RefreshQueue(); + Queue.instance?.Refresh(); UpdateQueueDataBase(); if (UseCastPlayer) @@ -827,8 +831,10 @@ namespace Opus.Resources.Portable_Class public static void InsertToQueue(int position, Song song) { queue.Insert(position, song); + Home.instance?.NotifyQueueInserted(position); + Queue.instance?.NotifyItemInserted(position); - if(UseCastPlayer) + if (UseCastPlayer) RemotePlayer.QueueInsertItems(new MediaQueueItem[] { GetQueueItem(song) }, RemotePlayer.MediaQueue.ItemIdAtIndex(position + 1), null); UpdateQueueDataBase(); @@ -837,6 +843,8 @@ namespace Opus.Resources.Portable_Class public static void RemoveFromQueue(int position) { queue.RemoveAt(position); + Home.instance?.NotifyQueueRemoved(position); + Queue.instance?.NotifyItemRemoved(position); if (UseCastPlayer) RemotePlayer.QueueRemoveItem(RemotePlayer.MediaQueue.ItemIdAtIndex(position), null); @@ -854,9 +862,9 @@ namespace Opus.Resources.Portable_Class { queue.Add(song); UpdateQueueItemDB(song, queue.Count - 1); - - Home.instance?.RefreshQueue(); } + Home.instance?.NotifyQueueInserted(queue.Count - 1); + Queue.instance?.NotifyItemInserted(queue.Count - 1); } public void PlayLastInQueue(Song song) @@ -867,8 +875,9 @@ namespace Opus.Resources.Portable_Class { queue.Add(song); UpdateQueueItemDB(song, queue.Count - 1); - Home.instance?.RefreshQueue(); } + Home.instance?.NotifyQueueInserted(queue.Count - 1); + Queue.instance?.NotifyItemInserted(queue.Count - 1); } public void PlayLastInQueue(string filePath, string title, string artist, string youtubeID, string thumbnailURI, bool isLive = false) @@ -885,8 +894,9 @@ namespace Opus.Resources.Portable_Class { queue.Add(song); UpdateQueueItemDB(song, queue.Count - 1); - Home.instance?.RefreshQueue(); } + Home.instance?.NotifyQueueInserted(queue.Count - 1); + Queue.instance?.NotifyItemInserted(queue.Count - 1); } public void PlayPrevious() diff --git a/Opus/Resources/Portable Class/Queue.cs b/Opus/Resources/Portable Class/Queue.cs index a1fe2d3..0bae790 100644 --- a/Opus/Resources/Portable Class/Queue.cs +++ b/Opus/Resources/Portable Class/Queue.cs @@ -35,7 +35,7 @@ public class Queue : Fragment, RecyclerView.IOnItemTouchListener instance = this; ListView = view.FindViewById(Resource.Id.recycler); ListView.SetLayoutManager(new LinearLayoutManager(Application.Context)); - adapter = new QueueAdapter(MusicPlayer.queue); + adapter = new QueueAdapter(); ListView.SetAdapter(adapter); adapter.ItemClick += ListView_ItemClick; adapter.ItemLongCLick += ListView_ItemLongCLick; @@ -74,6 +74,12 @@ public class Queue : Fragment, RecyclerView.IOnItemTouchListener adapter.NotifyDataSetChanged(); } + public void NotifyItemInserted(int position) + { + position++; + adapter.NotifyItemInserted(position); + } + public void NotifyItemChanged(int position) { position++; @@ -297,8 +303,8 @@ public class Queue : Fragment, RecyclerView.IOnItemTouchListener MusicPlayer.RemoveFromQueue(position); - if (instance != null) - instance.adapter.NotifyItemRemoved(position + 1); + //if (instance != null) + // instance.adapter.NotifyItemRemoved(position + 1); } public override void OnResume() diff --git a/Opus/Resources/Portable Class/QueueAdapter.cs b/Opus/Resources/Portable Class/QueueAdapter.cs index 7a309dd..f533a45 100644 --- a/Opus/Resources/Portable Class/QueueAdapter.cs +++ b/Opus/Resources/Portable Class/QueueAdapter.cs @@ -23,7 +23,7 @@ namespace Opus.Resources.Portable_Class public bool IsSliding { get; set; } - public QueueAdapter(List songList) { } + public QueueAdapter() { } public override int ItemCount => MusicPlayer.UseCastPlayer ? MusicPlayer.RemotePlayer.MediaQueue.ItemCount + 2 : MusicPlayer.queue.Count + 2; @@ -410,7 +410,7 @@ namespace Opus.Resources.Portable_Class .SetAction(Queue.instance.GetString(Resource.String.undo), (view) => { Queue.InsertToQueue(position, song); - NotifyItemInserted(position); + //NotifyItemInserted(position); }); snackbar.View.FindViewById(Resource.Id.snackbar_text).SetTextColor(Color.White); snackbar.Show();