mirror of
https://github.com/wismna/ModernKeePass.git
synced 2025-10-03 23:50:18 -04:00
Created a Settings Service
Created a Recent Service Created a Resources Service Code refactor Updated tests
This commit is contained in:
@@ -26,6 +26,8 @@ namespace ModernKeePass.Services
|
||||
Opened = 2
|
||||
}
|
||||
private readonly PwDatabase _pwDatabase = new PwDatabase();
|
||||
private readonly ISettings _settings;
|
||||
private readonly IResource _resource;
|
||||
private StorageFile _databaseFile;
|
||||
private GroupVm _recycleBin;
|
||||
|
||||
@@ -77,7 +79,15 @@ namespace ModernKeePass.Services
|
||||
get { return _pwDatabase.KdfParameters; }
|
||||
set { _pwDatabase.KdfParameters = value; }
|
||||
}
|
||||
|
||||
|
||||
public DatabaseService() : this(new SettingsService())
|
||||
{ }
|
||||
|
||||
public DatabaseService(ISettings settings)
|
||||
{
|
||||
_settings = settings;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Open a KeePass database
|
||||
/// </summary>
|
||||
@@ -99,8 +109,8 @@ namespace ModernKeePass.Services
|
||||
_pwDatabase.New(ioConnection, key);
|
||||
|
||||
//Get settings default values
|
||||
if (SettingsService.GetSetting<bool>("Sample")) CreateSampleData();
|
||||
var fileFormat = SettingsService.GetSetting<string>("DefaultFileFormat");
|
||||
if (_settings.GetSetting<bool>("Sample")) CreateSampleData();
|
||||
var fileFormat = _settings.GetSetting<string>("DefaultFileFormat");
|
||||
switch (fileFormat)
|
||||
{
|
||||
case "4":
|
||||
|
@@ -1,60 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Windows.Storage.Pickers;
|
||||
using Windows.UI.Popups;
|
||||
using ModernKeePass.Exceptions;
|
||||
using ModernKeePass.Interfaces;
|
||||
|
||||
namespace ModernKeePass.Services
|
||||
{
|
||||
public static class MessageDialogService
|
||||
{
|
||||
public static async void ShowActionDialog(string title, string contentText, string actionButtonText, string cancelButtonText, UICommandInvokedHandler action)
|
||||
{
|
||||
// Create the message dialog and set its content
|
||||
var messageDialog = new MessageDialog(contentText, title);
|
||||
|
||||
// Add commands and set their callbacks; both buttons use the same callback function instead of inline event handlers
|
||||
messageDialog.Commands.Add(new UICommand(actionButtonText, action));
|
||||
messageDialog.Commands.Add(new UICommand(cancelButtonText));
|
||||
|
||||
// Set the command that will be invoked by default
|
||||
messageDialog.DefaultCommandIndex = 1;
|
||||
|
||||
// Set the command to be invoked when escape is pressed
|
||||
messageDialog.CancelCommandIndex = 1;
|
||||
|
||||
// Show the message dialog
|
||||
await messageDialog.ShowAsync();
|
||||
}
|
||||
|
||||
public static void SaveErrorDialog(SaveException exception, IDatabase database)
|
||||
{
|
||||
ShowActionDialog("Save error", exception.InnerException.Message, "Save as", "Discard", async command =>
|
||||
{
|
||||
var savePicker = new FileSavePicker
|
||||
{
|
||||
SuggestedStartLocation = PickerLocationId.DocumentsLibrary,
|
||||
SuggestedFileName = $"{database.DatabaseFile.DisplayName} - copy"
|
||||
};
|
||||
savePicker.FileTypeChoices.Add("KeePass 2.x database", new List<string> { ".kdbx" });
|
||||
|
||||
var file = await savePicker.PickSaveFileAsync();
|
||||
if (file != null) database.Save(file);
|
||||
});
|
||||
}
|
||||
|
||||
public static async void ShowErrorDialog(Exception exception)
|
||||
{
|
||||
if (exception == null) return;
|
||||
// Create the message dialog and set its content
|
||||
var messageDialog = new MessageDialog(exception.Message, "Error occured");
|
||||
|
||||
// Add commands and set their callbacks; both buttons use the same callback function instead of inline event handlers
|
||||
messageDialog.Commands.Add(new UICommand("OK"));
|
||||
|
||||
// Show the message dialog
|
||||
await messageDialog.ShowAsync();
|
||||
}
|
||||
}
|
||||
}
|
45
ModernKeePass/Services/RecentService.cs
Normal file
45
ModernKeePass/Services/RecentService.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Threading.Tasks;
|
||||
using ModernKeePass.Interfaces;
|
||||
using Windows.Storage;
|
||||
using Windows.Storage.AccessCache;
|
||||
using ModernKeePass.ViewModels;
|
||||
|
||||
namespace ModernKeePass.Services
|
||||
{
|
||||
public class RecentService : IRecent
|
||||
{
|
||||
private readonly StorageItemMostRecentlyUsedList _mru = StorageApplicationPermissions.MostRecentlyUsedList;
|
||||
|
||||
public int EntryCount => _mru.Entries.Count;
|
||||
|
||||
public ObservableCollection<IRecentItem> GetAllFiles(bool removeIfNonExistant = true)
|
||||
{
|
||||
var result = new ObservableCollection<IRecentItem>();
|
||||
foreach (var entry in _mru.Entries)
|
||||
{
|
||||
try
|
||||
{
|
||||
var file = _mru.GetFileAsync(entry.Token, AccessCacheOptions.SuppressAccessTimeUpdate).GetAwaiter().GetResult();
|
||||
result.Add(new RecentItemVm(entry.Token, entry.Metadata, file));
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
if (removeIfNonExistant) _mru.Remove(entry.Token);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public void Add(IStorageItem file, string metadata)
|
||||
{
|
||||
_mru.Add(file, metadata);
|
||||
}
|
||||
|
||||
public async Task<IStorageItem> GetFileAsync(string token)
|
||||
{
|
||||
return await _mru.GetFileAsync(token);
|
||||
}
|
||||
}
|
||||
}
|
17
ModernKeePass/Services/ResourcesService.cs
Normal file
17
ModernKeePass/Services/ResourcesService.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using Windows.ApplicationModel.Resources;
|
||||
using ModernKeePass.Interfaces;
|
||||
|
||||
namespace ModernKeePass.Services
|
||||
{
|
||||
public class ResourcesService: IResource
|
||||
{
|
||||
private const string ResourceFileName = "CodeBehind";
|
||||
private readonly ResourceLoader _resourceLoader = ResourceLoader.GetForCurrentView();
|
||||
|
||||
public string GetResourceValue(string key)
|
||||
{
|
||||
var resource = _resourceLoader.GetString($"/{ResourceFileName}/{key}");
|
||||
return resource;
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,15 +1,19 @@
|
||||
using System;
|
||||
using Windows.Foundation.Collections;
|
||||
using Windows.Storage;
|
||||
using ModernKeePass.Interfaces;
|
||||
|
||||
namespace ModernKeePass.Services
|
||||
{
|
||||
public class SettingsService
|
||||
public class SettingsService : ISettings
|
||||
{
|
||||
public static T GetSetting<T>(string property)
|
||||
private readonly IPropertySet _values = ApplicationData.Current.LocalSettings.Values;
|
||||
|
||||
public T GetSetting<T>(string property)
|
||||
{
|
||||
try
|
||||
{
|
||||
return (T)Convert.ChangeType(ApplicationData.Current.LocalSettings.Values[property], typeof(T));
|
||||
return (T)Convert.ChangeType(_values[property], typeof(T));
|
||||
}
|
||||
catch (InvalidCastException)
|
||||
{
|
||||
@@ -17,12 +21,11 @@ namespace ModernKeePass.Services
|
||||
}
|
||||
}
|
||||
|
||||
public static void PutSetting<T>(string property, T value)
|
||||
public void PutSetting<T>(string property, T value)
|
||||
{
|
||||
var localSettings = ApplicationData.Current.LocalSettings;
|
||||
if (localSettings.Values.ContainsKey(property))
|
||||
localSettings.Values[property] = value;
|
||||
else localSettings.Values.Add(property, value);
|
||||
if (_values.ContainsKey(property))
|
||||
_values[property] = value;
|
||||
else _values.Add(property, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,49 +0,0 @@
|
||||
using System;
|
||||
using Windows.Data.Json;
|
||||
using Windows.Data.Xml.Dom;
|
||||
using Windows.UI.Notifications;
|
||||
using ModernKeePass.Interfaces;
|
||||
using ModernKeePass.ViewModels;
|
||||
|
||||
namespace ModernKeePass.Services
|
||||
{
|
||||
public static class ToastNotificationService
|
||||
{
|
||||
public static void ShowMovedToast(IPwEntity entity, string action, string text)
|
||||
{
|
||||
var entityType = entity is GroupVm ? "Group" : "Entry";
|
||||
var notificationXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02);
|
||||
var toastElements = notificationXml.GetElementsByTagName("text");
|
||||
toastElements[0].AppendChild(notificationXml.CreateTextNode($"{action} {entityType} {entity.Name}"));
|
||||
toastElements[1].AppendChild(notificationXml.CreateTextNode(text));
|
||||
var toastNode = notificationXml.SelectSingleNode("/toast");
|
||||
|
||||
var launch = new JsonObject
|
||||
{
|
||||
{"entityType", JsonValue.CreateStringValue(entityType)},
|
||||
{"entityId", JsonValue.CreateStringValue(entity.Id)}
|
||||
};
|
||||
((XmlElement)toastNode)?.SetAttribute("launch", launch.Stringify());
|
||||
|
||||
var toast = new ToastNotification(notificationXml)
|
||||
{
|
||||
ExpirationTime = DateTime.Now.AddSeconds(5)
|
||||
};
|
||||
ToastNotificationManager.CreateToastNotifier().Show(toast);
|
||||
}
|
||||
|
||||
public static void ShowGenericToast(string title, string text)
|
||||
{
|
||||
var notificationXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02);
|
||||
var toastElements = notificationXml.GetElementsByTagName("text");
|
||||
toastElements[0].AppendChild(notificationXml.CreateTextNode(title));
|
||||
toastElements[1].AppendChild(notificationXml.CreateTextNode(text));
|
||||
|
||||
var toast = new ToastNotification(notificationXml)
|
||||
{
|
||||
ExpirationTime = DateTime.Now.AddSeconds(5)
|
||||
};
|
||||
ToastNotificationManager.CreateToastNotifier().Show(toast);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user