Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Threading.Tasks;
- using System.Diagnostics;
- using Prism.Commands;
- using Prism.Navigation;
- using Prism.Services;
- using Xamarin.Forms;
- using Bitumix.Helpers;
- using Bitumix.Services;
- using Bitumix.Models.Requests;
- using Bitumix.Models.Responses;
- namespace Bitumix.ViewModels
- {
- public class LoginPageViewModel : BasePageViewModel
- {
- IPageDialogService _pageDialogService;
- ICryptographyService _cryptographyService;
- //Commands
- public DelegateCommand OnGoToMainCommand { get; set; }
- public DelegateCommand OnSignInCommand { get; set; }
- public AuthenticateUserRequest Data { get; set; } = new AuthenticateUserRequest();
- public LoginPageViewModel(IApiManager apiManager, INavigationService navigationService, IPageDialogService pageDialogService, ICryptographyService cryptographyService)
- : base(navigationService, apiManager)
- {
- _pageDialogService = pageDialogService;
- _cryptographyService = cryptographyService;
- //For Test
- #if DEBUG
- Data.UserCode = "25781377";
- Data.Password = "25781377";
- #endif
- OnGoToMainCommand = new DelegateCommand(MainPageAync);
- OnSignInCommand = new DelegateCommand(async () => await RunSafe(OnSignIn(), false));
- }
- async Task OnSignIn()
- {
- if (!Data.IsValid)
- {
- var message = GetModelErrorMessage(Data, bulletPoint: "-");
- await _pageDialogService.DisplayAlertAsync(Config.AppName, message, "Ok");
- return;
- }
- Data.PasswordEncryted = _cryptographyService.GetMD5(Data.Password);
- App.UserDialogs.ShowLoading(AppResources.LoadingMessage);
- var validateUserResponse = await ApiManager.ValidateUser(Data);
- var responseContent = await validateUserResponse.Content.ReadAsStringAsync();
- Debug.WriteLine($"OnSignIn Response Content: {responseContent}, StatusCode: {validateUserResponse.StatusCode}");
- App.UserDialogs.HideLoading();
- if (validateUserResponse.IsSuccessStatusCode)
- {
- var responseObj = await Task.Run(() => XmlConvert.DeserializeObject<AuthenticateUserResponse>(responseContent));
- //Fill Settings Values
- if (responseObj.State != "000")
- {
- App.UserDialogs.ShowError(responseObj.Message);
- return;
- }
- Settings.UserId = responseObj.Id;
- Settings.UserName = responseObj.Name;
- Settings.UserPassword = Data.Password;
- OnGoToMainCommand.Execute();
- }
- }
- async void MainPageAync()
- {
- await NavigationService.NavigateAsync(new Uri($"{NavigationConstants.RootAbsoluteNavigation}/{NavigationConstants.Main}", UriKind.Absolute));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement