Skip to content

Instantly share code, notes, and snippets.

@RolandPheasant
Last active July 14, 2016 16:07
Show Gist options
  • Save RolandPheasant/df4dc6e5ac6283b08aea056d455ad232 to your computer and use it in GitHub Desktop.
Save RolandPheasant/df4dc6e5ac6283b08aea056d455ad232 to your computer and use it in GitHub Desktop.
public static class MyDynamicDataExtensions
{
public static IObservable<IChangeSet<WidgetListItemViewModel, int>> TransformWithupdate(this IObservable<IChangeSet<Widget, int>> source )
{
return Observable.Create<IChangeSet<WidgetListItemViewModel, int>>(observer =>
{
//create local data source wich tansforms all items
var localDataSource = source
.Transform(w => new WidgetListItemViewModel(w))
.AsObservableCache();
//transform the result and manually update original items
var withoutUpdates = localDataSource
.Connect()
.Select(changes =>
{
var altered = new List<Change<WidgetListItemViewModel, int>>(changes.Count);
foreach (var change in changes)
switch (change.Reason)
{
case ChangeReason.Update:
//directly update original view model
var updated = change.Current;
var original = change.Previous.Value;
original.Distance = updated.Distance;
//evaluate is a command to operators to self-requery when there are in-line changes [should automatically re-sort]
altered.Add(new Change<WidgetListItemViewModel, int>(ChangeReason.Evaluate, change.Key, original));
break;
default:
altered.Add(change);
break;
}
return new ChangeSet<WidgetListItemViewModel, int>(altered);
})
.Where(changes => changes.Count != 0)
.SubscribeSafe(observer);
return new CompositeDisposable(localDataSource, withoutUpdates);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment