protected void BindCollection <TElement>(CollectionViewModel <TElement> viewModel, Func <TElement, Element> element, bool activateNow = false) { Action updateDel = () => { try { IEnumerable <TElement> items = viewModel.Items; var filterFn = viewModel.FilteringFunction; if (filterFn != null) { items = filterFn(items); } var sortFn = viewModel.SortingFunction; if (sortFn != null) { items = sortFn(items); } var groupingFn = viewModel.GroupingFunction; IEnumerable <IGrouping <string, TElement> > groupedItems = null; if (groupingFn != null) { groupedItems = groupingFn(items); } if (groupedItems == null) { RenderList(items, element, viewModel.MoreItems); } else { RenderGroupedItems(groupedItems, element, viewModel.MoreItems); } } catch (Exception e) { e.Report(); } }; viewModel.Bind(x => x.GroupingFunction, updateDel); viewModel.Bind(x => x.FilteringFunction, updateDel); viewModel.Bind(x => x.SortingFunction, updateDel); //The CollectionViewModel binds all of the collection events from the observablecollection + more //So just listen to it. viewModel.CollectionChanged += (sender, e) => InvokeOnMainThread(() => updateDel()); if (activateNow) { updateDel(); } }
protected void BindCollection <TElement>(CollectionViewModel <TElement> viewModel, Func <TElement, Element> element, bool activateNow = false) { var weakVm = new WeakReference <CollectionViewModel <TElement> >(viewModel); var weakRoot = new WeakReference <RootElement>(Root); Action updateDel = () => { try { IEnumerable <TElement> items = viewModel.Items; var filterFn = viewModel.FilteringFunction; if (filterFn != null) { items = filterFn(items); } var sortFn = viewModel.SortingFunction; if (sortFn != null) { items = sortFn(items); } var groupingFn = viewModel.GroupingFunction; IEnumerable <IGrouping <string, TElement> > groupedItems = null; if (groupingFn != null) { groupedItems = groupingFn(items); } ICollection <Section> newSections; if (groupedItems == null) { newSections = RenderList(items, element, weakVm.Get()?.MoreItems); } else { newSections = RenderGroupedItems(groupedItems, element, weakVm.Get()?.MoreItems); } CreateEmptyHandler(newSections.Sum(s => s.Elements.Count) == 0); weakRoot.Get()?.Reset(newSections); } catch { } }; viewModel.Bind(x => x.GroupingFunction).Subscribe(_ => updateDel()); viewModel.Bind(x => x.FilteringFunction).Subscribe(_ => updateDel()); viewModel.Bind(x => x.SortingFunction).Subscribe(_ => updateDel()); //The CollectionViewModel binds all of the collection events from the observablecollection + more //So just listen to it. viewModel.CollectionChanged += (sender, e) => _dumb.InvokeOnMainThread(updateDel); if (activateNow) { updateDel(); } }