From eacb3b182ed98c458ba40a619c76421b25b82f92 Mon Sep 17 00:00:00 2001 From: Geoffroy BONNEVILLE Date: Fri, 24 Apr 2020 13:58:30 +0200 Subject: [PATCH] Database files are added to the mru instead of the fal Simplify opening database workflow Corrected opening from recent bug --- .../Common/Interfaces/IRecentProxy.cs | 3 +-- .../CloseDatabase/CloseDatabaseCommand.cs | 5 +--- .../Queries/OpenDatabase/OpenDatabaseQuery.cs | 8 ++++--- .../UWP/UwpRecentFilesClient.cs | 24 ++++++------------- .../MainPageFrames/NewDatabasePage.xaml.cs | 2 +- .../MainPageFrames/OpenDatabasePage.xaml.cs | 10 ++++---- WinAppCommon/ViewModels/NewVm.cs | 2 +- WinAppCommon/ViewModels/OpenVm.cs | 18 ++------------ WinAppCommon/ViewModels/RecentVm.cs | 11 +-------- 9 files changed, 25 insertions(+), 58 deletions(-) diff --git a/ModernKeePass.Application/Common/Interfaces/IRecentProxy.cs b/ModernKeePass.Application/Common/Interfaces/IRecentProxy.cs index d165de0..edea303 100644 --- a/ModernKeePass.Application/Common/Interfaces/IRecentProxy.cs +++ b/ModernKeePass.Application/Common/Interfaces/IRecentProxy.cs @@ -7,9 +7,8 @@ namespace ModernKeePass.Application.Common.Interfaces public interface IRecentProxy { int EntryCount { get; } - Task Get(string token, bool updateAccessTime = false); + Task Get(string token, bool updateAccessTime = true); IEnumerable GetAll(); - Task Add(FileInfo recentItem); void ClearAll(); } } \ No newline at end of file diff --git a/ModernKeePass.Application/Database/Commands/CloseDatabase/CloseDatabaseCommand.cs b/ModernKeePass.Application/Database/Commands/CloseDatabase/CloseDatabaseCommand.cs index ae88bec..96c5040 100644 --- a/ModernKeePass.Application/Database/Commands/CloseDatabase/CloseDatabaseCommand.cs +++ b/ModernKeePass.Application/Database/Commands/CloseDatabase/CloseDatabaseCommand.cs @@ -9,18 +9,15 @@ namespace ModernKeePass.Application.Database.Commands.CloseDatabase public class CloseDatabaseCommandHandler : IRequestHandler { private readonly IDatabaseProxy _database; - private readonly IFileProxy _file; - public CloseDatabaseCommandHandler(IDatabaseProxy database, IFileProxy file) + public CloseDatabaseCommandHandler(IDatabaseProxy database) { _database = database; - _file = file; } public void Handle(CloseDatabaseCommand message) { if (!_database.IsOpen) throw new DatabaseClosedException(); _database.CloseDatabase(); - _file.ReleaseFile(_database.FileAccessToken); // Cleanup _database.FileAccessToken = null; diff --git a/ModernKeePass.Application/Database/Queries/OpenDatabase/OpenDatabaseQuery.cs b/ModernKeePass.Application/Database/Queries/OpenDatabase/OpenDatabaseQuery.cs index 8e7ec2f..7f4def8 100644 --- a/ModernKeePass.Application/Database/Queries/OpenDatabase/OpenDatabaseQuery.cs +++ b/ModernKeePass.Application/Database/Queries/OpenDatabase/OpenDatabaseQuery.cs @@ -16,18 +16,20 @@ namespace ModernKeePass.Application.Database.Queries.OpenDatabase { private readonly IDatabaseProxy _database; private readonly IFileProxy _file; + private readonly IRecentProxy _recent; - public OpenDatabaseQueryHandler(IDatabaseProxy database, IFileProxy file) + public OpenDatabaseQueryHandler(IDatabaseProxy database, IFileProxy file, IRecentProxy recent) { _database = database; _file = file; + _recent = recent; } public async Task Handle(OpenDatabaseQuery message) { if (_database.IsDirty) throw new DatabaseOpenException(); - - var file = await _file.OpenBinaryFile(message.FilePath); + + var file = await _recent.Get(message.FilePath); var hasKeyFile = !string.IsNullOrEmpty(message.KeyFilePath); await _database.Open(file, new Credentials { diff --git a/ModernKeePass.Infrastructure/UWP/UwpRecentFilesClient.cs b/ModernKeePass.Infrastructure/UWP/UwpRecentFilesClient.cs index 2b8f5ea..a7eb6cd 100644 --- a/ModernKeePass.Infrastructure/UWP/UwpRecentFilesClient.cs +++ b/ModernKeePass.Infrastructure/UWP/UwpRecentFilesClient.cs @@ -1,33 +1,30 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Runtime.InteropServices.WindowsRuntime; using System.Threading.Tasks; using Windows.Storage.AccessCache; using ModernKeePass.Application.Common.Interfaces; using ModernKeePass.Domain.Dtos; +using Windows.Storage; namespace ModernKeePass.Infrastructure.UWP { public class UwpRecentFilesClient: IRecentProxy { private readonly StorageItemMostRecentlyUsedList _mru = StorageApplicationPermissions.MostRecentlyUsedList; - private readonly StorageItemAccessList _fal = StorageApplicationPermissions.FutureAccessList; public int EntryCount => _mru.Entries.Count; - public async Task Get(string token, bool updateAccessTime = false) + public async Task Get(string token, bool updateAccessTime = true) { try { var file = await _mru.GetFileAsync(token, updateAccessTime ? AccessCacheOptions.None : AccessCacheOptions.SuppressAccessTimeUpdate).AsTask().ConfigureAwait(false); - _fal.AddOrReplace(token, file, file.Name); - return new FileInfo - { - Id = token, - Name = file.DisplayName, - Path = file.Path - }; + + var result = await FileIO.ReadBufferAsync(file).AsTask(); + return result.ToArray(); } catch (Exception) { @@ -45,18 +42,11 @@ namespace ModernKeePass.Infrastructure.UWP Path = e.Metadata }); } - - public async Task Add(FileInfo recentItem) - { - var file = await _fal.GetFileAsync(recentItem.Id).AsTask(); - _mru.Add(file, file.Path); - } - + public void ClearAll() { foreach (var entry in _mru.Entries) { - if (_fal.ContainsItem(entry.Token)) _fal.Remove(entry.Token); _mru.Remove(entry.Token); } } diff --git a/ModernKeePass/Views/MainPageFrames/NewDatabasePage.xaml.cs b/ModernKeePass/Views/MainPageFrames/NewDatabasePage.xaml.cs index 8b1276a..0d3fa7d 100644 --- a/ModernKeePass/Views/MainPageFrames/NewDatabasePage.xaml.cs +++ b/ModernKeePass/Views/MainPageFrames/NewDatabasePage.xaml.cs @@ -48,7 +48,7 @@ namespace ModernKeePass.Views Path = file.Path, Name = file.DisplayName }; - await Model.OpenFile(fileInfo); + Model.OpenFile(fileInfo); } private void ImportFormatComboBox_OnSelectionChanged(object sender, SelectionChangedEventArgs e) diff --git a/ModernKeePass/Views/MainPageFrames/OpenDatabasePage.xaml.cs b/ModernKeePass/Views/MainPageFrames/OpenDatabasePage.xaml.cs index 91023ca..594c2c7 100644 --- a/ModernKeePass/Views/MainPageFrames/OpenDatabasePage.xaml.cs +++ b/ModernKeePass/Views/MainPageFrames/OpenDatabasePage.xaml.cs @@ -22,13 +22,13 @@ namespace ModernKeePass.Views InitializeComponent(); } - protected override async void OnNavigatedTo(NavigationEventArgs e) + protected override void OnNavigatedTo(NavigationEventArgs e) { base.OnNavigatedTo(e); var file = e.Parameter as FileInfo; if (file != null) { - await Model.OpenFile(file); + Model.OpenFile(file); } } @@ -45,14 +45,16 @@ namespace ModernKeePass.Views var file = await picker.PickSingleFileAsync().AsTask(); if (file == null) return; - var token = StorageApplicationPermissions.FutureAccessList.Add(file, file.Name); + //var token = StorageApplicationPermissions.FutureAccessList.Add(file, file.Name); + // TODO: use service + var token = StorageApplicationPermissions.MostRecentlyUsedList.Add(file, file.Name); var fileInfo = new FileInfo { Path = file.Path, Name = file.DisplayName, Id = token }; - await Model.OpenFile(fileInfo); + Model.OpenFile(fileInfo); } } } diff --git a/WinAppCommon/ViewModels/NewVm.cs b/WinAppCommon/ViewModels/NewVm.cs index 971df9c..28e4095 100644 --- a/WinAppCommon/ViewModels/NewVm.cs +++ b/WinAppCommon/ViewModels/NewVm.cs @@ -36,7 +36,7 @@ namespace ModernKeePass.ViewModels } } - public NewVm(IMediator mediator, IRecentProxy recent, ISettingsProxy settings, INavigationService navigation) : base(recent) + public NewVm(IMediator mediator, IRecentProxy recent, ISettingsProxy settings, INavigationService navigation) { _mediator = mediator; _settings = settings; diff --git a/WinAppCommon/ViewModels/OpenVm.cs b/WinAppCommon/ViewModels/OpenVm.cs index fac6495..57674cb 100644 --- a/WinAppCommon/ViewModels/OpenVm.cs +++ b/WinAppCommon/ViewModels/OpenVm.cs @@ -1,13 +1,10 @@ -using System.Threading.Tasks; -using GalaSoft.MvvmLight; -using ModernKeePass.Application.Common.Interfaces; +using GalaSoft.MvvmLight; using ModernKeePass.Domain.Dtos; namespace ModernKeePass.ViewModels { public class OpenVm: ViewModelBase { - private readonly IRecentProxy _recent; private string _name; private string _path; private string _token; @@ -31,23 +28,12 @@ namespace ModernKeePass.ViewModels private set { Set(() => Path, ref _path, value); } } - public OpenVm(IRecentProxy recent) - { - _recent = recent; - } - - public async Task OpenFile(FileInfo file) + public void OpenFile(FileInfo file) { Token = file.Id; Name = file.Name; Path = file.Path; RaisePropertyChanged(nameof(IsFileSelected)); - await AddToRecentList(file); - } - - private async Task AddToRecentList(FileInfo file) - { - await _recent.Add(file); } } } diff --git a/WinAppCommon/ViewModels/RecentVm.cs b/WinAppCommon/ViewModels/RecentVm.cs index 46b618d..c855fbc 100644 --- a/WinAppCommon/ViewModels/RecentVm.cs +++ b/WinAppCommon/ViewModels/RecentVm.cs @@ -1,17 +1,15 @@ using System.Collections.ObjectModel; using System.Linq; -using System.Threading.Tasks; using System.Windows.Input; using GalaSoft.MvvmLight; using GalaSoft.MvvmLight.Command; -using Messages; using ModernKeePass.Application.Common.Interfaces; using ModernKeePass.Domain.Interfaces; using ModernKeePass.ViewModels.ListItems; namespace ModernKeePass.ViewModels { - public class RecentVm : ViewModelBase, IHasSelectableObject + public class RecentVm : ObservableObject, IHasSelectableObject { private readonly IRecentProxy _recent; private ISelectableModel _selectedItem; @@ -52,15 +50,8 @@ namespace ModernKeePass.ViewModels RecentItems = new ObservableCollection(recentItems); if (RecentItems.Count > 0) SelectedItem = RecentItems[0]; - - MessengerInstance.Register(this, async action => await UpdateAccessTime(action.Token)); } - private async Task UpdateAccessTime(string token) - { - await _recent.Get(token, true); - } - private void ClearAll() { _recent.ClearAll();