bitwarden-android/src/App/Pages/Accounts/LoginPageViewModel.cs

133 lines
4.6 KiB
C#
Raw Normal View History

2019-04-19 16:42:55 +03:00
using Bit.App.Abstractions;
using Bit.App.Resources;
using Bit.Core.Abstractions;
using Bit.Core.Exceptions;
using Bit.Core.Utilities;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace Bit.App.Pages
{
public class LoginPageViewModel : BaseViewModel
{
2019-05-01 22:29:57 +03:00
private const string Keys_RememberedEmail = "rememberedEmail";
private const string Keys_RememberEmail = "rememberEmail";
2019-04-19 16:42:55 +03:00
private readonly IDeviceActionService _deviceActionService;
private readonly IAuthService _authService;
2019-04-19 16:57:19 +03:00
private readonly ISyncService _syncService;
2019-05-01 22:29:57 +03:00
private readonly IStorageService _storageService;
2019-04-19 16:42:55 +03:00
2019-05-01 22:11:54 +03:00
private bool _showPassword;
2019-05-07 06:30:54 +03:00
private string _email;
2019-05-28 17:16:57 +03:00
private string _masterPassword;
2019-05-01 22:11:54 +03:00
2019-04-19 16:42:55 +03:00
public LoginPageViewModel()
{
_deviceActionService = ServiceContainer.Resolve<IDeviceActionService>("deviceActionService");
_authService = ServiceContainer.Resolve<IAuthService>("authService");
2019-04-19 16:57:19 +03:00
_syncService = ServiceContainer.Resolve<ISyncService>("syncService");
2019-05-01 22:29:57 +03:00
_storageService = ServiceContainer.Resolve<IStorageService>("storageService");
2019-04-19 16:42:55 +03:00
PageTitle = AppResources.Bitwarden;
2019-05-01 22:11:54 +03:00
TogglePasswordCommand = new Command(TogglePassword);
}
public bool ShowPassword
{
get => _showPassword;
set => SetProperty(ref _showPassword, value,
additionalPropertyNames: new string[]
{
nameof(ShowPasswordIcon)
});
2019-04-19 16:42:55 +03:00
}
2019-05-07 06:30:54 +03:00
public string Email
{
get => _email;
set => SetProperty(ref _email, value);
}
2019-05-28 17:16:57 +03:00
public string MasterPassword
{
get => _masterPassword;
set => SetProperty(ref _masterPassword, value);
}
2019-05-01 22:11:54 +03:00
public Command TogglePasswordCommand { get; }
public string ShowPasswordIcon => ShowPassword ? "" : "";
2019-05-01 22:29:57 +03:00
public bool RememberEmail { get; set; }
public async Task InitAsync()
{
if(string.IsNullOrWhiteSpace(Email))
{
Email = await _storageService.GetAsync<string>(Keys_RememberedEmail);
}
var rememberEmail = await _storageService.GetAsync<bool?>(Keys_RememberEmail);
RememberEmail = rememberEmail.GetValueOrDefault(true);
}
2019-04-19 16:42:55 +03:00
public async Task LogInAsync()
{
if(string.IsNullOrWhiteSpace(Email))
{
await Page.DisplayAlert(AppResources.AnErrorHasOccurred,
string.Format(AppResources.ValidationFieldRequired, AppResources.EmailAddress),
AppResources.Ok);
return;
}
if(!Email.Contains("@"))
{
await Page.DisplayAlert(AppResources.AnErrorHasOccurred, AppResources.InvalidEmail, AppResources.Ok);
return;
}
if(string.IsNullOrWhiteSpace(MasterPassword))
{
await Page.DisplayAlert(AppResources.AnErrorHasOccurred,
string.Format(AppResources.ValidationFieldRequired, AppResources.MasterPassword),
AppResources.Ok);
return;
}
try
{
await _deviceActionService.ShowLoadingAsync(AppResources.LoggingIn);
var response = await _authService.LogInAsync(Email, MasterPassword);
2019-05-28 17:16:57 +03:00
MasterPassword = string.Empty;
2019-05-01 22:29:57 +03:00
if(RememberEmail)
{
await _storageService.SaveAsync(Keys_RememberedEmail, Email);
}
else
{
await _storageService.RemoveAsync(Keys_RememberedEmail);
}
2019-05-28 16:04:20 +03:00
await _deviceActionService.HideLoadingAsync();
2019-04-19 16:42:55 +03:00
if(response.TwoFactor)
{
2019-05-27 18:57:10 +03:00
var page = new TwoFactorPage();
await Page.Navigation.PushModalAsync(new NavigationPage(page));
2019-04-19 16:42:55 +03:00
}
else
{
2019-04-19 16:57:19 +03:00
var task = Task.Run(async () => await _syncService.FullSyncAsync(true));
2019-04-19 16:42:55 +03:00
Application.Current.MainPage = new TabsPage();
}
}
catch(ApiException e)
{
await _deviceActionService.HideLoadingAsync();
await Page.DisplayAlert(AppResources.AnErrorHasOccurred, e.Error.GetSingleMessage(), AppResources.Ok);
}
}
2019-05-01 22:11:54 +03:00
public void TogglePassword()
{
ShowPassword = !ShowPassword;
2019-05-01 22:29:57 +03:00
(Page as LoginPage).MasterPasswordEntry.Focus();
2019-05-01 22:11:54 +03:00
}
2019-04-19 16:42:55 +03:00
}
}