Skip to content

Commit

Permalink
Update logic for saving when there is no write permission #137
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruben2776 committed Apr 11, 2024
1 parent 1edb313 commit e9e272e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 31 deletions.
5 changes: 2 additions & 3 deletions src/PicView.Avalonia/Keybindings/KeybindingsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Diagnostics;
using System.Text.Json;
using System.Text.Json.Serialization;
using PicView.Core.Keybindings;

namespace PicView.Avalonia.Keybindings;

Expand Down Expand Up @@ -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<string, string>), 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)
{
Expand Down
12 changes: 6 additions & 6 deletions src/PicView.Core/Config/SettingsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,14 @@ public static async Task<bool> 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);
Expand All @@ -135,11 +140,6 @@ public static async Task<bool> SaveSettingsAsync()
return false;
}
}
catch (Exception ex)
{
Trace.WriteLine($"{nameof(SaveSettingsAsync)} error saving settings:\n {ex.Message}");
return false;
}
return true;
}

Expand Down
25 changes: 25 additions & 0 deletions src/PicView.Core/Keybindings/KeybindingFunctions.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
32 changes: 10 additions & 22 deletions src/PicView.WPF/Shortcuts/CustomKeybindings.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -117,27 +117,15 @@ internal static async Task UpdateKeybindings(string json)
/// </summary>
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()
Expand Down

0 comments on commit e9e272e

Please sign in to comment.