Skip to content

Commit

Permalink
Merge pull request #22 from AndreasReitberger/21-add-encryption-for-s…
Browse files Browse the repository at this point in the history
…ecure-settings

Added `Encryption` support
  • Loading branch information
AndreasReitberger authored Mar 19, 2024
2 parents 80775f1 + 25164f4 commit 0b9cd25
Show file tree
Hide file tree
Showing 44 changed files with 1,491 additions and 277 deletions.
14 changes: 14 additions & 0 deletions src/MauiSettings.Example/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version = "1.0" encoding = "UTF-8" ?>
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MauiSettings.Example"
x:Class="MauiSettings.Example.App">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Styles/Colors.xaml" />
<ResourceDictionary Source="Resources/Styles/Styles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
45 changes: 45 additions & 0 deletions src/MauiSettings.Example/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using AndreasReitberger.Shared.Core.Utilities;
using MauiSettings.Example.Models.Settings;

namespace MauiSettings.Example
{
public partial class App : Application
{
//public static string Hash = "96ug+F5VbYkQjqyolK8SO/R/HHeBZ2srDfMRR7uwrKA=";
public static string Hash = "mYGUbR61NUNjIvdEv/veySPxQEWcCRUZ3SZ7TT72IuI=";
public App()
{
InitializeComponent();
// Example of how to generate a new key
//string t = EncryptionManager.GenerateBase64Key();
//SettingsApp.LoadSettings();
// Needed in order to load only the secure settings (for instance the license)
//Dispatcher.DispatchAsync(async() => await SettingsApp.LoadSecureSettingsAsync(Hash));

// Only Async methods do support encryption!
_ = Task.Run(async () => await SettingsApp.LoadSettingsAsync(Hash));
/*
Dispatcher.DispatchAsync(async () =>
{
await SettingsApp.LoadSettingsAsync(Hash);
});*/
MainPage = new AppShell();
}

protected override void OnSleep()
{
base.OnSleep();
if (SettingsApp.SettingsChanged)
{
try
{
SettingsApp.SaveSettings(Hash);
}
catch (Exception)
{

}
}
}
}
}
15 changes: 15 additions & 0 deletions src/MauiSettings.Example/AppShell.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Shell
x:Class="MauiSettings.Example.AppShell"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MauiSettings.Example"
Shell.FlyoutBehavior="Disabled"
Title="MauiSettings.Example">

<ShellContent
Title="Home"
ContentTemplate="{DataTemplate local:MainPage}"
Route="MainPage" />

</Shell>
10 changes: 10 additions & 0 deletions src/MauiSettings.Example/AppShell.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace MauiSettings.Example
{
public partial class AppShell : Shell
{
public AppShell()
{
InitializeComponent();
}
}
}
65 changes: 65 additions & 0 deletions src/MauiSettings.Example/MainPage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MauiSettings.Example.MainPage"
xmlns:viewModels="clr-namespace:MauiSettings.Example.ViewModels"

x:DataType="viewModels:MainPageViewModel"
>

<ScrollView>
<VerticalStackLayout
Padding="30,0"
Spacing="25">
<Label
Text="License"
Style="{StaticResource Headline}"
SemanticProperties.HeadingLevel="Level1"
/>

<Editor
Text="{Binding LicenseInfo}"
/>
<Label
Text="New hash"
Style="{StaticResource SubHeadline}"
SemanticProperties.HeadingLevel="Level1"
/>
<Grid
ColumnDefinitions="*,Auto"
>
<Editor
Text="{Binding HashKey}"
/>
<Button
Margin="4,2"
Grid.Column="1"
x:Name="ExchangeBtn"
Text="New key"
Command="{Binding ExchangeHashKeyCommand}"
/>
</Grid>

<Button
x:Name="SaveBtn"
Text="Save Settings"
Command="{Binding SaveSettingsCommand}"
HorizontalOptions="Fill"
/>

<Button
x:Name="LoadBtn"
Text="Load Settings"
Command="{Binding LoadSettingsFromDeviceCommand}"
HorizontalOptions="Fill" />

<Button
x:Name="ExportBtn"
Text="To dictionary"
Command="{Binding ToDictionaryCommand}"
HorizontalOptions="Fill" />
</VerticalStackLayout>
</ScrollView>

</ContentPage>
14 changes: 14 additions & 0 deletions src/MauiSettings.Example/MainPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using MauiSettings.Example.ViewModels;

namespace MauiSettings.Example
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
BindingContext = new MainPageViewModel();
}
}

}
25 changes: 25 additions & 0 deletions src/MauiSettings.Example/MauiProgram.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Microsoft.Extensions.Logging;

namespace MauiSettings.Example
{
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});

#if DEBUG
builder.Logging.AddDebug();
#endif
builder.Services.AddSingleton(Dispatcher.GetForCurrentThread());
return builder.Build();
}
}
}
69 changes: 69 additions & 0 deletions src/MauiSettings.Example/MauiSettings.Example.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<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> -->

