Skip to content

Instantly share code, notes, and snippets.

@davidortinau
Created February 26, 2015 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidortinau/1b5f17c6ef072cdbd518 to your computer and use it in GitHub Desktop.
Save davidortinau/1b5f17c6ef072cdbd518 to your computer and use it in GitHub Desktop.
using System;
using Cirrious.MvvmCross.Binding.BindingContext;
using Cirrious.MvvmCross.Binding.Touch.Views;
using UIKit;
using Foundation;
using DirecTV.iPad.Utils;
using Cirrious.MvvmCross.ViewModels;
using CoreGraphics;
using DirecTV.Core.Models;
using System.Collections.ObjectModel;
using DirecTV.iPad.Common.Views;
using DirecTV.Core.Apps.LiveService.ViewModels;
using DirecTV.iPad.Apps.LiveService.Views.Cells;
using System.Collections.Generic;
using Cirrious.CrossCore;
namespace DirecTV.iPad.Apps.LiveService.Views.Menus
{
public partial class MainMenuView : MvxView, IDestroyable
{
static NSString headerId = new NSString ("CollectionHeader");
public BaseMenuView BaseView;
private MvxImageViewLoader _imageLoader;
private TableSource _tableSource;
private CollectionSource _collectionSource;
private MvxPropertyChangedListener _listener;
public MainMenuViewModel VM {
get { return DataContext as MainMenuViewModel; }
}
public MainMenuView () : base ()
{
InitBindings();
CreateBaseView();
StyleHelper.MenuView(this);
}
public MainMenuView (IntPtr handle) : base (handle)
{
InitBindings ();
}
void CreateBaseView ()
{
BaseView = BaseMenuView.Create ();
BaseView.Frame = new CoreGraphics.CGRect (0, 0, Bounds.Width, Bounds.Height);
BaseView.BackgroundColor = UIColor.Clear;
Add (BaseView);
}
void InitBindings ()
{
this.DelayBind (() => {
_imageLoader = new MvxImageViewLoader (() => this.BaseView.StaticImageView) { };
var set = this.CreateBindingSet<MainMenuView, MainMenuViewModel> ();
set.Bind (_collectionSource).To (vm => vm.SubMenuItems);
set.Bind (_collectionSource).For (s => s.SelectionChangedCommand).To (vm => vm.SubMenuItemSelectedCommand);
set.Bind (_imageLoader).To (vm => vm.SubMenuStaticImage);
set.Apply ();
_listener = new MvxPropertyChangedListener (VM);
_listener.Listen (() => VM.SelectedTableViewMenuItem, OnTableViewChange);
_listener.Listen (() => VM.SelectedCollectionViewMenuItemIndex, OnCollectionViewChange);
_listener.Listen (() => VM.SubMenuItems, () => {
if (VM.SubMenuItems.Count > 0) {
SetupSubMenuItemCollectionView ();
}
});
_listener.Listen (() => VM.ProgramSections, () => {
if (VM.ProgramSections.Count > 0) {
SetupProgramsCollectionView ();
}
});
SetupProgramsCollectionView ();
SetupTable ();
});
}
void SetupTable ()
{
_tableSource = new TableSource (BaseView.MenuTableView) {
UseAnimations = false
};
this.AddBindings (new Dictionary<object, string> {
{ _tableSource, "ItemsSource MenuItems; SelectionChangedCommand SelectedCommand" }
});
BaseView.MenuTableView.Source = _tableSource;
BaseView.MenuTableView.ReloadData ();
OnTableViewChange ();
}
private void OnTableViewChange ()
{
// set the selection
_tableSource.TableView.SelectRow (NSIndexPath.FromRowSection (VM.SelectedTableViewMenuItem, 0), false, UITableViewScrollPosition.None);
// trigger the change of the collectionview
// VM.SelectedCommand.Execute();
_tableSource.RowSelected(_tableSource.TableView, NSIndexPath.FromRowSection (VM.SelectedTableViewMenuItem, 0));
}
private void OnCollectionViewChange ()
{
_collectionSource.CollectionView.SelectItem(NSIndexPath.FromRowSection (VM.SelectedCollectionViewMenuItemIndex, VM.SelectedCollectionViewMenuItemSection), false, UICollectionViewScrollPosition.None);
if(VM.SubMenuItems != null && VM.SubMenuItems.Count > 0) {
if (VM.SelectedCollectionViewMenuItemIndex < 0) {
VM.SubMenuItems [VM.ActiveSubMenuItem].Selected = false;
return;
}
VM.CollectionViewMenuItemCommand.Execute (VM.SubMenuItems [VM.SelectedCollectionViewMenuItemIndex]);
} else {
VM.ProgramsItemCommand.Execute (VM.ProgramSections [VM.SelectedCollectionViewMenuItemSection].Programs[VM.SelectedCollectionViewMenuItemIndex]);
}
}
void SetupSubMenuItemCollectionView()
{
VM.ItemsPerRow = 3;
this.BaseView.MenuCollectionView.RegisterNibForCell (UINib.FromName ("MenuGridItem", NSBundle.MainBundle), MenuGridItem.Key);
_collectionSource = new CollectionSource (this.BaseView.MenuCollectionView, MenuGridItem.Key);
_collectionSource.VM = VM;
this.BaseView.MenuCollectionView.Source = _collectionSource;
this.BaseView.MenuCollectionView.WeakDelegate = null;
// this.BaseView.MenuCollectionView.CollectionViewLayout = null;
var set = this.CreateBindingSet<MainMenuView, MainMenuViewModel> ();
set.Bind (_collectionSource).To (vm => vm.SubMenuItems);
set.Bind (_collectionSource).For (s => s.SelectionChangedCommand).To (vm => vm.SubMenuItemSelectedCommand);
set.Apply ();
}
void SetupProgramsCollectionView ()
{
VM.ItemsPerRow = 4;
this.BaseView.MenuCollectionView.RegisterNibForCell (UINib.FromName ("MovieViewCell", NSBundle.MainBundle), MovieViewCell.Key);
this.BaseView.MenuCollectionView.RegisterClassForSupplementaryView (typeof(CollectionHeader), UICollectionElementKindSection.Header, headerId);
_collectionSource = new MovieSource (this.BaseView.MenuCollectionView);
this.BaseView.MenuCollectionView.Source = _collectionSource;
this.BaseView.MenuCollectionView.WeakDelegate = new MovieDelegate ();
var set = this.CreateBindingSet<MainMenuView, MainMenuViewModel> ();
set.Bind (_collectionSource).To (vm => vm.ProgramSections);
set.Bind (_collectionSource).For (s => s.SelectionChangedCommand).To (vm => vm.SubMenuItemSelectedCommand);
set.Apply ();
this.BaseView.MenuCollectionView.ReloadData ();
}
public class TableSource : MvxSimpleTableViewSource
{
private UITableView _tableView;
public new UITableView TableView { get { return _tableView; } }
public TableSource (UITableView tableView) : base (tableView, "MenuTableViewCell", "MenuTableViewCell")
{
_tableView = tableView;
}
public override nfloat GetHeightForRow (UITableView tableView, NSIndexPath indexPath)
{
return 48;
}
public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
{
base.RowSelected (tableView, indexPath);
}
}
public class MovieSource : CollectionSource
{
public MovieSource (UICollectionView collectionView)
: base (collectionView, MovieViewCell.Key)
{
}
public override nint NumberOfSections (UICollectionView collectionView)
{
return (ItemsSource as ObservableCollection<ProgramSection>).Count;
}
public override UICollectionReusableView GetViewForSupplementaryElement (UICollectionView collectionView, NSString elementKind, NSIndexPath indexPath)
{
var headerView = (CollectionHeader)collectionView.DequeueReusableSupplementaryView (elementKind, headerId, indexPath);
headerView.Text = (ItemsSource as ObservableCollection<ProgramSection>)[indexPath.Section].SectionTitle; // TODO make this dynamic?
return headerView;
}
public override nint GetItemsCount (UICollectionView collectionView, nint section)
{
return (nint)(ItemsSource as ObservableCollection<ProgramSection>)[(int)section].Programs.Count;
}
protected override object GetItemAt (NSIndexPath indexPath)
{
var program = (ItemsSource as ObservableCollection<ProgramSection>)[indexPath.Section].Programs[indexPath.Row];
return program;
}
public override void ItemSelected (UICollectionView collectionView, NSIndexPath indexPath)
{
Mvx.Trace("SELECTED");
base.ItemSelected (collectionView, indexPath);
}
// public override void WillDisplaySupplementaryView (UICollectionView collectionView, UICollectionReusableView view, string elementKind, NSIndexPath indexPath)
// {
// throw new System.NotImplementedException ();
// }
}
public class CollectionSource : MvxCollectionViewSource
{
private UICollectionView _collectionView;
public new UICollectionView CollectionView { get { return _collectionView; } }
private MainMenuViewModel _vm;
public MainMenuViewModel VM {
get { return _vm; }
set { _vm = value; }
}
public CollectionSource (UICollectionView collectionView, NSString defaultCellIdentifier) : base (collectionView, defaultCellIdentifier)
{
_collectionView = collectionView;
}
public override void ItemSelected (UICollectionView collectionView, NSIndexPath indexPath)
{
_vm.SelectedCollectionViewMenuItemIndex = indexPath.Row;
Mvx.Trace("SELECTED");
base.ItemSelected (collectionView, indexPath);
}
}
public class MovieDelegate : UICollectionViewDelegateFlowLayout
{
public override CGSize GetSizeForItem (UICollectionView collectionView, UICollectionViewLayout layout, NSIndexPath indexPath)
{
return new CGSize (164, 240);//135
}
public override nfloat GetMinimumInteritemSpacingForSection (UICollectionView collectionView, UICollectionViewLayout layout, nint section)
{
return 0;
}
public override UIEdgeInsets GetInsetForSection (UICollectionView collectionView, UICollectionViewLayout layout, nint section)
{
return new UIEdgeInsets (0, 0, 0, 10);
}
public override nfloat GetMinimumLineSpacingForSection (UICollectionView collectionView, UICollectionViewLayout layout, nint section)
{
return 0;
}
public override CGSize GetReferenceSizeForHeader (UICollectionView collectionView, UICollectionViewLayout layout, nint section)
{
return new CGSize (575, 48);
}
}
public class CollectionHeader : UICollectionReusableView
{
UILabel label;
public string Text {
get {
return label.Text;
}
set {
label.Text = value;
SetNeedsDisplay ();
}
}
[Export ("initWithFrame:")]
public CollectionHeader (CGRect frame) : base (frame)
{
label = new UILabel () {
Frame = new CGRect (0, 0, 300, 50),
BackgroundColor = UIColor.Clear
};
AddSubview (label);
BackgroundColor = UIColor.Clear;
StyleHelper.StyleLabel (label, 28, 0xFFFFFF, true);
}
}
protected override void Dispose (bool disposing)
{
Destroy();
// Brute force, remove everything
foreach (var view in Subviews)
view.RemoveFromSuperview ();
base.Dispose (disposing);
}
public void Destroy(){
BindingContext.ClearAllBindings();
VM.Destroy();
_listener.Clear();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment