From c65b065dd744b01002685b6397443c6c96f375c4 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Thu, 16 May 2019 14:55:18 -0400 Subject: [PATCH] lock option picker --- src/App/Pages/Settings/SettingsPage.xaml | 8 +++ src/App/Pages/Settings/SettingsPage.xaml.cs | 10 +++ .../Pages/Settings/SettingsPageViewModel.cs | 66 +++++++++++++++++-- src/App/Resources/AppResources.Designer.cs | 18 +++++ src/App/Resources/AppResources.resx | 6 ++ 5 files changed, 102 insertions(+), 6 deletions(-) diff --git a/src/App/Pages/Settings/SettingsPage.xaml b/src/App/Pages/Settings/SettingsPage.xaml index 4977f88ed..717e77787 100644 --- a/src/App/Pages/Settings/SettingsPage.xaml +++ b/src/App/Pages/Settings/SettingsPage.xaml @@ -13,6 +13,8 @@ + + @@ -24,6 +26,12 @@ HorizontalOptions="StartAndExpand" VerticalOptions="CenterAndExpand" StyleClass="list-title"/> + diff --git a/src/App/Pages/Settings/SettingsPage.xaml.cs b/src/App/Pages/Settings/SettingsPage.xaml.cs index 79870e544..f87fa13ed 100644 --- a/src/App/Pages/Settings/SettingsPage.xaml.cs +++ b/src/App/Pages/Settings/SettingsPage.xaml.cs @@ -20,6 +20,12 @@ namespace Bit.App.Pages _vm.Page = this; } + protected async override void OnAppearing() + { + base.OnAppearing(); + await _vm.InitAsync(); + } + private async void RowSelected(object sender, SelectedItemChangedEventArgs e) { ((ListView)sender).SelectedItem = null; @@ -88,6 +94,10 @@ namespace Bit.App.Pages { await _vm.LockAsync(); } + else if(item.Name == AppResources.LockOptions) + { + await _vm.LockOptionsAsync(); + } } } } diff --git a/src/App/Pages/Settings/SettingsPageViewModel.cs b/src/App/Pages/Settings/SettingsPageViewModel.cs index b941085d3..34453103d 100644 --- a/src/App/Pages/Settings/SettingsPageViewModel.cs +++ b/src/App/Pages/Settings/SettingsPageViewModel.cs @@ -1,9 +1,11 @@ using Bit.App.Abstractions; using Bit.App.Resources; +using Bit.Core; using Bit.Core.Abstractions; using Bit.Core.Utilities; using System; using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; using Xamarin.Forms; @@ -18,6 +20,25 @@ namespace Bit.App.Pages private readonly IEnvironmentService _environmentService; private readonly IMessagingService _messagingService; private readonly ILockService _lockService; + private readonly IStorageService _storageService; + private readonly ISyncService _syncService; + + private bool _pin; + private bool _fingerprint; + private string _lastSyncDate; + private string _lockOptionValue; + private List> _lockOptions = + new List> + { + new KeyValuePair(AppResources.LockOptionImmediately, 0), + new KeyValuePair(AppResources.LockOption1Minute, 1), + new KeyValuePair(AppResources.LockOption5Minutes, 5), + new KeyValuePair(AppResources.LockOption15Minutes, 15), + new KeyValuePair(AppResources.LockOption30Minutes, 30), + new KeyValuePair(AppResources.LockOption1Hour, 60), + new KeyValuePair(AppResources.LockOption4Hours, 240), + new KeyValuePair(AppResources.Never, null), + }; public SettingsPageViewModel() { @@ -28,12 +49,30 @@ namespace Bit.App.Pages _environmentService = ServiceContainer.Resolve("environmentService"); _messagingService = ServiceContainer.Resolve("messagingService"); _lockService = ServiceContainer.Resolve("lockService"); + _storageService = ServiceContainer.Resolve("storageService"); + _syncService = ServiceContainer.Resolve("syncService"); + GroupedItems = new ExtendedObservableCollection(); PageTitle = AppResources.Settings; - BuildList(); } - public List GroupedItems { get; set; } + public ExtendedObservableCollection GroupedItems { get; set; } + + public async Task InitAsync() + { + var lastSync = await _syncService.GetLastSyncAsync(); + if(lastSync != null) + { + _lastSyncDate = string.Format("{0} {1}", lastSync.Value.ToShortDateString(), + lastSync.Value.ToShortTimeString()); + } + var option = await _storageService.GetAsync(Constants.LockOptionKey); + _lockOptionValue = _lockOptions.FirstOrDefault(o => o.Value == option).Key; + var pinSet = await _lockService.IsPinLockSetAsync(); + _pin = pinSet.Item1 || pinSet.Item2; + // TODO: Fingerprint + BuildList(); + } public async Task AboutAsync() { @@ -136,17 +175,32 @@ namespace Bit.App.Pages await _lockService.LockAsync(true); } + public async Task LockOptionsAsync() + { + var options = _lockOptions.Select(o => o.Key == _lockOptionValue ? $"✓ {o.Key}" : o.Key).ToArray(); + var selection = await Page.DisplayActionSheet(AppResources.LockOptions, AppResources.Cancel, null, options); + if(selection == AppResources.Cancel) + { + return; + } + var cleanSelection = selection.Replace("✓ ", string.Empty); + var selectionOption = _lockOptions.FirstOrDefault(o => o.Key == cleanSelection); + _lockOptionValue = selectionOption.Key; + await _storageService.SaveAsync(Constants.LockOptionKey, selectionOption.Value); + BuildList(); + } + private void BuildList() { var doUpper = Device.RuntimePlatform != Device.Android; var manageItems = new List { new SettingsPageListItem { Name = AppResources.Folders }, - new SettingsPageListItem { Name = AppResources.Sync } + new SettingsPageListItem { Name = AppResources.Sync, SubLabel = _lastSyncDate } }; var securityItems = new List { - new SettingsPageListItem { Name = AppResources.LockOptions }, + new SettingsPageListItem { Name = AppResources.LockOptions, SubLabel = _lockOptionValue }, new SettingsPageListItem { Name = string.Format(AppResources.UnlockWith, AppResources.Fingerprint) }, new SettingsPageListItem { Name = AppResources.UnlockWithPIN }, new SettingsPageListItem { Name = AppResources.LockNow }, @@ -172,14 +226,14 @@ namespace Bit.App.Pages new SettingsPageListItem { Name = AppResources.HelpAndFeedback }, new SettingsPageListItem { Name = AppResources.RateTheApp } }; - GroupedItems = new List + GroupedItems.ResetWithRange(new List { new SettingsPageListGroup(manageItems, AppResources.Manage, doUpper), new SettingsPageListGroup(securityItems, AppResources.Security, doUpper), new SettingsPageListGroup(accountItems, AppResources.Account, doUpper), new SettingsPageListGroup(toolsItems, AppResources.Tools, doUpper), new SettingsPageListGroup(otherItems, AppResources.Other, doUpper) - }; + }); } } } diff --git a/src/App/Resources/AppResources.Designer.cs b/src/App/Resources/AppResources.Designer.cs index 81cc7812e..f8a1d8119 100644 --- a/src/App/Resources/AppResources.Designer.cs +++ b/src/App/Resources/AppResources.Designer.cs @@ -2085,6 +2085,15 @@ namespace Bit.App.Resources { } } + /// + /// Looks up a localized string similar to 30 minutes. + /// + public static string LockOption30Minutes { + get { + return ResourceManager.GetString("LockOption30Minutes", resourceCulture); + } + } + /// /// Looks up a localized string similar to 4 hours. /// @@ -2094,6 +2103,15 @@ namespace Bit.App.Resources { } } + /// + /// Looks up a localized string similar to 5 minutes. + /// + public static string LockOption5Minutes { + get { + return ResourceManager.GetString("LockOption5Minutes", resourceCulture); + } + } + /// /// Looks up a localized string similar to Immediately. /// diff --git a/src/App/Resources/AppResources.resx b/src/App/Resources/AppResources.resx index 7f8c4b0a8..1ff96a1f0 100644 --- a/src/App/Resources/AppResources.resx +++ b/src/App/Resources/AppResources.resx @@ -1490,4 +1490,10 @@ Unlock + + 30 minutes + + + 5 minutes + \ No newline at end of file