Guest User

Untitled

a guest
Jun 19th, 2017
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.76 KB | None | 0 0
  1. using Acr.UserDialogs;
  2. using Plas.Datas;
  3. using Plas.Menus;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Diagnostics;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using Xamarin.Forms;
  11.  
  12. namespace Plas.Login
  13. {
  14.     public class LoginPageViewModel : BaseViewModel
  15.     {
  16.         private string mUsername;
  17.         public string Username
  18.         {
  19.             get { return mUsername; }
  20.             set
  21.             {
  22.                 SetProperty(ref mUsername, value);
  23.                 Validate();
  24.             }
  25.         }
  26.  
  27.         private string mPassword;
  28.         public string Password
  29.         {
  30.             get { return mPassword; }
  31.             set
  32.             {
  33.                 SetProperty(ref mPassword, value);
  34.                 Validate();
  35.             }
  36.         }
  37.  
  38.         private bool mEnabledLoginButton;
  39.         public bool EnabledLoginButton
  40.         {
  41.             get { return mEnabledLoginButton; }
  42.             set { SetProperty(ref mEnabledLoginButton, value); }
  43.         }
  44.  
  45.         public bool AlreadyLogin
  46.         {
  47.             get
  48.             {
  49.                 if (Application.Current.Properties.ContainsKey("isLogin"))
  50.                 {
  51.                     return (bool)Application.Current.Properties["isLogin"];
  52.                 }
  53.                 else
  54.                 {
  55.                     return false;
  56.                 }
  57.             }
  58.  
  59.             set
  60.             {
  61.                 Application.Current.Properties["isLogin"] = value;
  62.                 Application.Current.SavePropertiesAsync();
  63.             }
  64.         }
  65.  
  66.         public Command LoginCommand { get; private set; }
  67.  
  68.         private LoginDataProvider mDataProvider;
  69.  
  70.         public LoginPageViewModel()
  71.         {
  72.             LoginCommand = new Command(async () => await LoginAsync());
  73.             EnabledLoginButton = false;
  74.             mDataProvider = new LoginDataProvider();
  75.         }
  76.  
  77.         private async Task LoginAsync()
  78.         {
  79.             IsBusy = true;
  80.  
  81.             bool isAuthorize = false;
  82.             try
  83.             {
  84.                 var response = await mDataProvider.GetListAsync();
  85.                 if (response != null &&
  86.                     response.UserList != null &&
  87.                     response.UserList.Count > 0)
  88.                 {
  89.                     var user = response.UserList.FirstOrDefault(i => i.UserName == Username);
  90.                     if (user != null && user.Password == this.Password)
  91.                     {
  92.                         isAuthorize = true;
  93.                     }
  94.                 }
  95.             }
  96.             catch (Exception ex)
  97.             {
  98.                 Debug.WriteLine(ex);
  99.             }
  100.  
  101.             if (isAuthorize)
  102.             {
  103.                 AlreadyLogin = true;
  104.                 await LoginProcess();
  105.             }
  106.             else
  107.             {
  108.                 UserDialogs.Instance.Alert("Invalid username or password",
  109.                                            "Failed to login");
  110.             }
  111.  
  112.             IsBusy = false;
  113.         }
  114.  
  115.         private void Validate()
  116.         {
  117.             if (!string.IsNullOrEmpty(Username) && !string.IsNullOrEmpty(Password))
  118.             {
  119.                 EnabledLoginButton = true;
  120.             }
  121.             else
  122.             {
  123.                 EnabledLoginButton = false;
  124.             }
  125.         }
  126.  
  127.         public async Task LoginProcess()
  128.         {
  129.             var dataLoader = new DataLoader();
  130.             var isSuccess = await dataLoader.LoadData();
  131.             if (isSuccess)
  132.             {
  133.                 Application.Current.MainPage = new MenuPage();
  134.             }
  135.             else
  136.             {
  137.                 UserDialogs.Instance.Alert("Failed to load data", "Error");
  138.             }
  139.         }
  140.     }
  141. }
Add Comment
Please, Sign In to add comment