Skip to content

Instantly share code, notes, and snippets.

@rdelrosario
Created April 28, 2021 15:58
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 rdelrosario/d091eb80ee541e6990b109a738b0e782 to your computer and use it in GitHub Desktop.
Save rdelrosario/d091eb80ee541e6990b109a738b0e782 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using DynamicData;
using DynamicData.Binding;
using ReactiveUI;
namespace DynamicDataGroupingSample
{
public class ObservableGroupedCollection<TGroupKey, TObject, TKey> : ObservableCollectionExtended<TObject>, IDisposable
{
public TGroupKey Key { get; }
public ObservableGroupedCollection(IGroup<TObject, TKey, TGroupKey> group, IObservable<Func<TObject, bool>> filter = null, IObservable<IComparer<TObject>> comparer = null)
{
this.Key = group.Key;
//load and sort the grouped list
var dataLoader = group.Cache.Connect()
.RefCount()
.Filter(filter ?? Observable.Return<Func<TObject, bool>>(x => true))
.Sort(comparer ?? Observable.Return<IComparer<TObject>>(SortExpressionComparer<TObject>.Ascending(a => a.ToString())))
.ObserveOn(RxApp.MainThreadScheduler)
.Bind(this) //make the reset threshold large because xamarin is slow when reset is called (or at least I think it is @erlend, please enlighten me )
.Subscribe();
_cleanUp = new CompositeDisposable(dataLoader);
}
public void Dispose()
{
_cleanUp.Dispose();
}
private readonly IDisposable _cleanUp;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment