Skip to content

Commit

Permalink
iOS: Added contextual buttons to TableView in MobileLibraryBrowserVie…
Browse files Browse the repository at this point in the history
…wController. More bug fixes with contextual buttons in CollectionView.

Related to issue #405.
  • Loading branch information
ycastonguay committed Oct 5, 2013
1 parent a230195 commit 36d657f
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 32 deletions.
168 changes: 141 additions & 27 deletions MPfm/MPfm.iOS/Classes/Controllers/MobileLibraryBrowserViewController.cs
Expand Up @@ -53,7 +53,8 @@ public partial class MobileLibraryBrowserViewController : BaseViewController, IM
List<KeyValuePair<string, UIImage>> _thumbnailImageCache;
UIButton _btnDelete;
int _deleteCellIndex = -1;
int _editingAlbumRowPosition = -1;
int _editingTableCellRowPosition = -1;
int _editingCollectionCellRowPosition = -1;

public MobileLibraryBrowserViewController(Action<IBaseView> onViewReady)
: base (onViewReady, UserInterfaceIdiomIsPhone ? "MobileLibraryBrowserViewController_iPhone" : "MobileLibraryBrowserViewController_iPad", null)
Expand Down Expand Up @@ -137,10 +138,15 @@ public override void ViewDidLoad()
swipe.Direction = UISwipeGestureRecognizerDirection.Right;
tableView.AddGestureRecognizer(swipe);

UILongPressGestureRecognizer longPressAlbums = new UILongPressGestureRecognizer(HandleLongPressAlbums);
longPressAlbums.MinimumPressDuration = 0.7f;
longPressAlbums.WeakDelegate = this;
collectionView.AddGestureRecognizer(longPressAlbums);
UILongPressGestureRecognizer longPressTableView = new UILongPressGestureRecognizer(HandleLongPressTableCellRow);
longPressTableView.MinimumPressDuration = 0.7f;
longPressTableView.WeakDelegate = this;
tableView.AddGestureRecognizer(longPressTableView);

UILongPressGestureRecognizer longPressCollectionView = new UILongPressGestureRecognizer(HandleLongPressCollectionCellRow);
longPressCollectionView.MinimumPressDuration = 0.7f;
longPressCollectionView.WeakDelegate = this;
collectionView.AddGestureRecognizer(longPressCollectionView);

base.ViewDidLoad();
}
Expand Down Expand Up @@ -248,26 +254,26 @@ private void HandleSwipe(UISwipeGestureRecognizer gestureRecognizer)
}
}

private void HandleLongPressAlbums(UILongPressGestureRecognizer gestureRecognizer)
private void HandleLongPressCollectionCellRow(UILongPressGestureRecognizer gestureRecognizer)
{
if (gestureRecognizer.State != UIGestureRecognizerState.Began)
return;

Tracing.Log("MobileLibraryBrowserViewController - HandleLongPressAlbums");
Tracing.Log("MobileLibraryBrowserViewController - HandleLongPressCollectionCellRow");
PointF pt = gestureRecognizer.LocationInView(collectionView);
NSIndexPath indexPath = collectionView.IndexPathForItemAtPoint(pt);
SetEditingAlbumRow(indexPath.Row);
SetEditingCollectionCellRow(indexPath.Row);
}

private void ResetEditingAlbumRow()
private void ResetEditingCollectionCellRow()
{
SetEditingAlbumRow(-1);
SetEditingCollectionCellRow(-1);
}

