Skip to content

Commit

Permalink
Android: Changed MobileLibraryBrowserFragment to a simple Fragment in…
Browse files Browse the repository at this point in the history
…stead of ListFragment. Added grid view to MobileLibraryBrowserFragment for albums (the album art isn't loaded yet though).

Related to issue #406.
  • Loading branch information
ycastonguay committed Jul 12, 2013
1 parent c811cb1 commit bafc706
Show file tree
Hide file tree
Showing 10 changed files with 504 additions and 367 deletions.
7 changes: 7 additions & 0 deletions MPfm/MPfm.Android/Classes/Activities/MainActivity.cs
Expand Up @@ -29,6 +29,7 @@
using Java.Lang;
using MPfm.Android.Classes.Adapters;
using MPfm.Android.Classes.Fragments;
using MPfm.Android.Classes.Helpers;
using MPfm.Android.Classes.Navigation;
using MPfm.Android.Classes.Objects;
using MPfm.MVP.Bootstrap;
Expand All @@ -52,6 +53,7 @@ public class MainActivity : BaseActivity, IMobileOptionsMenuView
private TextView _lblArtistName;
private TextView _lblAlbumTitle;
private TextView _lblSongTitle;
private BitmapCache _bitmapCache;

protected override void OnCreate(Bundle bundle)
{
Expand Down Expand Up @@ -82,6 +84,11 @@ protected override void OnCreate(Bundle bundle)
_messengerHub.PublishAsync<MobileNavigationManagerCommandMessage>(new MobileNavigationManagerCommandMessage(this, MobileNavigationManagerCommandMessageType.ShowPlayerView));
};

// Create bitmap cache
int maxMemory = (int)(Runtime.GetRuntime().MaxMemory() / 1024);
int cacheSize = maxMemory / 8;
_bitmapCache = new BitmapCache(this, cacheSize, 800, 800);

// Listen to player changes to show/hide the mini player
_messengerHub = Bootstrapper.GetContainer().Resolve<ITinyMessengerHub>();
_messengerHub.Subscribe<PlayerPlaylistIndexChangedMessage>((message) => {
Expand Down
2 changes: 1 addition & 1 deletion MPfm/MPfm.Android/Classes/Activities/PlayerActivity.cs
Expand Up @@ -94,7 +94,7 @@ protected override void OnCreate(Bundle bundle)
// Create bitmap cache
int maxMemory = (int)(Runtime.GetRuntime().MaxMemory() / 1024);
int cacheSize = maxMemory / 8;
_bitmapCache = new BitmapCache(this, cacheSize, 800, 800);
_bitmapCache = new BitmapCache(this, cacheSize, 400, 400);

// Match height with width (cannot do that in xml)
//_imageViewAlbumArt.LayoutParameters = new ViewGroup.LayoutParams(_imageViewAlbumArt.Width, _imageViewAlbumArt.Width);
Expand Down
@@ -0,0 +1,79 @@
// Copyright © 2011-2013 Yanick Castonguay
//
// This file is part of MPfm.
//
// MPfm is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// MPfm is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with MPfm. If not, see <http://www.gnu.org/licenses/>.

using System.Collections.Generic;
using System.Linq;
using Android.App;
using Android.Graphics;
using Android.Views;
using Android.Widget;
using MPfm.Android.Classes.Objects;
using MPfm.MVP.Models;

namespace MPfm.Android.Classes.Adapters
{
public class MobileLibraryBrowserGridAdapter : BaseAdapter<LibraryBrowserEntity>
{
readonly Activity _context;
List<LibraryBrowserEntity> _items;

public MobileLibraryBrowserGridAdapter(Activity context, List<LibraryBrowserEntity> items)
{
_context = context;
_items = items;
}

public void SetData(IEnumerable<LibraryBrowserEntity> items)
{
_items = items.ToList();
NotifyDataSetChanged();
}

public override long GetItemId(int position)
{
return position;
}

public override LibraryBrowserEntity this[int position]
{
get { return _items[position]; }
}

public override int Count
{
get { return _items.Count; }
}

public override View GetView(int position, View convertView, ViewGroup parent)
{
var item = _items[position];
View view = convertView;
if (view == null) // no view to re-use, create new
view = _context.LayoutInflater.Inflate(Resource.Layout.AlbumCell, null);

//view.SetBackgroundColor(Color.White);

var title = view.FindViewById<TextView>(Resource.Id.genericcell_title);
title.Text = _items[position].Title;

var image = view.FindViewById<ImageView>(Resource.Id.genericcell_image);
image.SetBackgroundColor(Color.White);

return view;
}
}
}
52 changes: 0 additions & 52 deletions MPfm/MPfm.Android/Classes/Fragments/Base/BaseListFragment.cs

This file was deleted.

107 changes: 75 additions & 32 deletions MPfm/MPfm.Android/Classes/Fragments/MobileLibraryBrowserFragment.cs
Expand Up @@ -18,47 +18,75 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Android.App;
using Android.Graphics;
using Android.OS;
using Android.Views;
using Android.Widget;
using MPfm.Android.Classes.Adapters;
using MPfm.Android.Classes.Objects;
using MPfm.Android.Classes.Fragments.Base;
using MPfm.MVP.Models;
using MPfm.MVP.Views;
using MPfm.Sound.AudioFiles;

namespace MPfm.Android.Classes.Fragments
{
public class MobileLibraryBrowserFragment : BaseListFragment, IMobileLibraryBrowserView
public class MobileLibraryBrowserFragment : BaseFragment, IMobileLibraryBrowserView
{
private View _view;
private Button _button;
private EditText _editText;
private IEnumerable<LibraryBrowserEntity> _entities = new List<LibraryBrowserEntity>();
private ListView _listView;
private GridView _gridView;
private MobileLibraryBrowserListAdapter _listAdapter;
private MobileLibraryBrowserGridAdapter _gridAdapter;
private List<LibraryBrowserEntity> _entities = new List<LibraryBrowserEntity>();

// Leave an empty constructor or the application will crash at runtime
public MobileLibraryBrowserFragment() : base(null)
{
Console.WriteLine("MobileLibraryBrowserFragment - Empty constructor");
}

public MobileLibraryBrowserFragment(Action<IBaseView> onViewReady)
: base(onViewReady)
{
}

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, global::Android.OS.Bundle savedInstanceState)
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
Console.WriteLine("MLBFragment - OnCreateView");
ListAdapter = new MobileLibraryBrowserListAdapter(Activity, _entities.ToList());
return base.OnCreateView(inflater, container, savedInstanceState);

_view = inflater.Inflate(Resource.Layout.MobileLibraryBrowser, container, false);
_listView = _view.FindViewById<ListView>(Resource.Id.mobileLibraryBrowser_listView);
_listView.Visibility = ViewStates.Gone;
_gridView = _view.FindViewById<GridView>(Resource.Id.mobileLibraryBrowser_gridView);
_gridView.Visibility = ViewStates.Gone;

_listAdapter = new MobileLibraryBrowserListAdapter(Activity, _entities.ToList());
_listView.SetAdapter(_listAdapter);
_listView.ItemClick += ListViewOnItemClick;
_listView.ItemLongClick += ListViewOnItemLongClick;

_gridAdapter = new MobileLibraryBrowserGridAdapter(Activity, _entities.ToList());
_gridView.SetAdapter(_gridAdapter);
_gridView.ItemClick += GridViewOnItemClick;
_gridView.ItemLongClick += GridViewOnItemLongClick;

return _view;
}

private void ListViewOnItemClick(object sender, AdapterView.ItemClickEventArgs itemClickEventArgs)
{
OnItemClick(itemClickEventArgs.Position);
}

public override void OnListItemClick(ListView l, View v, int position, long id)
private void ListViewOnItemLongClick(object sender, AdapterView.ItemLongClickEventArgs itemLongClickEventArgs)
{
}

private void GridViewOnItemClick(object sender, AdapterView.ItemClickEventArgs itemClickEventArgs)
{
OnItemClick(itemClickEventArgs.Position);
}

private void GridViewOnItemLongClick(object sender, AdapterView.ItemLongClickEventArgs itemLongClickEventArgs)
{
base.OnListItemClick(l, v, position, id);
OnItemClick(position);
}

public override void OnResume()
Expand All @@ -79,12 +107,6 @@ public override void OnStop()
base.OnStop();
}

public override void OnDestroyView()
{
Console.WriteLine("MLBFragment - OnDestroyView");
base.OnDestroyView();
}

public override void OnPause()
{
Console.WriteLine("MLBFragment - OnPause");
Expand All @@ -97,10 +119,10 @@ public override void OnDestroy()
base.OnDestroy();
}

public override void OnSaveInstanceState(global::Android.OS.Bundle outState)
public override void OnDestroyView()
{
Console.WriteLine("MLBFragment - OnSaveInstanceState");
base.OnSaveInstanceState(outState);
Console.WriteLine("MLBFragment - OnDestroyView");
base.OnDestroyView();
}

#region IMobileLibraryBrowserView implementation
Expand All @@ -119,18 +141,39 @@ public void RefreshLibraryBrowser(IEnumerable<LibraryBrowserEntity> entities, Mo
{
Console.WriteLine("MLBF - RefreshLibraryBrowser - Count: {0}", entities.Count());
Activity.RunOnUiThread(() => {
_entities = entities;
var listAdapter = (MobileLibraryBrowserListAdapter)ListAdapter;
_entities = entities.ToList();
switch (browserType)
{
case MobileLibraryBrowserType.Artists:
_listView.Visibility = ViewStates.Visible;
break;
case MobileLibraryBrowserType.Albums:
_gridView.Visibility = ViewStates.Visible;
break;
case MobileLibraryBrowserType.Songs:
_listView.Visibility = ViewStates.Visible;
break;
case MobileLibraryBrowserType.Playlists:
_listView.Visibility = ViewStates.Visible;
break;
}
// Update list adapter only if the view was created
if (listAdapter != null)
if (browserType == MobileLibraryBrowserType.Albums)
{
if (_gridView != null)
{
_gridAdapter.SetData(entities);
_gridAdapter.NotifyDataSetChanged();
}
}
else
{
// http://stackoverflow.com/questions/6837397/updating-listview-by-notifydatasetchanged-has-to-use-runonuithread
Activity.RunOnUiThread(() =>
if (_listView != null)
{
listAdapter.SetData(entities);
listAdapter.NotifyDataSetChanged();
});
_listAdapter.SetData(entities);
_listAdapter.NotifyDataSetChanged();
}
}
});
}
Expand Down

0 comments on commit bafc706

Please sign in to comment.