bitwarden-android/src/App/Pages/LoginPage.cs

208 lines
7.3 KiB
C#
Raw Normal View History

2016-05-02 09:52:09 +03:00
using System;
using Bit.App.Abstractions;
using Bit.App.Controls;
using Bit.App.Resources;
2016-05-02 09:52:09 +03:00
using Xamarin.Forms;
using XLabs.Ioc;
2016-06-24 06:03:00 +03:00
using System.Threading.Tasks;
using Plugin.Settings.Abstractions;
2017-05-30 21:13:53 +03:00
using Bit.App.Utilities;
2016-05-02 09:52:09 +03:00
2016-05-03 00:50:16 +03:00
namespace Bit.App.Pages
2016-05-02 09:52:09 +03:00
{
public class LoginPage : ExtendedContentPage
2016-05-02 09:52:09 +03:00
{
2016-06-24 06:03:00 +03:00
private IAuthService _authService;
private ISyncService _syncService;
private IDeviceActionService _deviceActionService;
private ISettings _settings;
private IGoogleAnalyticsService _googleAnalyticsService;
2017-10-10 15:25:23 +03:00
private IPushNotificationService _pushNotification;
private readonly string _email;
2016-06-24 06:03:00 +03:00
public LoginPage(string email = null)
: base(updateActivity: false)
2016-05-02 09:52:09 +03:00
{
_email = email;
2016-06-24 06:03:00 +03:00
_authService = Resolver.Resolve<IAuthService>();
_syncService = Resolver.Resolve<ISyncService>();
_deviceActionService = Resolver.Resolve<IDeviceActionService>();
_settings = Resolver.Resolve<ISettings>();
_googleAnalyticsService = Resolver.Resolve<IGoogleAnalyticsService>();
2017-10-10 15:25:23 +03:00
_pushNotification = Resolver.Resolve<IPushNotificationService>();
2016-05-02 09:52:09 +03:00
2016-06-24 06:03:00 +03:00
Init();
}
2016-05-02 09:52:09 +03:00
2016-06-24 06:03:00 +03:00
public FormEntryCell PasswordCell { get; set; }
public FormEntryCell EmailCell { get; set; }
2016-05-02 09:52:09 +03:00
2016-06-24 06:03:00 +03:00
private void Init()
{
MessagingCenter.Send(Application.Current, "ShowStatusBar", true);
2017-05-30 21:13:53 +03:00
var padding = Helpers.OnPlatform(
iOS: new Thickness(15, 20),
Android: new Thickness(15, 8),
Windows: new Thickness(10, 8));
PasswordCell = new FormEntryCell(AppResources.MasterPassword, isPassword: true,
useLabelAsPlaceholder: true, imageSource: "lock.png", containerPadding: padding);
EmailCell = new FormEntryCell(AppResources.EmailAddress, nextElement: PasswordCell.Entry,
entryKeyboard: Keyboard.Email, useLabelAsPlaceholder: true, imageSource: "envelope.png",
containerPadding: padding);
2016-05-02 09:52:09 +03:00
var lastLoginEmail = _settings.GetValueOrDefault(Constants.LastLoginEmail, string.Empty);
if(!string.IsNullOrWhiteSpace(_email))
{
EmailCell.Entry.Text = _email;
}
else if(!string.IsNullOrWhiteSpace(lastLoginEmail))
{
EmailCell.Entry.Text = lastLoginEmail;
}
2016-06-24 06:03:00 +03:00
PasswordCell.Entry.ReturnType = Enums.ReturnType.Go;
2016-05-02 09:52:09 +03:00
2016-06-24 06:03:00 +03:00
var table = new ExtendedTableView
2016-05-02 09:52:09 +03:00
{
2016-06-24 06:03:00 +03:00
Intent = TableIntent.Settings,
2016-07-23 07:40:17 +03:00
EnableScrolling = false,
2016-06-24 06:03:00 +03:00
HasUnevenRows = true,
2016-07-23 07:40:17 +03:00
EnableSelection = true,
NoFooter = true,
VerticalOptions = LayoutOptions.Start,
2016-06-24 06:03:00 +03:00
Root = new TableRoot
2016-05-02 09:52:09 +03:00
{
2017-11-21 06:39:49 +03:00
new TableSection(Helpers.GetEmptyTableSectionTitle())
2016-05-02 09:52:09 +03:00
{
2016-06-24 06:03:00 +03:00
EmailCell,
PasswordCell
2016-05-02 09:52:09 +03:00
}
2016-06-24 06:03:00 +03:00
}
};
2016-05-02 09:52:09 +03:00
var forgotPasswordButton = new ExtendedButton
2016-06-24 06:03:00 +03:00
{
Text = AppResources.GetPasswordHint,
2016-07-23 07:40:17 +03:00
Style = (Style)Application.Current.Resources["btn-primaryAccent"],
Command = new Command(async () => await ForgotPasswordAsync()),
2018-01-03 20:18:15 +03:00
VerticalOptions = LayoutOptions.End,
Uppercase = false,
BackgroundColor = Color.Transparent
2016-07-23 07:40:17 +03:00
};
2018-01-03 20:18:15 +03:00
var layout = new RedrawableStackLayout
2016-07-23 07:40:17 +03:00
{
Children = { table, forgotPasswordButton },
2018-01-03 20:18:15 +03:00
Spacing = 10
2016-07-23 07:40:17 +03:00
};
2018-01-03 20:18:15 +03:00
table.WrappingStackLayout = () => layout;
2016-07-23 07:40:17 +03:00
var scrollView = new ScrollView { Content = layout };
2016-05-02 09:52:09 +03:00
if(Device.RuntimePlatform == Device.iOS)
2016-06-24 06:03:00 +03:00
{
table.RowHeight = -1;
table.EstimatedRowHeight = 70;
ToolbarItems.Add(new DismissModalToolBarItem(this, AppResources.Cancel, () =>
{
MessagingCenter.Send(Application.Current, "ShowStatusBar", false);
}));
2016-06-24 06:03:00 +03:00
}
2016-05-02 09:52:09 +03:00
var loginToolbarItem = new ToolbarItem(AppResources.LogIn, Helpers.ToolbarImage("ion_chevron_right.png"), async () =>
2016-07-23 07:40:17 +03:00
{
await LogIn();
}, ToolbarItemOrder.Default, 0);
2016-06-24 06:03:00 +03:00
ToolbarItems.Add(loginToolbarItem);
Title = AppResources.Bitwarden;
2016-07-23 07:40:17 +03:00
Content = scrollView;
NavigationPage.SetBackButtonTitle(this, AppResources.LogIn);
2016-06-24 06:03:00 +03:00
}
2016-05-02 09:52:09 +03:00
2016-06-24 06:03:00 +03:00
protected override void OnAppearing()
{
base.OnAppearing();
PasswordCell.InitEvents();
EmailCell.InitEvents();
PasswordCell.Entry.Completed += Entry_Completed;
MessagingCenter.Send(Application.Current, "ShowStatusBar", true);
if(string.IsNullOrWhiteSpace(_email))
{
if(!string.IsNullOrWhiteSpace(EmailCell.Entry.Text))
{
PasswordCell.Entry.FocusWithDelay();
}
else
{
EmailCell.Entry.FocusWithDelay();
}
}
2016-06-24 06:03:00 +03:00
}
2016-05-02 09:52:09 +03:00
protected override void OnDisappearing()
{
base.OnDisappearing();
PasswordCell.Dispose();
EmailCell.Dispose();
PasswordCell.Entry.Completed -= Entry_Completed;
}
2016-06-24 06:06:33 +03:00
private async void Entry_Completed(object sender, EventArgs e)
2016-06-24 06:03:00 +03:00
{
2016-06-24 06:06:33 +03:00
await LogIn();
2016-06-24 06:03:00 +03:00
}
2016-07-23 07:40:17 +03:00
private async Task ForgotPasswordAsync()
{
await Navigation.PushAsync(new PasswordHintPage());
}
2016-05-03 00:50:16 +03:00
2016-06-24 06:03:00 +03:00
private async Task LogIn()
{
if(string.IsNullOrWhiteSpace(EmailCell.Entry.Text))
{
await DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired,
AppResources.EmailAddress), AppResources.Ok);
2016-06-24 06:03:00 +03:00
return;
}
if(string.IsNullOrWhiteSpace(PasswordCell.Entry.Text))
{
await DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired,
AppResources.MasterPassword), AppResources.Ok);
2016-06-24 06:03:00 +03:00
return;
}
2016-05-02 09:52:09 +03:00
_deviceActionService.ShowLoading(AppResources.LoggingIn);
var result = await _authService.TokenPostAsync(EmailCell.Entry.Text, PasswordCell.Entry.Text);
_deviceActionService.HideLoading();
if(!result.Success)
{
await DisplayAlert(AppResources.AnErrorHasOccurred, result.ErrorMessage, AppResources.Ok);
2016-06-24 06:03:00 +03:00
return;
}
2017-06-27 23:35:29 +03:00
PasswordCell.Entry.Text = string.Empty;
if(result.TwoFactorRequired)
{
_googleAnalyticsService.TrackAppEvent("LoggedIn To Two-step");
2017-06-27 23:18:32 +03:00
await Navigation.PushAsync(new LoginTwoFactorPage(EmailCell.Entry.Text, result));
return;
}
2016-08-04 07:32:37 +03:00
_googleAnalyticsService.TrackAppEvent("LoggedIn");
2016-06-24 06:03:00 +03:00
var task = Task.Run(async () => await _syncService.FullSyncAsync(true));
Application.Current.MainPage = new MainPage();
2016-05-02 09:52:09 +03:00
}
}
}