async Task PollForDoorStatus(DoorState destinationState) { int maxChecks = 30; while (doorState != destinationState && maxChecks != 0) { DoorState = await myQ.GetDoorState(Door.MyQDeviceId); await Task.Delay(2000); maxChecks--; } }
public MainViewModel() { this.activator = new ViewModelActivator(); myQ = DependencyService.Get <IMyQService>(DependencyFetchTarget.GlobalInstance); canToggleDoor = this.WhenAnyValue(x => x.Door, y => y.DoorState, (x, y) => x != null && (y == DoorState.Open || y == DoorState.Closed)) .ToProperty(this, x => x.CanToggleDoor); GetDevicesCommand = ReactiveCommand.CreateFromTask <object, Unit>( async _ => { if (myQ.IsAuthenticated) { var devices = await myQ.GetDevices(); if (devices == null) { myQ.SetSecurityToken(string.Empty); ApplicationStore.SecurityToken = string.Empty; await Nav.PushModalAsync(new LoginView()); } else { Door = devices.First(); DoorState = await myQ.GetDoorState(devices.First().MyQDeviceId); } } else { await Nav.PushModalAsync(new LoginView()); } return(Unit.Default); }); var canExecuteToggle = this.WhenAnyValue(x => x.CanToggleDoor, (arg) => arg == true); ToggleDoorCommand = ReactiveCommand.CreateFromTask <object, Unit>( async _ => { await ToggleDoor(); return(Unit.Default); }, canExecuteToggle); SettingsCommand = ReactiveCommand.CreateFromTask <object, Unit>( async _ => { await Nav.PushModalAsync(new SettingsView()); return(Unit.Default); } ); VoiceCommand = ReactiveCommand.CreateFromTask <string, Unit>( async(phrase) => { await ProcessSpeechCommand(phrase); return(Unit.Default); } ); this.WhenActivated( d => { // this isn't useful but it gets rid of the ambiguous compile error IsActiveCount++; d(Disposable.Create(() => IsActiveCount--)); if (string.IsNullOrEmpty(ApplicationStore.SecurityToken) && !myQ.IsAuthenticated) { Nav.PushModalAsync(new LoginView()); } else { myQ.SetSecurityToken(ApplicationStore.SecurityToken); OnGetDevices(); } }); }