MonoMobile.Views.DataContextBinder.HandleNotifyCollectionChanged C# (CSharp) Method

HandleNotifyCollectionChanged() public method

public HandleNotifyCollectionChanged ( object sender, NotifyCollectionChangedEventArgs e ) : void
sender object
e System.Collections.Specialized.NotifyCollectionChangedEventArgs
return void
		public void HandleNotifyCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
		{	
			var needsReload = Section.DataContext.Count == 0;

			if (e.Action == NotifyCollectionChangedAction.Add)
			{
				var index = 0;
				foreach (var item in e.NewItems)
				{
					var row = e.NewStartingIndex < 0 ? 0 : e.NewStartingIndex;
					AddRow(Section, row + index, item);
					index++;
				}
			}

			if (e.Action == NotifyCollectionChangedAction.Remove)
			{
				foreach (var item in e.OldItems)
				{
					RemoveRow(Section, item);
				}
			}

			if (e.Action == NotifyCollectionChangedAction.Reset)
			{
				foreach(var item in Section.DataContext)
				{
					RemovePropertyChangedHandler(item);
				}

				Section.DataContext.Clear();
				needsReload = true;
			}

			if (e.Action == NotifyCollectionChangedAction.Move)
			{
				var index = 0;
				foreach (var newItem in e.NewItems)
				{
					var row = e.OldStartingIndex + index;
					var oldItem = Section.DataContext[row];

					row = e.NewStartingIndex + index;

					RemoveRow(Section, oldItem, UITableViewRowAnimation.Fade);
					InsertRow(Section, row, newItem, UITableViewRowAnimation.Left);
					index++;
				}				
			}

			if (e.Action == NotifyCollectionChangedAction.Replace)
			{
				var index = 0;
				foreach (var item in e.NewItems)
				{
					var row = index;
					ReplaceRow(Section, e.OldItems[row], item);
					index++;
				}
			}

			needsReload = Section.DataContext.Count == 0 || needsReload;

			if (needsReload)
			{
				if (Controller == MonoMobileApplication.CurrentViewController)
				{
					InvokeOnMainThread(()=> Controller.TableView.ReloadSections(NSIndexSet.FromIndex(Section.Index), UITableViewRowAnimation.Automatic));
				}
				else if (Controller.TableView != null)
				{
					InvokeOnMainThread(()=> Controller.TableView.ReloadData());
				}	
			}
		}