コード例 #1
0
        public ExerciseViewModel(ISchedulerService schedulerService, Exercise model, IObservable<ExecutionContext> executionContext)
        {
            schedulerService.AssertNotNull(nameof(schedulerService));
            model.AssertNotNull(nameof(model));
            executionContext.AssertNotNull(nameof(executionContext));

            this.disposables = new CompositeDisposable();
            this.model = model;

            executionContext
                .ObserveOn(schedulerService.MainScheduler)
                .Subscribe(x => this.ExecutionContext = x)
                .AddTo(this.disposables);

            Observable
                .CombineLatest(
                    this
                        .WhenAnyValue(x => x.ExecutionContext)
                        .Select(ec => ec == null ? Observable.Never<TimeSpan>() : ec.WhenAnyValue(x => x.SkipAhead))
                        .Switch(),
                    this
                        .WhenAnyValue(x => x.ExecutionContext)
                        .Select(ec => ec == null ? Observable.Never<Exercise>() : ec.WhenAnyValue(x => x.CurrentExercise))
                        .Switch(),
                    (skip, current) => skip == TimeSpan.Zero && current == this.model)
                .ObserveOn(schedulerService.MainScheduler)
                .Subscribe(x => this.IsActive = x)
                .AddTo(this.disposables);

            this
                .WhenAnyValue(x => x.ExecutionContext)
                .Select(
                    ec =>
                        ec == null
                            ? Observable.Return(TimeSpan.Zero)
                            : ec
                                .WhenAnyValue(x => x.CurrentExerciseProgress)
                                .Where(_ => ec.CurrentExercise == this.model)
                                .StartWith(TimeSpan.Zero))
                .Switch()
                .ObserveOn(schedulerService.MainScheduler)
                .Subscribe(x => this.ProgressTimeSpan = x)
                .AddTo(this.disposables);

            this
                .WhenAny(
                    x => x.Duration,
                    x => x.ProgressTimeSpan,
                    (duration, progressTimeSpan) => progressTimeSpan.Value.TotalMilliseconds / duration.Value.TotalMilliseconds)
                .Select(progressRatio => double.IsNaN(progressRatio) || double.IsInfinity(progressRatio) ? 0d : progressRatio)
                .Select(progressRatio => Math.Min(1d, progressRatio))
                .Select(progressRatio => Math.Max(0d, progressRatio))
                .ObserveOn(schedulerService.MainScheduler)
                .Subscribe(x => this.Progress = x)
                .AddTo(this.disposables);
        }
コード例 #2
0
 public ExerciseViewModelBuilder WithModel(Exercise model)
 {
     this.model = model;
     return this;
 }
コード例 #3
0
 public ExerciseViewModelBuilder()
 {
     this.schedulerService = new SchedulerServiceMock(MockBehavior.Loose);
     this.model = new ExerciseBuilder();
     this.executionContext = Observable.Never<ExecutionContext>();
 }
コード例 #4
0
 internal void SetCurrentExercise(Exercise exercise) =>
     this.CurrentExercise = exercise;
コード例 #5
0
 public ExerciseViewModelBuilder WithModel(Exercise model) =>
     this.With(ref this.model, model);
コード例 #6
0
 public ExerciseProgramBuilder AddExercise(Exercise exercise)
 {
     this.exercises.Add(exercise);
     return this;
 }
コード例 #7
0
 internal void SetCurrentExercise(Exercise exercise) =>
 this.CurrentExercise = exercise;
コード例 #8
0
 public ExerciseProgramBuilder WithExercise(Exercise exercise) =>
     this.With(ref this.exercises, exercise);