Remove entry from mru when it does not exist anymore

This commit is contained in:
Geoffroy BONNEVILLE
2020-04-25 22:34:50 +02:00
parent 7778e45deb
commit 59ab43ca9c
7 changed files with 54 additions and 6 deletions

View File

@@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices.WindowsRuntime; using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks; using System.Threading.Tasks;
using Windows.Storage; using Windows.Storage;
@@ -38,10 +39,21 @@ namespace ModernKeePass.Infrastructure.UWP
private async Task<StorageFile> GetFile(string token) private async Task<StorageFile> GetFile(string token)
{ {
if (StorageApplicationPermissions.MostRecentlyUsedList.ContainsItem(token)) if (StorageApplicationPermissions.MostRecentlyUsedList.ContainsItem(token))
{
try
{
return await StorageApplicationPermissions.MostRecentlyUsedList.GetFileAsync(token).AsTask(); return await StorageApplicationPermissions.MostRecentlyUsedList.GetFileAsync(token).AsTask();
}
catch (Exception)
{
StorageApplicationPermissions.MostRecentlyUsedList.Remove(token);
throw new FileNotFoundException();
}
}
if (StorageApplicationPermissions.FutureAccessList.ContainsItem(token)) if (StorageApplicationPermissions.FutureAccessList.ContainsItem(token))
return await StorageApplicationPermissions.FutureAccessList.GetFileAsync(token).AsTask(); return await StorageApplicationPermissions.FutureAccessList.GetFileAsync(token).AsTask();
return null; throw new FileNotFoundException();
} }
} }
} }

View File

@@ -267,4 +267,10 @@
<data name="CompositeKeyOpenButtonLabel" xml:space="preserve"> <data name="CompositeKeyOpenButtonLabel" xml:space="preserve">
<value>Open</value> <value>Open</value>
</data> </data>
<data name="FileNotFoundDescription" xml:space="preserve">
<value>File does not exist anymore</value>
</data>
<data name="FileNotFoundTitle" xml:space="preserve">
<value>File not found</value>
</data>
</root> </root>

View File

@@ -267,4 +267,10 @@
<data name="CompositeKeyOpenButtonLabel" xml:space="preserve"> <data name="CompositeKeyOpenButtonLabel" xml:space="preserve">
<value>Open</value> <value>Open</value>
</data> </data>
<data name="FileNotFoundDescription" xml:space="preserve">
<value>Le fichier n'existe plus.</value>
</data>
<data name="FileNotFoundTitle" xml:space="preserve">
<value>Fichier manquant</value>
</data>
</root> </root>

View File

@@ -0,0 +1,7 @@
namespace Messages
{
public class FileNotFoundMessage
{
}
}

View File

