My Volatilities
property is set when the user interacts with a specific set of vols. Everything hinges off of it. It's currently an ItemObservableCollection ItemObservableCollection, but I may change it later to be a reactive collection. I don't believe that's relevant here, but I could be wrong.
My Volatilities
property is set when the user interacts with a specific set of vols. Everything hinges off of it. It's currently an ItemObservableCollection, but I may change it later to be a reactive collection. I don't believe that's relevant here, but I could be wrong.
My Volatilities
property is set when the user interacts with a specific set of vols. Everything hinges off of it. It's currently an ItemObservableCollection, but I may change it later to be a reactive collection. I don't believe that's relevant here, but I could be wrong.
Can this reactive code be improved?
I have a view model that I'm migrating to RxUI. It exposes a collection of brokers along with a bool
indicating whether there is a market (ie. at least one broker). In addition, it has a property for the currently selected broker (the user can choose a broker via a drop-down):
private IReactiveCollection<SourceViewModel> brokers;
public IReactiveCollection<SourceViewModel> Brokers
{
get { return this.brokers; }
private set { this.RaiseAndSetIfChanged(x => x.Brokers, ref this.brokers, value); }
}
private ObservableAsPropertyHelper<bool> isMarket;
public bool IsMarket
{
get { return this.isMarket.Value; }
}
private SourceViewModel selectedBroker;
public SourceViewModel SelectedBroker
{
get { return this.selectedBroker; }
set { this.RaiseAndSetIfChanged(x => x.SelectedBroker, ref this.selectedBroker, value); }
}
I have these pieces hooked up and working, but I am not convinced I've done this in the most reactive-friendly fashion:
this.WhenAny(x => x.Volatilities, x => x.Value)
.StartWith((ItemObservableCollection<VolatilityViewModel>)null)
.Subscribe(x =>
{
if (x == null)
{
// no volatilities, so definitely no market
this.Brokers = null;
this.SelectedBroker = null;
this.isMarket = Observable.Return(false).ToProperty(this, y => y.IsMarket);
}
else
{
// we have volatilities, so potentially we have a market
// TODO: is there a better means of doing this?
this.Brokers = x.CreateDerivedCollection(y => y.Source);
// TODO: is there a cleaner way to do this?
this.Brokers.Changed
.Select(y => true)
.StartWith(true)
.Subscribe(y =>
{
var volatility = x.FirstOrDefault();
this.SelectedBroker = volatility == null ? null : volatility.Source;
});
// TODO: is there a cleaner way to do this?
this.isMarket = this.Brokers.CollectionCountChanged.Select(y => y > 0).StartWith(x.Count > 0).ToProperty(this, y => y.IsMarket);
}
});
My Volatilities
property is set when the user interacts with a specific set of vols. Everything hinges off of it. It's currently an ItemObservableCollection, but I may change it later to be a reactive collection. I don't believe that's relevant here, but I could be wrong.
The parts I'm concerned about have TODOs in the code, but to summarize:
- when my
Volatilities
property changes, so too do the available brokers. I currently create a new derived collection each timeVolatilities
changes. I couldn't come up with any way to avoid this or implement it more cleanly. - when I recreate my brokers collection, I immediately set up a
Changed
observer so that any changes result in the first broker being selected. The reasoning is that the first broker will (once I do sorting) be the preferred broker, so any changes in the market should instigate the selection of the preferred broker. I couldn't think of a way to make this logic cleaner and simpler. - in addition, I also recreate my
isMarket
member, which istrue
whenever there is at least one broker in the brokers collection. Again, I'm not sure that this is the best approach to achieving this.
Can anyone weigh in with their thoughts on this?