mirror of
https://github.com/zoriya/Opus.git
synced 2025-12-05 22:16:17 +00:00
Reworked fav add/remove/refresh UI callbacks.
Adding fav shuffle.
This commit is contained in:
@@ -790,10 +790,10 @@ namespace Opus.Api.Services
|
||||
if (queue.Count == 0)
|
||||
{
|
||||
MainActivity.instance.HideSmallPlayer();
|
||||
if (Home.instance != null && Home.adapterItems?.Count > 0 && Home.adapterItems[0]?.SectionTitle == "Queue")
|
||||
if (Home.instance != null && Home.sections?.Count > 0 && Home.sections[0]?.SectionTitle == "Queue")
|
||||
{
|
||||
Home.instance?.adapter?.NotifyItemRemoved(0);
|
||||
Home.adapterItems?.RemoveAt(0);
|
||||
Home.sections?.RemoveAt(0);
|
||||
}
|
||||
Queue.instance?.NotifyItemRemoved(position);
|
||||
}
|
||||
@@ -1530,10 +1530,10 @@ namespace Opus.Api.Services
|
||||
currentID = -1;
|
||||
|
||||
MainActivity.instance?.HideSmallPlayer();
|
||||
if (Home.adapterItems?.Count > 0 && Home.adapterItems[0]?.SectionTitle == "Queue")
|
||||
if (Home.sections?.Count > 0 && Home.sections[0]?.SectionTitle == "Queue")
|
||||
{
|
||||
Home.instance?.adapter?.NotifyItemRemoved(0);
|
||||
Home.adapterItems?.RemoveAt(0);
|
||||
Home.sections?.RemoveAt(0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ using Android.Widget;
|
||||
using Opus.Api;
|
||||
using Opus.Api.Services;
|
||||
using Opus.DataStructure;
|
||||
using Opus.Fragments;
|
||||
using Opus.Others;
|
||||
using Square.Picasso;
|
||||
using System.Collections.Generic;
|
||||
@@ -16,17 +17,23 @@ namespace Opus.Adapter
|
||||
{
|
||||
public class LineAdapter : RecyclerView.Adapter
|
||||
{
|
||||
private enum ListType { Song, Queue, Favs }
|
||||
|
||||
public RecyclerView recycler;
|
||||
public int listPadding = 0;
|
||||
private readonly bool UseQueue = false;
|
||||
private readonly List<Song> songList;
|
||||
private readonly ListType type = ListType.Song;
|
||||
private List<Song> songs;
|
||||
private bool isEmpty = false;
|
||||
|
||||
public override int ItemCount
|
||||
{
|
||||
get
|
||||
{
|
||||
int count = UseQueue && MusicPlayer.UseCastPlayer ? MusicPlayer.RemotePlayer.MediaQueue.ItemCount : songList.Count;
|
||||
int count = type == ListType.Queue && MusicPlayer.UseCastPlayer ? MusicPlayer.RemotePlayer.MediaQueue.ItemCount : songs.Count;
|
||||
|
||||
if (type == ListType.Favs)
|
||||
count++;
|
||||
|
||||
if (count == 0)
|
||||
{
|
||||
count++;
|
||||
@@ -42,7 +49,15 @@ namespace Opus.Adapter
|
||||
public LineAdapter(List<Song> songList, RecyclerView recycler)
|
||||
{
|
||||
this.recycler = recycler;
|
||||
this.songList = songList;
|
||||
this.songs = songList;
|
||||
}
|
||||
|
||||
public LineAdapter(bool FavPlaylist, List<Song> songList, RecyclerView recycler)
|
||||
{
|
||||
if (FavPlaylist)
|
||||
type = ListType.Favs;
|
||||
this.recycler = recycler;
|
||||
this.songs = songList;
|
||||
}
|
||||
/*
|
||||
* Use this method if the songList is the queue
|
||||
@@ -50,8 +65,8 @@ namespace Opus.Adapter
|
||||
public LineAdapter(RecyclerView recycler)
|
||||
{
|
||||
this.recycler = recycler;
|
||||
UseQueue = true;
|
||||
songList = MusicPlayer.queue;
|
||||
type = ListType.Queue;
|
||||
songs = MusicPlayer.queue;
|
||||
}
|
||||
|
||||
public override void OnBindViewHolder(RecyclerView.ViewHolder viewHolder, int position)
|
||||
@@ -59,7 +74,7 @@ namespace Opus.Adapter
|
||||
if (position == 0 && isEmpty)
|
||||
{
|
||||
EmptyHolder holder = (EmptyHolder)viewHolder;
|
||||
holder.text.Text = UseQueue ? MainActivity.instance.GetString(Resource.String.empty_queue) : MainActivity.instance.GetString(Resource.String.long_loading);
|
||||
holder.text.Text = type == ListType.Queue ? MainActivity.instance.GetString(Resource.String.empty_queue) : MainActivity.instance.GetString(Resource.String.long_loading);
|
||||
holder.text.SetHeight(MainActivity.instance.DpToPx(157));
|
||||
return;
|
||||
}
|
||||
@@ -67,33 +82,44 @@ namespace Opus.Adapter
|
||||
{
|
||||
SongHolder holder = (SongHolder)viewHolder;
|
||||
|
||||
Song song = songList.Count <= position ? null : songList[position];
|
||||
if (type == ListType.Favs)
|
||||
position--;
|
||||
|
||||
|
||||
if (song == null && UseQueue)
|
||||
if(position == -1)
|
||||
{
|
||||
holder.Title.Text = "";
|
||||
holder.AlbumArt.SetImageResource(Resource.Color.background_material_dark);
|
||||
|
||||
MusicPlayer.RemotePlayer.MediaQueue.GetItemAtIndex(position);
|
||||
return;
|
||||
}
|
||||
|
||||
holder.Title.Text = song.Title;
|
||||
|
||||
if (song.AlbumArt == -1 || song.IsYt)
|
||||
{
|
||||
if(song.Album != null)
|
||||
Picasso.With(Application.Context).Load(song.Album).Placeholder(Resource.Color.background_material_dark).Transform(new RemoveBlackBorder(true)).Into(holder.AlbumArt);
|
||||
else
|
||||
Picasso.With(Application.Context).Load(Resource.Color.background_material_dark).Transform(new RemoveBlackBorder(true)).Into(holder.AlbumArt);
|
||||
holder.Title.Text = MainActivity.instance.GetString(Resource.String.shuffle_all);
|
||||
holder.AlbumArt.SetImageResource(Resource.Drawable.Shuffle);
|
||||
}
|
||||
else
|
||||
{
|
||||
var songCover = Uri.Parse("content://media/external/audio/albumart");
|
||||
var songAlbumArtUri = ContentUris.WithAppendedId(songCover, song.AlbumArt);
|
||||
Song song = songs.Count <= position ? null : songs[position];
|
||||
|
||||
Picasso.With(Application.Context).Load(songAlbumArtUri).Placeholder(Resource.Color.background_material_dark).Resize(400, 400).CenterCrop().Into(holder.AlbumArt);
|
||||
|
||||
if (song == null && type == ListType.Queue)
|
||||
{
|
||||
holder.Title.Text = "";
|
||||
holder.AlbumArt.SetImageResource(Resource.Color.background_material_dark);
|
||||
|
||||
MusicPlayer.RemotePlayer.MediaQueue.GetItemAtIndex(position);
|
||||
return;
|
||||
}
|
||||
|
||||
holder.Title.Text = song.Title;
|
||||
|
||||
if (song.AlbumArt == -1 || song.IsYt)
|
||||
{
|
||||
if (song.Album != null)
|
||||
Picasso.With(Application.Context).Load(song.Album).Placeholder(Resource.Color.background_material_dark).Transform(new RemoveBlackBorder(true)).Into(holder.AlbumArt);
|
||||
else
|
||||
Picasso.With(Application.Context).Load(Resource.Color.background_material_dark).Transform(new RemoveBlackBorder(true)).Into(holder.AlbumArt);
|
||||
}
|
||||
else
|
||||
{
|
||||
var songCover = Uri.Parse("content://media/external/audio/albumart");
|
||||
var songAlbumArtUri = ContentUris.WithAppendedId(songCover, song.AlbumArt);
|
||||
|
||||
Picasso.With(Application.Context).Load(songAlbumArtUri).Placeholder(Resource.Color.background_material_dark).Resize(400, 400).CenterCrop().Into(holder.AlbumArt);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -147,9 +173,27 @@ namespace Opus.Adapter
|
||||
return 0;
|
||||
}
|
||||
|
||||
public async void Refresh()
|
||||
{
|
||||
if(type == ListType.Favs)
|
||||
songs = await SongManager.GetFavorites();
|
||||
|
||||
if(songs.Count > 0)
|
||||
NotifyDataSetChanged();
|
||||
else
|
||||
{
|
||||
int pos = Home.sections.FindIndex(x => x.SectionTitle == "Fav");
|
||||
Home.sections.RemoveAt(pos);
|
||||
Home.instance.adapter.NotifyItemRemoved(pos);
|
||||
}
|
||||
}
|
||||
|
||||
void OnClick(int position)
|
||||
{
|
||||
if (UseQueue)
|
||||
if (type == ListType.Favs)
|
||||
position--;
|
||||
|
||||
if (type == ListType.Queue)
|
||||
{
|
||||
if (MusicPlayer.instance != null)
|
||||
MusicPlayer.instance.SwitchQueue(position);
|
||||
@@ -161,15 +205,23 @@ namespace Opus.Adapter
|
||||
MainActivity.instance.StartService(intent);
|
||||
}
|
||||
}
|
||||
else if (position == -1)
|
||||
SongManager.Shuffle(songs);
|
||||
else
|
||||
SongManager.Play(songList[position]);
|
||||
SongManager.Play(songs[position]);
|
||||
}
|
||||
|
||||
async void OnLongClick(int position)
|
||||
{
|
||||
Song item = songList[position];
|
||||
if (type == ListType.Favs)
|
||||
position--;
|
||||
|
||||
if (UseQueue)
|
||||
if (position == -1)
|
||||
return;
|
||||
|
||||
Song item = songs[position];
|
||||
|
||||
if (type == ListType.Queue)
|
||||
{
|
||||
BottomSheetDialog bottomSheet = new BottomSheetDialog(MainActivity.instance);
|
||||
View bottomView = MainActivity.instance.LayoutInflater.Inflate(Resource.Layout.BottomSheet, null);
|
||||
|
||||
@@ -73,6 +73,16 @@ namespace Opus.Adapter
|
||||
if (MusicPlayer.CurrentID() != -1 && MusicPlayer.CurrentID() <= MusicPlayer.queue.Count)
|
||||
holder.recycler.ScrollToPosition(MusicPlayer.CurrentID());
|
||||
}
|
||||
else if(items[position].SectionTitle == "Fav")
|
||||
{
|
||||
holder.title.Text = MainActivity.instance.GetString(Resource.String.favorite);
|
||||
holder.recycler.SetAdapter(new LineAdapter(true, items[position].contentValue.GetRange(0, items[position].contentValue.Count > 20 ? 20 : items[position].contentValue.Count), holder.recycler));
|
||||
holder.more.Click += (sender, e) =>
|
||||
{
|
||||
position = holder.AdapterPosition;
|
||||
MainActivity.instance.SupportFragmentManager.BeginTransaction().Replace(Resource.Id.contentView, PlaylistTracks.NewInstance(items[position].contentValue, items[position].SectionTitle)).AddToBackStack(null).Commit();
|
||||
};
|
||||
}
|
||||
else if (items[position].SectionTitle == null)
|
||||
{
|
||||
//The playlist is loading
|
||||
|
||||
@@ -25,7 +25,7 @@ namespace Opus.Fragments
|
||||
public SectionAdapter adapter;
|
||||
public LineAdapter QueueAdapter;
|
||||
public ItemTouchHelper itemTouchHelper;
|
||||
public static List<Section> adapterItems = new List<Section>();
|
||||
public static List<Section> sections = new List<Section>();
|
||||
public List<string> selectedTopics = new List<string>();
|
||||
public List<string> selectedTopicsID = new List<string>();
|
||||
public View view;
|
||||
@@ -39,8 +39,6 @@ namespace Opus.Fragments
|
||||
|
||||
public override void OnDestroy()
|
||||
{
|
||||
MainActivity.instance.contentRefresh.Refresh -= OnRefresh;
|
||||
ViewGroup rootView = Activity.FindViewById<ViewGroup>(Android.Resource.Id.Content);
|
||||
base.OnDestroy();
|
||||
instance = null;
|
||||
}
|
||||
@@ -64,21 +62,27 @@ namespace Opus.Fragments
|
||||
}
|
||||
#pragma warning restore CS4014
|
||||
|
||||
public override void OnDestroyView()
|
||||
{
|
||||
base.OnDestroyView();
|
||||
MainActivity.instance.contentRefresh.Refresh -= OnRefresh;
|
||||
}
|
||||
|
||||
private async Task PopulateView()
|
||||
{
|
||||
if (!populating)
|
||||
{
|
||||
populating = true;
|
||||
adapterItems = new List<Section>();
|
||||
sections = new List<Section>();
|
||||
|
||||
if (MusicPlayer.UseCastPlayer || (MusicPlayer.queue != null && MusicPlayer.queue?.Count > 0))
|
||||
{
|
||||
Section queue = new Section("Queue", SectionType.SinglePlaylist, MusicPlayer.queue);
|
||||
adapterItems.Add(queue);
|
||||
sections.Add(queue);
|
||||
}
|
||||
|
||||
Section shuffle = new Section(Resources.GetString(Resource.String.shuffle), SectionType.Shuffle);
|
||||
adapterItems.Add(shuffle);
|
||||
sections.Add(shuffle);
|
||||
|
||||
await Task.Run(() =>
|
||||
{
|
||||
@@ -127,17 +131,17 @@ namespace Opus.Fragments
|
||||
if (songList.Count > 0)
|
||||
{
|
||||
Section featured = new Section(Resources.GetString(Resource.String.featured), SectionType.SinglePlaylist, songList.GetRange(0, songList.Count > 50 ? 50 : songList.Count));
|
||||
adapterItems.Add(featured);
|
||||
sections.Add(featured);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
List<Song> favorites = await SongManager.GetFavorites();
|
||||
if(favorites.Count > 0)
|
||||
adapterItems.Add(new Section(GetString(Resource.String.favorite), SectionType.SinglePlaylist, favorites));
|
||||
sections.Add(new Section("Fav", SectionType.SinglePlaylist, favorites));
|
||||
|
||||
view.FindViewById(Resource.Id.loading).Visibility = ViewStates.Gone;
|
||||
adapter = new SectionAdapter(adapterItems);
|
||||
adapter = new SectionAdapter(sections);
|
||||
ListView.SetAdapter(adapter);
|
||||
adapter.ItemClick += ListView_ItemClick;
|
||||
ListView.SetItemAnimator(new DefaultItemAnimator());
|
||||
@@ -150,8 +154,8 @@ namespace Opus.Fragments
|
||||
sp.AddRange(saved);
|
||||
sp.AddRange(pl);
|
||||
|
||||
adapterItems.Add(new Section(GetString(Resource.String.playlists), SectionType.PlaylistList, sp));
|
||||
adapter.NotifyItemInserted(adapterItems.Count - 1);
|
||||
sections.Add(new Section(GetString(Resource.String.playlists), SectionType.PlaylistList, sp));
|
||||
adapter.NotifyItemInserted(sections.Count - 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -159,8 +163,8 @@ namespace Opus.Fragments
|
||||
|
||||
if(saved != null && saved.Count > 0)
|
||||
{
|
||||
adapterItems.Add(new Section(GetString(Resource.String.playlists), SectionType.PlaylistList, saved));
|
||||
adapter.NotifyItemInserted(adapterItems.Count - 1);
|
||||
sections.Add(new Section(GetString(Resource.String.playlists), SectionType.PlaylistList, saved));
|
||||
adapter.NotifyItemInserted(sections.Count - 1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -170,10 +174,10 @@ namespace Opus.Fragments
|
||||
|
||||
public void AddQueue()
|
||||
{
|
||||
if (adapterItems[0].SectionTitle != "Queue")
|
||||
if (sections[0].SectionTitle != "Queue")
|
||||
{
|
||||
Section queue = new Section("Queue", SectionType.SinglePlaylist, MusicPlayer.queue);
|
||||
adapterItems.Insert(0, queue);
|
||||
sections.Insert(0, queue);
|
||||
adapter?.NotifyItemInserted(0);
|
||||
}
|
||||
}
|
||||
@@ -198,22 +202,35 @@ namespace Opus.Fragments
|
||||
|
||||
public void RefreshQueue(bool scroll = true)
|
||||
{
|
||||
if (adapterItems.Count > 0)
|
||||
if (sections.Count > 0)
|
||||
{
|
||||
QueueAdapter?.NotifyDataSetChanged();
|
||||
if (scroll && MusicPlayer.CurrentID() != -1 && MusicPlayer.CurrentID() <= MusicPlayer.queue.Count)
|
||||
adapterItems[0].recycler?.ScrollToPosition(MusicPlayer.CurrentID());
|
||||
sections[0].recycler?.ScrollToPosition(MusicPlayer.CurrentID());
|
||||
}
|
||||
}
|
||||
|
||||
public void RefreshFavs()
|
||||
public async void RefreshFavs()
|
||||
{
|
||||
adapterItems.Find(x => x.SectionTitle == GetString(Resource.String.favorite))?.recycler.GetAdapter().NotifyDataSetChanged();
|
||||
Section section = sections.Find(x => x.SectionTitle == "Fav");
|
||||
|
||||
if(section != null)
|
||||
((LineAdapter)section.recycler.GetAdapter())?.Refresh();
|
||||
else
|
||||
{
|
||||
List<Song> favorites = await SongManager.GetFavorites();
|
||||
if (favorites.Count > 0)
|
||||
{
|
||||
int x = MainActivity.instance.HasReadPermission() ? 1 : 0;
|
||||
sections.Insert(sections.Count - x, new Section("Fav", SectionType.SinglePlaylist, favorites));
|
||||
adapter.NotifyItemInserted(sections.Count - x);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void NotifyQueueInserted(int position)
|
||||
{
|
||||
if (adapterItems.Count > 0)
|
||||
if (sections.Count > 0)
|
||||
{
|
||||
if (MusicPlayer.queue.Count == 1)
|
||||
QueueAdapter?.NotifyItemChanged(0);
|
||||
@@ -224,25 +241,25 @@ namespace Opus.Fragments
|
||||
|
||||
public void NotifyQueueRangeInserted(int position, int count)
|
||||
{
|
||||
if (adapterItems.Count > 0)
|
||||
if (sections.Count > 0)
|
||||
QueueAdapter?.NotifyItemRangeInserted(position, count);
|
||||
}
|
||||
|
||||
public void NotifyQueueChanged(int position, Java.Lang.Object payload)
|
||||
{
|
||||
if (adapterItems.Count > 0)
|
||||
if (sections.Count > 0)
|
||||
QueueAdapter?.NotifyItemChanged(position, payload);
|
||||
}
|
||||
|
||||
public void NotifyQueueRemoved(int position)
|
||||
{
|
||||
if (adapterItems.Count > 0)
|
||||
if (sections.Count > 0)
|
||||
QueueAdapter?.NotifyItemRemoved(position);
|
||||
}
|
||||
|
||||
private void ListView_ItemClick(object sender, int position)
|
||||
{
|
||||
if(adapterItems[position].contentType == SectionType.Shuffle)
|
||||
if(sections[position].contentType == SectionType.Shuffle)
|
||||
{
|
||||
LocalManager.ShuffleAll();
|
||||
}
|
||||
@@ -253,11 +270,11 @@ namespace Opus.Fragments
|
||||
base.OnResume();
|
||||
instance = this;
|
||||
|
||||
if(adapterItems.Count > 0)
|
||||
if(sections.Count > 0)
|
||||
{
|
||||
adapterItems[0].recycler?.GetAdapter()?.NotifyDataSetChanged();
|
||||
sections[0].recycler?.GetAdapter()?.NotifyDataSetChanged();
|
||||
if (MusicPlayer.CurrentID() != -1 && MusicPlayer.CurrentID() <= MusicPlayer.queue.Count)
|
||||
adapterItems[0].recycler?.ScrollToPosition(MusicPlayer.CurrentID());
|
||||
sections[0].recycler?.ScrollToPosition(MusicPlayer.CurrentID());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user