public MainWindowViewModel() { app = Locator.Current.GetService <AthameApp>(); downloader = new MediaDownloadManager(); source = new MediaDownloadSource(); Router = new RoutingState(); MediaSearch = new MediaSearchViewModel(); ProgressStatus = new ProgressStatusViewModel(); MediaItems = new MediaItemsViewModel(source); DownloadMediaCommand = ReactiveCommand.CreateFromTask( DownloadMedia, MediaItems.CanDownload); CancelDownloadCommand = ReactiveCommand.Create(RemoveMedia); CanRestoreCommand = ReactiveCommand.Create(CanRestore); SaveSettingsCommand = ReactiveCommand.Create(SaveSettings); ViewSettingsCommand = ReactiveCommand.CreateFromObservable( Router.Navigate <SettingsViewModel>); ViewAboutAppCommand = ReactiveCommand.CreateFromObservable( Router.Navigate <AboutMeViewModel>); Activator = new ViewModelActivator(); this.WhenActivated((CompositeDisposable disposables) => { this.WhenAnyValue(x => x.MediaSearch.SearchResult) .Where(media => media != null) .Subscribe(AddMedia) .DisposeWith(disposables); Observable .FromEventPattern <MediaDownloadEventArgs>(downloader, nameof(downloader.MediaDownloadStarted)) .Subscribe(e => MediaServiceDequeued(e.EventArgs)) .DisposeWith(disposables); Observable .FromEventPattern <TrackDownloadEventArgs>(downloader, nameof(downloader.TrackDownloadStarted)) .Subscribe() .DisposeWith(disposables); Observable .FromEventPattern <TrackDownloadEventArgs>( downloader.TrackDownloader, nameof(downloader.TrackDownloader.TrackDownloadStarted), RxApp.MainThreadScheduler) .Subscribe(e => TrackDownloadStarted(e.EventArgs)); Observable .FromEventPattern <TrackDownloadEventArgs>(downloader, nameof(downloader.TrackDownloadProgressChanged)) .Subscribe(e => TrackDownloadProgressed(e.EventArgs)) .DisposeWith(disposables); Observable .FromEventPattern <TrackDownloadEventArgs>(downloader, nameof(downloader.TrackDownloadCompleted)) .Subscribe(e => TrackDownloadCompleted(e.EventArgs)) .DisposeWith(disposables); }); }
public MediaItemsViewModel(MediaDownloadSource source) { source .Connect() .Transform(ms => new MediaViewItem(ms.Media)) .ObserveOn(RxApp.MainThreadScheduler) .Bind(out items) .DisposeMany() .Subscribe(); CanDownload = this .WhenAnyValue(x => x.MediaItems.Count) .Select(count => count > 0 && items.Any(x => x.IsDownloadable())); }