diff --git a/common.props b/common.props index a7a9984..f38de64 100644 --- a/common.props +++ b/common.props @@ -7,7 +7,7 @@ 10.0.19041.41 - 1.0.9 + 1.0.9 ar_128.png en https://github.com/AndreasReitberger/MauiSettings diff --git a/src/MauiSettings/Attributes/MauiSettingBaseAttribute.cs b/src/MauiSettings/Attributes/MauiSettingBaseAttribute.cs index 8918227..238d264 100644 --- a/src/MauiSettings/Attributes/MauiSettingBaseAttribute.cs +++ b/src/MauiSettings/Attributes/MauiSettingBaseAttribute.cs @@ -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 { diff --git a/src/MauiSettings/Helper/MauiSettingsHelper.cs b/src/MauiSettings/Helper/MauiSettingsHelper.cs index e131266..6e04bf6 100644 --- a/src/MauiSettings/Helper/MauiSettingsHelper.cs +++ b/src/MauiSettings/Helper/MauiSettingsHelper.cs @@ -69,11 +69,11 @@ internal class MauiSettingsHelper SetSettingsValue(key, defaultValue); return defaultValue; } - return ChangeSettingsType(returnValue, defaultValue); + return ChangeSettingsType(returnValue, defaultValue); //return (T)Convert.ChangeType(returnValue, typeof(T)); } - public static T? ChangeSettingsType(object settingsValue, T defaultValue) => (T)Convert.ChangeType(settingsValue, typeof(T)) ?? default; + public static T? ChangeSettingsType(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 diff --git a/src/MauiSettings/Helper/MauiSettingsInfo.cs b/src/MauiSettings/Helper/MauiSettingsInfo.cs index 4edf04a..b45c62a 100644 --- a/src/MauiSettings/Helper/MauiSettingsInfo.cs +++ b/src/MauiSettings/Helper/MauiSettingsInfo.cs @@ -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; diff --git a/src/MauiSettings/Helper/MauiSettingsObjectHelper.cs b/src/MauiSettings/Helper/MauiSettingsObjectHelper.cs index d7b3a14..b625c52 100644 --- a/src/MauiSettings/Helper/MauiSettingsObjectHelper.cs +++ b/src/MauiSettings/Helper/MauiSettingsObjectHelper.cs @@ -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) { @@ -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()); + 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 { @@ -44,11 +44,8 @@ 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) @@ -56,7 +53,7 @@ public static void SetSettingValue(MemberInfo memberInfo, object settings, objec settingValue = GetConvertedTypeValue(settingValue, settingType); } - setMethod.Invoke(settings, new object[1] { settingValue }); + setMethod.Invoke(settings, [settingValue]); return; } } @@ -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 { @@ -88,7 +85,7 @@ 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) { @@ -96,18 +93,18 @@ public static object GetTypeDefaultValue(Type 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 }); @@ -124,7 +121,7 @@ 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) { @@ -132,8 +129,8 @@ 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 }) + return setting?.GetType() == typeof(string) + ? Activator.CreateInstance(settingsType, [setting as string]) : Convert.ChangeType(setting, settingsType); } catch (Exception) diff --git a/src/MauiSettings/MauiSettingsGeneric.cs b/src/MauiSettings/MauiSettingsGeneric.cs index 9696b87..ab18b36 100644 --- a/src/MauiSettings/MauiSettingsGeneric.cs +++ b/src/MauiSettings/MauiSettingsGeneric.cs @@ -69,17 +69,19 @@ public MauiSettingsGeneric(SO settingsObject, string settingsKey) public static Task LoadSettingAsync(Expression> value, string? key = null) => Task.Run(async delegate { + if (SettingsObject is null) return; await LoadObjectSettingAsync(SettingsObject, value, key: key); }); public static Task LoadSecureSettingAsync(Expression> 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(object settingsObject, Expression> value) + public static void LoadObjectSetting(object? settingsObject, Expression> value) => GetExpressionMeta(settings: settingsObject, value, MauiSettingsActions.Load); public static Task LoadObjectSettingAsync(object settingsObject, Expression> value, string? key = null) => Task.Run(async delegate @@ -91,7 +93,7 @@ public static Task LoadSecureObjectSettingAsync(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 @@ -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 @@ -354,8 +357,9 @@ static void GetClassMeta(object settings, MauiSettingsActions mode, MauiSettings } } } - static async Task GetClassMetaAsync(object settings, MauiSettingsActions mode, MauiSettingsTarget target = MauiSettingsTarget.Local, bool secureOnly = false, string? key = null, bool justTryLoading = false) + static async Task 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 declaredMembers = settings.GetType().GetTypeInfo().DeclaredMembers; @@ -414,8 +418,9 @@ static async Task GetMetaFromDictionaryAsync(object settings, Dictionary(object settings, Expression> value, MauiSettingsActions mode, MauiSettingsTarget target = MauiSettingsTarget.Local) + static void GetExpressionMeta(object? settings, Expression> value, MauiSettingsActions mode, MauiSettingsTarget target = MauiSettingsTarget.Local) { + if (settings is null) return; lock (lockObject) { if (value.Body is MemberExpression memberExpression) @@ -430,9 +435,9 @@ static void GetExpressionMeta(object settings, Expression> value, } } - static async Task GetExpressionMetaAsync(object settings, Expression> value, MauiSettingsActions mode, MauiSettingsTarget target = MauiSettingsTarget.Local, bool secureOnly = false, string? key = null) + static async Task GetExpressionMetaAsync(object? settings, Expression> 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() @@ -446,6 +451,7 @@ static async Task GetExpressionMetaAsync(object settings, Expression GetExpressionMetaAsKeyValuePairAsync(object? settings, Expression> value, string? key = null) { + if (settings is null) return null; if (value.Body is MemberExpression memberExpression) { return await ProcessSettingsInfoAsKeyValuePairAsync(new MauiSettingsMemberInfo() @@ -479,7 +485,7 @@ List 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); diff --git a/src/MauiSettings/Platforms/iOS/Cloud/ICloudStoreManager.cs b/src/MauiSettings/Platforms/iOS/Cloud/ICloudStoreManager.cs index 8d40dae..d840033 100644 --- a/src/MauiSettings/Platforms/iOS/Cloud/ICloudStoreManager.cs +++ b/src/MauiSettings/Platforms/iOS/Cloud/ICloudStoreManager.cs @@ -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 { @@ -20,6 +15,7 @@ public partial class ICloudStoreManager #endregion #region Methods + public static object? GetValue(string key) => Store?.GetString(key); public static void SetValue(string key, T value, bool synchronize = true) diff --git a/src/MauiSettings/Platforms/iOS/MauiSettingsGeneric.cs b/src/MauiSettings/Platforms/iOS/MauiSettingsGeneric.cs index 319c07e..34630ad 100644 --- a/src/MauiSettings/Platforms/iOS/MauiSettingsGeneric.cs +++ b/src/MauiSettings/Platforms/iOS/MauiSettingsGeneric.cs @@ -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); @@ -19,11 +20,13 @@ await Task.Run(async delegate public void SyncSettingsToICloud(Expression> 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); @@ -31,6 +34,7 @@ await Task.Run(async delegate } public void SyncSettingsToICloud(object settings, Expression> value) { + ArgumentNullException.ThrowIfNull(settings); GetExpressionMeta(settings: settings, value: value, mode: MauiSettingsActions.Save, target: MauiSettingsTarget.ICloud); } @@ -40,6 +44,7 @@ public void SyncSettingsToICloud(object settings, Expression> val public async Task SyncSettingsFromICloudAsync() { + ArgumentNullException.ThrowIfNull(SettingsObject); await Task.Run(async delegate { await SyncSettingsFromICloudAsync(settings: SettingsObject); @@ -48,11 +53,13 @@ await Task.Run(async delegate public void SyncSettingsFromICloud(Expression> 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); @@ -60,6 +67,7 @@ await Task.Run(async delegate } public void SyncSettingsFromICloud(object settings, Expression> value) { + ArgumentNullException.ThrowIfNull(settings); GetExpressionMeta(settings: settings, value: value, mode: MauiSettingsActions.Load, target: MauiSettingsTarget.ICloud); } #endregion