Skip to content

Commit

Permalink
Added LoadSecureSettingsAsync
Browse files Browse the repository at this point in the history
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
  • Loading branch information
AndreasReitberger committed Jul 8, 2023
1 parent 2ed4e64 commit df87a81
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/MauiSettings/MauiSettingsGeneric.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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);
}
}
}
Expand All @@ -243,7 +257,7 @@ static void GetExpressionMeta<T>(object settings, Expression<Func<SO, T>> 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;
Expand Down Expand Up @@ -300,7 +314,7 @@ List<MauiSettingAttribute> 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
}
}
Expand Down Expand Up @@ -389,7 +403,7 @@ List<MauiSettingAttribute> settingBaseAttributes
return true;
}

static async Task<bool> ProcessSettingsInfoAsync(MauiSettingsMemberInfo settingsObjectInfo, MauiSettingsInfo settingsInfo, MauiSettingsActions mode, MauiSettingsTarget target)
static async Task<bool> ProcessSettingsInfoAsync(MauiSettingsMemberInfo settingsObjectInfo, MauiSettingsInfo settingsInfo, MauiSettingsActions mode, MauiSettingsTarget target, bool secureOnly = false)
{
settingsInfo ??= new();
MauiSettingBaseAttribute settingBaseAttribute = null;
Expand Down Expand Up @@ -422,6 +436,9 @@ List<MauiSettingAttribute> settingBaseAttributes
secure = settingAttribute.Secure;
if (!secure)
{
// If only secure storage should be loaded, stop here.
if (secureOnly)
return true;
switch (target)
{
#if IOS
Expand Down

0 comments on commit df87a81

Please sign in to comment.