public TaxonManagementVM( IConnectivityService Connectivity, ITaxonService Taxa, IDiversityServiceClient Service, INotificationService Notification ) { this.Connectivity = Connectivity; this.Service = Service; this.Taxa = Taxa; this.Notification = Notification; _IsOnlineAvailable = this.ObservableToProperty(Connectivity.WifiAvailable(), x => x.IsOnlineAvailable); var localLists = this.FirstActivation() .SelectMany(_ => Taxa.getTaxonSelections() .ToObservable(ThreadPoolScheduler.Instance) .Select(list => new TaxonListVM(list))) .Publish(); LocalLists = localLists .ObserveOnDispatcher() .CreateCollection(); var onlineLists = localLists .IgnoreElements() //only download lists once the local ones are loaded .Concat(Observable.Return(null as TaxonListVM)) .CombineLatest(this.OnActivation(), (_, _2) => _2) .CheckConnectivity(Connectivity, Notification) .SelectMany(_ => { return Service.GetTaxonLists() .DisplayProgress(Notification, DiversityResources.TaxonManagement_State_DownloadingLists) .TakeUntil(this.OnDeactivation()); }) .ObserveOnDispatcher() .SelectMany(lists => lists.Where(list => !LocalLists.Any(loc => loc.Model == list)) // Filter lists already present locally .Select(list => new TaxonListVM(list)) ) .Publish(); PersonalLists = onlineLists.Where(vm => !vm.Model.IsPublicList) .CreateCollection(); PublicLists = onlineLists.Where(vm => vm.Model.IsPublicList) .CreateCollection(); onlineLists.Connect(); localLists.Connect(); Select = new ReactiveCommand<TaxonListVM>(vm => !vm.IsSelected && !vm.IsDownloading); Select.Subscribe(taxonlist => { foreach (var list in LocalLists) { if (list.Model.TaxonomicGroup == taxonlist.Model.TaxonomicGroup) list.Model.IsSelected = false; } Taxa.selectTaxonList(taxonlist.Model); }); Download = new ReactiveCommand<TaxonListVM>(vm => !vm.IsDownloading); Download .CheckConnectivity(Connectivity, Notification) .Subscribe(taxonlist => { if (Taxa.getTaxonTableFreeCount() > 0) { CurrentPivot = Pivot.Local; taxonlist.IsDownloading = true; makeListLocal(taxonlist); DownloadTaxonList(taxonlist) .DisplayProgress(Notification, DiversityResources.TaxonManagement_State_DownloadingList) .ObserveOnDispatcher() .ShowServiceErrorNotifications(Notification) .Subscribe(_ => { //Download Succeeded taxonlist.IsDownloading = false; if (Select.CanExecute(taxonlist)) Select.Execute(taxonlist); }, _ => //Download Failed { taxonlist.IsDownloading = false; removeLocalList(taxonlist); }, () => { }); } }); Delete = new ReactiveCommand<TaxonListVM>(vm => !vm.IsDownloading); Delete .Subscribe(taxonlist => { removeLocalList(taxonlist); }); Refresh = new ReactiveCommand<TaxonListVM>(vm => !vm.IsDownloading); Refresh .Subscribe(taxonlist => { if (Delete.CanExecute(taxonlist)) //Deletes synchronously Delete.Execute(taxonlist); if (Download.CanExecute(taxonlist)) Download.Execute(taxonlist); }); //Download all only on Personal pivot var canDownloadAll = this.WhenAny(x => x.CurrentPivot, x => x.GetValue()) .Select(p => p == Pivot.Personal) .CombineLatest(Connectivity.WifiAvailable(), (p, wi) => p && wi); DownloadAll = new ReactiveCommand(canDownloadAll, initialCondition: false); DownloadAll .SelectMany(_ => PersonalLists.ToArray()) .Where(vm => Download.CanExecute(vm)) .Subscribe(Download.Execute); }