예제 #1
0
        private GlobalStatusStore()
        {
            Test = ReactiveCommand.CreateFromTask(TestImpl);

            var canOperate = this.WhenAnyValue(x => x.ConnectStatus)
                             .Select(p =>
            {
                if (p == ConnectStatus.Checking &&
                    p == ConnectStatus.Connecting &&
                    p == ConnectStatus.Disconnecting)
                {
                    return(false);
                }
                else
                {
                    return(true);
                }
            });


            _canConnect = canOperate.ToProperty(this, x => x.CanOperate, false);

            Connect = ReactiveCommand.CreateFromTask(async() =>
            {
                ConnectStatus = ConnectStatus.Connecting;

                if (await Locator.Current.GetService <IUserStorageService>().CheckUserPinExist(CurrentUser.Username))
                {
                    throw new ConnectionException(ConnectionError.LostPin);
                }
                else
                {
                    await DoConnect.Execute("");
                }
            }, canOperate);


            Connect.ThrownExceptions.Subscribe(ex => { });


            DoConnect = ReactiveCommand.CreateFromTask <string, bool>(async(pin) =>
            {
                string password;
                try
                {
                    password = await Locator.Current.GetService <IUserStorageService>().DecryptedUserPassword(CurrentUser.Username, pin);
                }
                catch
                {
                    throw new ConnectionException(ConnectionError.InvalidPin);
                }

                return(await Locator.Current.GetService <IInternetGatewayService>().Connect(CurrentUser.Username, password));
            }, canOperate);

            DoConnect.ThrownExceptions.Subscribe(ex => { });

            DoConnect.Where(u => u).Subscribe(p =>
            {
                ConnectStatus = ConnectStatus.Connected;
            });


            Disconnect = ReactiveCommand.CreateFromTask(async() =>
            {
                ConnectStatus = ConnectStatus.Disconnecting;
                return(await Locator.Current.GetService <IInternetGatewayService>().Disconnect());
            }, canOperate);

            Disconnect.Where(u => u).Subscribe(_ =>
            {
                ConnectStatus = ConnectStatus.Disconnected;
            });

            Disconnect.ThrownExceptions.Subscribe(ex => { });

            Toggle = ReactiveCommand.CreateFromTask(async() =>
            {
                if (ConnectStatus == ConnectStatus.Disconnected)
                {
                    await Connect.Execute();
                }
                else if (ConnectStatus == ConnectStatus.Connected)
                {
                    await Disconnect.Execute();
                }
            }, canOperate);

            Toggle.ThrownExceptions.Subscribe(_ => { });
        }