bitwarden-android/src/App/Pages/Vault/VaultAutofillListLoginsPage.cs

337 lines
12 KiB
C#
Raw Normal View History

using System;
using System.Linq;
using System.Threading.Tasks;
using Acr.UserDialogs;
using Bit.App.Abstractions;
using Bit.App.Controls;
using Bit.App.Models.Page;
using Bit.App.Resources;
using Xamarin.Forms;
using XLabs.Ioc;
using Bit.App.Utilities;
using System.Threading;
using Bit.App.Models;
2017-01-31 03:26:39 +03:00
using System.Collections.Generic;
namespace Bit.App.Pages
{
public class VaultAutofillListLoginsPage : ExtendedContentPage
{
private readonly ILoginService _loginService;
private readonly IDeviceInfoService _deviceInfoService;
private readonly IDeviceActionService _clipboardService;
private readonly ISettingsService _settingsService;
private CancellationTokenSource _filterResultsCancellationTokenSource;
2017-01-31 03:26:39 +03:00
private readonly string _name;
public VaultAutofillListLoginsPage(string uriString)
: base(true)
{
Uri = uriString;
Uri uri;
if(uriString?.StartsWith(Constants.AndroidAppProtocol) ?? false)
{
_name = uriString.Substring(Constants.AndroidAppProtocol.Length);
}
else if(!System.Uri.TryCreate(uriString, UriKind.Absolute, out uri) ||
2017-09-07 06:08:24 +03:00
!DomainName.TryParseBaseDomain(uri.Host, out _name))
2017-01-31 03:26:39 +03:00
{
_name = "--";
2017-01-31 03:26:39 +03:00
}
_loginService = Resolver.Resolve<ILoginService>();
_deviceInfoService = Resolver.Resolve<IDeviceInfoService>();
_clipboardService = Resolver.Resolve<IDeviceActionService>();
_settingsService = Resolver.Resolve<ISettingsService>();
2017-02-14 06:35:16 +03:00
UserDialogs = Resolver.Resolve<IUserDialogs>();
GoogleAnalyticsService = Resolver.Resolve<IGoogleAnalyticsService>();
Init();
}
2017-02-14 06:35:16 +03:00
public ExtendedObservableCollection<VaultListPageModel.AutofillGrouping> PresentationLoginsGroup { get; private set; }
= new ExtendedObservableCollection<VaultListPageModel.AutofillGrouping>();
2017-01-31 03:26:39 +03:00
public StackLayout NoDataStackLayout { get; set; }
public ListView ListView { get; set; }
2017-01-31 03:26:39 +03:00
public ActivityIndicator LoadingIndicator { get; set; }
private SearchToolBarItem SearchItem { get; set; }
private AddLoginToolBarItem AddLoginItem { get; set; }
private IGoogleAnalyticsService GoogleAnalyticsService { get; set; }
2017-02-14 06:35:16 +03:00
private IUserDialogs UserDialogs { get; set; }
private string Uri { get; set; }
private void Init()
{
2017-01-31 03:26:39 +03:00
var noDataLabel = new Label
{
Text = string.Format(AppResources.NoLoginsForUri, _name ?? "--"),
HorizontalTextAlignment = TextAlignment.Center,
FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)),
Style = (Style)Application.Current.Resources["text-muted"]
};
var addLoginButton = new ExtendedButton
{
Text = AppResources.AddALogin,
Command = new Command(() => AddLoginAsync()),
Style = (Style)Application.Current.Resources["btn-primaryAccent"]
};
NoDataStackLayout = new StackLayout
{
Children = { noDataLabel, addLoginButton },
VerticalOptions = LayoutOptions.CenterAndExpand,
Padding = new Thickness(20, 0),
Spacing = 20
};
AddLoginItem = new AddLoginToolBarItem(this);
ToolbarItems.Add(AddLoginItem);
SearchItem = new SearchToolBarItem(this);
ToolbarItems.Add(SearchItem);
ListView = new ListView(ListViewCachingStrategy.RecycleElement)
{
IsGroupingEnabled = true,
2017-02-14 06:35:16 +03:00
ItemsSource = PresentationLoginsGroup,
HasUnevenRows = true,
GroupHeaderTemplate = new DataTemplate(() => new HeaderViewCell()),
2017-01-31 03:26:39 +03:00
ItemTemplate = new DataTemplate(() => new VaultListViewCell(
(VaultListPageModel.Login l) => MoreClickedAsync(l)))
};
2017-05-30 21:13:53 +03:00
if(Device.RuntimePlatform == Device.iOS)
{
ListView.RowHeight = -1;
}
2017-01-31 03:26:39 +03:00
Title = string.Format(AppResources.LoginsForUri, _name ?? "--");
LoadingIndicator = new ActivityIndicator
{
IsRunning = true,
VerticalOptions = LayoutOptions.CenterAndExpand,
HorizontalOptions = LayoutOptions.Center
};
2017-01-31 03:26:39 +03:00
Content = LoadingIndicator;
}
protected override void OnAppearing()
{
base.OnAppearing();
ListView.ItemSelected += LoginSelected;
AddLoginItem.InitEvents();
SearchItem.InitEvents();
_filterResultsCancellationTokenSource = FetchAndLoadVault();
}
protected override void OnDisappearing()
{
base.OnDisappearing();
ListView.ItemSelected -= LoginSelected;
AddLoginItem.Dispose();
SearchItem.Dispose();
}
2017-01-31 03:26:39 +03:00
protected override bool OnBackButtonPressed()
{
GoogleAnalyticsService.TrackExtensionEvent("BackClosed", Uri.StartsWith("http") ? "Website" : "App");
2017-01-31 03:26:39 +03:00
MessagingCenter.Send(Application.Current, "Autofill", (VaultListPageModel.Login)null);
return true;
}
2017-01-31 03:26:39 +03:00
private void AdjustContent()
{
2017-02-14 06:35:16 +03:00
if(PresentationLoginsGroup.Count > 0)
2017-01-31 03:26:39 +03:00
{
Content = ListView;
}
else
{
2017-01-31 03:26:39 +03:00
Content = NoDataStackLayout;
}
2017-01-31 03:26:39 +03:00
}
2017-01-31 03:26:39 +03:00
private CancellationTokenSource FetchAndLoadVault()
{
var cts = new CancellationTokenSource();
_filterResultsCancellationTokenSource?.Cancel();
Task.Run(async () =>
{
2017-02-14 06:10:34 +03:00
var autofillGroupings = new List<VaultListPageModel.AutofillGrouping>();
var logins = await _loginService.GetAllAsync(Uri);
2017-02-14 06:10:34 +03:00
var normalLogins = logins?.Item1.Select(l => new VaultListPageModel.AutofillLogin(l, false))
.OrderBy(s => s.Name)
.ThenBy(s => s.Username)
.ToList();
2017-02-14 06:10:34 +03:00
if(normalLogins?.Any() ?? false)
{
autofillGroupings.Add(new VaultListPageModel.AutofillGrouping(normalLogins, AppResources.MatchingLogins));
}
var fuzzyLogins = logins?.Item2.Select(l => new VaultListPageModel.AutofillLogin(l, true))
.OrderBy(s => s.Name)
.ThenBy(s => s.Username)
.ToList();
2017-02-14 06:10:34 +03:00
if(fuzzyLogins?.Any() ?? false)
{
2017-02-14 06:10:34 +03:00
autofillGroupings.Add(new VaultListPageModel.AutofillGrouping(fuzzyLogins,
AppResources.PossibleMatchingLogins));
}
2017-01-31 03:26:39 +03:00
Device.BeginInvokeOnMainThread(() =>
{
2017-02-14 06:10:34 +03:00
if(autofillGroupings.Any())
{
2017-02-14 06:35:16 +03:00
PresentationLoginsGroup.ResetWithRange(autofillGroupings);
2017-02-14 06:10:34 +03:00
}
2017-01-31 03:26:39 +03:00
AdjustContent();
});
}, cts.Token);
return cts;
}
2017-02-14 06:10:34 +03:00
private async void LoginSelected(object sender, SelectedItemChangedEventArgs e)
{
2017-02-14 06:10:34 +03:00
var login = e.SelectedItem as VaultListPageModel.AutofillLogin;
if(login == null)
{
return;
}
if(Uri.StartsWith("http") && _deviceInfoService.Version < 21)
{
MoreClickedAsync(login);
2017-02-14 06:10:34 +03:00
}
else
{
bool doAutofill = true;
if(login.Fuzzy)
{
2017-02-14 06:35:16 +03:00
doAutofill = await UserDialogs.ConfirmAsync(
2017-02-14 06:10:34 +03:00
string.Format(AppResources.BitwardenAutofillServiceMatchConfirm, _name),
okText: AppResources.Yes, cancelText: AppResources.No);
}
if(doAutofill)
{
GoogleAnalyticsService.TrackExtensionEvent("AutoFilled", Uri.StartsWith("http") ? "Website" : "App");
MessagingCenter.Send(Application.Current, "Autofill", login as VaultListPageModel.Login);
}
}
2017-02-14 06:10:34 +03:00
((ListView)sender).SelectedItem = null;
}
2017-01-31 03:26:39 +03:00
private async void AddLoginAsync()
{
var page = new VaultAddLoginPage(Uri, _name, true);
await Navigation.PushForDeviceAsync(page);
}
2017-01-31 03:26:39 +03:00
private async void MoreClickedAsync(VaultListPageModel.Login login)
{
2017-01-31 03:26:39 +03:00
var buttons = new List<string> { AppResources.View, AppResources.Edit };
if(!string.IsNullOrWhiteSpace(login.Password.Value))
{
2017-01-31 03:26:39 +03:00
buttons.Add(AppResources.CopyPassword);
}
if(!string.IsNullOrWhiteSpace(login.Username))
{
buttons.Add(AppResources.CopyUsername);
}
2017-01-31 03:26:39 +03:00
var selection = await DisplayActionSheet(login.Name, AppResources.Cancel, null, buttons.ToArray());
if(selection == AppResources.View)
{
var page = new VaultViewLoginPage(login.Id);
await Navigation.PushForDeviceAsync(page);
}
else if(selection == AppResources.Edit)
{
2017-01-31 03:26:39 +03:00
var page = new VaultEditLoginPage(login.Id);
await Navigation.PushForDeviceAsync(page);
}
else if(selection == AppResources.CopyPassword)
{
Copy(login.Password.Value, AppResources.Password);
}
else if(selection == AppResources.CopyUsername)
{
Copy(login.Username, AppResources.Username);
}
}
2017-01-31 03:26:39 +03:00
private void Copy(string copyText, string alertLabel)
{
2017-01-31 03:26:39 +03:00
_clipboardService.CopyToClipboard(copyText);
2017-02-14 06:35:16 +03:00
UserDialogs.Toast(string.Format(AppResources.ValueHasBeenCopied, alertLabel));
2017-01-31 03:26:39 +03:00
}
private class AddLoginToolBarItem : ExtendedToolbarItem
2017-01-31 03:26:39 +03:00
{
public AddLoginToolBarItem(VaultAutofillListLoginsPage page)
: base(() => page.AddLoginAsync())
{
2017-01-31 03:26:39 +03:00
Text = AppResources.Add;
Icon = "plus";
2017-02-14 06:35:16 +03:00
Priority = 2;
}
}
private class SearchToolBarItem : ExtendedToolbarItem
{
private readonly VaultAutofillListLoginsPage _page;
2017-02-14 06:35:16 +03:00
public SearchToolBarItem(VaultAutofillListLoginsPage page)
{
_page = page;
2017-02-14 06:35:16 +03:00
Text = AppResources.Search;
Icon = "search";
Priority = 1;
ClickAction = () => DoClick();
}
private void DoClick()
{
2017-02-14 06:10:34 +03:00
_page.GoogleAnalyticsService.TrackExtensionEvent("CloseToSearch",
_page.Uri.StartsWith("http") ? "Website" : "App");
Application.Current.MainPage = new MainPage(_page.Uri);
2017-02-14 06:35:16 +03:00
_page.UserDialogs.Toast(string.Format(AppResources.BitwardenAutofillServiceSearch, _page._name),
TimeSpan.FromSeconds(10));
}
}
private class HeaderViewCell : ExtendedViewCell
{
public HeaderViewCell()
{
var label = new Label
{
FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label)),
Style = (Style)Application.Current.Resources["text-muted"],
VerticalTextAlignment = TextAlignment.Center
};
2017-05-30 21:13:53 +03:00
label.SetBinding(Label.TextProperty, nameof(VaultListPageModel.AutofillGrouping.Name));
var grid = new ContentView
{
Padding = new Thickness(16, 8, 0, 8),
Content = label
};
View = grid;
BackgroundColor = Color.FromHex("efeff4");
}
}
}
}