Navigation Menu

Skip to content

Commit

Permalink
iOS: MobileLibraryBrowserViewCtrl - Now flushing images when the view…
Browse files Browse the repository at this point in the history
… disappears. The images are reloaded once the view appears again. This optimizes memory usage.

Related to issue #405.
  • Loading branch information
ycastonguay committed Apr 18, 2013
1 parent e44edb3 commit f863735
Showing 1 changed file with 42 additions and 2 deletions.
Expand Up @@ -38,6 +38,7 @@ namespace MPfm.iOS.Classes.Controllers
{
public partial class MobileLibraryBrowserViewController : BaseViewController, IMobileLibraryBrowserView
{
private bool _viewHasAlreadyBeenShown = false;
private List<LibraryBrowserEntity> _items;
private string _cellIdentifier = "MobileLibraryBrowserCell";
private string _navigationBarTitle;
Expand Down Expand Up @@ -96,13 +97,19 @@ public override void ViewWillAppear(bool animated)

MPfmNavigationController navCtrl = (MPfmNavigationController)this.NavigationController;
navCtrl.SetTitle(_navigationBarTitle, _navigationBarSubtitle);

if(_viewHasAlreadyBeenShown)
ReloadImages();

_viewHasAlreadyBeenShown = true;
}

public override void ViewDidDisappear(bool animated)
{
base.ViewDidDisappear(animated);

tableView.DeselectRow(tableView.IndexPathForSelectedRow, false);
FlushImages();
}

[Export ("tableView:numberOfRowsInSection:")]
Expand Down Expand Up @@ -132,12 +139,12 @@ public UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
if (_browserType == MobileLibraryBrowserType.Albums)
{
// Check if album art is cached
string key = _items [indexPath.Row].Query.ArtistName + "_" + _items [indexPath.Row].Query.AlbumTitle;
string key = _items[indexPath.Row].Query.ArtistName + "_" + _items[indexPath.Row].Query.AlbumTitle;
KeyValuePair<string, UIImage> keyPair = _imageCache.FirstOrDefault(x => x.Key == key);
if (keyPair.Equals(default(KeyValuePair<string, UIImage>)))
{
cell.ImageView.Image = UIImage.FromBundle("Images/emptyalbumart");
OnRequestAlbumArt(_items [indexPath.Row].Query.ArtistName, _items [indexPath.Row].Query.AlbumTitle);
OnRequestAlbumArt(_items[indexPath.Row].Query.ArtistName, _items[indexPath.Row].Query.AlbumTitle);
}
else
{
Expand All @@ -161,6 +168,39 @@ public void RowSelected(UITableView tableView, NSIndexPath indexPath)
OnItemClick(indexPath.Row);
}

private void FlushImages()
{
if(imageViewAlbumCover.Image != null)
{
imageViewAlbumCover.Image.Dispose();
imageViewAlbumCover.Image = null;
}

// Flush images in table view
for(int section = 0; section < tableView.NumberOfSections(); section++)
{
for(int row = 0; row < tableView.NumberOfRowsInSection(section); row++)
{
NSIndexPath indexPath = NSIndexPath.FromItemSection(row, section);
UITableViewCell cell = tableView.CellAt(indexPath);
if(cell != null && cell.ImageView != null && cell.ImageView.Image != null)
{
cell.ImageView.Image.Dispose();
cell.ImageView.Image = null;
}
}
}
}

private void ReloadImages()
{
foreach(UITableViewCell cell in tableView.VisibleCells)
{
NSIndexPath indexPath = tableView.IndexPathForCell(cell);
OnRequestAlbumArt(_items[indexPath.Row].Query.ArtistName, _items[indexPath.Row].Query.AlbumTitle);
}
}

#region IMobileLibraryBrowserView implementation

public Action<int> OnItemClick { get; set; }
Expand Down

0 comments on commit f863735

Please sign in to comment.