Advertisement
Guest User

Untitled

a guest
Nov 17th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. using System;
  2. using System.Threading.Tasks;
  3. using System.Diagnostics;
  4.  
  5. using Prism.Commands;
  6. using Prism.Navigation;
  7. using Prism.Services;
  8. using Xamarin.Forms;
  9.  
  10. using Bitumix.Helpers;
  11. using Bitumix.Services;
  12. using Bitumix.Models.Requests;
  13. using Bitumix.Models.Responses;
  14.  
  15. namespace Bitumix.ViewModels
  16. {
  17. public class LoginPageViewModel : BasePageViewModel
  18. {
  19. IPageDialogService _pageDialogService;
  20. ICryptographyService _cryptographyService;
  21.  
  22. //Commands
  23. public DelegateCommand OnGoToMainCommand { get; set; }
  24. public DelegateCommand OnSignInCommand { get; set; }
  25.  
  26. public AuthenticateUserRequest Data { get; set; } = new AuthenticateUserRequest();
  27.  
  28. public LoginPageViewModel(IApiManager apiManager, INavigationService navigationService, IPageDialogService pageDialogService, ICryptographyService cryptographyService)
  29. : base(navigationService, apiManager)
  30. {
  31. _pageDialogService = pageDialogService;
  32. _cryptographyService = cryptographyService;
  33. //For Test
  34. #if DEBUG
  35. Data.UserCode = "25781377";
  36. Data.Password = "25781377";
  37. #endif
  38.  
  39. OnGoToMainCommand = new DelegateCommand(MainPageAync);
  40. OnSignInCommand = new DelegateCommand(async () => await RunSafe(OnSignIn(), false));
  41. }
  42.  
  43. async Task OnSignIn()
  44. {
  45. if (!Data.IsValid)
  46. {
  47. var message = GetModelErrorMessage(Data, bulletPoint: "-");
  48. await _pageDialogService.DisplayAlertAsync(Config.AppName, message, "Ok");
  49. return;
  50. }
  51.  
  52. Data.PasswordEncryted = _cryptographyService.GetMD5(Data.Password);
  53.  
  54. App.UserDialogs.ShowLoading(AppResources.LoadingMessage);
  55.  
  56. var validateUserResponse = await ApiManager.ValidateUser(Data);
  57. var responseContent = await validateUserResponse.Content.ReadAsStringAsync();
  58.  
  59. Debug.WriteLine($"OnSignIn Response Content: {responseContent}, StatusCode: {validateUserResponse.StatusCode}");
  60.  
  61. App.UserDialogs.HideLoading();
  62.  
  63. if (validateUserResponse.IsSuccessStatusCode)
  64. {
  65. var responseObj = await Task.Run(() => XmlConvert.DeserializeObject<AuthenticateUserResponse>(responseContent));
  66.  
  67. //Fill Settings Values
  68. if (responseObj.State != "000")
  69. {
  70. App.UserDialogs.ShowError(responseObj.Message);
  71. return;
  72. }
  73.  
  74. Settings.UserId = responseObj.Id;
  75. Settings.UserName = responseObj.Name;
  76. Settings.UserPassword = Data.Password;
  77.  
  78. OnGoToMainCommand.Execute();
  79. }
  80. }
  81.  
  82. async void MainPageAync()
  83. {
  84. await NavigationService.NavigateAsync(new Uri($"{NavigationConstants.RootAbsoluteNavigation}/{NavigationConstants.Main}", UriKind.Absolute));
  85. }
  86. }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement