From e9e272ee476e601e2a61625eedb4a1b665b49af5 Mon Sep 17 00:00:00 2001 From: Ruben Date: Thu, 11 Apr 2024 14:39:23 +0200 Subject: [PATCH] Update logic for saving when there is no write permission #137 --- .../Keybindings/KeybindingsHelper.cs | 5 ++- src/PicView.Core/Config/SettingsHelper.cs | 12 +++---- .../Keybindings/KeybindingFunctions.cs | 25 +++++++++++++++ .../Shortcuts/CustomKeybindings.cs | 32 ++++++------------- 4 files changed, 43 insertions(+), 31 deletions(-) create mode 100644 src/PicView.Core/Keybindings/KeybindingFunctions.cs diff --git a/src/PicView.Avalonia/Keybindings/KeybindingsHelper.cs b/src/PicView.Avalonia/Keybindings/KeybindingsHelper.cs index 1844b6c3..05c5eb7d 100644 --- a/src/PicView.Avalonia/Keybindings/KeybindingsHelper.cs +++ b/src/PicView.Avalonia/Keybindings/KeybindingsHelper.cs @@ -4,6 +4,7 @@ using System.Diagnostics; using System.Text.Json; using System.Text.Json.Serialization; +using PicView.Core.Keybindings; namespace PicView.Avalonia.Keybindings; @@ -117,9 +118,7 @@ public static async Task UpdateKeyBindingsFile() var json = JsonSerializer.Serialize( CustomShortcuts.ToDictionary(kvp => kvp.Key.ToString(), kvp => GetFunctionNameByFunction(kvp.Value)), typeof(Dictionary), SourceGenerationContext.Default); - - var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config/keybindings.json"); - await File.WriteAllTextAsync(path, json).ConfigureAwait(false); + await KeybindingFunctions.SaveKeyBindingsFile(json).ConfigureAwait(false); } catch (Exception exception) { diff --git a/src/PicView.Core/Config/SettingsHelper.cs b/src/PicView.Core/Config/SettingsHelper.cs index 10af5ae9..580607e3 100644 --- a/src/PicView.Core/Config/SettingsHelper.cs +++ b/src/PicView.Core/Config/SettingsHelper.cs @@ -122,9 +122,14 @@ public static async Task SaveSettingsAsync() var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config/UserSettings.json"); await PerformSave(path).ConfigureAwait(false); } - catch (UnauthorizedAccessException ex) + catch (Exception ex) { var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Ruben2776/PicView/Config/UserSettings.json"); + if (!File.Exists(path)) + { + var fileInfo = new FileInfo(path); + fileInfo.Directory?.Create(); + } try { await PerformSave(path).ConfigureAwait(false); @@ -135,11 +140,6 @@ public static async Task SaveSettingsAsync() return false; } } - catch (Exception ex) - { - Trace.WriteLine($"{nameof(SaveSettingsAsync)} error saving settings:\n {ex.Message}"); - return false; - } return true; } diff --git a/src/PicView.Core/Keybindings/KeybindingFunctions.cs b/src/PicView.Core/Keybindings/KeybindingFunctions.cs new file mode 100644 index 00000000..64c0a0e7 --- /dev/null +++ b/src/PicView.Core/Keybindings/KeybindingFunctions.cs @@ -0,0 +1,25 @@ +namespace PicView.Core.Keybindings; + +public static class KeybindingFunctions +{ + public static async Task SaveKeyBindingsFile(string json) + { + try + { + var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config/keybindings.json"); + await using var writer = new StreamWriter(path); + await writer.WriteAsync(json).ConfigureAwait(false); + } + catch (Exception) + { + var newPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Ruben2776/PicView/Config/keybindings.json"); + if (!File.Exists(newPath)) + { + var fileInfo = new FileInfo(newPath); + fileInfo.Directory?.Create(); + } + await using var newWriter = new StreamWriter(newPath); + await newWriter.WriteAsync(json).ConfigureAwait(false); + } + } +} \ No newline at end of file diff --git a/src/PicView.WPF/Shortcuts/CustomKeybindings.cs b/src/PicView.WPF/Shortcuts/CustomKeybindings.cs index d875150a..2862d8f3 100644 --- a/src/PicView.WPF/Shortcuts/CustomKeybindings.cs +++ b/src/PicView.WPF/Shortcuts/CustomKeybindings.cs @@ -1,5 +1,5 @@ -using PicView.WPF.UILogic; -using System.Diagnostics; +using PicView.Core.Keybindings; +using PicView.WPF.UILogic; using System.IO; using System.Text.Json; using System.Windows.Input; @@ -117,27 +117,15 @@ internal static async Task UpdateKeybindings(string json) /// internal static async Task UpdateKeyBindingsFile() { - try - { - // Serialize the CustomShortcuts dictionary to JSON - var json = - JsonSerializer.Serialize(CustomShortcuts.ToDictionary(kvp => kvp.Key.ToString(), - kvp => UIHelper.GetFunctionNameByFunction(kvp.Value)), new JsonSerializerOptions - { - WriteIndented = true - }); + // Serialize the CustomShortcuts dictionary to JSON + var json = + JsonSerializer.Serialize(CustomShortcuts.ToDictionary(kvp => kvp.Key.ToString(), + kvp => UIHelper.GetFunctionNameByFunction(kvp.Value)), new JsonSerializerOptions + { + WriteIndented = true + }); - // Write the JSON to the keybindings.json file - var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config/keybindings.json"); - await File.WriteAllTextAsync(path, json).ConfigureAwait(false); - } - catch (Exception exception) - { -#if DEBUG - Trace.WriteLine($"{nameof(UpdateKeyBindingsFile)} exception:\n{exception.Message}"); -#endif - Tooltip.ShowTooltipMessage(exception.Message, true, TimeSpan.FromSeconds(5)); - } + await KeybindingFunctions.SaveKeyBindingsFile(json).ConfigureAwait(false); } internal static async Task SetDefaultKeybindings()