StorageFile client more intelligent

Save is working again
This commit is contained in:
Geoffroy BONNEVILLE
2020-04-24 16:16:48 +02:00
parent 3967db41b3
commit df973c5f62
5 changed files with 16 additions and 31 deletions

View File

@@ -1,5 +1,4 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks;
using ModernKeePass.Domain.Dtos; using ModernKeePass.Domain.Dtos;
namespace ModernKeePass.Application.Common.Interfaces namespace ModernKeePass.Application.Common.Interfaces
@@ -7,7 +6,6 @@ namespace ModernKeePass.Application.Common.Interfaces
public interface IRecentProxy public interface IRecentProxy
{ {
int EntryCount { get; } int EntryCount { get; }
Task<byte[]> Get(string token, bool updateAccessTime = true);
IEnumerable<FileInfo> GetAll(); IEnumerable<FileInfo> GetAll();
void ClearAll(); void ClearAll();
} }

View File

@@ -16,20 +16,18 @@ namespace ModernKeePass.Application.Database.Queries.OpenDatabase
{ {
private readonly IDatabaseProxy _database; private readonly IDatabaseProxy _database;
private readonly IFileProxy _file; private readonly IFileProxy _file;
private readonly IRecentProxy _recent;
public OpenDatabaseQueryHandler(IDatabaseProxy database, IFileProxy file, IRecentProxy recent) public OpenDatabaseQueryHandler(IDatabaseProxy database, IFileProxy file)
{ {
_database = database; _database = database;
_file = file; _file = file;
_recent = recent;
} }
public async Task Handle(OpenDatabaseQuery message) public async Task Handle(OpenDatabaseQuery message)
{ {
if (_database.IsDirty) throw new DatabaseOpenException(); if (_database.IsDirty) throw new DatabaseOpenException();
var file = await _recent.Get(message.FilePath); var file = await _file.OpenBinaryFile(message.FilePath);
var hasKeyFile = !string.IsNullOrEmpty(message.KeyFilePath); var hasKeyFile = !string.IsNullOrEmpty(message.KeyFilePath);
await _database.Open(file, new Credentials await _database.Open(file, new Credentials
{ {

View File

@@ -12,14 +12,14 @@ namespace ModernKeePass.Infrastructure.UWP
{ {
public async Task<byte[]> OpenBinaryFile(string path) public async Task<byte[]> OpenBinaryFile(string path)
{ {
var file = await StorageApplicationPermissions.FutureAccessList.GetFileAsync(path).AsTask(); var file = await GetFile(path);
var result = await FileIO.ReadBufferAsync(file).AsTask(); var result = await FileIO.ReadBufferAsync(file).AsTask();
return result.ToArray(); return result.ToArray();
} }
public async Task<IList<string>> OpenTextFile(string path) public async Task<IList<string>> OpenTextFile(string path)
{ {
var file = await StorageApplicationPermissions.FutureAccessList.GetFileAsync(path).AsTask(); var file = await GetFile(path);
var result = await FileIO.ReadLinesAsync(file).AsTask(); var result = await FileIO.ReadLinesAsync(file).AsTask();
return result; return result;
} }
@@ -31,8 +31,17 @@ namespace ModernKeePass.Infrastructure.UWP
public async Task WriteBinaryContentsToFile(string path, byte[] contents) public async Task WriteBinaryContentsToFile(string path, byte[] contents)
{ {
var file = await StorageApplicationPermissions.FutureAccessList.GetFileAsync(path).AsTask(); var file = await GetFile(path);
await FileIO.WriteBytesAsync(file, contents).AsTask(); await FileIO.WriteBytesAsync(file, contents).AsTask();
} }
private async Task<StorageFile> GetFile(string token)
{
if (StorageApplicationPermissions.MostRecentlyUsedList.ContainsItem(token))
return await StorageApplicationPermissions.MostRecentlyUsedList.GetFileAsync(token).AsTask();
if (StorageApplicationPermissions.FutureAccessList.ContainsItem(token))
return await StorageApplicationPermissions.FutureAccessList.GetFileAsync(token).AsTask();
return null;
}
} }
} }

View File

@@ -1,12 +1,9 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Storage.AccessCache; using Windows.Storage.AccessCache;
using ModernKeePass.Application.Common.Interfaces; using ModernKeePass.Application.Common.Interfaces;
using ModernKeePass.Domain.Dtos; using ModernKeePass.Domain.Dtos;
using Windows.Storage;
namespace ModernKeePass.Infrastructure.UWP namespace ModernKeePass.Infrastructure.UWP
{ {
@@ -15,24 +12,7 @@ namespace ModernKeePass.Infrastructure.UWP
private readonly StorageItemMostRecentlyUsedList _mru = StorageApplicationPermissions.MostRecentlyUsedList; private readonly StorageItemMostRecentlyUsedList _mru = StorageApplicationPermissions.MostRecentlyUsedList;
public int EntryCount => _mru.Entries.Count; public int EntryCount => _mru.Entries.Count;
public async Task<byte[]> Get(string token, bool updateAccessTime = true)
{
try
{
var file = await _mru.GetFileAsync(token,
updateAccessTime ? AccessCacheOptions.None : AccessCacheOptions.SuppressAccessTimeUpdate).AsTask().ConfigureAwait(false);
var result = await FileIO.ReadBufferAsync(file).AsTask();
return result.ToArray();
}
catch (Exception)
{
_mru.Remove(token);
return null;
}
}
public IEnumerable<FileInfo> GetAll() public IEnumerable<FileInfo> GetAll()
{ {
return _mru.Entries.Select(e => new FileInfo return _mru.Entries.Select(e => new FileInfo

View File

@@ -111,7 +111,7 @@ namespace ModernKeePass.ViewModels
Destination = destinationFrame, Destination = destinationFrame,
Parameter = databaseFile, Parameter = databaseFile,
SymbolIcon = Symbol.Page2, SymbolIcon = Symbol.Page2,
IsSelected = databaseFile != null && !database.IsOpen IsSelected = databaseFile != null
}, },
new MainMenuItemVm new MainMenuItemVm
{ {