<!-- 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>.
The Mac App Store will NOT accept apps with ONLY maccatalyst-arm64 indicated;
either BOTH runtimes must be indicated or ONLY macatalyst-x64. -->
<!-- For example: <RuntimeIdentifiers>maccatalyst-x64;maccatalyst-arm64</RuntimeIdentifiers> -->

<OutputType>Exe</OutputType>
<RootNamespace>MauiSettings.Example</RootNamespace>
<UseMaui>true</UseMaui>
<SingleProject>true</SingleProject>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<!-- Display name -->
<ApplicationTitle>MauiSettings Example</ApplicationTitle>

<!-- App Identifier -->
<ApplicationId>com.companyname.mauisettingsexample</ApplicationId>

<!-- Versions -->
<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
<ApplicationVersion>1</ApplicationVersion>

<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">11.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'maccatalyst'">13.1</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">21.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</SupportedOSPlatformVersion>
<TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</TargetPlatformMinVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'tizen'">6.5</SupportedOSPlatformVersion>
</PropertyGroup>

<ItemGroup>
<!-- App Icon -->
<MauiIcon Include="Resources\AppIcon\appicon.svg" ForegroundFile="Resources\AppIcon\appiconfg.svg" Color="#512BD4" />

<!-- Splash Screen -->
<MauiSplashScreen Include="Resources\Splash\splash.svg" Color="#512BD4" BaseSize="128,128" />

<!-- Images -->
<MauiImage Include="Resources\Images\*" />
<MauiImage Update="Resources\Images\dotnet_bot.png" Resize="True" BaseSize="300,185" />

<!-- Custom Fonts -->
<MauiFont Include="Resources\Fonts\*" />

<!-- Raw Assets (also remove the "Resources\Raw" prefix) -->
<MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Maui.Controls" Version="8.0.10" />
<PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="8.0.10" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\MauiSettings\MauiSettings.csproj" />
</ItemGroup>

</Project>
78 changes: 78 additions & 0 deletions src/MauiSettings.Example/Models/Settings/SettingsApp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using AndreasReitberger.Maui;
using AndreasReitberger.Maui.Attributes;
using System.Runtime.CompilerServices;

namespace MauiSettings.Example.Models.Settings
{
/// <summary>
/// Holds the whole local application settings
/// </summary>
public partial class SettingsApp : MauiSettings<SettingsApp>
{
#region Properties
static bool _settingsChanged = false;
public static bool SettingsChanged
{
get => _settingsChanged;
set
{
if (_settingsChanged == value) return;
_settingsChanged = value;
}
}
#endregion

#region DeviceSettings
/// <summary>
/// Opens the settings UI on the current device
/// </summary>
public static void OpenDeviceSettings()
{
AppInfo.ShowSettingsUI();
}
#endregion

#region Settings

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

#endregion

#region Default

[MauiSetting(Name = nameof(License_AcceptEndUserLicenseAgreement), DefaultValue = false)]
public static bool License_AcceptEndUserLicenseAgreement { get; set; }

[MauiSetting(Name = nameof(License_ProUpgradePurchased), DefaultValue = false)]
public static bool License_ProUpgradePurchased { get; set; }

[MauiSetting(Name = nameof(LicenseInfo), DefaultValue = "", Secure = true, Encrypt = true)]
public static string LicenseInfo { get; set; } = string.Empty;
#endregion

#region Privacy
[MauiSetting(Name = nameof(Privacy_AcceptPrivacyPolicy), DefaultValue = false, SkipForExport = true)]
public static bool Privacy_AcceptPrivacyPolicy { get; set; }

#endregion

#endregion

#region Methods

/// <summary>
/// Creates a copy of the current <c>SettingsApp</c>
/// </summary>
/// <returns>Copy of the current <c>SettingsApp</c></returns>
public static SettingsApp Copy()
{
SettingsApp app = new();
app.LoadObjectSettings();
return app;
}

#endregion
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application android:allowBackup="true" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true"></application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
11 changes: 11 additions & 0 deletions src/MauiSettings.Example/Platforms/Android/MainActivity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Android.App;
using Android.Content.PM;
using Android.OS;

namespace MauiSettings.Example
{
[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
public class MainActivity : MauiAppCompatActivity
{
}
}
16 changes: 16 additions & 0 deletions src/MauiSettings.Example/Platforms/Android/MainApplication.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Android.App;
using Android.Runtime;

namespace MauiSettings.Example
{
[Application]
public class MainApplication : MauiApplication
{
public MainApplication(IntPtr handle, JniHandleOwnership ownership)
: base(handle, ownership)
{
}

protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#512BD4</color>
<color name="colorPrimaryDark">#2B0B98</color>
<color name="colorAccent">#2B0B98</color>
</resources>
10 changes: 10 additions & 0 deletions src/MauiSettings.Example/Platforms/MacCatalyst/AppDelegate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Foundation;

namespace MauiSettings.Example
{
[Register("AppDelegate")]
public class AppDelegate : MauiUIApplicationDelegate
{
protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
}
}
Loading

0 comments on commit 0b9cd25

Please sign in to comment.