Added the option to close DB without saving

Changed the way recent files are retrieved
Stopped showing the DB Closed exception on suspend
Reordering entries works
Moved code from infra to application
Cleanup
This commit is contained in:
Geoffroy BONNEVILLE
2020-04-09 19:43:06 +02:00
parent 14fd4634db
commit d972b6cb5a
35 changed files with 141 additions and 100 deletions

View File

@@ -45,18 +45,13 @@ namespace ModernKeePass.Infrastructure.KeePass
public string RecycleBinId
{
get
get
{
if (_pwDatabase.RecycleBinEnabled && !_pwDatabase.RecycleBinUuid.Equals(PwUuid.Zero))
{
return _pwDatabase.RecycleBinUuid.ToHexString();
}
return null;
return _pwDatabase.RecycleBinUuid.ToHexString();
}
set
{
_pwDatabase.RecycleBinUuid = value != null ? BuildIdFromString(value) : PwUuid.Zero;
_pwDatabase.RecycleBinUuid = BuildIdFromString(value);
_pwDatabase.RecycleBinChanged = _dateTime.Now;
}
}
@@ -152,7 +147,7 @@ namespace ModernKeePass.Infrastructure.KeePass
});
}
public async Task<byte[]> SaveDatabase(byte[] newFileContents)
public async Task<byte[]> SaveDatabase(byte[] newFileContents)
{
return await Task.Run(() =>
{
@@ -176,12 +171,15 @@ namespace ModernKeePass.Infrastructure.KeePass
});
}
public async Task InsertEntry(string parentGroupId, string entryId, int index)
public async Task MoveEntry(string parentGroupId, string entryId, int index)
{
await Task.Run(() =>
{
var parentPwGroup = _pwDatabase.RootGroup.FindGroup(BuildIdFromString(parentGroupId), true);
var pwEntry = _pwDatabase.RootGroup.FindEntry(BuildIdFromString(entryId), true);
var currentIndex = (uint)parentPwGroup.Entries.IndexOf(pwEntry);
parentPwGroup.Entries.RemoveAt(currentIndex);
parentPwGroup.Entries.Insert((uint)index, pwEntry);
});
}

View File

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

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Windows.Storage.AccessCache;
using ModernKeePass.Application.Common.Interfaces;
@@ -10,51 +11,52 @@ 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<FileInfo> Get(string token, bool updateAccessTime = false)
{
var file = await _mru.GetFileAsync(token, updateAccessTime ? AccessCacheOptions.None : AccessCacheOptions.SuppressAccessTimeUpdate);
StorageApplicationPermissions.FutureAccessList.AddOrReplace(token, file);
return new FileInfo
try
{
Id = token,
Name = file.DisplayName,
Path = file.Path
};
var file = await _mru.GetFileAsync(token,
updateAccessTime ? AccessCacheOptions.None : AccessCacheOptions.SuppressAccessTimeUpdate).AsTask().ConfigureAwait(false);
_fal.AddOrReplace(token, file);
return new FileInfo
{
Id = token,
Name = file.DisplayName,
Path = file.Path
};
}
catch (Exception)
{
_mru.Remove(token);
return null;
}
}
public async Task<IEnumerable<FileInfo>> GetAll()
public IEnumerable<FileInfo> GetAll()
{
var result = new List<FileInfo>();
foreach (var entry in _mru.Entries)
return _mru.Entries.Select(e => new FileInfo
{
try
{
var recentItem = await Get(entry.Token);
result.Add(recentItem);
}
catch (Exception)
{
_mru.Remove(entry.Token);
}
}
return result;
Id = e.Token,
Name = e.Metadata?.Substring(e.Metadata.LastIndexOf("\\", StringComparison.OrdinalIgnoreCase) + 1),
Path = e.Metadata
});
}
public async Task Add(FileInfo recentItem)
{
var file = await StorageApplicationPermissions.FutureAccessList.GetFileAsync(recentItem.Id);
_mru.Add(file);
var file = await _fal.GetFileAsync(recentItem.Id).AsTask();
_mru.Add(file, file.Path);
}
public void ClearAll()
{
for (var i = _mru.Entries.Count; i > 0; i--)
foreach (var entry in _mru.Entries)
{
var entry = _mru.Entries[i];
StorageApplicationPermissions.FutureAccessList.Remove(entry.Token);
if (_fal.ContainsItem(entry.Token)) _fal.Remove(entry.Token);
_mru.Remove(entry.Token);
}
}