Skip to content

Commit

Permalink
Merge branch '50-fix-warnings-about-possible-nullable-references' int…
Browse files Browse the repository at this point in the history
…o fix-pr
  • Loading branch information
AndreasReitberger authored Sep 13, 2024
2 parents 7319141 + 91d3f2b commit 7f693c0
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 39 deletions.
2 changes: 1 addition & 1 deletion common.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<!-- <TargetFrameworks>$(TargetFrameworks);net8.0-tizen</TargetFrameworks> -->
<WindowsSdkPackageVersion>10.0.19041.41</WindowsSdkPackageVersion>

<Version>1.0.9</Version>
<Version>1.0.9</Version>
<PackageIcon>ar_128.png</PackageIcon>
<NeutralLanguage>en</NeutralLanguage>
<PackageProjectUrl>https://github.com/AndreasReitberger/MauiSettings</PackageProjectUrl>
Expand Down
6 changes: 3 additions & 3 deletions src/MauiSettings/Attributes/MauiSettingBaseAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
public class MauiSettingBaseAttribute : Attribute
{
#region Properties
public string Name { get; set; }
public string Name { get; set; } = string.Empty;

private object _default;
public object DefaultValue
private object? _default;
public object? DefaultValue
{
get
{
Expand Down
4 changes: 2 additions & 2 deletions src/MauiSettings/Helper/MauiSettingsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ internal class MauiSettingsHelper
SetSettingsValue(key, defaultValue);
return defaultValue;
}
return ChangeSettingsType<T>(returnValue, defaultValue);
return ChangeSettingsType(returnValue, defaultValue);
//return (T)Convert.ChangeType(returnValue, typeof(T));
}

public static T? ChangeSettingsType<T>(object settingsValue, T defaultValue) => (T)Convert.ChangeType(settingsValue, typeof(T)) ?? default;
public static T? ChangeSettingsType<T>(object? settingsValue, T defaultValue) => settingsValue is not null ? (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
Expand Down
8 changes: 4 additions & 4 deletions src/MauiSettings/Helper/MauiSettingsInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
internal class MauiSettingsInfo
{
#region Properties
public string Name { get; set; }
public object Value { get; set; }
public Type SettingsType { get; set; }
public object Default { get; set; }
public string Name { get; set; } = string.Empty;
public object? Value { get; set; }
public Type? SettingsType { get; set; }
public object? Default { get; set; }
public bool IsSecure { get; set; } = false;
public bool Encrypt { get; set; } = false;
public bool SkipForExport { get; set; } = false;
Expand Down
31 changes: 14 additions & 17 deletions src/MauiSettings/Helper/MauiSettingsObjectHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace AndreasReitberger.Maui.Helper

internal class MauiSettingsObjectHelper
{
public static object GetSettingValue(MemberInfo mi, object o)
public static object? GetSettingValue(MemberInfo mi, object o)
{
if (mi is FieldInfo fieldInfo)
{
Expand All @@ -24,10 +24,10 @@ public static object GetSettingValue(MemberInfo mi, object o)
{
return propertyInfo.GetValue(o);
}
return (mi as PropertyInfo)?.GetMethod.Invoke(o, Array.Empty<object>());
return (mi as PropertyInfo)?.GetMethod?.Invoke(o, []);
}

public static void SetSettingValue(MemberInfo memberInfo, object settings, object settingValue, Type settingType)
public static void SetSettingValue(MemberInfo memberInfo, object settings, object? settingValue, Type settingType)
{
try
{
Expand All @@ -44,19 +44,16 @@ public static void SetSettingValue(MemberInfo memberInfo, object settings, objec

if (memberInfo is PropertyInfo propertyInfo)
{
MethodInfo setMethod = propertyInfo.SetMethod;
if (setMethod is null)
{
throw new NullReferenceException($"MauiSettings: Cannot set {memberInfo.Name} property! (Read only)");
}
MethodInfo? setMethod = (propertyInfo?.SetMethod)
?? throw new NullReferenceException($"MauiSettings: Cannot set {memberInfo.Name} property! (Read only)");
// If the settings value type doesn't match the target type of the field.
// Maui saves the settings as string, so this conversion is needed.
if (settingValue.GetType() != settingType)
{
settingValue = GetConvertedTypeValue(settingValue, settingType);
}

setMethod.Invoke(settings, new object[1] { settingValue });
setMethod.Invoke(settings, [settingValue]);
return;
}
}
Expand All @@ -67,7 +64,7 @@ public static void SetSettingValue(MemberInfo memberInfo, object settings, objec

}

public static Type GetSettingType(MemberInfo memberInfo)
public static Type? GetSettingType(MemberInfo memberInfo)
{
try
{
Expand All @@ -88,26 +85,26 @@ public static Type GetSettingType(MemberInfo memberInfo)
}
}

public static object GetTypeDefaultValue(Type type)
public static object? GetTypeDefaultValue(Type type)
{
if (type is not null && type.GetTypeInfo().IsValueType)
{
return Activator.CreateInstance(type);
}
return null;
}
public static object GetDefaultValue(MauiSettingBaseAttribute attr, Type settingType)
public static object? GetDefaultValue(MauiSettingBaseAttribute attr, Type settingType)
{
try
{
if (attr != null && attr.DefaultValueInUse)
{
object obj = attr.DefaultValue;
object? obj = attr.DefaultValue;
if (obj is null)
return GetTypeDefaultValue(settingType);
if (obj?.GetType() != settingType)
{
if (obj.GetType() == typeof(string))
if (obj?.GetType() == typeof(string))
{
// Try to pass the string object for the constructor
//return Activator.CreateInstance(settingType, new string[] { obj as string });
Expand All @@ -124,16 +121,16 @@ public static object GetDefaultValue(MauiSettingBaseAttribute attr, Type setting
}
}

public static object GetConvertedTypeValue(object setting, Type settingsType)
public static object? GetConvertedTypeValue(object setting, Type settingsType)
{
if (settingsType is not null)
{
try
{
if (setting?.GetType() == settingsType)
return setting;
return setting.GetType() == typeof(string)
? Activator.CreateInstance(settingsType, new string[] { setting as string })
return setting?.GetType() == typeof(string)
? Activator.CreateInstance(settingsType, [setting as string])
: Convert.ChangeType(setting, settingsType);
}
catch (Exception)
Expand Down
20 changes: 13 additions & 7 deletions src/MauiSettings/MauiSettingsGeneric.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,19 @@ public MauiSettingsGeneric(SO settingsObject, string settingsKey)

public static Task LoadSettingAsync<T>(Expression<Func<SO, T>> value, string? key = null) => Task.Run(async delegate
{
if (SettingsObject is null) return;
await LoadObjectSettingAsync(SettingsObject, value, key: key);
});

public static Task LoadSecureSettingAsync<T>(Expression<Func<SO, T>> value, string? key = null) => Task.Run(async delegate
{
if (SettingsObject is null) return;
await LoadSecureObjectSettingAsync(SettingsObject, value, key: key);
});

public void LoadObjectSettings() => LoadSettings(this);

public static void LoadObjectSetting<T>(object settingsObject, Expression<Func<SO, T>> value)
public static void LoadObjectSetting<T>(object? settingsObject, Expression<Func<SO, T>> value)
=> GetExpressionMeta(settings: settingsObject, value, MauiSettingsActions.Load);

public static Task LoadObjectSettingAsync<T>(object settingsObject, Expression<Func<SO, T>> value, string? key = null) => Task.Run(async delegate
Expand All @@ -91,7 +93,7 @@ public static Task LoadSecureObjectSettingAsync<T>(object settingsObject, Expres
await GetExpressionMetaAsync(settings: settingsObject, value, MauiSettingsActions.Load, secureOnly: true, key: key);
});

public static void LoadSettings(object settings) => GetClassMeta(settings: settings, mode: MauiSettingsActions.Load);
public static void LoadSettings(object? settings) => GetClassMeta(settings: settings, mode: MauiSettingsActions.Load);

public static Task LoadSettingsAsync(string? key = null)
=> Task.Run(async delegate
Expand Down Expand Up @@ -337,6 +339,7 @@ public static Task ExhangeKeyAsync(string newKey, string? oldKey = null, bool re
}
static void GetClassMeta(object settings, MauiSettingsActions mode, MauiSettingsTarget target = MauiSettingsTarget.Local)
{
ArgumentNullException.ThrowIfNull(settings);
lock (lockObject)
{
// Get all member infos from the passed settingsObject
Expand All @@ -354,8 +357,9 @@ static void GetClassMeta(object settings, MauiSettingsActions mode, MauiSettings
}
}
}
static async Task<bool> GetClassMetaAsync(object settings, MauiSettingsActions mode, MauiSettingsTarget target = MauiSettingsTarget.Local, bool secureOnly = false, string? key = null, bool justTryLoading = false)
static async Task<bool> GetClassMetaAsync(object? settings, MauiSettingsActions mode, MauiSettingsTarget target = MauiSettingsTarget.Local, bool secureOnly = false, string? key = null, bool justTryLoading = false)
{
ArgumentNullException.ThrowIfNull(settings);
// Get all member infos from the passed settingsObject
IEnumerable<MemberInfo> declaredMembers = settings.GetType().GetTypeInfo().DeclaredMembers;

Expand Down Expand Up @@ -414,8 +418,9 @@ static async Task<bool> GetMetaFromDictionaryAsync(object settings, Dictionary<s
return true;
}

static void GetExpressionMeta<T>(object settings, Expression<Func<SO, T>> value, MauiSettingsActions mode, MauiSettingsTarget target = MauiSettingsTarget.Local)
static void GetExpressionMeta<T>(object? settings, Expression<Func<SO, T>> value, MauiSettingsActions mode, MauiSettingsTarget target = MauiSettingsTarget.Local)
{
if (settings is null) return;
lock (lockObject)
{
if (value.Body is MemberExpression memberExpression)
Expand All @@ -430,9 +435,9 @@ static void GetExpressionMeta<T>(object settings, Expression<Func<SO, T>> value,
}
}

static async Task GetExpressionMetaAsync<T>(object settings, Expression<Func<SO, T>> value, MauiSettingsActions mode, MauiSettingsTarget target = MauiSettingsTarget.Local, bool secureOnly = false, string? key = null)
static async Task GetExpressionMetaAsync<T>(object? settings, Expression<Func<SO, T>> value, MauiSettingsActions mode, MauiSettingsTarget target = MauiSettingsTarget.Local, bool secureOnly = false, string? key = null)
{

if (settings is null) return;
if (value.Body is MemberExpression memberExpression)
{
_ = await ProcessSettingsInfoAsync(new MauiSettingsMemberInfo()
Expand All @@ -446,6 +451,7 @@ static async Task GetExpressionMetaAsync<T>(object settings, Expression<Func<SO,

static async Task<MauiSettingsInfo?> GetExpressionMetaAsKeyValuePairAsync<T>(object? settings, Expression<Func<SO, T>> value, string? key = null)
{
if (settings is null) return null;
if (value.Body is MemberExpression memberExpression)
{
return await ProcessSettingsInfoAsKeyValuePairAsync(new MauiSettingsMemberInfo()
Expand Down Expand Up @@ -479,7 +485,7 @@ List<MauiSettingAttribute> settingBaseAttributes
}
if (settingsObjectInfo.Info is not null)
{
settingsInfo.Name = MauiSettingNameFormater.GetFullSettingName(settingsObjectInfo.OrignalSettingsObject.GetType(), settingsObjectInfo.Info, settingBaseAttribute);
settingsInfo.Name = MauiSettingNameFormater.GetFullSettingName(settingsObjectInfo.OrignalSettingsObject?.GetType(), settingsObjectInfo.Info, settingBaseAttribute);
settingsInfo.SettingsType = (settingsInfo.SettingsType = MauiSettingsObjectHelper.GetSettingType(settingsObjectInfo.Info));

settingsInfo.Default = MauiSettingsObjectHelper.GetDefaultValue(settingBaseAttribute, settingsInfo.SettingsType);
Expand Down
6 changes: 1 addition & 5 deletions src/MauiSettings/Platforms/iOS/Cloud/ICloudStoreManager.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using Foundation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AndreasReitberger.Maui.Cloud
{
Expand All @@ -20,6 +15,7 @@ public partial class ICloudStoreManager
#endregion

#region Methods

public static object? GetValue(string key) => Store?.GetString(key);

public static void SetValue<T>(string key, T value, bool synchronize = true)
Expand Down
8 changes: 8 additions & 0 deletions src/MauiSettings/Platforms/iOS/MauiSettingsGeneric.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace AndreasReitberger.Maui
#region Save
public async Task SyncSettingsToICloudAsync()
{
ArgumentNullException.ThrowIfNull(SettingsObject);
await Task.Run(async delegate
{
await SyncSettingsToICloudAsync(settings: SettingsObject);
Expand All @@ -19,18 +20,21 @@ await Task.Run(async delegate

public void SyncSettingsToICloud<T>(Expression<Func<SO, T>> value)
{
ArgumentNullException.ThrowIfNull(SettingsObject);
SyncSettingsToICloud(settings: SettingsObject, value: value);
}

public async Task SyncSettingsToICloudAsync(object settings)
{
ArgumentNullException.ThrowIfNull(settings);
await Task.Run(async delegate
{
await GetClassMetaAsync(settings: settings, mode: MauiSettingsActions.Save, target: MauiSettingsTarget.ICloud);
});
}
public void SyncSettingsToICloud<T>(object settings, Expression<Func<SO, T>> value)
{
ArgumentNullException.ThrowIfNull(settings);
GetExpressionMeta(settings: settings, value: value, mode: MauiSettingsActions.Save, target: MauiSettingsTarget.ICloud);
}

Expand All @@ -40,6 +44,7 @@ public void SyncSettingsToICloud<T>(object settings, Expression<Func<SO, T>> val

public async Task SyncSettingsFromICloudAsync()
{
ArgumentNullException.ThrowIfNull(SettingsObject);
await Task.Run(async delegate
{
await SyncSettingsFromICloudAsync(settings: SettingsObject);
Expand All @@ -48,18 +53,21 @@ await Task.Run(async delegate

public void SyncSettingsFromICloud<T>(Expression<Func<SO, T>> value)
{
ArgumentNullException.ThrowIfNull(SettingsObject);
SyncSettingsFromICloud(settings: SettingsObject, value: value);
}

public async Task SyncSettingsFromICloudAsync(object settings)
{
ArgumentNullException.ThrowIfNull(settings);
await Task.Run(async delegate
{
await GetClassMetaAsync(settings: settings, mode: MauiSettingsActions.Load, target: MauiSettingsTarget.ICloud);
});
}
public void SyncSettingsFromICloud<T>(object settings, Expression<Func<SO, T>> value)
{
ArgumentNullException.ThrowIfNull(settings);
GetExpressionMeta(settings: settings, value: value, mode: MauiSettingsActions.Load, target: MauiSettingsTarget.ICloud);
}
#endregion
Expand Down

0 comments on commit 7f693c0

Please sign in to comment.