示例#1
0
        private async void Login()
        {
            if (string.IsNullOrEmpty(Email))
            {
                await dialogService.ShowMessage("Error", "Debe de ingresar un email valido");

                return;
            }

            if (string.IsNullOrEmpty(Password))
            {
                await dialogService.ShowMessage("Error", "Debe de ingresar un password");

                return;
            }

            IsRunning = true;
            IsEnabled = false;

            var connection = await apiService.CheckConnection();

            if (!connection.IsSuccess)
            {
                IsRunning = false;
                IsEnabled = true;
                await dialogService.ShowMessage("Error", connection.Message);

                return;
            }

            var response = await apiService.GetToken("http://condoscrwebapi.azurewebsites.net", Email, Password);

            if (response == null)
            {
                IsRunning = false;
                IsEnabled = true;
                await dialogService.ShowMessage("Error", "Ocurrió un error inesperado. Invente más tarde");

                Email    = string.Empty;
                Password = string.Empty;
                return;
            }

            if (string.IsNullOrEmpty(response.AccessToken))
            {
                IsRunning = false;
                IsEnabled = true;
                await dialogService.ShowMessage("Error", response.ErrorDescription);

                Email    = string.Empty;
                Password = string.Empty;
                return;
            }

            var mainViewModel = MainViewModel.GetInstance();

            mainViewModel.Token = response;

            var infoUsuario = await apiService.Get <Usuario>("http://condoscrwebapi.azurewebsites.net/",
                                                             "api",
                                                             "/infoUsuario",
                                                             response.TokenType,
                                                             response.AccessToken,
                                                             "NombreUsuario",
                                                             response.UserName);

            if (!infoUsuario.IsSuccess)
            {
                IsRunning = false;
                IsEnabled = true;
                await dialogService.ShowMessage("Error", infoUsuario.Message);

                Email    = string.Empty;
                Password = string.Empty;
                return;
            }
            mainViewModel.InfoUsuario = infoUsuario.Result as Usuario;
            mainViewModel.Principal   = new PrincipalViewModel();

            await navigationService.Navigate("PrincipalView");

            Email    = null;
            Password = null;

            IsRunning = false;
            IsEnabled = true;
        }