/// <summary> /// Initializes a new instance of the <see cref="ImageExplorerViewModel" /> class. /// </summary> /// <param name="imageCache">Cache which notifies about loaded or removed images.</param> public ImageExplorerViewModel(IObservableCache <ImageHandle, string> imageCache) { Guard.Against.ArgumentNull(imageCache, nameof(imageCache)); var connection = imageCache.Connect(); SelectNext = ReactiveCommand.Create(SelectNextImpl); SelectPrevious = ReactiveCommand.Create(SelectPreviousImpl); _requestRemove = ReactiveCommand.Create <IImageExplorerItemViewModel, ImageHandle>( vm => imageCache.Items.FirstOrDefault(x => x.LoadingSettings.FilePath == vm.ExplorerItem.FilePath), outputScheduler: RxApp.TaskpoolScheduler); DeletionRequests = _requestRemove.AsObservable(); Selections = this.WhenAnyValue(x => x.SelectedItem) .DistinctUntilChanged() .Select(vm => imageCache.Items.FirstOrDefault(x => x.LoadingSettings.FilePath == vm?.ExplorerItem.FilePath)); this.WhenActivated(d => { connection .ObserveOn(RxApp.TaskpoolScheduler) .Filter(x => x?.Preview != null) .Transform(x => new ImageExplorerItem(x.LoadingSettings.FilePath, x.OriginalImage.ToThumbnail(50))) .DisposeMany() .Transform(x => new ImageExplorerItemViewModel(x) as IImageExplorerItemViewModel) .ObserveOn(RxApp.MainThreadScheduler) .Bind(out _children) .Subscribe(_ => SelectedItem ??= Children.FirstOrDefault()) .DisposeWith(d); connection .Where(x => x != null) .Throttle(TimeSpan.FromMilliseconds(200)) .Select(_ => Children.Select(x => x.RequestRemove).Merge()) .Switch() .InvokeCommand(_requestRemove) .DisposeWith(d); }); }