public SelectedAlbumViewModel(IEventAggregator eventAggregator, IMusicInfoProvider musicInfoProvider) { _musicInfoProvider = musicInfoProvider; _allTracks = this .WhenAnyValue(x => x.SelectedItem) .Throttle(TimeSpan.FromMilliseconds(50), RxApp.TaskpoolScheduler) .Select(x => x?.CollectionId ?? 0) .SelectMany(GetAllMusicTracks) .ObserveOn(RxApp.MainThreadScheduler) .ToProperty(this, x => x.AllTracks); _isAvailable = this .WhenAnyValue(x => x.AllTracks) .Select(x => x != null && x.Any()) .ToProperty(this, x => x.IsAvailable); eventAggregator .GetEvent <AlbumSelectedEvent>() .Subscribe(album => SelectedItem = album, ThreadOption.UIThread); this.ObservableForProperty(x => x.SelectedTrack) .Throttle(TimeSpan.FromMilliseconds(50), RxApp.TaskpoolScheduler) .Select(x => x.Value) .Where(x => x != null) .ObserveOn(RxApp.TaskpoolScheduler) .Select(x => x.PreviewUrl) .Subscribe(x => eventAggregator .GetEvent <TrackSelectedEvent>() .Publish(x)); }
public MainWindowViewModel(IRegionManager regionManager, IMusicInfoProvider musicInfoProvider, IImageProvider imageProvider, IEventAggregator eventAggregator, IAudioPlayer audioPlayer) { _musicInfoProvider = musicInfoProvider; _imageProvider = imageProvider; _audioPlayer = audioPlayer; regionManager.RegisterViewWithRegion(UIConstants.SelectedAlbumContentRegionName, typeof(SelectedAlbumView)); _searchResults = this .WhenAnyValue(x => x.SearchString) .Throttle(TimeSpan.FromMilliseconds(800), RxApp.TaskpoolScheduler) .Select(x => x?.Trim()) .DistinctUntilChanged() .Where(x => !string.IsNullOrWhiteSpace(x)) .SelectMany(SearchAllAlbums) .ObserveOn(RxApp.MainThreadScheduler) .Do(x => SelectedItem = x.FirstOrDefault()) .ToProperty(this, x => x.SearchResults); _isAvailable = this .WhenAnyValue(x => x.SearchResults) .Select(x => x != null) .ToProperty(this, x => x.IsAvailable); this.ObservableForProperty(x => x.SelectedItem) .Throttle(TimeSpan.FromMilliseconds(50), RxApp.TaskpoolScheduler) .Select(x => x.Value) .Subscribe(x => OnSelectedItemChanged(eventAggregator, x)); this.ObservableForProperty(x => x._audioPlayer.CurrentState) .Throttle(TimeSpan.FromMilliseconds(1000), RxApp.TaskpoolScheduler) .Select(x => x.Value) .Subscribe(OnAudioPlayerCurrentStateChanged); eventAggregator .GetEvent <TrackSelectedEvent>() .Subscribe(audioPlayer.SetTrack, ThreadOption.BackgroundThread); InitializeCommands(); }