Guest User

Untitled

a guest
Apr 2nd, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.15 KB | None | 0 0
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  4. x:Class="MyApp.Views.LoginPage">
  5. <ContentPage.Content>
  6. <StackLayout VerticalOptions="Center" Padding="10">
  7. <Label Text="Username" />
  8. <Entry x:Name="UsernameEntry" Text="{Binding Username}" />
  9. <Label Text="Password" />
  10. <Entry x:Name="PasswordEntry" Text="{Binding Password}" IsPassword="true" />
  11. <Button Text="Log in" Command="{Binding LoginCommand}" />
  12. <Button Text="Cancel" Command="{Binding CancelCommand}" />
  13. </StackLayout>
  14. </ContentPage.Content>
  15. </ContentPage>
  16.  
  17. namespace MyApp.Views
  18. {
  19. [XamlCompilation(XamlCompilationOptions.Compile)]
  20. public partial class LoginPage : ContentPage
  21. {
  22. public LoginPage(LoginViewModel vm)
  23. {
  24. vm.ShowEmptyUsernameAlert += () => DisplayAlert(string.Empty, "Username is empty.", "OK");
  25. vm.ShowEmptyPasswordAlert += () => DisplayAlert(string.Empty, "Password is empty.", "OK");
  26. vm.ShowInvalidLoginAlert += () => DisplayAlert(string.Empty, "Wrong user or password.", "OK");
  27. vm.ShowNoConnectionAlert += () => DisplayAlert(string.Empty, "Can't connect to the service. Are you connected to the Internet?", "OK");
  28. vm.ShowOtherErrorAlert += (message) => DisplayAlert("We're sorry", message, "OK");
  29. BindingContext = vm;
  30. InitializeComponent();
  31. }
  32. }
  33. }
  34.  
  35. namespace MyApp.ViewModels
  36. {
  37. public class LoginViewModel : INotifyPropertyChanged
  38. {
  39. INavigation _navigation;
  40.  
  41. public LoginViewModel(INavigation navigation)
  42. {
  43. _navigation = navigation;
  44. }
  45.  
  46. public event PropertyChangedEventHandler PropertyChanged;
  47. public Action ShowEmptyUsernameAlert;
  48. public Action ShowEmptyPasswordAlert;
  49. public Action ShowInvalidLoginAlert;
  50. public Action ShowNoConnectionAlert;
  51. public Action<string> ShowOtherErrorAlert;
  52.  
  53. string _username;
  54. string _password;
  55.  
  56. public string Username
  57. {
  58. get => _username;
  59. set
  60. {
  61. _username = value;
  62. PropertyChanged(this, new PropertyChangedEventArgs("Username"));
  63. }
  64. }
  65.  
  66. public string Password
  67. {
  68. get => _password;
  69. set
  70. {
  71. _password = value;
  72. PropertyChanged(this, new PropertyChangedEventArgs("Password"));
  73. }
  74. }
  75.  
  76. public ICommand LoginCommand => new Command(async () =>
  77. {
  78. // Validate fields.
  79.  
  80. if (string.IsNullOrEmpty(Username))
  81. {
  82. ShowEmptyUsernameAlert();
  83. return;
  84. }
  85.  
  86. if (string.IsNullOrEmpty(Password))
  87. {
  88. ShowEmptyPasswordAlert();
  89. return;
  90. }
  91.  
  92. // Verify the login.
  93.  
  94. var loadingPage = new LoadingPopupPage();
  95. await _navigation.PushPopupAsync(loadingPage);
  96.  
  97. try
  98. {
  99. var customer = await App.Api.LoginCustomerAsync(Username, Password);
  100. Properties.CrmId.Set(customer.CrmId);
  101. await _navigation.RemovePopupPageAsync(loadingPage);
  102. LoginSucceeded?.Invoke(this, new LoginSuccessEventArgs(customer));
  103. await _navigation.PopModalAsync(); // Close the login modal.
  104. }
  105. catch (VerifyLoginFaultException)
  106. {
  107. await _navigation.RemovePopupPageAsync(loadingPage);
  108. ShowInvalidLoginAlert();
  109. }
  110. catch (SoapFaultException ex)
  111. {
  112. await _navigation.RemovePopupPageAsync(loadingPage);
  113. Crashes.TrackError(ex);
  114. ShowOtherErrorAlert(ex.Message);
  115. }
  116. catch (WebException ex)
  117. {
  118. await _navigation.RemovePopupPageAsync(loadingPage);
  119. Crashes.TrackError(ex);
  120. switch (ex.Status)
  121. {
  122. case WebExceptionStatus.Timeout:
  123. case WebExceptionStatus.ConnectFailure:
  124. case WebExceptionStatus.NameResolutionFailure:
  125. ShowNoConnectionAlert();
  126. break;
  127. default:
  128. ShowOtherErrorAlert(ex.Message);
  129. break;
  130. }
  131. }
  132. catch (Exception ex)
  133. {
  134. await _navigation.RemovePopupPageAsync(loadingPage);
  135. Crashes.TrackError(ex);
  136. ShowOtherErrorAlert(ex.Message);
  137. }
  138. });
  139.  
  140. public ICommand CancelCommand => new Command(async () => await _navigation.PopModalAsync());
  141.  
  142. public class LoginSuccessEventArgs : EventArgs
  143. {
  144. public Customer Customer { get; set; }
  145. public LoginSuccessEventArgs(Customer customer) => Customer = customer;
  146. }
  147.  
  148. public event EventHandler<LoginSuccessEventArgs> LoginSucceeded;
  149. }
  150. }
Add Comment
Please, Sign In to add comment