Skip to content

Commit

Permalink
Merge pull request #36 from AndreasReitberger/35-nullcheck-for-defaul…
Browse files Browse the repository at this point in the history
…t-value

Added `nullcheck` at `GetTypeDefaultValue`
  • Loading branch information
AndreasReitberger authored Mar 22, 2024
2 parents ed7f37e + 8f4de16 commit ea81ace
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
2 changes: 2 additions & 0 deletions src/MauiSettings/Helper/MauiSettingsObjectHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ public static object GetDefaultValue(MauiSettingBaseAttribute attr, Type setting
if (attr != null && attr.DefaultValueInUse)
{
object obj = attr.DefaultValue;
if (obj is null)
return GetTypeDefaultValue(settingType);
if (obj?.GetType() != settingType)
{
if (obj.GetType() == typeof(string))
Expand Down
26 changes: 12 additions & 14 deletions src/MauiSettings/MauiSettingsGeneric.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
using System.Collections.Concurrent;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security;

namespace AndreasReitberger.Maui
{
Expand Down Expand Up @@ -52,14 +50,14 @@ public MauiSettingsGeneric(SO settingsObject)
_settingsObject = settingsObject;
}
/*
public MauiSettingsGeneric(string key)
public MauiSettingsGeneric(string settingsKey)
{
_passPhrase = key;
_passPhrase = settingsKey;
}
public MauiSettingsGeneric(SO settingsObject, string key)
public MauiSettingsGeneric(SO settingsObject, string settingsKey)
{
_settingsObject = settingsObject;
_passPhrase = key;
_passPhrase = settingsKey;
}
*/
#endregion
Expand Down Expand Up @@ -132,10 +130,10 @@ public static Task LoadSettingsAsync(Dictionary<string, Tuple<object, Type>> dic
await LoadSettingsAsync(settings: SettingsObject, dictionary: dictionary, save: save, key: key);
});

public static Task LoadSettingsAsync(string key, Tuple<object, Type> data, bool save = true, string? skey = null)
public static Task LoadSettingsAsync(string settingsKey, Tuple<object, Type> data, bool save = true, string? key = null)
=> Task.Run(async delegate
{
await LoadSettingsAsync(settings: SettingsObject, dictionary: new() { { key, data } }, save: save, key: skey);
await LoadSettingsAsync(settings: SettingsObject, dictionary: new() { { settingsKey, data } }, save: save, key: key);
});

public static Task LoadSettingsAsync(object settings, Dictionary<string, Tuple<object, Type>> dictionary, bool save = true, string? key = null)
Expand Down Expand Up @@ -366,15 +364,15 @@ static async Task GetMetaFromDictionaryAsync(object settings, Dictionary<string,
foreach (MemberInfo mInfo in declaredMembers)
{
bool useValueFromSettingsInfo = false;
// Try to find the matching key
// Try to find the matching settingsKey
KeyValuePair<string, Tuple<object, Type>>? keyPair = dictionary?.FirstOrDefault(keypair =>
keypair.Key.EndsWith(mInfo.Name
//?.Replace("get_", string.Empty)
));
if (keyPair?.Key != null)
{
useValueFromSettingsInfo = true;
// If a matching key was found, prepare the settingsInfo with the loaded data
// If a matching settingsKey was found, prepare the settingsInfo with the loaded data
settingsInfo = new()
{
Name = mInfo.Name?.Replace("get_", string.Empty),
Expand Down Expand Up @@ -549,12 +547,12 @@ List<MauiSettingAttribute> settingBaseAttributes
case MauiSettingsTarget.ICloud:
if (settingsInfo.Value != null)
{
// If there is a default value, do not delete the key. Instead write the default value
// If there is a default value, do not delete the settingsKey. Instead write the default value
ICloudStoreManager.SetValue(settingsInfo.Name, settingsInfo.Value?.ToString());
}
else
{
// Otherwise delete the key from the cloud storage
// Otherwise delete the settingsKey from the cloud storage
ICloudStoreManager.DeleteValue(settingsInfo.Name);
}
break;
Expand Down Expand Up @@ -752,12 +750,12 @@ List<MauiSettingAttribute> settingBaseAttributes
case MauiSettingsTarget.ICloud:
if (settingsInfo.Value != null)
{
// If there is a default value, do not delete the key. Instead write the default value
// If there is a default value, do not delete the settingsKey. Instead write the default value
ICloudStoreManager.SetValue(settingsInfo.Name, settingsInfo.Value?.ToString());
}
else
{
// Otherwise delete the key from the cloud storage
// Otherwise delete the settingsKey from the cloud storage
ICloudStoreManager.DeleteValue(settingsInfo.Name);
}
break;
Expand Down

0 comments on commit ea81ace

Please sign in to comment.