From 73d285db1e87c56bb02dee1dfab1926db02d60b8 Mon Sep 17 00:00:00 2001
From: Tristan Roux
Date: Tue, 19 Feb 2019 14:48:14 +0100
Subject: [PATCH] Solving bugs with queue remove and slide.
---
.../Portable Class/CurrentItemDecoration.cs | 13 ++++++++++--
.../Portable Class/ItemTouchCallback.cs | 15 ++++++++++----
.../Portable Class/PlaylistTrackAdapter.cs | 1 +
MusicApp/Resources/Portable Class/Queue.cs | 4 ++--
.../Resources/Portable Class/QueueAdapter.cs | 20 +++++++++----------
MusicApp/Resources/layout/PreferenceRoot.xml | 2 ++
6 files changed, 36 insertions(+), 19 deletions(-)
diff --git a/MusicApp/Resources/Portable Class/CurrentItemDecoration.cs b/MusicApp/Resources/Portable Class/CurrentItemDecoration.cs
index 7a7c6dc..d5652b7 100644
--- a/MusicApp/Resources/Portable Class/CurrentItemDecoration.cs
+++ b/MusicApp/Resources/Portable Class/CurrentItemDecoration.cs
@@ -12,13 +12,22 @@ namespace MusicApp.Resources.Portable_Class
{
public class CurrentItemDecoration : RecyclerView.ItemDecoration
{
- public CurrentItemDecoration() { }
+ public IItemTouchAdapter adapter;
+
+ public CurrentItemDecoration(IItemTouchAdapter adapter) { this.adapter = adapter; }
public override void OnDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state)
{
base.OnDrawOver(c, parent, state);
- if(parent.ChildCount > 1 && parent.Width > 0)
+ if (adapter.IsSliding)
+ {
+ parent.SetPadding(0, 0, 0, 0);
+ return;
+ }
+
+
+ if (parent.ChildCount > 1 && parent.Width > 0)
{
int firstPos = parent.GetChildAdapterPosition(parent.GetChildAt(0));
int lastPos = parent.GetChildAdapterPosition(parent.GetChildAt(parent.ChildCount - 1));
diff --git a/MusicApp/Resources/Portable Class/ItemTouchCallback.cs b/MusicApp/Resources/Portable Class/ItemTouchCallback.cs
index 3744f60..f578d25 100644
--- a/MusicApp/Resources/Portable Class/ItemTouchCallback.cs
+++ b/MusicApp/Resources/Portable Class/ItemTouchCallback.cs
@@ -59,12 +59,12 @@ namespace MusicApp.Resources.Portable_Class
public override bool OnMove(RecyclerView recyclerView, RecyclerView.ViewHolder source, RecyclerView.ViewHolder target)
{
- if (Queue.instance != null && target.AdapterPosition + 1 == ((QueueAdapter)adapter).ItemCount)
+ adapter.IsSliding = true;
+
+ if (Queue.instance != null && (target.AdapterPosition + 1 == ((QueueAdapter)adapter).ItemCount || target.AdapterPosition == 0))
return false;
- if (from == -1)
- from = source.AdapterPosition;
-
+ from = source.AdapterPosition;
to = target.AdapterPosition;
adapter.ItemMoved(source.AdapterPosition, target.AdapterPosition);
return true;
@@ -119,13 +119,18 @@ namespace MusicApp.Resources.Portable_Class
public override void ClearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder)
{
base.ClearView(recyclerView, viewHolder);
+ adapter.IsSliding = false;
viewHolder.ItemView.Alpha = 1;
MainActivity.instance.contentRefresh.Enabled = true;
if (from != -1 && to != -1 && from != to)
+ {
adapter.ItemMoveEnded(from, to);
+ from = -1;
+ to = -1;
+ }
if (viewHolder is IItemTouchHolder)
@@ -135,6 +140,8 @@ namespace MusicApp.Resources.Portable_Class
public interface IItemTouchAdapter
{
+ bool IsSliding { get; set; }
+
void ItemMoved(int fromPosition, int toPosition);
void ItemMoveEnded(int fromPosition, int toPosition);
void ItemDismissed(int position);
diff --git a/MusicApp/Resources/Portable Class/PlaylistTrackAdapter.cs b/MusicApp/Resources/Portable Class/PlaylistTrackAdapter.cs
index be566d4..c723688 100644
--- a/MusicApp/Resources/Portable Class/PlaylistTrackAdapter.cs
+++ b/MusicApp/Resources/Portable Class/PlaylistTrackAdapter.cs
@@ -20,6 +20,7 @@ namespace MusicApp.Resources.Portable_Class
public event EventHandler ItemLongClick;
public int listPadding;
public bool IsEmpty = false;
+ public bool IsSliding { get; set; }
public PlaylistTrackAdapter(List songList)
{
diff --git a/MusicApp/Resources/Portable Class/Queue.cs b/MusicApp/Resources/Portable Class/Queue.cs
index f1936eb..c7d9fe5 100644
--- a/MusicApp/Resources/Portable Class/Queue.cs
+++ b/MusicApp/Resources/Portable Class/Queue.cs
@@ -39,7 +39,7 @@ public class Queue : Fragment
adapter.ItemClick += ListView_ItemClick;
adapter.ItemLongCLick += ListView_ItemLongCLick;
ListView.SetItemAnimator(new DefaultItemAnimator());
- ListView.AddItemDecoration(new CurrentItemDecoration());
+ ListView.AddItemDecoration(new CurrentItemDecoration(adapter));
ListView.ScrollChange += Scroll;
ItemTouchHelper.Callback callback = new ItemTouchCallback(adapter, true);
@@ -282,7 +282,7 @@ public class Queue : Fragment
MusicPlayer.RemoveFromQueue(position);
if (instance != null)
- instance.adapter.NotifyItemRemoved(position);
+ instance.adapter.NotifyItemRemoved(position + 1);
}
public override void OnResume()
diff --git a/MusicApp/Resources/Portable Class/QueueAdapter.cs b/MusicApp/Resources/Portable Class/QueueAdapter.cs
index 9221266..1c89683 100644
--- a/MusicApp/Resources/Portable Class/QueueAdapter.cs
+++ b/MusicApp/Resources/Portable Class/QueueAdapter.cs
@@ -20,6 +20,9 @@ namespace MusicApp.Resources.Portable_Class
public event EventHandler ItemClick;
public event EventHandler ItemLongCLick;
+ public bool IsSliding { get; set; }
+
+
public QueueAdapter(List songList) { }
public override int ItemCount => MusicPlayer.UseCastPlayer ? MusicPlayer.RemotePlayer.MediaQueue.ItemCount + 2 : MusicPlayer.queue.Count + 2;
@@ -353,18 +356,15 @@ namespace MusicApp.Resources.Portable_Class
public void ItemMoved(int fromPosition, int toPosition)
{
- fromPosition--;
- toPosition--;
-
- if(fromPosition < toPosition)
+ if (fromPosition < toPosition)
{
- for(int i = fromPosition; i < toPosition; i++)
- MusicPlayer.queue = Swap(MusicPlayer.queue, i, i + 1);
+ for (int i = fromPosition; i < toPosition; i++)
+ MusicPlayer.queue = Swap(MusicPlayer.queue, i - 1, i);
}
else
{
- for(int i = fromPosition; i > toPosition; i--)
- MusicPlayer.queue = Swap(MusicPlayer.queue, i, i - 1);
+ for (int i = fromPosition; i > toPosition; i--)
+ MusicPlayer.queue = Swap(MusicPlayer.queue, i - 1, i - 2);
}
NotifyItemMoved(fromPosition, toPosition);
@@ -394,8 +394,6 @@ namespace MusicApp.Resources.Portable_Class
List Swap(List list, int fromPosition, int toPosition)
{
- fromPosition--;
- toPosition--;
T item = list[fromPosition];
list[fromPosition] = list[toPosition];
list[toPosition] = item;
@@ -408,7 +406,7 @@ namespace MusicApp.Resources.Portable_Class
Song song = MusicPlayer.queue[position];
Queue.RemoveFromQueue(position);
- Snackbar snackbar = Snackbar.Make(MainActivity.instance.FindViewById(Resource.Id.recycler), (song.Title.Length > 20 ? song.Title.Substring(0, 17) + "..." : song.Title) + Queue.instance.GetString(Resource.String.removed_from_queue), Snackbar.LengthShort)
+ Snackbar snackbar = Snackbar.Make(Player.instance.View, (song.Title.Length > 20 ? song.Title.Substring(0, 17) + "..." : song.Title) + Queue.instance.GetString(Resource.String.removed_from_queue), Snackbar.LengthShort)
.SetAction(Queue.instance.GetString(Resource.String.undo), (view) =>
{
Queue.InsertToQueue(position, song);
diff --git a/MusicApp/Resources/layout/PreferenceRoot.xml b/MusicApp/Resources/layout/PreferenceRoot.xml
index 6d78b6b..aaa90c4 100644
--- a/MusicApp/Resources/layout/PreferenceRoot.xml
+++ b/MusicApp/Resources/layout/PreferenceRoot.xml
@@ -4,11 +4,13 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
+ android:fitsSystemWindows="true"
android:orientation="vertical" >