Skip to content

Instantly share code, notes, and snippets.

@RolandPheasant
Created March 27, 2019 21:11
Show Gist options
  • Save RolandPheasant/4f33823a8616b79a80e2ac3644c1d5a8 to your computer and use it in GitHub Desktop.
Save RolandPheasant/4f33823a8616b79a80e2ac3644c1d5a8 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reactive.Linq;
using System.Text;
using System.Threading;
using DynamicData.Binding;
using Xunit;
namespace DynamicData.Snippets
{
public class Deployment
{
public string Id { get; set; }
public string Name { get; set; }
public DateTime? DeploymentDate { get; set; }
}
public class DeploymentCellViewModel
{
public Deployment Deployment { get; }
public DeploymentCellViewModel(Deployment deployment)
{
Deployment = deployment;
}
}
public class MockDeploymentService : IDeploymentsService
{
public IObservable<IEnumerable<Deployment>> AllDeployments
{
get
{
DateTime date = DateTime.Now;
var deployments = new Deployment[]
{
new Deployment()
{
Id = "1",
Name = "One",
},
new Deployment()
{
Id = "2",
Name = "Two",
DeploymentDate = date
},
new Deployment()
{
Id = "3",
Name = "Three",
}
};
var deployments2 = new Deployment[]
{
new Deployment()
{
Id = "1",
Name = "One",
},
new Deployment()
{
Id = "2",
Name = "Two",
DeploymentDate = date
},
new Deployment()
{
Id = "3",
Name = "Three",
DeploymentDate = date
}
};
return Observable.Timer(DateTimeOffset.Now.AddSeconds(10))
.SelectMany(_ => Observable.Return(deployments2))
.StartWith(deployments);
}
}
}
public interface IDeploymentsService
{
IObservable<IEnumerable<Deployment>> AllDeployments { get; }
}
public class TableSection<T> : ObservableCollectionExtended<T>
{
public TableSection(string title, IEnumerable<T> items) : base(items)
{
Title = title;
}
public string Title { get; private set; }
}
public class TestRunner
{
[Fact]
public void RunIt()
{
var viewModel = new DeploymentsPageViewModel(new MockDeploymentService());
// Thread.Sleep(10_000);
}
}
public class DeploymentsPageViewModel : AbstractNotifyPropertyChanged
{
private readonly ISourceCache<DeploymentCellViewModel, string> _deploymentSourceCache;
public ReadOnlyObservableCollection<TableSection<DeploymentCellViewModel>> DeploymentViewModels { get; }
public DeploymentsPageViewModel(IDeploymentsService deploymentsService)
{
_deploymentSourceCache = new SourceCache<DeploymentCellViewModel, string>((x) => x.Deployment.Id);
deploymentsService.AllDeployments
.Do((IEnumerable<Deployment> deployments) =>
{
var oldItemIds = _deploymentSourceCache.Items.Select((x) => x.Deployment.Id);
var newItemIds = deployments.Select((x) => x.Id);
var deletedKeys = oldItemIds.Where((x) => !newItemIds.Contains(x));
_deploymentSourceCache.Edit((editableCache) =>
{
editableCache.RemoveKeys(deletedKeys);
editableCache.AddOrUpdate(TransformToViewModel(deployments));
});
})
.Subscribe();
_deploymentSourceCache
.Connect()
// Hacky temporary way of grouping deployments
.GroupWithImmutableState((arg) => arg.Deployment.DeploymentDate == null ? "Not Deployed" : "Deployed")
.Transform((grouping) =>
{
return new TableSection<DeploymentCellViewModel>(grouping.Key, grouping.Items);
})
.Bind(out var deploymentViewModels)
.Subscribe();
DeploymentViewModels = deploymentViewModels;
}
private IEnumerable<DeploymentCellViewModel> TransformToViewModel(IEnumerable<Deployment> deployments)
{
return deployments.Select((x) => new DeploymentCellViewModel(x));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment