Skip to content

Commit

Permalink
Merge pull request #61 from AndreasReitberger/main
Browse files Browse the repository at this point in the history
Merge `main` into `branch`
  • Loading branch information
AndreasReitberger authored Sep 14, 2024
2 parents 0054acf + 0e55a50 commit 22fd386
Show file tree
Hide file tree
Showing 12 changed files with 154 additions and 111 deletions.
10 changes: 8 additions & 2 deletions common.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@

<Project>
<PropertyGroup>
<Version>1.0.9</Version>
<PropertyGroup>
<TargetFrameworks>net8.0-android;net8.0-ios;net8.0-maccatalyst</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net8.0-windows10.0.19041.0</TargetFrameworks>
<!-- Uncomment to also build the tizen app. You will need to install tizen by following this: https://github.com/Samsung/Tizen.NET -->
<!-- <TargetFrameworks>$(TargetFrameworks);net8.0-tizen</TargetFrameworks> -->
<WindowsSdkPackageVersion>10.0.19041.41</WindowsSdkPackageVersion>

<Version>1.0.9</Version>
<PackageIcon>ar_128.png</PackageIcon>
<NeutralLanguage>en</NeutralLanguage>
<PackageProjectUrl>https://github.com/AndreasReitberger/MauiSettings</PackageProjectUrl>
Expand Down
8 changes: 4 additions & 4 deletions src/MauiSettings.Example/MauiSettings.Example.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net8.0-android;net8.0-ios;net8.0-maccatalyst</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net8.0-windows10.0.19041.0</TargetFrameworks>
<!-- Uncomment to also build the tizen app. You will need to install tizen by following this: https://github.com/Samsung/Tizen.NET -->
<!-- <TargetFrameworks>$(TargetFrameworks);net8.0-tizen</TargetFrameworks> -->

<WindowsSdkPackageVersion>10.0.19041.41</WindowsSdkPackageVersion>
<!-- Note for MacCatalyst:
The default runtime is maccatalyst-x64, except in Release config, in which case the default is maccatalyst-x64;maccatalyst-arm64.
When specifying both architectures, use the plural <RuntimeIdentifiers> instead of the singular <RuntimeIdentifier>.
Expand Down Expand Up @@ -57,8 +56,9 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Maui.Controls" Version="8.0.80" />
<PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="8.0.80" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.3.1" />
<PackageReference Include="Microsoft.Maui.Controls" Version="8.0.82" />
<PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="8.0.82" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion src/MauiSettings.Example/Models/Settings/SettingsApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static void OpenDeviceSettings()

#region Version
[MauiSetting(Name = nameof(App_SettingsVersion), DefaultValue = "1.0.0", SkipForExport = true)]
public static Version App_SettingsVersion { get; set; }
public static Version? App_SettingsVersion { get; set; }

[MauiSetting(Name = nameof(ResourcesCurrentVersionAvailable))]
public static string ResourcesCurrentVersionAvailable { get; set; } = string.Empty;
Expand Down
4 changes: 2 additions & 2 deletions src/MauiSettings.Example/ViewModels/MainPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ async Task ExchangeHashKey()
async Task ToDictionary()
{
// All "SkipForExport" should be missing here.
Dictionary<string, Tuple<object, Type>> dict = await SettingsApp.ToDictionaryAsync();
Settings = [.. dict.Select(kp => new SettingsItem() { Key = kp.Key, Value = kp.Value.Item1.ToString() })];
Dictionary<string, Tuple<object?, Type>> dict = await SettingsApp.ToDictionaryAsync();
Settings = [.. dict.Select(kp => new SettingsItem() { Key = kp.Key, Value = kp.Value?.Item1?.ToString() ?? string.Empty})];
}
#endregion
}
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
10 changes: 6 additions & 4 deletions src/MauiSettings/Helper/MauiSettingsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,26 +62,28 @@ internal class MauiSettingsHelper
}
#if DEBUG
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
#else
catch (Exception)
#endif
{
#endif
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)) : defaultValue;

// Docs: https://docs.microsoft.com/en-us/dotnet/maui/platform-integration/storage/secure-storage?tabs=ios
// Only string is allowed for secure storage
public static async Task<string> GetSecureSettingsValueAsync(string key, string? defaultValue)
{
defaultValue ??= string.Empty;
string? settingsObject = await SecureStorage.Default.GetAsync(key);
return settingsObject == null ? defaultValue : settingsObject;
return settingsObject ?? defaultValue;
}

public static void SetSettingsValue(string key, object? value)
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
35 changes: 17 additions & 18 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 @@ -36,6 +36,7 @@ public static void SetSettingValue(MemberInfo memberInfo, object settings, objec
Debug.WriteLine($"MauiSettings: The setting value was null for {memberInfo.Name}");
return;
}
settingType ??= typeof(object);
if (memberInfo is FieldInfo fieldInfo)
{
fieldInfo.SetValue(settings, settingValue);
Expand All @@ -44,19 +45,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 +65,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 +86,27 @@ 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)
{
settingType ??= typeof(object);
try
{
if (attr != null && attr.DefaultValueInUse)
if (attr is not 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 +123,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
10 changes: 3 additions & 7 deletions src/MauiSettings/MauiSettings.csproj
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\common.props" />
<PropertyGroup>
<TargetFrameworks>net8.0;net8.0-android;net8.0-ios;net8.0-maccatalyst</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net8.0-windows10.0.19041.0</TargetFrameworks>
<!-- Uncomment to also build the tizen app. You will need to install tizen by following this: https://github.com/Samsung/Tizen.NET -->
<!-- <TargetFrameworks>$(TargetFrameworks);net8.0-tizen</TargetFrameworks> -->
<UseMaui>true</UseMaui>
<SingleProject>true</SingleProject>
<ImplicitUsings>enable</ImplicitUsings>
Expand Down Expand Up @@ -33,10 +29,10 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Maui.Controls" Version="8.0.80" />
<PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="8.0.80" />
<PackageReference Include="Microsoft.Maui.Controls" Version="8.0.82" />
<PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="8.0.82" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="SharedNetCoreLibrary" Version="1.1.10" />
<PackageReference Include="SharedNetCoreLibrary" Version="1.1.11" />
</ItemGroup>

</Project>
Loading

0 comments on commit 22fd386

Please sign in to comment.