From cda69227eb08ed7a7df0283a84bc2faed007b3cd Mon Sep 17 00:00:00 2001 From: Me on Windows Date: Sat, 3 Jul 2021 23:04:11 +0000 Subject: [PATCH] Implement a PoC of the recycler view --- QuickPlay/Interfaces.cs | 6 +---- QuickPlay/LayoutStrategies.cs | 21 +++++++++++++++++ QuickPlay/MainActivity.cs | 4 ++-- QuickPlay/QuickPlay.csproj | 6 +++++ QuickPlay/Resources/Resource.designer.cs | 24 +++++++++++-------- QuickPlay/Resources/layout/songLayout.xml | 28 +++++++++++++++++++++++ QuickPlay/SongRecycler.cs | 15 ++++++++---- 7 files changed, 84 insertions(+), 20 deletions(-) create mode 100644 QuickPlay/LayoutStrategies.cs create mode 100644 QuickPlay/Resources/layout/songLayout.xml diff --git a/QuickPlay/Interfaces.cs b/QuickPlay/Interfaces.cs index 9b35594..3281365 100644 --- a/QuickPlay/Interfaces.cs +++ b/QuickPlay/Interfaces.cs @@ -49,13 +49,9 @@ namespace QuickPlay string Identifier { get; } PlayableMetadata Metadata { get; } } - interface ILayout - { - // TODO - } interface ILayoutStrategy { - ILayout LayOut(ICollection playables); + List LayOut(ICollection playables); } public class CannotConnectException: Exception diff --git a/QuickPlay/LayoutStrategies.cs b/QuickPlay/LayoutStrategies.cs new file mode 100644 index 0000000..9d9d8b4 --- /dev/null +++ b/QuickPlay/LayoutStrategies.cs @@ -0,0 +1,21 @@ +using Android.App; +using Android.Content; +using Android.OS; +using Android.Runtime; +using Android.Views; +using Android.Widget; +using System; +using System.Collections.Generic; + +namespace QuickPlay +{ + class LexicographicLayoutStrategy : ILayoutStrategy + { + public List LayOut(ICollection playables) + { + List list = new List(playables); + list.Sort((IPlayable x, IPlayable y) => x.Identifier.CompareTo(y.Identifier)); + return list; + } + } +} \ No newline at end of file diff --git a/QuickPlay/MainActivity.cs b/QuickPlay/MainActivity.cs index 0d1bc87..0670541 100644 --- a/QuickPlay/MainActivity.cs +++ b/QuickPlay/MainActivity.cs @@ -44,7 +44,6 @@ namespace QuickPlay } catch (CannotConnectException e) { //TODO: View a toast with details and change some colors? - Console.WriteLine("This is bad."); var t = Toast.MakeText(this, e.Message + ": " + e.InnerException.Message ?? "", ToastLength.Long); t.Show(); } @@ -69,7 +68,8 @@ namespace QuickPlay private void InitializeRecyclerView() { recyclerView = FindViewById(Resource.Id.recyclerView1); - var adapter = new SongRecyclerAdapter(currentPlayer); + var layoutStrategy = new LexicographicLayoutStrategy(); + var adapter = new SongRecyclerAdapter(currentPlayer, layoutStrategy); recyclerView.SetAdapter(adapter); var layoutManager = new GridLayoutManager(this, 2, GridLayoutManager.Vertical, false); recyclerView.SetLayoutManager(layoutManager); diff --git a/QuickPlay/QuickPlay.csproj b/QuickPlay/QuickPlay.csproj index bfdf3c7..2f7d5e7 100644 --- a/QuickPlay/QuickPlay.csproj +++ b/QuickPlay/QuickPlay.csproj @@ -71,6 +71,7 @@ + @@ -221,6 +222,11 @@ + + + Designer + + + + + + + + diff --git a/QuickPlay/SongRecycler.cs b/QuickPlay/SongRecycler.cs index e0a0927..39fbd88 100644 --- a/QuickPlay/SongRecycler.cs +++ b/QuickPlay/SongRecycler.cs @@ -14,9 +14,11 @@ namespace QuickPlay class SongRecyclerAdapter : Android.Support.V7.Widget.RecyclerView.Adapter { IPlayer player; - public SongRecyclerAdapter(IPlayer player) + ILayoutStrategy layoutStrategy; + public SongRecyclerAdapter(IPlayer player, ILayoutStrategy layoutStrategy) { this.player = player; + this.layoutStrategy = layoutStrategy; } public override int ItemCount { @@ -27,18 +29,23 @@ namespace QuickPlay } public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) { - throw new NotImplementedException(); + // I admit I have little idea what I am doing. + View itemView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.songLayout, parent, false); + return new SongRecyclerViewHolder(itemView); } public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position) { - throw new NotImplementedException(); + SongRecyclerViewHolder vh = (SongRecyclerViewHolder)holder; + List layout = layoutStrategy.LayOut(player.Songs.Values); + vh.SongName.Text = layout[position].Identifier; } } class SongRecyclerViewHolder : Android.Support.V7.Widget.RecyclerView.ViewHolder { + public TextView SongName { get; private set; } public SongRecyclerViewHolder(View itemView) : base(itemView) { - throw new NotImplementedException(); + SongName = itemView.FindViewById(Resource.Id.songName); } } } \ No newline at end of file