Skip to content

Commit

Permalink
Merge pull request #43 from AndreasReitberger/42-catch-and-process-en…
Browse files Browse the repository at this point in the history
…cryption-errors

Add `Event` for `EncryptionErrors`
  • Loading branch information
AndreasReitberger authored Apr 8, 2024
2 parents 2e35cd1 + 1218706 commit f9d5997
Show file tree
Hide file tree
Showing 5 changed files with 205 additions and 43 deletions.
10 changes: 10 additions & 0 deletions src/MauiSettings/Enums/MauiSettingsResults.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace AndreasReitberger.Maui.Enums
{
public enum MauiSettingsResults
{
Success,
Skipped,
EncryptionError,
Failed,
}
}
8 changes: 8 additions & 0 deletions src/MauiSettings/Events/EncryptionErrorEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace AndreasReitberger.Maui.Events
{
public partial class EncryptionErrorEventArgs : EventArgs
{
public Exception? Exception { get; set; }
public string Key { get; set; } = string.Empty;
}
}
9 changes: 5 additions & 4 deletions src/MauiSettings/Helper/MauiSettingsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal class MauiSettingsHelper
* - String
* - DateTime
*/
public static T GetSettingsValue<T>(string key, T defaultValue)
public static T? GetSettingsValue<T>(string key, T defaultValue)
{
object? returnValue = null;
try
Expand Down Expand Up @@ -73,17 +73,18 @@ public static T GetSettingsValue<T>(string key, T defaultValue)
//return (T)Convert.ChangeType(returnValue, typeof(T));
}

public static T ChangeSettingsType<T>(object settingsValue, T defaultValue) => (T)Convert.ChangeType(settingsValue, typeof(T));
public static T? ChangeSettingsType<T>(object settingsValue, T defaultValue) => (T)Convert.ChangeType(settingsValue, typeof(T)) ?? default;

// Docs: https://docs.microsoft.com/en-us/dotnet/maui/platform-integration/storage/secure-storage?tabs=ios
// Only string is allowed for secure storage
public static async Task<string> GetSecureSettingsValueAsync(string key, string defaultValue)
public static async Task<string> GetSecureSettingsValueAsync(string key, string? defaultValue)
{
defaultValue ??= string.Empty;
string? settingsObject = await SecureStorage.Default.GetAsync(key);
return settingsObject == null ? defaultValue : settingsObject;
}

public static void SetSettingsValue(string key, object value)
public static void SetSettingsValue(string key, object? value)
{
switch (value)
{
Expand Down
Loading

0 comments on commit f9d5997

Please sign in to comment.