From df87a813d7d4f478b06a37dd56f099a0ef25bce5 Mon Sep 17 00:00:00 2001 From: Andreas Date: Sat, 8 Jul 2023 07:35:25 +0200 Subject: [PATCH] Added `LoadSecureSettingsAsync` This commit adds a new `LoadSecureSettingsAsync` method in order to only load secure settings, which need a async method. Also the `LoadSettings` method now doesn't throw if a secure setting is tried to be loaded (expected if the `throwOnError` parameter is set to `true`) Fixed #11 --- src/MauiSettings/MauiSettingsGeneric.cs | 27 ++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/MauiSettings/MauiSettingsGeneric.cs b/src/MauiSettings/MauiSettingsGeneric.cs index 3675458..65da0b9 100644 --- a/src/MauiSettings/MauiSettingsGeneric.cs +++ b/src/MauiSettings/MauiSettingsGeneric.cs @@ -85,6 +85,20 @@ await Task.Run(async delegate await GetClassMetaAsync(settings: settings, mode: MauiSettingsActions.Load); }); } + public static async Task LoadSecureSettingsAsync() + { + await Task.Run(async delegate + { + await LoadSecureSettingsAsync(settings: SettingsObject); + }); + } + public static async Task LoadSecureSettingsAsync(object settings) + { + await Task.Run(async delegate + { + await GetClassMetaAsync(settings: settings, mode: MauiSettingsActions.Load, secureOnly: true); + }); + } #endregion #region SaveSettings @@ -205,7 +219,7 @@ static void GetClassMeta(object settings, MauiSettingsActions mode, MauiSettings } } } - static async Task GetClassMetaAsync(object settings, MauiSettingsActions mode, MauiSettingsTarget target = MauiSettingsTarget.Local) + static async Task GetClassMetaAsync(object settings, MauiSettingsActions mode, MauiSettingsTarget target = MauiSettingsTarget.Local, bool secureOnly = false) { //lock (lockObject) if (true) @@ -222,7 +236,7 @@ static async Task GetClassMetaAsync(object settings, MauiSettingsActions mode, M settingsObjectInfo.OrignalSettingsObject = settings; settingsObjectInfo.Info = mInfo; // Handles saving the settings to the Maui.Storage.Preferences - _ = await ProcessSettingsInfoAsync(settingsObjectInfo, settingsInfo, mode, target); + _ = await ProcessSettingsInfoAsync(settingsObjectInfo, settingsInfo, mode, target, secureOnly: secureOnly); } } } @@ -243,7 +257,7 @@ static void GetExpressionMeta(object settings, Expression> value, } } - static bool ProcessSettingsInfo(MauiSettingsMemberInfo settingsObjectInfo, MauiSettingsInfo settingsInfo, MauiSettingsActions mode, MauiSettingsTarget target) + static bool ProcessSettingsInfo(MauiSettingsMemberInfo settingsObjectInfo, MauiSettingsInfo settingsInfo, MauiSettingsActions mode, MauiSettingsTarget target, bool throwOnError = false) { settingsInfo ??= new(); MauiSettingBaseAttribute settingBaseAttribute = null; @@ -300,7 +314,7 @@ List settingBaseAttributes } #else - throw new NotSupportedException("SecureStorage is only available in the Async methods!"); + if (throwOnError) throw new NotSupportedException("SecureStorage is only available in the Async methods!"); #endif } } @@ -389,7 +403,7 @@ List settingBaseAttributes return true; } - static async Task ProcessSettingsInfoAsync(MauiSettingsMemberInfo settingsObjectInfo, MauiSettingsInfo settingsInfo, MauiSettingsActions mode, MauiSettingsTarget target) + static async Task ProcessSettingsInfoAsync(MauiSettingsMemberInfo settingsObjectInfo, MauiSettingsInfo settingsInfo, MauiSettingsActions mode, MauiSettingsTarget target, bool secureOnly = false) { settingsInfo ??= new(); MauiSettingBaseAttribute settingBaseAttribute = null; @@ -422,6 +436,9 @@ List settingBaseAttributes secure = settingAttribute.Secure; if (!secure) { + // If only secure storage should be loaded, stop here. + if (secureOnly) + return true; switch (target) { #if IOS