public void reinitializeStations(Station[] uniqueStations) { //Load stations OpenStations.Clear(); if (stationMonitoring != null) { stationMonitoring.Dispose(); } //Start by adding all stations as open stations to the collection foreach (var s in uniqueStations) { if (s.Status == StationStatus.Open) { OpenStations.Add(s); } } //Load up Stations instance with new stations var stations = Stations.Instance; stations.LoadNew(uniqueStations); //Hook up station status change monitoring events for all stations var allStations = stations.Dict; var subscriptions = allStations.Select(kvp => kvp.Value).Select(s => { var stationPropChanged = Observable.FromEventPattern <PropertyChangedEventHandler, PropertyChangedEventArgs>(h => s.PropertyChanged += h, h => s.PropertyChanged -= h); return(stationPropChanged.Where(ep => ep.EventArgs.PropertyName == "Status") .ObserveOnDispatcher().Subscribe(_ => { if (s.Status == StationStatus.Open) { OpenStations.Add(s); } else { OpenStations.Remove(s); } })); }).ToArray(); //Get the names of all currently "in use" stations and mark them as in use, removing them from the observable collection via the event listener that was just hooked up var inUseStations = OpenMatches.Select(m => m.Match.StationAssignment).Where(sn => sn != null); foreach (var sn in inUseStations) { stations.AttemptClaimStation(sn); } stationMonitoring = new CompositeDisposable(subscriptions); stations.Save(); }
private void initialize(DisplayMatch[] matches) { //load stations from settings... loadStationsFromSettings(); //Clear the observable collection that is used to display matches on screen OpenMatches.Clear(); //Dispose of previous match state monitoring if (matchStateMonitoring != null) { matchStateMonitoring.Dispose(); } //Subscribe to new matches state change events and get back the dispose objects to unsubscribe if matches change var subscriptions = matches.Select(dm => { var m = dm.Match; var matchPropChanged = Observable.FromEventPattern <PropertyChangedEventHandler, PropertyChangedEventArgs>(h => m.PropertyChanged += h, h => m.PropertyChanged -= h); //Monitor state changes to add and remove matches from being visible return(matchPropChanged.Where(ep => ep.EventArgs.PropertyName == "State") .Select(_ => System.Reactive.Unit.Default).StartWith(System.Reactive.Unit.Default) .ObserveOnDispatcher().Subscribe(_ => { //If match just opened, add it to visible matches, if match changed to a different state, hide it if (m.State == "open") { OpenMatches.Add(dm); } else { OpenMatches.Remove(dm); } })); }).ToArray(); //Return object that can be used to unsubscribe from all match state change notifications matchStateMonitoring = new CompositeDisposable(subscriptions); }