@@ -3,13 +3,14 @@ using System.Linq;
using System.Windows.Input; using System.Windows.Input;
using GalaSoft.MvvmLight; using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command; using GalaSoft.MvvmLight.Command;
using Messages;
using ModernKeePass.Application.Common.Interfaces; using ModernKeePass.Application.Common.Interfaces;
using ModernKeePass.Domain.Interfaces; using ModernKeePass.Domain.Interfaces;
using ModernKeePass.ViewModels.ListItems; using ModernKeePass.ViewModels.ListItems;
namespace ModernKeePass.ViewModels namespace ModernKeePass.ViewModels
{ {
public class RecentVm : ObservableObject, IHasSelectableObject public class RecentVm : ViewModelBase, IHasSelectableObject
{ {
private readonly IRecentProxy _recent; private readonly IRecentProxy _recent;
private ISelectableModel _selectedItem; private ISelectableModel _selectedItem;
@@ -45,7 +46,12 @@ namespace ModernKeePass.ViewModels
{ {
_recent = recent; _recent = recent;
ClearAllCommand = new RelayCommand(ClearAll); ClearAllCommand = new RelayCommand(ClearAll);
PopulateRecentItems();
MessengerInstance.Register<FileNotFoundMessage>(this, _ => PopulateRecentItems());
}
private void PopulateRecentItems()
{
var recentItems = _recent.GetAll().Select(r => new RecentItemVm(r)); var recentItems = _recent.GetAll().Select(r => new RecentItemVm(r));
RecentItems = new ObservableCollection<RecentItemVm>(recentItems); RecentItems = new ObservableCollection<RecentItemVm>(recentItems);
if (RecentItems.Count > 0) if (RecentItems.Count > 0)

View File

@@ -1,4 +1,5 @@
using System; using System;
using System.IO;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using GalaSoft.MvvmLight; using GalaSoft.MvvmLight;
@@ -95,6 +96,7 @@ namespace ModernKeePass.ViewModels
private readonly IMediator _mediator; private readonly IMediator _mediator;
private readonly IResourceProxy _resource; private readonly IResourceProxy _resource;
private readonly INotificationService _notification;
private bool _hasPassword; private bool _hasPassword;
private bool _hasKeyFile; private bool _hasKeyFile;
private bool _isOpening; private bool _isOpening;
@@ -105,10 +107,11 @@ namespace ModernKeePass.ViewModels
private string _keyFileText; private string _keyFileText;
private string _openButtonLabel; private string _openButtonLabel;
public OpenDatabaseControlVm(IMediator mediator, IResourceProxy resource) public OpenDatabaseControlVm(IMediator mediator, IResourceProxy resource, INotificationService notification)
{ {
_mediator = mediator; _mediator = mediator;
_resource = resource; _resource = resource;
_notification = notification;
OpenDatabaseCommand = new RelayCommand<string>(async databaseFilePath => await TryOpenDatabase(databaseFilePath), _ => IsValid); OpenDatabaseCommand = new RelayCommand<string>(async databaseFilePath => await TryOpenDatabase(databaseFilePath), _ => IsValid);
_keyFileText = _resource.GetResourceValue("CompositeKeyDefaultKeyFile"); _keyFileText = _resource.GetResourceValue("CompositeKeyDefaultKeyFile");
_openButtonLabel = _resource.GetResourceValue("CompositeKeyOpenButtonLabel"); _openButtonLabel = _resource.GetResourceValue("CompositeKeyOpenButtonLabel");
@@ -153,6 +156,13 @@ namespace ModernKeePass.ViewModels
if (HasKeyFile) errorMessage.AppendLine(_resource.GetResourceValue("CompositeKeyErrorUserKeyFile")); if (HasKeyFile) errorMessage.AppendLine(_resource.GetResourceValue("CompositeKeyErrorUserKeyFile"));
UpdateStatus(errorMessage.ToString(), StatusTypes.Error); UpdateStatus(errorMessage.ToString(), StatusTypes.Error);
} }
catch (FileNotFoundException)
{
_notification.Show(
$"{_resource.GetResourceValue("FileNotFoundTitle")}",
$"{_resource.GetResourceValue("FileNotFoundDescription")}");
MessengerInstance.Send(new FileNotFoundMessage());
}
catch (Exception e) catch (Exception e)
{ {
var error = $"{_resource.GetResourceValue("CompositeKeyErrorOpen")}{e.Message}"; var error = $"{_resource.GetResourceValue("CompositeKeyErrorOpen")}{e.Message}";

View File

@@ -36,6 +36,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Messages\DatabaseClosedMessage.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Messages\DatabaseClosedMessage.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Messages\DatabaseOpenedMessage.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Messages\DatabaseOpenedMessage.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Messages\DatabaseOpeningMessage.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Messages\DatabaseOpeningMessage.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Messages\FileNotFoundMessage.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Messages\NavigateToPageMessage.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Messages\NavigateToPageMessage.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Messages\SaveErrorMessage.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Messages\SaveErrorMessage.cs" />
<Compile Include="$(MSBuildThisFileDirectory)TemplateSelectors\FirstItemDataTemplateSelector.cs" /> <Compile Include="$(MSBuildThisFileDirectory)TemplateSelectors\FirstItemDataTemplateSelector.cs" />