Guest User

Untitled

a guest
Apr 2nd, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.99 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. // Display a loading animation. (Rg.Plugins.Popup)
  93.  
  94. var loadingPage = new LoadingPopupPage();
  95. await _navigation.PushPopupAsync(loadingPage);
  96.  
  97. // Verify the login.
  98.  
  99. try
  100. {
  101. var customer = await App.Api.LoginCustomerAsync(Username, Password);
  102. Properties.CrmId.Set(customer.CrmId);
  103. LoginSucceeded?.Invoke(this, new LoginSuccessEventArgs(customer));
  104. await _navigation.PopModalAsync(); // Close the login modal.
  105. }
  106. catch (VerifyLoginFaultException)
  107. {
  108. ShowInvalidLoginAlert();
  109. }
  110. catch (SoapFaultException ex)
  111. {
  112. Crashes.TrackError(ex);
  113. ShowOtherErrorAlert(ex.Message);
  114. }
  115. catch (WebException ex)
  116. {
  117. Crashes.TrackError(ex);
  118. switch (ex.Status)
  119. {
  120. case WebExceptionStatus.Timeout:
  121. case WebExceptionStatus.ConnectFailure:
  122. case WebExceptionStatus.NameResolutionFailure:
  123. ShowNoConnectionAlert();
  124. break;
  125. default:
  126. ShowOtherErrorAlert(ex.Message);
  127. break;
  128. }
  129. }
  130. catch (Exception ex)
  131. {
  132. Crashes.TrackError(ex);
  133. ShowOtherErrorAlert(ex.Message);
  134. }
  135. finally
  136. {
  137. await _navigation.RemovePopupPageAsync(loadingPage);
  138. }
  139. });
  140.  
  141. public ICommand CancelCommand => new Command(async () => await _navigation.PopModalAsync());
  142.  
  143. public class LoginSuccessEventArgs : EventArgs
  144. {
  145. public Customer Customer { get; set; }
  146. public LoginSuccessEventArgs(Customer customer) => Customer = customer;
  147. }
  148.  
  149. public event EventHandler<LoginSuccessEventArgs> LoginSucceeded;
  150. }
  151. }
Add Comment
Please, Sign In to add comment