/// <summary> /// Initializes a new instance of the <see cref="MachineOffCheckViewModel"/> class. /// </summary> /// <param name="powerService">The power service to use.</param> /// <param name="policyProvider">The policy provider to use.</param> public MachineOffCheckViewModel(IPowerService powerService, ICommandPolicyProvider policyProvider) { if (powerService is null) { throw new System.ArgumentNullException(nameof(powerService)); } if (policyProvider is null) { throw new System.ArgumentNullException(nameof(policyProvider)); } this.WhenActivated(disposable => { this.poweredOn = powerService.PoweredOn.ToProperty(this, x => x.PoweredOn).DisposeWith(disposable); var policy = policyProvider.GetPolicy(); this.TurnOn = ReactiveCommand.CreateFromTask( async(ct) => await policy.ExecuteAsync( async(token) => await powerService.PowerOn(token), ct), powerService.PoweredOn.Select(x => !x)).DisposeWith(disposable); this.canExecuteTurnOn = this.TurnOn.CanExecute.ToProperty(this, x => x.CanExecuteTurnOn).DisposeWith(disposable); }); }
/// <summary> /// Initializes a new instance of the <see cref="HeaderViewModel"/> class. /// </summary> /// <param name="powerService">The power service implementation.</param> /// <param name="titleService">The title service.</param> /// <param name="policyProvider">The policy provider to execute ordinary commands.</param> /// <param name="navigationManager">The navigation manager to navigate to a different page.</param> public HeaderViewModel( IPowerService powerService, ITitleService titleService, ICommandPolicyProvider policyProvider, NavigationManager navigationManager) { if (powerService is null) { throw new ArgumentNullException(nameof(powerService)); } if (titleService is null) { throw new ArgumentNullException(nameof(titleService)); } if (policyProvider is null) { throw new ArgumentNullException(nameof(policyProvider)); } if (navigationManager is null) { throw new ArgumentNullException(nameof(navigationManager)); } this.WhenActivated(disposable => { this.poweredOn = powerService.PoweredOn.ToProperty(this, x => x.PoweredOn).DisposeWith(disposable); this.title = titleService.Title.ToProperty(this, x => x.Title).DisposeWith(disposable); var policy = policyProvider.GetPolicy(); this.TogglePower = ReactiveCommand.CreateFromTask(async(ct) => { switch (this.PoweredOn) { case true: await policy.ExecuteAsync( async(token) => await powerService.PowerOff(token), ct); break; case false: await policy.ExecuteAsync( async(token) => await powerService.PowerOn(token), ct); break; } }).DisposeWith(disposable); this.Settings = ReactiveCommand.Create(() => { navigationManager.NavigateTo("/Settings"); }).DisposeWith(disposable); this.canExecuteTogglePower = this.TogglePower.CanExecute.ToProperty(this, x => x.CanExecuteTogglePower).DisposeWith(disposable); }); }
/// <summary> /// Initializes a new instance of the <see cref="BoilerViewModel"/> class. /// </summary> /// <param name="boilerService">The boiler Service to use.</param> /// <param name="policyProvider">The policy provider.</param> public BoilerViewModel(IBoilerService boilerService, ICommandPolicyProvider policyProvider) { if (boilerService is null) { throw new ArgumentNullException(nameof(boilerService)); } if (policyProvider is null) { throw new ArgumentNullException(nameof(policyProvider)); } this.WhenActivated(disposable => { var policy = policyProvider.GetPolicy(); this.pressure = boilerService.Pressure.ToProperty(this, x => x.Pressure).DisposeWith(disposable); this.temperature = boilerService.Temperature.ToProperty(this, x => x.Temperature).DisposeWith(disposable); this.temperatureTrend = boilerService.Temperature .WithPrevious((p, c) => c <p?Trend.Falling : c> p ? Trend.Rising : Trend.Stable) .ToProperty(this, x => x.TemperatureTrend) .DisposeWith(disposable); this.protection = boilerService.Protection.ToProperty(this, x => x.Protection).DisposeWith(disposable); this.heating = boilerService.Heating.ToProperty(this, x => x.Heating).DisposeWith(disposable); this.targetPressure = boilerService.TargetPressure .Merge(this.selectedTargetPressure) .ToProperty(this, x => x.TargetPressure) .DisposeWith(disposable); var setTargetPressureCommand = ReactiveCommand.CreateFromTask <decimal, Unit>( async(value, ct) => { await policy.ExecuteAsync( async token => await boilerService.SetTargetPressure(value, token), ct); return(Unit.Default); }).DisposeWith(disposable); this.selectedTargetPressure .Throttle(TimeSpan.FromMilliseconds(1000)) .InvokeCommand(setTargetPressureCommand) .DisposeWith(disposable); this.ResetProtection = ReactiveCommand.CreateFromTask( async ct => { await policy.ExecuteAsync(async token => await boilerService.ResetProtection(token), ct); }, boilerService.Protection).DisposeWith(disposable); }); }