public DashboardViewModelCore(DustService dustService = null, ExcelGenerator excelGenerator = null, IScreen screen = null) { _excelGenerator = excelGenerator ?? Locator.Current.GetService <ExcelGenerator>(); _dustService = dustService ?? Locator.Current.GetService <DustService>(); HostScreen = screen ?? Locator.Current.GetService <IScreen>(); var canLoadDataCommand = this.WhenAnyValue(p => p.SelectedStation) .Select(p => p != null); LoadDataCommand = ReactiveCommand.CreateFromObservable <Station, Record[]>(station => { //Info = new DashboardInfo(); _values.Clear(); IObservable <Record[]> s = _dustService.GetAvailableParametersAsync(station) .Select(@params => @params.Select(p => p.Param).ToArray()) .Select(@params => _dustService.GetStationRecordsAsync(station.Code, @params)).Switch(); return(Observable.Start(() => s, RxApp.TaskpoolScheduler).Switch().TakeUntil(CancelCommand));; }, canLoadDataCommand); CancelCommand = ReactiveCommand.Create( () => { }, this.LoadDataCommand.IsExecuting); this.WhenActivated(cleanup => { var share = _values.Connect().Publish().RefCount(); share.ToCollection().Select(records => { return(Enum.GetValues(typeof(RecordType)).Cast <RecordType>() .Select(type => records.LastOrDefault(r => r.Type == type))); }).BindTo(this, vm => vm.LastRecords).DisposeWith(cleanup); share.Bind(StationData).Subscribe().DisposeWith(cleanup); LoadDataCommand.ThrownExceptions.Subscribe(ShowError).DisposeWith(cleanup); LoadDataCommand.Subscribe(records => { _values.Edit(e => e.AddRange(records)); }).DisposeWith(cleanup); var canSaveToExcelCommand = StationData.ObserveCollectionChanges().Select(_ => StationData.Count > 0); SaveToExcelCommand = ReactiveCommand.CreateFromTask <IEnumerable <Record> >(data => _excelGenerator.CreateExcel(SelectedStation.Code, data), canSaveToExcelCommand); SaveToExcelCommand.ThrownExceptions.Subscribe(ShowError); this.WhenAnyValue(p => p.SelectedStation).Where(p => p != null).Do(_ => CancelCommand.Execute().Subscribe()).InvokeCommand(LoadDataCommand).DisposeWith(cleanup);; }); }
public StationManagerViewModel(DustService dustService = null, IScreen screen = null) { _dustService = dustService ?? Locator.Current.GetService <DustService>(); HostScreen = screen ?? Locator.Current.GetService <IScreen>(); _stationsSource.AddRange(Core.Model.Stations.All); _stationsSource.Connect() .AutoRefreshOnObservable(_ => ManagedStations.GetCollectionChangedObservable()) .Filter(s => !ManagedStations.Any(p => p.Code.Equals(s.Code, StringComparison.InvariantCultureIgnoreCase))) .Sort(SortExpressionComparer <Station> .Ascending(s => s.Code)) .Bind(AvailableStations) .Subscribe(); AddStationsCommand = ReactiveCommand.Create <IEnumerable <Station> >(stations => { foreach (var station in stations) { if (ManagedStations.Contains(station)) { continue; } ManagedStations.Add(station); } }, this.WhenAnyValue(p => p.SelectedAvailableStation).Where(p => p != null).Select(s => s.Any())); RemoveStationsCommand = ReactiveCommand.Create <IEnumerable <Station> >(stations => { foreach (var station in stations) { ManagedStations.Remove(station); } }, this.WhenAnyValue(p => p.SelectedManagedStation).Where(p => p != null).Select(s => s.Any())); SaveStationsCommand = ReactiveCommand.CreateFromTask(_dustService.SaveManagedStationsToDatabase); }