From cd93ce647eb644df4d57bccbdd6749a1829c4837 Mon Sep 17 00:00:00 2001 From: Andreas Date: Wed, 3 Apr 2024 07:48:43 +0200 Subject: [PATCH] Updated `nugets` Fixed #40 --- cleanup.ps1 | 9 ++ src/MauiSettings/MauiSettings.csproj | 2 +- src/MauiSettings/MauiSettingsGeneric.cs | 137 +++++++++++------------- src/restore.bat | 6 ++ 4 files changed, 76 insertions(+), 78 deletions(-) create mode 100644 cleanup.ps1 create mode 100644 src/restore.bat diff --git a/cleanup.ps1 b/cleanup.ps1 new file mode 100644 index 0000000..f101b1b --- /dev/null +++ b/cleanup.ps1 @@ -0,0 +1,9 @@ +#Set-ExecutionPolicy Unrestricted +$ErrorActionPreference = "Stop" + +# Set current directory to script directory +Set-Location -Path $PSScriptRoot +#Get-ChildItem .\ -include bin,obj -Recurse | format-table +#pause +cls +Get-ChildItem .\ -include bin,obj -Recurse | ForEach-Object ($_) { Remove-Item $_.FullName -Force -Recurse } diff --git a/src/MauiSettings/MauiSettings.csproj b/src/MauiSettings/MauiSettings.csproj index 31ffbb5..2e4930d 100644 --- a/src/MauiSettings/MauiSettings.csproj +++ b/src/MauiSettings/MauiSettings.csproj @@ -44,7 +44,7 @@ - + diff --git a/src/MauiSettings/MauiSettingsGeneric.cs b/src/MauiSettings/MauiSettingsGeneric.cs index 0fa7e3e..d5d92d7 100644 --- a/src/MauiSettings/MauiSettingsGeneric.cs +++ b/src/MauiSettings/MauiSettingsGeneric.cs @@ -29,10 +29,7 @@ public static SO SettingsObject { get { - if (_settingsObject == null) - { - _settingsObject = new SO(); - } + _settingsObject ??= new SO(); return (SO)_settingsObject; } } @@ -69,36 +66,31 @@ public MauiSettingsGeneric(SO settingsObject, string settingsKey) public static void LoadSetting(Expression> value) => LoadObjectSetting(SettingsObject, value); - public static Task LoadSettingAsync(Expression> value, string? key = null) - => Task.Run(async delegate - { - await LoadObjectSettingAsync(SettingsObject, value, key: key); - }); - - public static Task LoadSecureSettingAsync(Expression> value, string? key = null) - => Task.Run(async delegate - { - await LoadSecureObjectSettingAsync(SettingsObject, value, key: key); - }); + public static Task LoadSettingAsync(Expression> value, string? key = null) => Task.Run(async delegate + { + await LoadObjectSettingAsync(SettingsObject, value, key: key); + }); + public static Task LoadSecureSettingAsync(Expression> value, string? key = null) => Task.Run(async delegate + { + await LoadSecureObjectSettingAsync(SettingsObject, value, key: key); + }); public void LoadObjectSettings() => LoadSettings(this); 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 - { - await GetExpressionMetaAsync(settings: settingsObject, value, MauiSettingsActions.Load, key: key); - }); - public static Task LoadSecureObjectSettingAsync(object settingsObject, Expression> value, string? key = null) - => Task.Run(async delegate - { - await GetExpressionMetaAsync(settings: settingsObject, value, MauiSettingsActions.Load, secureOnly: true, key: key); - }); + public static Task LoadObjectSettingAsync(object settingsObject, Expression> value, string? key = null) => Task.Run(async delegate + { + await GetExpressionMetaAsync(settings: settingsObject, value, MauiSettingsActions.Load, key: key); + }); + public static Task LoadSecureObjectSettingAsync(object settingsObject, Expression> value, string? key = null) => Task.Run(async delegate + { + await GetExpressionMetaAsync(settings: settingsObject, value, MauiSettingsActions.Load, secureOnly: true, key: key); + }); - public static void LoadSettings(object settings) => GetClassMeta(settings: settings, mode: MauiSettingsActions.Load, key: null); + public static void LoadSettings(object settings) => GetClassMeta(settings: settings, mode: MauiSettingsActions.Load); public static Task LoadSettingsAsync(string? key = null) => Task.Run(async delegate @@ -277,7 +269,8 @@ public static async Task>> ToCo return setting; } - public static Task>> ToSettingsTupleAsync(Expression> value) => ToSettingsTupleAsync(settings: SettingsObject, value: value); + public static Task>> ToSettingsTupleAsync(Expression> value) + => ToSettingsTupleAsync(settings: SettingsObject, value: value); public static async Task>> ToSettingsTupleAsync(object? settings, Expression> value) { @@ -311,7 +304,7 @@ public static Task ExhangeKeyAsync(string newKey, string? oldKey = null) return declaredMembers?.ToList(); } } - static void GetClassMeta(object settings, MauiSettingsActions mode, MauiSettingsTarget target = MauiSettingsTarget.Local, string? key = null) + static void GetClassMeta(object settings, MauiSettingsActions mode, MauiSettingsTarget target = MauiSettingsTarget.Local) { lock (lockObject) { @@ -332,63 +325,55 @@ 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) { - //lock (lockObject) - if (true) - { - // Get all member infos from the passed settingsObject - IEnumerable declaredMembers = settings.GetType().GetTypeInfo().DeclaredMembers; + // Get all member infos from the passed settingsObject + IEnumerable declaredMembers = settings.GetType().GetTypeInfo().DeclaredMembers; - MauiSettingsMemberInfo settingsObjectInfo = new(); - MauiSettingsInfo settingsInfo = new(); + MauiSettingsMemberInfo settingsObjectInfo = new(); + MauiSettingsInfo settingsInfo = new(); - foreach (MemberInfo mInfo in declaredMembers) - { - settingsObjectInfo.OrignalSettingsObject = settings; - settingsObjectInfo.Info = mInfo; - // Handles saving the settings to the Maui.Storage.Preferences - _ = await ProcessSettingsInfoAsync(settingsObjectInfo, settingsInfo, mode, target, secureOnly: secureOnly, key: key); - } - } + foreach (MemberInfo mInfo in declaredMembers) + { + settingsObjectInfo.OrignalSettingsObject = settings; + settingsObjectInfo.Info = mInfo; + // Handles saving the settings to the Maui.Storage.Preferences + _ = await ProcessSettingsInfoAsync(settingsObjectInfo, settingsInfo, mode, target, secureOnly: secureOnly, key: key); + } } static async Task GetMetaFromDictionaryAsync(object settings, Dictionary> dictionary, MauiSettingsActions mode, MauiSettingsTarget target = MauiSettingsTarget.Local, bool secureOnly = false, string? key = null) { - //lock (lockObject) - if (true) - { - // Get all member infos from the passed settingsObject - IEnumerable declaredMembers = settings.GetType().GetTypeInfo().DeclaredMembers; + // Get all member infos from the passed settingsObject + IEnumerable declaredMembers = settings.GetType().GetTypeInfo().DeclaredMembers; - MauiSettingsMemberInfo settingsObjectInfo = new(); - MauiSettingsInfo settingsInfo = new(); + MauiSettingsMemberInfo settingsObjectInfo = new(); + MauiSettingsInfo settingsInfo = new(); - foreach (MemberInfo mInfo in declaredMembers) + foreach (MemberInfo mInfo in declaredMembers) + { + bool useValueFromSettingsInfo = false; + // Try to find the matching settingsKey + KeyValuePair>? keyPair = dictionary?.FirstOrDefault(keypair => + keypair.Key.EndsWith(mInfo.Name + //?.Replace("get_", string.Empty) + )); + if (keyPair?.Key != null) { - bool useValueFromSettingsInfo = false; - // Try to find the matching settingsKey - KeyValuePair>? keyPair = dictionary?.FirstOrDefault(keypair => - keypair.Key.EndsWith(mInfo.Name - //?.Replace("get_", string.Empty) - )); - if (keyPair?.Key != null) + useValueFromSettingsInfo = true; + // If a matching settingsKey was found, prepare the settingsInfo with the loaded data + settingsInfo = new() { - useValueFromSettingsInfo = true; - // If a matching settingsKey was found, prepare the settingsInfo with the loaded data - settingsInfo = new() - { - Name = mInfo.Name?.Replace("get_", string.Empty), - Value = keyPair.Value.Value.Item1, - SettingsType = keyPair.Value.Value.Item2, - }; - } - else - useValueFromSettingsInfo = false; - settingsObjectInfo.OrignalSettingsObject = settings; - settingsObjectInfo.Info = mInfo; - // Handles saving the settings to the Maui.Storage.Preferences - _ = await ProcessSettingsInfoAsync( - settingsObjectInfo, settingsInfo, mode, target, secureOnly: secureOnly, useValueFromSettingsInfo: useValueFromSettingsInfo, key: key); + Name = mInfo.Name?.Replace("get_", string.Empty), + Value = keyPair.Value.Value.Item1, + SettingsType = keyPair.Value.Value.Item2, + }; } - } + else + useValueFromSettingsInfo = false; + settingsObjectInfo.OrignalSettingsObject = settings; + settingsObjectInfo.Info = mInfo; + // Handles saving the settings to the Maui.Storage.Preferences + _ = await ProcessSettingsInfoAsync( + settingsObjectInfo, settingsInfo, mode, target, secureOnly: secureOnly, useValueFromSettingsInfo: useValueFromSettingsInfo, key: key); + } } static void GetExpressionMeta(object settings, Expression> value, MauiSettingsActions mode, MauiSettingsTarget target = MauiSettingsTarget.Local) @@ -884,8 +869,6 @@ List settingBaseAttributes throw new InvalidDataException($"Only data type of '{typeof(string)}' is allowed for secure storage!"); } } - // Sets the loaded value back to the settingsObject - //MauiSettingsObjectHelper.SetSettingValue(settingsObjectInfo.Info, settingsObjectInfo.OrignalSettingsObject, settingsInfo.Value, settingsInfo.SettingsType); return settingsInfo; } diff --git a/src/restore.bat b/src/restore.bat new file mode 100644 index 0000000..a405ec2 --- /dev/null +++ b/src/restore.bat @@ -0,0 +1,6 @@ +echo off + +echo "Restore..." +dotnet restore + +pause \ No newline at end of file