Example #1
0
        protected SoundPlayerViewModelBase(string displayName, string groupName)
        {
            DisplayName = displayName;
            GroupName   = groupName;

            IsWave = groupName == SoundMachineViewModel.GenWaveGroup;

            var isPlayingChanged = this.WhenAnyValue(x => x.IsPlaying);

            var canPlay = isPlayingChanged.Select(isPlaying => !isPlaying);
            var canStop = isPlayingChanged.Select(IsPlaying => IsPlaying);

            canPlay.ToProperty(this, nameof(IsLoopedEnabled), out _isLoopedEnabled)
            .DisposeWith(Disposables);

            PlayCommand = ReactiveCommand.Create(ExecutePlay, canPlay);
            StopCommand = ReactiveCommand.Create(ExecuteStop, canStop);

            PlayCommand.ObserveOn(RxApp.MainThreadScheduler)
            .Do(_ => IsPlaying = true)
            .Subscribe();

            StopCommand.ObserveOn(RxApp.MainThreadScheduler)
            .Do(_ => IsPlaying = false)
            .Subscribe();

            this.WhenAnyValue(x => x.Duration, x => x.IsPlaying, x => x.IsLooped,
                              (duration, isPlaying, isLooped) => (duration, isPlaying, isLooped))
            .Where(x => x.duration > 0 && x.isPlaying && !x.isLooped)
            .Select(x => Observable.Timer(TimeSpan.FromSeconds(x.duration)))
            .Switch()
            .ObserveOn(RxApp.MainThreadScheduler)
            .Subscribe(_ => IsPlaying = false)
            .DisposeWith(Disposables);
        }