Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?xml version="1.0" encoding="utf-8" ?>
- <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
- xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
- x:Class="MyApp.Views.LoginPage">
- <ContentPage.Content>
- <StackLayout VerticalOptions="Center" Padding="10">
- <Label Text="Username" />
- <Entry x:Name="UsernameEntry" Text="{Binding Username}" />
- <Label Text="Password" />
- <Entry x:Name="PasswordEntry" Text="{Binding Password}" IsPassword="true" />
- <Button Text="Log in" Command="{Binding LoginCommand}" />
- <Button Text="Cancel" Command="{Binding CancelCommand}" />
- </StackLayout>
- </ContentPage.Content>
- </ContentPage>
- namespace MyApp.Views
- {
- [XamlCompilation(XamlCompilationOptions.Compile)]
- public partial class LoginPage : ContentPage
- {
- public LoginPage(LoginViewModel vm)
- {
- vm.ShowEmptyUsernameAlert += () => DisplayAlert(string.Empty, "Username is empty.", "OK");
- vm.ShowEmptyPasswordAlert += () => DisplayAlert(string.Empty, "Password is empty.", "OK");
- vm.ShowInvalidLoginAlert += () => DisplayAlert(string.Empty, "Wrong user or password.", "OK");
- vm.ShowNoConnectionAlert += () => DisplayAlert(string.Empty, "Can't connect to the service. Are you connected to the Internet?", "OK");
- vm.ShowOtherErrorAlert += (message) => DisplayAlert("We're sorry", message, "OK");
- BindingContext = vm;
- InitializeComponent();
- }
- }
- }
- namespace MyApp.ViewModels
- {
- public class LoginViewModel : INotifyPropertyChanged
- {
- INavigation _navigation;
- public LoginViewModel(INavigation navigation)
- {
- _navigation = navigation;
- }
- public event PropertyChangedEventHandler PropertyChanged;
- public Action ShowEmptyUsernameAlert;
- public Action ShowEmptyPasswordAlert;
- public Action ShowInvalidLoginAlert;
- public Action ShowNoConnectionAlert;
- public Action<string> ShowOtherErrorAlert;
- string _username;
- string _password;
- public string Username
- {
- get => _username;
- set
- {
- _username = value;
- PropertyChanged(this, new PropertyChangedEventArgs("Username"));
- }
- }
- public string Password
- {
- get => _password;
- set
- {
- _password = value;
- PropertyChanged(this, new PropertyChangedEventArgs("Password"));
- }
- }
- public ICommand LoginCommand => new Command(async () =>
- {
- // Validate fields.
- if (string.IsNullOrEmpty(Username))
- {
- ShowEmptyUsernameAlert();
- return;
- }
- if (string.IsNullOrEmpty(Password))
- {
- ShowEmptyPasswordAlert();
- return;
- }
- // Verify the login.
- var loadingPage = new LoadingPopupPage();
- await _navigation.PushPopupAsync(loadingPage);
- try
- {
- var customer = await App.Api.LoginCustomerAsync(Username, Password);
- Properties.CrmId.Set(customer.CrmId);
- await _navigation.RemovePopupPageAsync(loadingPage);
- LoginSucceeded?.Invoke(this, new LoginSuccessEventArgs(customer));
- await _navigation.PopModalAsync(); // Close the login modal.
- }
- catch (VerifyLoginFaultException)
- {
- await _navigation.RemovePopupPageAsync(loadingPage);
- ShowInvalidLoginAlert();
- }
- catch (SoapFaultException ex)
- {
- await _navigation.RemovePopupPageAsync(loadingPage);
- Crashes.TrackError(ex);
- ShowOtherErrorAlert(ex.Message);
- }
- catch (WebException ex)
- {
- await _navigation.RemovePopupPageAsync(loadingPage);
- Crashes.TrackError(ex);
- switch (ex.Status)
- {
- case WebExceptionStatus.Timeout:
- case WebExceptionStatus.ConnectFailure:
- case WebExceptionStatus.NameResolutionFailure:
- ShowNoConnectionAlert();
- break;
- default:
- ShowOtherErrorAlert(ex.Message);
- break;
- }
- }
- catch (Exception ex)
- {
- await _navigation.RemovePopupPageAsync(loadingPage);
- Crashes.TrackError(ex);
- ShowOtherErrorAlert(ex.Message);
- }
- });
- public ICommand CancelCommand => new Command(async () => await _navigation.PopModalAsync());
- public class LoginSuccessEventArgs : EventArgs
- {
- public Customer Customer { get; set; }
- public LoginSuccessEventArgs(Customer customer) => Customer = customer;
- }
- public event EventHandler<LoginSuccessEventArgs> LoginSucceeded;
- }
- }
Add Comment
Please, Sign In to add comment