Skip to content

Commit

Permalink
Merge branch 'main' into 17-migrate-to-net8
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasReitberger authored Nov 14, 2023
2 parents 68e4809 + 0653703 commit c2e6bdd
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 46 deletions.
80 changes: 44 additions & 36 deletions src/MauiSettings/Helper/MauiSettingsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,43 +20,51 @@ internal class MauiSettingsHelper
public static T GetSettingsValue<T>(string key, T defaultValue)
{
object returnValue = null;
switch (defaultValue)
try
{
case bool b:
returnValue = Preferences.Get(key, b);
break;
case double d:
returnValue = Preferences.Get(key, d);
break;
case int i:
returnValue = Preferences.Get(key, i);
break;
case float f:
returnValue = Preferences.Get(key, f);
break;
case long l:
returnValue = Preferences.Get(key, l);
break;
case string s:
returnValue = Preferences.Get(key, s);
break;
case DateTime dt:
returnValue = Preferences.Get(key, dt);
break;
default:
// For all other types try to serialize it as JSON
string jsonString = Preferences.Get(key, string.Empty) ?? string.Empty;
if (defaultValue == null)
{
// In this case it's unkown to what data type the string should be deserialized.
// So just return the string as it is to avoid exceptions while converting.
returnValue = jsonString;
}
else
{
returnValue = JsonConvert.DeserializeObject<T>(jsonString);
}
break;
switch (defaultValue)
{
case bool b:
returnValue = Preferences.Get(key, b);
break;
case double d:
returnValue = Preferences.Get(key, d);
break;
case int i:
returnValue = Preferences.Get(key, i);
break;
case float f:
returnValue = Preferences.Get(key, f);
break;
case long l:
returnValue = Preferences.Get(key, l);
break;
case string s:
returnValue = Preferences.Get(key, s);
break;
case DateTime dt:
returnValue = Preferences.Get(key, dt);
break;
default:
// For all other types try to serialize it as JSON
string jsonString = Preferences.Get(key, string.Empty) ?? string.Empty;
if (defaultValue == null)
{
// In this case it's unkown to what data type the string should be deserialized.
// So just return the string as it is to avoid exceptions while converting.
returnValue = jsonString;
}
else
{
returnValue = JsonConvert.DeserializeObject<T>(jsonString);
}
break;
}
}
catch (Exception ex)
{
SetSettingsValue(key, defaultValue);
return defaultValue;
}
return ChangeSettingsType<T>(returnValue, defaultValue);
//return (T)Convert.ChangeType(returnValue, typeof(T));
Expand Down
25 changes: 15 additions & 10 deletions src/MauiSettings/Helper/MauiSettingsObjectHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,23 +62,30 @@ public static void SetSettingValue(MemberInfo memberInfo, object settings, objec
}
catch (Exception)
{

//throw new NotSupportedException($"MauiSettings: The type '{memberInfo.GetType()}' is not supported for the field: {memberInfo.Name}", exc);
}
throw new NotSupportedException($"MauiSettings: The type '{memberInfo.GetType()}' is not supported for the field: {memberInfo.Name}");

}

public static Type GetSettingType(MemberInfo memberInfo)
{
if (memberInfo is FieldInfo fieldInfo)
try
{
return fieldInfo.FieldType;
if (memberInfo is FieldInfo fieldInfo)
{
return fieldInfo.FieldType;
}
if (memberInfo is PropertyInfo propertyInfo)
{
return propertyInfo.PropertyType;
}
else
return default;
}

if (memberInfo is PropertyInfo propertyInfo)
catch (Exception exc)
{
return propertyInfo.PropertyType;
throw new NotSupportedException($"MauiSettings: The type '{memberInfo.GetType()}' is not supported for the field: {memberInfo.Name}", exc);
}
throw new NotSupportedException($"MauiSettings: The type '{memberInfo.GetType()}' is not supported for the field: {memberInfo.Name}");
}

public static object GetTypeDefaultValue(Type type)
Expand Down Expand Up @@ -107,7 +114,6 @@ public static object GetDefaultValue(MauiSettingBaseAttribute attr, Type setting
}
return obj;
}

return GetTypeDefaultValue(settingType);
}
catch (Exception)
Expand All @@ -124,7 +130,6 @@ public static object GetConvertedTypeValue(object setting, Type settingsType)
{
if (setting?.GetType() == settingsType)
return setting;
//
return setting.GetType() == typeof(string)
? Activator.CreateInstance(settingsType, new string[] { setting as string })
: Convert.ChangeType(setting, settingsType);
Expand Down

0 comments on commit c2e6bdd

Please sign in to comment.