Created
October 1, 2013 15:12
-
-
Save deapsquatter/6780011 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Specialized; | |
using System.Linq; | |
using Cirrious.CrossCore.Core; | |
using Cirrious.CrossCore.WeakSubscription; | |
using Cirrious.MvvmCross.Binding.Attributes; | |
using Cirrious.MvvmCross.Binding.BindingContext; | |
using Cirrious.MvvmCross.Binding.ExtensionMethods; | |
using MonoMac.AppKit; | |
using MonoMac.Foundation; | |
namespace CentraStage.Mac | |
{ | |
public class CsTableViewSource : NSTableViewSource | |
{ | |
IEnumerable itemsSource; | |
IDisposable subscription; | |
NSTableView tableView; | |
public CsTableViewSource (NSTableView tableView): base() | |
{ | |
this.tableView = tableView; | |
} | |
public override int GetRowCount (NSTableView tableView) | |
{ | |
return ItemsSource.Count (); | |
} | |
[MvxSetToNullAfterBinding] | |
public virtual IEnumerable ItemsSource | |
{ | |
get { return itemsSource; } | |
set | |
{ | |
if (itemsSource == value) | |
return; | |
if (subscription != null) | |
{ | |
subscription.Dispose(); | |
subscription = null; | |
} | |
itemsSource = value; | |
var collectionChanged = itemsSource as INotifyCollectionChanged; | |
if (collectionChanged != null) | |
{ | |
subscription = collectionChanged.WeakSubscribe(CollectionChangedOnCollectionChanged); | |
} | |
ReloadTableData(); | |
} | |
} | |
void ReloadTableData () | |
{ | |
tableView.ReloadData (); | |
} | |
NSView GetOrCreateViewFor (NSTableView tableView, NSTableColumn tableColumn) | |
{ | |
var view = tableView.MakeView (tableColumn.Identifier, this); | |
var bindableColumn = tableColumn as CsTableColumn; | |
if (bindableColumn != null){ | |
if (view == null) | |
view = new CsTableCellView (bindableColumn.BindingText); | |
else { | |
IMvxBindingContextOwner bindableView = view as IMvxBindingContextOwner; | |
bindableView.CreateBindingContext (bindableColumn.BindingText); | |
} | |
} | |
return view; | |
} | |
public override NSView GetViewForItem (NSTableView tableView, NSTableColumn tableColumn, int row) | |
{ | |
if (ItemsSource == null) | |
return null; | |
var item = ItemsSource.ElementAt(row); | |
var view = GetOrCreateViewFor (tableView, tableColumn); | |
var bindable = view as IMvxDataConsumer; | |
if (bindable != null) | |
bindable.DataContext = item; | |
return view; | |
} | |
protected virtual void CollectionChangedOnCollectionChanged(object sender, NotifyCollectionChangedEventArgs args) | |
{ | |
TryDoAnimatedChange (args); | |
} | |
protected static NSIndexSet CreateNSIndexSet(int startingPosition, int count) | |
{ | |
return NSIndexSet.FromArray (Enumerable.Range(startingPosition, count).ToArray()); | |
} | |
protected bool TryDoAnimatedChange(NotifyCollectionChangedEventArgs args) | |
{ | |
switch (args.Action) | |
{ | |
case NotifyCollectionChangedAction.Add: | |
{ | |
var newIndexSet = CreateNSIndexSet(args.NewStartingIndex, args.NewItems.Count); | |
tableView.InsertRows (newIndexSet, NSTableViewAnimation.Fade); | |
return true; | |
} | |
case NotifyCollectionChangedAction.Remove: | |
{ | |
var newIndexSet = CreateNSIndexSet(args.OldStartingIndex, args.OldItems.Count); | |
tableView.RemoveRows (newIndexSet, NSTableViewAnimation.Fade); | |
return true; | |
} | |
case NotifyCollectionChangedAction.Move: | |
{ | |
if (args.NewItems.Count != 1 && args.OldItems.Count != 1) | |
return false; | |
tableView.MoveRow (args.OldStartingIndex, args.NewStartingIndex); | |
return true; | |
} | |
case NotifyCollectionChangedAction.Replace: | |
{ | |
// if (args.NewItems.Count != args.OldItems.Count) | |
// return false; | |
// | |
// var indexPath = NSIndexPath.FromItemSection(args.NewStartingIndex, 0); | |
// TableView.ReloadRows(new[] | |
// { | |
// indexPath | |
// }, UITableViewRowAnimation.Fade); | |
tableView.ReloadData (); | |
return true; | |
} | |
default: | |
return false; | |
} | |
} | |
// protected static NSIndexPath[] CreateNSIndexPathArray(int startingPosition, int count) | |
// { | |
// var newIndexPaths = new NSIndexPath[count]; | |
// for (var i = 0; i < count; i++) | |
// { | |
// newIndexPaths[i] = NSIndexPath.FromItemSection(i + startingPosition, 0); | |
// } | |
// return newIndexPaths; | |
// } | |
// public override int RowsInSection(NSTableView tableview, int section) | |
// { | |
// if (ItemsSource == null) | |
// return 0; | |
// | |
// return ItemsSource.Count(); | |
// } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment