Ejemplo n.º 1
0
        public LoginViewModel()
        {
            _svc = DependencyService.Get <IMyQService>(DependencyFetchTarget.GlobalInstance);

            LoginCommand    = new Command(OnLogin);
            SettingsCommand = new Command(async() => await Nav.PushModalAsync(new SettingsView()));
        }
Ejemplo n.º 2
0
        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();
                }
            });
        }