Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Acr.UserDialogs;
- using Plas.Datas;
- using Plas.Menus;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Xamarin.Forms;
- namespace Plas.Login
- {
- public class LoginPageViewModel : BaseViewModel
- {
- private string mUsername;
- public string Username
- {
- get { return mUsername; }
- set
- {
- SetProperty(ref mUsername, value);
- Validate();
- }
- }
- private string mPassword;
- public string Password
- {
- get { return mPassword; }
- set
- {
- SetProperty(ref mPassword, value);
- Validate();
- }
- }
- private bool mEnabledLoginButton;
- public bool EnabledLoginButton
- {
- get { return mEnabledLoginButton; }
- set { SetProperty(ref mEnabledLoginButton, value); }
- }
- public bool AlreadyLogin
- {
- get
- {
- if (Application.Current.Properties.ContainsKey("isLogin"))
- {
- return (bool)Application.Current.Properties["isLogin"];
- }
- else
- {
- return false;
- }
- }
- set
- {
- Application.Current.Properties["isLogin"] = value;
- Application.Current.SavePropertiesAsync();
- }
- }
- public Command LoginCommand { get; private set; }
- private LoginDataProvider mDataProvider;
- public LoginPageViewModel()
- {
- LoginCommand = new Command(async () => await LoginAsync());
- EnabledLoginButton = false;
- mDataProvider = new LoginDataProvider();
- }
- private async Task LoginAsync()
- {
- IsBusy = true;
- bool isAuthorize = false;
- try
- {
- var response = await mDataProvider.GetListAsync();
- if (response != null &&
- response.UserList != null &&
- response.UserList.Count > 0)
- {
- var user = response.UserList.FirstOrDefault(i => i.UserName == Username);
- if (user != null && user.Password == this.Password)
- {
- isAuthorize = true;
- }
- }
- }
- catch (Exception ex)
- {
- Debug.WriteLine(ex);
- }
- if (isAuthorize)
- {
- AlreadyLogin = true;
- await LoginProcess();
- }
- else
- {
- UserDialogs.Instance.Alert("Invalid username or password",
- "Failed to login");
- }
- IsBusy = false;
- }
- private void Validate()
- {
- if (!string.IsNullOrEmpty(Username) && !string.IsNullOrEmpty(Password))
- {
- EnabledLoginButton = true;
- }
- else
- {
- EnabledLoginButton = false;
- }
- }
- public async Task LoginProcess()
- {
- var dataLoader = new DataLoader();
- var isSuccess = await dataLoader.LoadData();
- if (isSuccess)
- {
- Application.Current.MainPage = new MenuPage();
- }
- else
- {
- UserDialogs.Instance.Alert("Failed to load data", "Error");
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment