-
Notifications
You must be signed in to change notification settings - Fork 105
/
LiveTradesViewer.cs
64 lines (52 loc) · 2.25 KB
/
LiveTradesViewer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using System;
using System.Collections.ObjectModel;
using System.Reactive;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using DynamicData;
using DynamicData.Binding;
using Trader.Domain.Infrastucture;
using Trader.Domain.Model;
using Trader.Domain.Services;
namespace Trader.Client.Views
{
public class LiveTradesViewer :AbstractNotifyPropertyChanged, IDisposable
{
private readonly IDisposable _cleanUp;
private readonly ReadOnlyObservableCollection<TradeProxy> _data;
private bool _paused;
public LiveTradesViewer(ITradeService tradeService,SearchHints searchHints,ISchedulerProvider schedulerProvider)
{
SearchHints = searchHints;
var filter = SearchHints.WhenValueChanged(t => t.SearchText)
.Select(BuildFilter);
var loader = tradeService.Live.Connect()
.BatchIf(this.WhenValueChanged(x=>x.Paused),null,null) //I need to fix the API, so nulls do not have to be passed in
.Filter(filter) // apply user filter
.Transform(trade => new TradeProxy(trade))
.Sort(SortExpressionComparer<TradeProxy>.Descending(t => t.Timestamp),SortOptimisations.ComparesImmutableValuesOnly, 25)
.ObserveOn(schedulerProvider.MainThread)
.Bind(out _data) // update observable collection bindings
.DisposeMany() //since TradeProxy is disposable dispose when no longer required
.Subscribe();
_cleanUp = new CompositeDisposable(loader, searchHints);
}
private Func<Trade, bool> BuildFilter(string searchText)
{
if (string.IsNullOrEmpty(searchText)) return trade => true;
return t => t.CurrencyPair.Contains(searchText, StringComparison.OrdinalIgnoreCase)
|| t.Customer.Contains(searchText, StringComparison.OrdinalIgnoreCase);
}
public ReadOnlyObservableCollection<TradeProxy> Data => _data;
public SearchHints SearchHints { get; }
public bool Paused
{
get => _paused;
set => SetAndRaise(ref _paused, value);
}
public void Dispose()
{
_cleanUp.Dispose();
}
}
}