private void SetEditingAlbumRow(int position)
private void SetEditingCollectionCellRow(int position)
{
int oldPosition = _editingAlbumRowPosition;
_editingAlbumRowPosition = position;
int oldPosition = _editingCollectionCellRowPosition;
_editingCollectionCellRowPosition = position;

if (oldPosition >= 0)
{
Expand All @@ -279,6 +285,9 @@ private void SetEditingAlbumRow(int position)
oldCell.PlayButton.Alpha = 0;
oldCell.AddButton.Alpha = 0;
oldCell.DeleteButton.Alpha = 0;
oldCell.PlayButton.Frame = new RectangleF(((oldCell.Frame.Width - 44) / 2) - 8, (oldCell.Frame.Height - 44) / 2, 44, 44);
oldCell.AddButton.Frame = new RectangleF((oldCell.Frame.Width - 44) / 2 + 44, (oldCell.Frame.Height - 44) / 2, 44, 44);
oldCell.DeleteButton.Frame = new RectangleF(((oldCell.Frame.Width - 44) / 2) + 96, (oldCell.Frame.Height - 44) / 2, 44, 44);
}, null);
}
}
Expand All @@ -289,10 +298,19 @@ private void SetEditingAlbumRow(int position)
var cell = (MPfmCollectionAlbumViewCell)collectionView.VisibleCells.FirstOrDefault(x => x.Tag == position);
if (cell != null)
{
cell.PlayButton.Alpha = 0;
cell.AddButton.Alpha = 0;
cell.DeleteButton.Alpha = 0;
cell.PlayButton.Frame = new RectangleF(((cell.Frame.Width - 44) / 2) - 8, (cell.Frame.Height - 44) / 2, 44, 44);
cell.AddButton.Frame = new RectangleF((cell.Frame.Width - 44) / 2 + 44, (cell.Frame.Height - 44) / 2, 44, 44);
cell.DeleteButton.Frame = new RectangleF(((cell.Frame.Width - 44) / 2) + 96, (cell.Frame.Height - 44) / 2, 44, 44);
UIView.Animate(0.2, 0, UIViewAnimationOptions.CurveEaseIn, () => {
cell.PlayButton.Alpha = 1;
cell.AddButton.Alpha = 1;
cell.DeleteButton.Alpha = 1;
cell.PlayButton.Frame = new RectangleF(((cell.Frame.Width - 44) / 2) - 52, (cell.Frame.Height - 44) / 2, 44, 44);
cell.AddButton.Frame = new RectangleF((cell.Frame.Width - 44) / 2, (cell.Frame.Height - 44) / 2, 44, 44);
cell.DeleteButton.Frame = new RectangleF(((cell.Frame.Width - 44) / 2) + 52, (cell.Frame.Height - 44) / 2, 44, 44);
}, null);
}
}
Expand Down Expand Up @@ -328,13 +346,13 @@ public UICollectionViewCell CellForItemAtIndexPath(UICollectionView collectionVi
}
}

cell.PlayButton.Alpha = _editingAlbumRowPosition == indexPath.Row ? 1 : 0;
cell.AddButton.Alpha = _editingAlbumRowPosition == indexPath.Row ? 1 : 0;
cell.DeleteButton.Alpha = _editingAlbumRowPosition == indexPath.Row ? 1 : 0;
cell.PlayButton.Alpha = _editingCollectionCellRowPosition == indexPath.Row ? 1 : 0;
cell.AddButton.Alpha = _editingCollectionCellRowPosition == indexPath.Row ? 1 : 0;
cell.DeleteButton.Alpha = _editingCollectionCellRowPosition == indexPath.Row ? 1 : 0;

cell.PlayButton.TouchUpInside += HandleBtnPlayTouchUpInside;
cell.AddButton.TouchUpInside += HandleBtnAddTouchUpInside;
cell.DeleteButton.TouchUpInside += HandleBtnDeleteTouchUpInside;
cell.PlayButton.TouchUpInside += HandleCollectionViewPlayTouchUpInside;
cell.AddButton.TouchUpInside += HandleCollectionViewAddTouchUpInside;
cell.DeleteButton.TouchUpInside += HandleCollectionViewDeleteTouchUpInside;

return cell;
}
Expand All @@ -358,7 +376,7 @@ public int NumberOfSectionsInCollectionView(UICollectionView collectionView)
[Export ("collectionView:didSelectItemAtIndexPath:")]
public void CollectionDidSelectItemAtIndexPath(UICollectionView collectionView, NSIndexPath indexPath)
{
ResetEditingAlbumRow();
ResetEditingCollectionCellRow();
OnItemClick(indexPath.Row);
}

Expand Down Expand Up @@ -387,19 +405,22 @@ public bool CollectionShouldShowMenuForItemAtIndexPath(UICollectionView collecti
return true;
}

private void HandleBtnAddTouchUpInside(object sender, EventArgs e)
private void HandleCollectionViewAddTouchUpInside(object sender, EventArgs e)
{
ResetEditingAlbumRow();
Tracing.Log("HandleCollectionViewAddTouchUpInside");
ResetEditingCollectionCellRow();
}

private void HandleBtnDeleteTouchUpInside(object sender, EventArgs e)
private void HandleCollectionViewDeleteTouchUpInside(object sender, EventArgs e)
{
ResetEditingAlbumRow();
Tracing.Log("HandleCollectionViewDeleteTouchUpInside");
ResetEditingCollectionCellRow();
}

private void HandleBtnPlayTouchUpInside(object sender, EventArgs e)
private void HandleCollectionViewPlayTouchUpInside(object sender, EventArgs e)
{
ResetEditingAlbumRow();
Tracing.Log("HandleCollectionViewPlayTouchUpInside");
ResetEditingCollectionCellRow();
}

// [Export ("collectionView:viewForSupplementaryElementOfKind:atIndexPath:")]
Expand All @@ -410,6 +431,68 @@ private void HandleBtnPlayTouchUpInside(object sender, EventArgs e)

#region UITableView DataSource/Delegate

private void HandleLongPressTableCellRow(UILongPressGestureRecognizer gestureRecognizer)
{
if (gestureRecognizer.State != UIGestureRecognizerState.Began)
return;

Tracing.Log("MobileLibraryBrowserViewController - HandleLongPressTableCellRow");
PointF pt = gestureRecognizer.LocationInView(tableView);
NSIndexPath indexPath = tableView.IndexPathForRowAtPoint(pt);
SetEditingTableCellRow(indexPath.Row);
}

private void ResetEditingTableCellRow()
{
SetEditingTableCellRow(-1);
}

private void SetEditingTableCellRow(int position)
{
int oldPosition = _editingTableCellRowPosition;
_editingTableCellRowPosition = position;

if (oldPosition >= 0)
{
var oldItem = _items[oldPosition];
var oldCell = (MPfmTableViewCell)tableView.VisibleCells.FirstOrDefault(x => x.Tag == oldPosition);
if (oldCell != null)
{
UIView.Animate(0.2, 0, UIViewAnimationOptions.CurveEaseIn, () => {
oldCell.PlayButton.Frame = new RectangleF(UIScreen.MainScreen.Bounds.Width - 138, 4, 44, 44);
oldCell.AddButton.Frame = new RectangleF(UIScreen.MainScreen.Bounds.Width - 86, 4, 44, 44);
oldCell.DeleteButton.Frame = new RectangleF(UIScreen.MainScreen.Bounds.Width - 34, 4, 44, 44);
oldCell.PlayButton.Alpha = 0;
oldCell.AddButton.Alpha = 0;
oldCell.DeleteButton.Alpha = 0;
}, null);
}
}

if (position >= 0)
{
var item = _items[position];
var cell = (MPfmTableViewCell)tableView.VisibleCells.FirstOrDefault(x => x.Tag == position);
if (cell != null)
{
cell.PlayButton.Alpha = 0;
cell.AddButton.Alpha = 0;
cell.DeleteButton.Alpha = 0;
cell.PlayButton.Frame = new RectangleF(UIScreen.MainScreen.Bounds.Width - 138, 4, 44, 44);
cell.AddButton.Frame = new RectangleF(UIScreen.MainScreen.Bounds.Width - 86, 4, 44, 44);
cell.DeleteButton.Frame = new RectangleF(UIScreen.MainScreen.Bounds.Width - 34, 4, 44, 44);
UIView.Animate(0.2, 0, UIViewAnimationOptions.CurveEaseIn, () => {
cell.PlayButton.Alpha = 1;
cell.AddButton.Alpha = 1;
cell.DeleteButton.Alpha = 1;
cell.PlayButton.Frame = new RectangleF(UIScreen.MainScreen.Bounds.Width - 182, 4, 44, 44);
cell.AddButton.Frame = new RectangleF(UIScreen.MainScreen.Bounds.Width - 130, 4, 44, 44);
cell.DeleteButton.Frame = new RectangleF(UIScreen.MainScreen.Bounds.Width - 78, 4, 44, 44);
}, null);
}
}
}

[Export ("tableView:numberOfRowsInSection:")]
public int RowsInSection(UITableView tableview, int section)
{
Expand All @@ -426,8 +509,15 @@ public UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
var item = _items[indexPath.Row];
MPfmTableViewCell cell = (MPfmTableViewCell)tableView.DequeueReusableCell(_cellIdentifier);
if (cell == null)
{
cell = new MPfmTableViewCell(UITableViewCellStyle.Subtitle, _cellIdentifier);

// Register events only once!
cell.PlayButton.TouchUpInside += HandleTableViewPlayTouchUpInside;
cell.AddButton.TouchUpInside += HandleTableViewAddTouchUpInside;
cell.DeleteButton.TouchUpInside += HandleTableViewDeleteTouchUpInside;
}

cell.Tag = indexPath.Row;
cell.Accessory = UITableViewCellAccessory.None;
cell.TextLabel.Font = UIFont.FromName("HelveticaNeue", 14);
Expand Down Expand Up @@ -456,6 +546,10 @@ public UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
if(String.IsNullOrEmpty(item.Subtitle))
cell.TextLabel.Font = UIFont.FromName("HelveticaNeue-Light", 16);

cell.PlayButton.Alpha = _editingTableCellRowPosition == indexPath.Row ? 1 : 0;
cell.AddButton.Alpha = _editingTableCellRowPosition == indexPath.Row ? 1 : 0;
cell.DeleteButton.Alpha = _editingTableCellRowPosition == indexPath.Row ? 1 : 0;

if (_browserType == MobileLibraryBrowserType.Songs)
{
cell.IndexTextLabel.Text = item.AudioFile.TrackNumber.ToString();
Expand Down Expand Up @@ -556,6 +650,7 @@ public void DidHighlightRowAtIndexPath(UITableView tableView, NSIndexPath indexP
if (cell == null)
return;

ResetEditingTableCellRow();
cell.ImageChevron.Image = UIImage.FromBundle("Images/Tables/chevron_white");
cell.RightImage.Image = UIImage.FromBundle("Images/Icons/icon_speaker_white");
}
Expand All @@ -577,6 +672,24 @@ public float HeightForRow(UITableView tableView, NSIndexPath indexPath)
return 52;
}

private void HandleTableViewAddTouchUpInside(object sender, EventArgs e)
{
Tracing.Log("HandleTableViewAddTouchUpInside");
ResetEditingTableCellRow();
}

private void HandleTableViewDeleteTouchUpInside(object sender, EventArgs e)
{
Tracing.Log("HandleTableViewDeleteTouchUpInside");
ResetEditingTableCellRow();
}

private void HandleTableViewPlayTouchUpInside(object sender, EventArgs e)
{
Tracing.Log("HandleTableViewPlayTouchUpInside");
ResetEditingTableCellRow();
}

#endregion

private void FlushImages()
Expand Down Expand Up @@ -775,7 +888,8 @@ public async void RefreshAlbumArtCell(string artistName, string albumTitle, byte
public void RefreshLibraryBrowser(IEnumerable<LibraryBrowserEntity> entities, MobileLibraryBrowserType browserType, string navigationBarTitle, string navigationBarSubtitle, string breadcrumb, bool isPopBackstack, bool isBackstackEmpty)
{
InvokeOnMainThread(() => {
_editingAlbumRowPosition = -1;
_editingTableCellRowPosition = -1;
_editingCollectionCellRowPosition = -1;
_items = entities.ToList();
_browserType = browserType;
_navigationBarTitle = navigationBarTitle;
Expand Down
9 changes: 6 additions & 3 deletions MPfm/MPfm.iOS/Classes/Controls/MPfmCollectionAlbumViewCell.cs
Expand Up @@ -118,17 +118,20 @@ private void Initialize()

PlayButton = new UIButton(new RectangleF(((Frame.Width - 44) / 2) - 52, (Frame.Height - 44) / 2, 44, 44));
PlayButton.BackgroundColor = UIColor.FromRGBA(80, 80, 80, 225);
PlayButton.SetImage(UIImage.FromBundle("Images/Buttons/previous_on"), UIControlState.Normal);
PlayButton.SetImage(UIImage.FromBundle("Images/ContextualButtons/play"), UIControlState.Normal);
PlayButton.Layer.CornerRadius = 4;
PlayButton.Alpha = 0;

AddButton = new UIButton(new RectangleF((Frame.Width - 44) / 2, (Frame.Height - 44) / 2, 44, 44));
AddButton.BackgroundColor = UIColor.FromRGBA(80, 80, 80, 225);
AddButton.SetImage(UIImage.FromBundle("Images/Buttons/play_on"), UIControlState.Normal);
AddButton.SetImage(UIImage.FromBundle("Images/ContextualButtons/add"), UIControlState.Normal);
AddButton.Layer.CornerRadius = 4;
AddButton.Alpha = 0;

DeleteButton = new UIButton(new RectangleF(((Frame.Width - 44) / 2) + 52, (Frame.Height - 44) / 2, 44, 44));
DeleteButton.BackgroundColor = UIColor.FromRGBA(80, 80, 80, 225);
DeleteButton.SetImage(UIImage.FromBundle("Images/Buttons/next_on"), UIControlState.Normal);
DeleteButton.SetImage(UIImage.FromBundle("Images/ContextualButtons/trash"), UIControlState.Normal);
DeleteButton.Layer.CornerRadius = 4;
DeleteButton.Alpha = 0;

_imageView = new UIImageView(new RectangleF(0, 0, Frame.Width, Frame.Height));
Expand Down
29 changes: 27 additions & 2 deletions MPfm/MPfm.iOS/Classes/Controls/MPfmTableViewCell.cs
Expand Up @@ -42,6 +42,10 @@ public class MPfmTableViewCell : UITableViewCell
public UIImageView ImageAlbum3 { get; private set; }
public UILabel AlbumCountLabel { get; private set; }

public UIButton PlayButton { get; set; }
public UIButton AddButton { get; set; }
public UIButton DeleteButton { get; set; }

public float RightOffset { get; set; }

public delegate void RightButtonTap(MPfmTableViewCell cell);
Expand Down Expand Up @@ -86,14 +90,14 @@ public void Initialize()
ImageAlbum2.BackgroundColor = UIColor.White;
ImageAlbum2.Hidden = true;
ImageAlbum2.Alpha = 0.4f;
ImageAlbum2.Frame = new RectangleF(UIScreen.MainScreen.Bounds.Width - 132, 4, 44, 44);
ImageAlbum2.Frame = new RectangleF(UIScreen.MainScreen.Bounds.Width - 130, 4, 44, 44);
AddSubview(ImageAlbum2);

ImageAlbum3 = new UIImageView();
ImageAlbum3.BackgroundColor = UIColor.White;
ImageAlbum3.Hidden = true;
ImageAlbum3.Alpha = 0.15f;
ImageAlbum3.Frame = new RectangleF(UIScreen.MainScreen.Bounds.Width - 186, 4, 44, 44);
ImageAlbum3.Frame = new RectangleF(UIScreen.MainScreen.Bounds.Width - 182, 4, 44, 44);
AddSubview(ImageAlbum3);

AlbumCountLabel = new UILabel();
Expand Down Expand Up @@ -147,6 +151,27 @@ public void Initialize()
// Make sure the text label is over all other subviews
TextLabel.RemoveFromSuperview();
AddSubview(TextLabel);

PlayButton = new UIButton(new RectangleF(UIScreen.MainScreen.Bounds.Width - 182, 4, 44, 44));
PlayButton.BackgroundColor = UIColor.FromRGBA(80, 80, 80, 225);
PlayButton.SetImage(UIImage.FromBundle("Images/ContextualButtons/play"), UIControlState.Normal);
PlayButton.Layer.CornerRadius = 4;
PlayButton.Alpha = 0;
AddSubview(PlayButton);

AddButton = new UIButton(new RectangleF(UIScreen.MainScreen.Bounds.Width - 130, 4, 44, 44));
AddButton.BackgroundColor = UIColor.FromRGBA(80, 80, 80, 225);
AddButton.SetImage(UIImage.FromBundle("Images/ContextualButtons/add"), UIControlState.Normal);
AddButton.Layer.CornerRadius = 4;
AddButton.Alpha = 0;
AddSubview(AddButton);

DeleteButton = new UIButton(new RectangleF(UIScreen.MainScreen.Bounds.Width - 78, 4, 44, 44));
DeleteButton.BackgroundColor = UIColor.FromRGBA(80, 80, 80, 225);
DeleteButton.SetImage(UIImage.FromBundle("Images/ContextualButtons/trash"), UIControlState.Normal);
DeleteButton.Layer.CornerRadius = 4;
DeleteButton.Alpha = 0;
AddSubview(DeleteButton);
}

private void HandleRightButtonTouchUpInside(object sender, EventArgs e)
Expand Down
Binary file added MPfm/MPfm.iOS/Images/ContextualButtons/add.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added MPfm/MPfm.iOS/Images/ContextualButtons/add@2x.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added MPfm/MPfm.iOS/Images/ContextualButtons/play.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added MPfm/MPfm.iOS/Images/ContextualButtons/trash.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 36d657f

Please sign in to comment.