Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasReitberger committed Apr 8, 2024
1 parent c46ec2e commit 1218706
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
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
6 changes: 5 additions & 1 deletion src/MauiSettings/Utilities/MauiSettingNameFormater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ internal class MauiSettingNameFormater
public const string Version = "v1";
public const string NamePrefix = "AR";
#endregion
public static string GetFullSettingName(Type settingsType, MemberInfo mi, MauiSettingBaseAttribute baseSettingAttr)

#region Methods
public static string GetFullSettingName(Type? settingsType, MemberInfo mi, MauiSettingBaseAttribute? baseSettingAttr)
{
string name;
if (string.IsNullOrEmpty(baseSettingAttr?.Name))
Expand All @@ -30,5 +32,7 @@ public static string GetFullSettingName(Type settingsType, MemberInfo mi, MauiSe
}
return $"{NamePrefix}_{Version}_{name}{(settingsType == null ? "" : $"_{settingsType}")}";
}
public static string GetSettingsNamePrefix() => $"{NamePrefix}_{Version}";
#endregion
}
}

0 comments on commit 1218706

Please sign in to comment.