2016-05-02 09:52:09 +03:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
2016-05-04 02:49:49 +03:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Acr.UserDialogs;
|
2016-05-02 09:52:09 +03:00
|
|
|
|
using Bit.App.Abstractions;
|
2016-05-13 07:11:32 +03:00
|
|
|
|
using Bit.App.Controls;
|
2016-05-08 06:11:47 +03:00
|
|
|
|
using Bit.App.Models.Page;
|
2016-05-07 10:06:27 +03:00
|
|
|
|
using Bit.App.Resources;
|
2016-05-02 09:52:09 +03:00
|
|
|
|
using Xamarin.Forms;
|
|
|
|
|
using XLabs.Ioc;
|
2016-05-19 03:29:03 +03:00
|
|
|
|
using Bit.App.Utilities;
|
2016-06-28 07:55:53 +03:00
|
|
|
|
using PushNotification.Plugin.Abstractions;
|
|
|
|
|
using Plugin.Settings.Abstractions;
|
2016-07-02 01:54:00 +03:00
|
|
|
|
using Plugin.Connectivity.Abstractions;
|
2016-07-07 05:59:13 +03:00
|
|
|
|
using System.Collections.Generic;
|
2016-08-14 08:42:18 +03:00
|
|
|
|
using System.Threading;
|
2017-02-16 05:56:02 +03:00
|
|
|
|
using FFImageLoading.Forms;
|
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
|
|
|
|
{
|
2017-01-03 08:17:15 +03:00
|
|
|
|
public class VaultListLoginsPage : ExtendedContentPage
|
2016-05-02 09:52:09 +03:00
|
|
|
|
{
|
|
|
|
|
private readonly IFolderService _folderService;
|
2017-01-03 08:17:15 +03:00
|
|
|
|
private readonly ILoginService _loginService;
|
2016-05-04 02:49:49 +03:00
|
|
|
|
private readonly IUserDialogs _userDialogs;
|
2016-07-02 01:54:00 +03:00
|
|
|
|
private readonly IConnectivity _connectivity;
|
2016-05-07 20:42:09 +03:00
|
|
|
|
private readonly IClipboardService _clipboardService;
|
2016-07-12 06:55:16 +03:00
|
|
|
|
private readonly ISyncService _syncService;
|
2016-06-28 07:55:53 +03:00
|
|
|
|
private readonly IPushNotification _pushNotification;
|
2016-12-19 17:41:47 +03:00
|
|
|
|
private readonly IDeviceInfoService _deviceInfoService;
|
2016-06-28 07:55:53 +03:00
|
|
|
|
private readonly ISettings _settings;
|
2017-02-14 06:10:34 +03:00
|
|
|
|
private readonly IGoogleAnalyticsService _googleAnalyticsService;
|
2016-06-15 06:23:05 +03:00
|
|
|
|
private readonly bool _favorites;
|
2016-08-06 06:58:31 +03:00
|
|
|
|
private bool _loadExistingData;
|
2016-08-14 08:42:18 +03:00
|
|
|
|
private CancellationTokenSource _filterResultsCancellationTokenSource;
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2017-02-14 03:12:02 +03:00
|
|
|
|
public VaultListLoginsPage(bool favorites, string uri = null)
|
2016-08-06 18:43:22 +03:00
|
|
|
|
: base(true)
|
2016-05-02 09:52:09 +03:00
|
|
|
|
{
|
2016-06-15 06:23:05 +03:00
|
|
|
|
_favorites = favorites;
|
2016-05-02 09:52:09 +03:00
|
|
|
|
_folderService = Resolver.Resolve<IFolderService>();
|
2017-01-03 08:17:15 +03:00
|
|
|
|
_loginService = Resolver.Resolve<ILoginService>();
|
2016-07-02 01:54:00 +03:00
|
|
|
|
_connectivity = Resolver.Resolve<IConnectivity>();
|
2016-05-04 02:49:49 +03:00
|
|
|
|
_userDialogs = Resolver.Resolve<IUserDialogs>();
|
2016-05-07 20:42:09 +03:00
|
|
|
|
_clipboardService = Resolver.Resolve<IClipboardService>();
|
2016-07-12 06:55:16 +03:00
|
|
|
|
_syncService = Resolver.Resolve<ISyncService>();
|
2016-06-28 07:55:53 +03:00
|
|
|
|
_pushNotification = Resolver.Resolve<IPushNotification>();
|
2016-12-19 17:41:47 +03:00
|
|
|
|
_deviceInfoService = Resolver.Resolve<IDeviceInfoService>();
|
2016-06-28 07:55:53 +03:00
|
|
|
|
_settings = Resolver.Resolve<ISettings>();
|
2017-02-14 06:10:34 +03:00
|
|
|
|
_googleAnalyticsService = Resolver.Resolve<IGoogleAnalyticsService>();
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2016-08-06 06:58:31 +03:00
|
|
|
|
var cryptoService = Resolver.Resolve<ICryptoService>();
|
|
|
|
|
_loadExistingData = !_settings.GetValueOrDefault(Constants.FirstVaultLoad, true) || !cryptoService.KeyChanged;
|
|
|
|
|
|
2017-02-14 03:12:02 +03:00
|
|
|
|
Uri = uri;
|
|
|
|
|
|
2016-05-04 02:49:49 +03:00
|
|
|
|
Init();
|
|
|
|
|
}
|
2016-05-03 01:35:01 +03:00
|
|
|
|
|
2016-07-11 08:12:31 +03:00
|
|
|
|
public ExtendedObservableCollection<VaultListPageModel.Folder> PresentationFolders { get; private set; }
|
|
|
|
|
= new ExtendedObservableCollection<VaultListPageModel.Folder>();
|
|
|
|
|
public ListView ListView { get; set; }
|
2017-01-03 08:17:15 +03:00
|
|
|
|
public VaultListPageModel.Login[] Logins { get; set; } = new VaultListPageModel.Login[] { };
|
2016-08-18 04:19:19 +03:00
|
|
|
|
public VaultListPageModel.Folder[] Folders { get; set; } = new VaultListPageModel.Folder[] { };
|
2016-07-12 02:36:39 +03:00
|
|
|
|
public SearchBar Search { get; set; }
|
2016-08-25 06:57:45 +03:00
|
|
|
|
public StackLayout NoDataStackLayout { get; set; }
|
|
|
|
|
public StackLayout ResultsStackLayout { get; set; }
|
|
|
|
|
public ActivityIndicator LoadingIndicator { get; set; }
|
2017-02-17 08:16:09 +03:00
|
|
|
|
private AddLoginToolBarItem AddLoginItem { get; set; }
|
2017-02-14 03:12:02 +03:00
|
|
|
|
public string Uri { get; set; }
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2016-05-04 02:49:49 +03:00
|
|
|
|
private void Init()
|
|
|
|
|
{
|
2016-07-15 08:52:33 +03:00
|
|
|
|
MessagingCenter.Subscribe<Application, bool>(Application.Current, "SyncCompleted", (sender, success) =>
|
2016-07-01 03:08:34 +03:00
|
|
|
|
{
|
2016-07-12 06:55:16 +03:00
|
|
|
|
if(success)
|
|
|
|
|
{
|
2016-08-14 08:42:18 +03:00
|
|
|
|
_filterResultsCancellationTokenSource = FetchAndLoadVault();
|
2016-07-12 06:55:16 +03:00
|
|
|
|
}
|
2016-07-01 03:08:34 +03:00
|
|
|
|
});
|
|
|
|
|
|
2016-06-15 06:23:05 +03:00
|
|
|
|
if(!_favorites)
|
|
|
|
|
{
|
2017-02-17 08:16:09 +03:00
|
|
|
|
AddLoginItem = new AddLoginToolBarItem(this);
|
|
|
|
|
ToolbarItems.Add(AddLoginItem);
|
2016-06-15 06:23:05 +03:00
|
|
|
|
}
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2016-08-14 04:43:15 +03:00
|
|
|
|
ListView = new ListView(ListViewCachingStrategy.RecycleElement)
|
2016-05-14 08:34:42 +03:00
|
|
|
|
{
|
|
|
|
|
IsGroupingEnabled = true,
|
2016-07-11 08:12:31 +03:00
|
|
|
|
ItemsSource = PresentationFolders,
|
2016-05-14 08:34:42 +03:00
|
|
|
|
HasUnevenRows = true,
|
2016-05-19 06:30:46 +03:00
|
|
|
|
GroupHeaderTemplate = new DataTemplate(() => new VaultListHeaderViewCell(this)),
|
2017-01-31 03:26:39 +03:00
|
|
|
|
ItemTemplate = new DataTemplate(() => new VaultListViewCell(
|
|
|
|
|
(VaultListPageModel.Login l) => MoreClickedAsync(l)))
|
2016-05-14 08:34:42 +03:00
|
|
|
|
};
|
2016-06-29 07:34:20 +03:00
|
|
|
|
|
2016-06-18 01:14:24 +03:00
|
|
|
|
if(Device.OS == TargetPlatform.iOS)
|
|
|
|
|
{
|
2016-07-11 08:12:31 +03:00
|
|
|
|
ListView.RowHeight = -1;
|
2016-06-18 01:14:24 +03:00
|
|
|
|
}
|
2016-06-29 07:34:20 +03:00
|
|
|
|
|
2016-07-12 02:36:39 +03:00
|
|
|
|
Search = new SearchBar
|
2016-07-11 08:12:31 +03:00
|
|
|
|
{
|
2016-11-26 00:42:52 +03:00
|
|
|
|
Placeholder = AppResources.SearchVault,
|
2016-07-19 02:16:27 +03:00
|
|
|
|
FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Button)),
|
|
|
|
|
CancelButtonColor = Color.FromHex("3c8dbc")
|
2016-07-11 08:12:31 +03:00
|
|
|
|
};
|
2016-12-19 17:41:47 +03:00
|
|
|
|
// Bug with searchbar on android 7, ref https://bugzilla.xamarin.com/show_bug.cgi?id=43975
|
|
|
|
|
if(Device.OS == TargetPlatform.Android && _deviceInfoService.Version >= 24)
|
|
|
|
|
{
|
|
|
|
|
Search.HeightRequest = 50;
|
|
|
|
|
}
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2016-06-15 06:23:05 +03:00
|
|
|
|
Title = _favorites ? AppResources.Favorites : AppResources.MyVault;
|
2016-08-25 06:57:45 +03:00
|
|
|
|
|
|
|
|
|
ResultsStackLayout = new StackLayout
|
2016-07-11 08:12:31 +03:00
|
|
|
|
{
|
2017-02-16 03:55:52 +03:00
|
|
|
|
Children = { Search, ListView },
|
2016-07-11 08:12:31 +03:00
|
|
|
|
Spacing = 0
|
|
|
|
|
};
|
2016-08-25 06:57:45 +03:00
|
|
|
|
|
|
|
|
|
var noDataLabel = new Label
|
|
|
|
|
{
|
2017-01-03 08:17:15 +03:00
|
|
|
|
Text = _favorites ? AppResources.NoFavorites : AppResources.NoLogins,
|
2016-08-25 06:57:45 +03:00
|
|
|
|
HorizontalTextAlignment = TextAlignment.Center,
|
|
|
|
|
FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)),
|
|
|
|
|
Style = (Style)Application.Current.Resources["text-muted"]
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
NoDataStackLayout = new StackLayout
|
|
|
|
|
{
|
|
|
|
|
Children = { noDataLabel },
|
|
|
|
|
VerticalOptions = LayoutOptions.CenterAndExpand,
|
|
|
|
|
Padding = new Thickness(20, 0),
|
|
|
|
|
Spacing = 20
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if(!_favorites)
|
|
|
|
|
{
|
2017-01-03 08:17:15 +03:00
|
|
|
|
var addLoginButton = new ExtendedButton
|
2016-08-25 06:57:45 +03:00
|
|
|
|
{
|
2017-01-03 08:17:15 +03:00
|
|
|
|
Text = AppResources.AddALogin,
|
|
|
|
|
Command = new Command(() => AddLogin()),
|
2016-08-27 06:53:50 +03:00
|
|
|
|
Style = (Style)Application.Current.Resources["btn-primaryAccent"]
|
2016-08-25 06:57:45 +03:00
|
|
|
|
};
|
|
|
|
|
|
2017-01-03 08:17:15 +03:00
|
|
|
|
NoDataStackLayout.Children.Add(addLoginButton);
|
2016-08-25 06:57:45 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LoadingIndicator = new ActivityIndicator
|
|
|
|
|
{
|
|
|
|
|
IsRunning = true,
|
|
|
|
|
VerticalOptions = LayoutOptions.CenterAndExpand,
|
|
|
|
|
HorizontalOptions = LayoutOptions.Center
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Content = LoadingIndicator;
|
2016-07-11 08:12:31 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SearchBar_SearchButtonPressed(object sender, EventArgs e)
|
|
|
|
|
{
|
2016-08-16 06:09:41 +03:00
|
|
|
|
_filterResultsCancellationTokenSource = FilterResultsBackground(((SearchBar)sender).Text,
|
|
|
|
|
_filterResultsCancellationTokenSource);
|
2016-07-11 08:12:31 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SearchBar_TextChanged(object sender, TextChangedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
var oldLength = e.OldTextValue?.Length ?? 0;
|
|
|
|
|
var newLength = e.NewTextValue?.Length ?? 0;
|
|
|
|
|
if(oldLength < 2 && newLength < 2 && oldLength < newLength)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-16 06:09:41 +03:00
|
|
|
|
_filterResultsCancellationTokenSource = FilterResultsBackground(e.NewTextValue,
|
|
|
|
|
_filterResultsCancellationTokenSource);
|
2016-07-11 08:12:31 +03:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-14 08:42:18 +03:00
|
|
|
|
private CancellationTokenSource FilterResultsBackground(string searchFilter, CancellationTokenSource previousCts)
|
2016-07-11 08:12:31 +03:00
|
|
|
|
{
|
2016-08-14 08:42:18 +03:00
|
|
|
|
var cts = new CancellationTokenSource();
|
2016-07-12 02:36:39 +03:00
|
|
|
|
Task.Run(async () =>
|
2016-07-11 08:12:31 +03:00
|
|
|
|
{
|
2016-07-12 06:55:16 +03:00
|
|
|
|
if(!string.IsNullOrWhiteSpace(searchFilter))
|
2016-07-12 02:36:39 +03:00
|
|
|
|
{
|
2016-07-12 06:55:16 +03:00
|
|
|
|
await Task.Delay(300);
|
|
|
|
|
if(searchFilter != Search.Text)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-08-14 08:42:18 +03:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
previousCts?.Cancel();
|
|
|
|
|
}
|
2016-07-12 02:36:39 +03:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-14 08:42:18 +03:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
FilterResults(searchFilter, cts.Token);
|
|
|
|
|
}
|
|
|
|
|
catch(OperationCanceledException) { }
|
|
|
|
|
}, cts.Token);
|
|
|
|
|
|
|
|
|
|
return cts;
|
2016-05-02 09:52:09 +03:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-14 08:42:18 +03:00
|
|
|
|
private void FilterResults(string searchFilter, CancellationToken ct)
|
2016-07-12 06:55:16 +03:00
|
|
|
|
{
|
2016-08-16 06:09:41 +03:00
|
|
|
|
ct.ThrowIfCancellationRequested();
|
2016-08-14 08:42:18 +03:00
|
|
|
|
|
2016-07-12 06:55:16 +03:00
|
|
|
|
if(string.IsNullOrWhiteSpace(searchFilter))
|
|
|
|
|
{
|
2017-01-03 08:17:15 +03:00
|
|
|
|
LoadFolders(Logins, ct);
|
2016-07-12 06:55:16 +03:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
searchFilter = searchFilter.ToLower();
|
2017-01-03 08:17:15 +03:00
|
|
|
|
var filteredLogins = Logins
|
2016-08-14 08:42:18 +03:00
|
|
|
|
.Where(s => s.Name.ToLower().Contains(searchFilter) || s.Username.ToLower().Contains(searchFilter))
|
|
|
|
|
.TakeWhile(s => !ct.IsCancellationRequested)
|
2016-08-18 04:19:19 +03:00
|
|
|
|
.ToArray();
|
2016-08-14 08:42:18 +03:00
|
|
|
|
|
2016-08-16 06:09:41 +03:00
|
|
|
|
ct.ThrowIfCancellationRequested();
|
2017-01-03 08:17:15 +03:00
|
|
|
|
LoadFolders(filteredLogins, ct);
|
2016-07-12 06:55:16 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-15 08:52:33 +03:00
|
|
|
|
protected override void OnAppearing()
|
2016-05-02 09:52:09 +03:00
|
|
|
|
{
|
|
|
|
|
base.OnAppearing();
|
2017-02-17 08:16:09 +03:00
|
|
|
|
ListView.ItemSelected += LoginSelected;
|
|
|
|
|
Search.TextChanged += SearchBar_TextChanged;
|
|
|
|
|
Search.SearchButtonPressed += SearchBar_SearchButtonPressed;
|
|
|
|
|
AddLoginItem?.InitEvents();
|
|
|
|
|
|
2016-08-06 06:58:31 +03:00
|
|
|
|
if(_loadExistingData)
|
|
|
|
|
{
|
2016-08-14 08:42:18 +03:00
|
|
|
|
_filterResultsCancellationTokenSource = FetchAndLoadVault();
|
2016-08-06 06:58:31 +03:00
|
|
|
|
}
|
2016-06-28 07:55:53 +03:00
|
|
|
|
|
2016-07-02 01:54:00 +03:00
|
|
|
|
if(_connectivity.IsConnected && Device.OS == TargetPlatform.iOS && !_favorites)
|
2016-06-28 07:55:53 +03:00
|
|
|
|
{
|
2016-08-30 06:06:29 +03:00
|
|
|
|
var pushPromptShow = _settings.GetValueOrDefault(Constants.PushInitialPromptShown, false);
|
2016-07-26 06:34:19 +03:00
|
|
|
|
Action registerAction = () =>
|
|
|
|
|
{
|
2016-08-30 06:06:29 +03:00
|
|
|
|
var lastPushRegistration = _settings.GetValueOrDefault<DateTime?>(Constants.PushLastRegistrationDate, null);
|
2016-08-25 06:57:45 +03:00
|
|
|
|
if(!pushPromptShow || !lastPushRegistration.HasValue
|
2016-08-16 06:09:41 +03:00
|
|
|
|
|| (DateTime.UtcNow - lastPushRegistration) > TimeSpan.FromDays(1))
|
2016-07-26 06:34:19 +03:00
|
|
|
|
{
|
|
|
|
|
_pushNotification.Register();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2016-07-02 02:16:47 +03:00
|
|
|
|
if(!pushPromptShow)
|
2016-06-28 07:55:53 +03:00
|
|
|
|
{
|
2016-07-02 09:01:47 +03:00
|
|
|
|
_settings.AddOrUpdateValue(Constants.PushInitialPromptShown, true);
|
2016-07-26 06:34:19 +03:00
|
|
|
|
_userDialogs.Alert(new AlertConfig
|
|
|
|
|
{
|
2016-11-26 00:42:52 +03:00
|
|
|
|
Message = AppResources.PushNotificationAlert,
|
|
|
|
|
Title = AppResources.EnableAutomaticSyncing,
|
2016-12-07 06:27:14 +03:00
|
|
|
|
OnAction = registerAction,
|
2016-11-26 00:42:52 +03:00
|
|
|
|
OkText = AppResources.OkGotIt
|
2016-07-26 06:34:19 +03:00
|
|
|
|
});
|
2016-06-28 07:55:53 +03:00
|
|
|
|
}
|
2016-07-26 06:34:19 +03:00
|
|
|
|
else
|
2016-07-02 02:16:47 +03:00
|
|
|
|
{
|
2016-07-26 06:34:19 +03:00
|
|
|
|
// Check push registration once per day
|
|
|
|
|
registerAction();
|
2016-07-02 02:16:47 +03:00
|
|
|
|
}
|
2016-06-28 07:55:53 +03:00
|
|
|
|
}
|
2016-05-04 02:49:49 +03:00
|
|
|
|
}
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2017-02-17 08:16:09 +03:00
|
|
|
|
protected override void OnDisappearing()
|
|
|
|
|
{
|
|
|
|
|
base.OnDisappearing();
|
|
|
|
|
ListView.ItemSelected -= LoginSelected;
|
|
|
|
|
Search.TextChanged -= SearchBar_TextChanged;
|
|
|
|
|
Search.SearchButtonPressed -= SearchBar_SearchButtonPressed;
|
|
|
|
|
AddLoginItem.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-14 03:12:02 +03:00
|
|
|
|
protected override bool OnBackButtonPressed()
|
|
|
|
|
{
|
|
|
|
|
if(string.IsNullOrWhiteSpace(Uri))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-14 06:10:34 +03:00
|
|
|
|
_googleAnalyticsService.TrackExtensionEvent("BackClosed", Uri.StartsWith("http") ? "Website" : "App");
|
2017-02-14 03:12:02 +03:00
|
|
|
|
MessagingCenter.Send(Application.Current, "Autofill", (VaultListPageModel.Login)null);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-25 06:57:45 +03:00
|
|
|
|
private void AdjustContent()
|
|
|
|
|
{
|
2016-09-06 07:34:27 +03:00
|
|
|
|
if(PresentationFolders.Count > 0 || !string.IsNullOrWhiteSpace(Search.Text))
|
2016-08-25 06:57:45 +03:00
|
|
|
|
{
|
|
|
|
|
Content = ResultsStackLayout;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Content = NoDataStackLayout;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-14 08:42:18 +03:00
|
|
|
|
private CancellationTokenSource FetchAndLoadVault()
|
2016-07-11 08:12:31 +03:00
|
|
|
|
{
|
2016-08-14 08:42:18 +03:00
|
|
|
|
var cts = new CancellationTokenSource();
|
2016-08-06 06:58:31 +03:00
|
|
|
|
_settings.AddOrUpdateValue(Constants.FirstVaultLoad, false);
|
|
|
|
|
_loadExistingData = true;
|
|
|
|
|
|
2016-07-12 06:55:16 +03:00
|
|
|
|
if(PresentationFolders.Count > 0 && _syncService.SyncInProgress)
|
|
|
|
|
{
|
2016-08-14 08:42:18 +03:00
|
|
|
|
return cts;
|
2016-07-12 06:55:16 +03:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-16 06:09:41 +03:00
|
|
|
|
_filterResultsCancellationTokenSource?.Cancel();
|
|
|
|
|
|
2016-07-15 08:52:33 +03:00
|
|
|
|
Task.Run(async () =>
|
2016-07-12 02:36:39 +03:00
|
|
|
|
{
|
|
|
|
|
var foldersTask = _folderService.GetAllAsync();
|
2017-01-03 08:17:15 +03:00
|
|
|
|
var loginsTask = _favorites ? _loginService.GetAllAsync(true) : _loginService.GetAllAsync();
|
|
|
|
|
await Task.WhenAll(foldersTask, loginsTask);
|
2016-07-11 08:12:31 +03:00
|
|
|
|
|
2016-07-12 02:36:39 +03:00
|
|
|
|
var folders = await foldersTask;
|
2017-01-03 08:17:15 +03:00
|
|
|
|
var logins = await loginsTask;
|
2016-07-11 08:12:31 +03:00
|
|
|
|
|
2016-08-18 04:19:19 +03:00
|
|
|
|
Folders = folders
|
|
|
|
|
.Select(f => new VaultListPageModel.Folder(f))
|
|
|
|
|
.OrderBy(s => s.Name)
|
|
|
|
|
.ToArray();
|
|
|
|
|
|
2017-01-03 08:17:15 +03:00
|
|
|
|
Logins = logins
|
|
|
|
|
.Select(s => new VaultListPageModel.Login(s))
|
2016-08-18 04:19:19 +03:00
|
|
|
|
.OrderBy(s => s.Name)
|
|
|
|
|
.ThenBy(s => s.Username)
|
|
|
|
|
.ToArray();
|
2016-07-11 08:12:31 +03:00
|
|
|
|
|
2016-08-14 08:42:18 +03:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
FilterResults(Search.Text, cts.Token);
|
|
|
|
|
}
|
|
|
|
|
catch(OperationCanceledException) { }
|
|
|
|
|
}, cts.Token);
|
|
|
|
|
|
|
|
|
|
return cts;
|
2016-07-11 08:12:31 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-03 08:17:15 +03:00
|
|
|
|
private void LoadFolders(VaultListPageModel.Login[] logins, CancellationToken ct)
|
2016-05-04 02:49:49 +03:00
|
|
|
|
{
|
2016-07-11 08:12:31 +03:00
|
|
|
|
var folders = new List<VaultListPageModel.Folder>(Folders);
|
|
|
|
|
|
|
|
|
|
foreach(var folder in folders)
|
|
|
|
|
{
|
|
|
|
|
if(folder.Any())
|
|
|
|
|
{
|
|
|
|
|
folder.Clear();
|
|
|
|
|
}
|
2016-08-14 08:42:18 +03:00
|
|
|
|
|
2017-01-03 08:17:15 +03:00
|
|
|
|
var loginsToAdd = logins
|
2016-08-14 08:42:18 +03:00
|
|
|
|
.Where(s => s.FolderId == folder.Id)
|
|
|
|
|
.TakeWhile(s => !ct.IsCancellationRequested)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
2016-08-16 06:09:41 +03:00
|
|
|
|
ct.ThrowIfCancellationRequested();
|
2017-01-03 08:17:15 +03:00
|
|
|
|
folder.AddRange(loginsToAdd);
|
2016-07-11 08:12:31 +03:00
|
|
|
|
}
|
2016-05-04 02:49:49 +03:00
|
|
|
|
|
2017-01-03 08:17:15 +03:00
|
|
|
|
var noneToAdd = logins
|
2016-08-14 08:42:18 +03:00
|
|
|
|
.Where(s => s.FolderId == null)
|
|
|
|
|
.TakeWhile(s => !ct.IsCancellationRequested)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
2016-08-16 06:09:41 +03:00
|
|
|
|
ct.ThrowIfCancellationRequested();
|
2016-08-14 08:42:18 +03:00
|
|
|
|
|
|
|
|
|
var noneFolder = new VaultListPageModel.Folder(noneToAdd);
|
2016-07-11 08:12:31 +03:00
|
|
|
|
folders.Add(noneFolder);
|
2016-05-19 03:29:03 +03:00
|
|
|
|
|
2016-08-14 08:42:18 +03:00
|
|
|
|
var foldersToAdd = folders
|
|
|
|
|
.Where(f => f.Any())
|
|
|
|
|
.TakeWhile(s => !ct.IsCancellationRequested)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
2016-08-16 06:09:41 +03:00
|
|
|
|
ct.ThrowIfCancellationRequested();
|
2016-08-25 06:57:45 +03:00
|
|
|
|
Device.BeginInvokeOnMainThread(() =>
|
|
|
|
|
{
|
|
|
|
|
PresentationFolders.ResetWithRange(foldersToAdd);
|
|
|
|
|
AdjustContent();
|
|
|
|
|
});
|
2016-05-02 09:52:09 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-03 08:17:15 +03:00
|
|
|
|
private async void LoginSelected(object sender, SelectedItemChangedEventArgs e)
|
2016-05-02 09:52:09 +03:00
|
|
|
|
{
|
2017-01-03 08:17:15 +03:00
|
|
|
|
var login = e.SelectedItem as VaultListPageModel.Login;
|
2017-02-14 06:10:34 +03:00
|
|
|
|
if(login == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-02-14 03:12:02 +03:00
|
|
|
|
|
|
|
|
|
string selection = null;
|
|
|
|
|
if(!string.IsNullOrWhiteSpace(Uri))
|
|
|
|
|
{
|
2017-02-14 06:10:34 +03:00
|
|
|
|
selection = await DisplayActionSheet(AppResources.AutofillOrView, AppResources.Cancel, null,
|
|
|
|
|
AppResources.Autofill, AppResources.View);
|
2017-02-14 03:12:02 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(selection == AppResources.View || string.IsNullOrWhiteSpace(Uri))
|
|
|
|
|
{
|
|
|
|
|
var page = new VaultViewLoginPage(login.Id);
|
|
|
|
|
await Navigation.PushForDeviceAsync(page);
|
|
|
|
|
}
|
2017-02-14 06:10:34 +03:00
|
|
|
|
else if(selection == AppResources.Autofill)
|
2017-02-14 03:12:02 +03:00
|
|
|
|
{
|
|
|
|
|
if(Uri.StartsWith("http") && _deviceInfoService.Version < 21)
|
|
|
|
|
{
|
|
|
|
|
MoreClickedAsync(login);
|
|
|
|
|
}
|
2017-02-14 06:10:34 +03:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_googleAnalyticsService.TrackExtensionEvent("AutoFilled", Uri.StartsWith("http") ? "Website" : "App");
|
|
|
|
|
MessagingCenter.Send(Application.Current, "Autofill", login);
|
|
|
|
|
}
|
2017-02-14 03:12:02 +03:00
|
|
|
|
}
|
2017-02-14 06:10:34 +03:00
|
|
|
|
|
|
|
|
|
((ListView)sender).SelectedItem = null;
|
2016-05-02 09:52:09 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-03 08:17:15 +03:00
|
|
|
|
private async void MoreClickedAsync(VaultListPageModel.Login login)
|
2016-05-02 09:52:09 +03:00
|
|
|
|
{
|
2016-07-07 05:59:13 +03:00
|
|
|
|
var buttons = new List<string> { AppResources.View, AppResources.Edit };
|
2017-01-03 08:17:15 +03:00
|
|
|
|
if(!string.IsNullOrWhiteSpace(login.Password.Value))
|
2016-07-07 05:59:13 +03:00
|
|
|
|
{
|
|
|
|
|
buttons.Add(AppResources.CopyPassword);
|
|
|
|
|
}
|
2017-01-03 08:17:15 +03:00
|
|
|
|
if(!string.IsNullOrWhiteSpace(login.Username))
|
2016-07-07 05:59:13 +03:00
|
|
|
|
{
|
|
|
|
|
buttons.Add(AppResources.CopyUsername);
|
|
|
|
|
}
|
2017-01-03 08:17:15 +03:00
|
|
|
|
if(!string.IsNullOrWhiteSpace(login.Uri.Value) && (login.Uri.Value.StartsWith("http://")
|
|
|
|
|
|| login.Uri.Value.StartsWith("https://")))
|
2016-07-07 05:59:13 +03:00
|
|
|
|
{
|
|
|
|
|
buttons.Add(AppResources.GoToWebsite);
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-03 08:17:15 +03:00
|
|
|
|
var selection = await DisplayActionSheet(login.Name, AppResources.Cancel, null, buttons.ToArray());
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2016-05-07 20:42:09 +03:00
|
|
|
|
if(selection == AppResources.View)
|
2016-05-04 02:49:49 +03:00
|
|
|
|
{
|
2017-01-03 08:17:15 +03:00
|
|
|
|
var page = new VaultViewLoginPage(login.Id);
|
2016-08-30 06:50:22 +03:00
|
|
|
|
await Navigation.PushForDeviceAsync(page);
|
2016-05-04 02:49:49 +03:00
|
|
|
|
}
|
2016-05-07 20:42:09 +03:00
|
|
|
|
else if(selection == AppResources.Edit)
|
|
|
|
|
{
|
2017-01-03 08:17:15 +03:00
|
|
|
|
var page = new VaultEditLoginPage(login.Id);
|
2016-08-30 06:50:22 +03:00
|
|
|
|
await Navigation.PushForDeviceAsync(page);
|
2016-05-07 20:42:09 +03:00
|
|
|
|
}
|
|
|
|
|
else if(selection == AppResources.CopyPassword)
|
|
|
|
|
{
|
2017-01-03 08:17:15 +03:00
|
|
|
|
Copy(login.Password.Value, AppResources.Password);
|
2016-05-07 20:42:09 +03:00
|
|
|
|
}
|
|
|
|
|
else if(selection == AppResources.CopyUsername)
|
|
|
|
|
{
|
2017-01-03 08:17:15 +03:00
|
|
|
|
Copy(login.Username, AppResources.Username);
|
2016-05-07 20:42:09 +03:00
|
|
|
|
}
|
|
|
|
|
else if(selection == AppResources.GoToWebsite)
|
|
|
|
|
{
|
2017-01-03 08:17:15 +03:00
|
|
|
|
Device.OpenUri(new Uri(login.Uri.Value));
|
2016-05-07 20:42:09 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Copy(string copyText, string alertLabel)
|
|
|
|
|
{
|
|
|
|
|
_clipboardService.CopyToClipboard(copyText);
|
2016-07-26 07:38:41 +03:00
|
|
|
|
_userDialogs.Toast(string.Format(AppResources.ValueHasBeenCopied, alertLabel));
|
2016-05-04 02:49:49 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-03 08:17:15 +03:00
|
|
|
|
private async void AddLogin()
|
2016-08-25 06:57:45 +03:00
|
|
|
|
{
|
2017-02-14 03:12:02 +03:00
|
|
|
|
var page = new VaultAddLoginPage(Uri);
|
2016-08-30 06:50:22 +03:00
|
|
|
|
await Navigation.PushForDeviceAsync(page);
|
2016-08-25 06:57:45 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-17 08:16:09 +03:00
|
|
|
|
private class AddLoginToolBarItem : ExtendedToolbarItem
|
2016-05-04 02:49:49 +03:00
|
|
|
|
{
|
2017-01-03 08:17:15 +03:00
|
|
|
|
private readonly VaultListLoginsPage _page;
|
2016-05-04 02:49:49 +03:00
|
|
|
|
|
2017-01-03 08:17:15 +03:00
|
|
|
|
public AddLoginToolBarItem(VaultListLoginsPage page)
|
2017-02-17 08:16:09 +03:00
|
|
|
|
: base(() => page.AddLogin())
|
2016-05-04 02:49:49 +03:00
|
|
|
|
{
|
|
|
|
|
_page = page;
|
2016-05-07 20:42:09 +03:00
|
|
|
|
Text = AppResources.Add;
|
2016-06-14 05:09:16 +03:00
|
|
|
|
Icon = "plus";
|
2016-05-04 02:49:49 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-01 02:08:09 +03:00
|
|
|
|
private class VaultListHeaderViewCell : ExtendedViewCell
|
2016-05-14 08:34:42 +03:00
|
|
|
|
{
|
2017-01-03 08:17:15 +03:00
|
|
|
|
public VaultListHeaderViewCell(VaultListLoginsPage page)
|
2016-05-14 08:34:42 +03:00
|
|
|
|
{
|
2017-02-16 05:56:02 +03:00
|
|
|
|
var image = new CachedImage
|
2016-05-14 08:34:42 +03:00
|
|
|
|
{
|
2016-07-30 07:46:17 +03:00
|
|
|
|
Source = "folder",
|
2016-12-18 08:19:14 +03:00
|
|
|
|
WidthRequest = 18,
|
|
|
|
|
HeightRequest = 18
|
2016-05-14 08:34:42 +03:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var label = new Label
|
|
|
|
|
{
|
2016-08-23 01:59:15 +03:00
|
|
|
|
FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label)),
|
2016-12-26 19:30:57 +03:00
|
|
|
|
Style = (Style)Application.Current.Resources["text-muted"],
|
|
|
|
|
VerticalTextAlignment = TextAlignment.Center
|
2016-05-14 08:34:42 +03:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
label.SetBinding<VaultListPageModel.Folder>(Label.TextProperty, s => s.Name);
|
|
|
|
|
|
2016-12-18 08:19:14 +03:00
|
|
|
|
var grid = new Grid
|
2016-05-14 08:34:42 +03:00
|
|
|
|
{
|
2016-12-18 08:19:14 +03:00
|
|
|
|
ColumnSpacing = 10,
|
|
|
|
|
Padding = new Thickness(16, 8, 0, 8)
|
2016-05-14 08:34:42 +03:00
|
|
|
|
};
|
2016-12-18 08:19:14 +03:00
|
|
|
|
grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
|
|
|
|
|
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(18, GridUnitType.Absolute) });
|
|
|
|
|
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
|
|
|
|
|
grid.Children.Add(image, 0, 0);
|
|
|
|
|
grid.Children.Add(label, 1, 0);
|
2016-06-15 05:19:40 +03:00
|
|
|
|
|
2016-12-18 08:19:14 +03:00
|
|
|
|
View = grid;
|
2016-07-01 02:08:09 +03:00
|
|
|
|
BackgroundColor = Color.FromHex("efeff4");
|
2016-05-04 02:49:49 +03:00
|
|
|
|
}
|
2016-05-02 09:52:09 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|