Skip to content

Commit

Permalink
Refactor MAUI (#24)
Browse files Browse the repository at this point in the history
* Refactor

* Set up unit testing, add net6.0 target

* Add more tests

* Rename AccentColorService > SeedColorService
  • Loading branch information
albi005 authored Oct 18, 2022
1 parent 1c1c15a commit a705549
Show file tree
Hide file tree
Showing 27 changed files with 835 additions and 362 deletions.
95 changes: 95 additions & 0 deletions MaterialColorUtilities.Maui.Tests/DynamicColorServiceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using Microsoft.Extensions.Options;

namespace MaterialColorUtilities.Maui.Tests;

[TestClass]
public class DynamicColorServiceTests
{
private readonly ISeedColorService _seedColorService = new MockSeedColorService();
private readonly Application _application = new();
private readonly IPreferences _preferences = new MockPreferences();

[TestMethod]
public void DisableTheming()
{
IOptions<DynamicColorOptions> options = CreateOptions(opt =>
{
opt.EnableTheming = false;
});

DynamicColorService dynamicColorService = new(options, _seedColorService, _application, _preferences);

dynamicColorService.Initialize(null);

Assert.IsFalse(dynamicColorService.EnableTheming);
Assert.IsNull(dynamicColorService.CorePalette);
Assert.IsNull(dynamicColorService.SchemeMaui);
Assert.IsTrue(_application.Resources.Count == 0);
}

[TestMethod]
public void DisableDynamicColor()
{
IOptions<DynamicColorOptions> options = CreateOptions(opt =>
{
opt.EnableDynamicColor = false;
});

DynamicColorService dynamicColorService = new(options, _seedColorService, _application, _preferences);

dynamicColorService.Initialize(null);

Assert.IsTrue(dynamicColorService.EnableTheming);
Assert.IsFalse(dynamicColorService.EnableDynamicColor);
Assert.AreEqual(unchecked((int)0xff4285F4), dynamicColorService.Seed);
Assert.AreEqual(unchecked((int)0xff005AC1), dynamicColorService.SchemeInt.Primary);
Assert.AreEqual(dynamicColorService.SchemeInt.Primary, dynamicColorService.SchemeMaui.Primary.ToInt());
Assert.IsNotNull(_application.Resources[Schemes.Keys.Primary]);
}

[TestMethod]
public void EnableDynamicColor_SeedColorNull()
{
IOptions<DynamicColorOptions> options = CreateOptions();

DynamicColorService dynamicColorService = new(options, _seedColorService, _application, _preferences);

dynamicColorService.Initialize(null);

Assert.IsTrue(dynamicColorService.EnableTheming);
Assert.IsTrue(dynamicColorService.EnableDynamicColor);
Assert.AreEqual(unchecked((int)0xff4285F4), dynamicColorService.Seed);
Assert.AreEqual(unchecked((int)0xff005AC1), dynamicColorService.SchemeInt.Primary);
Assert.AreEqual(dynamicColorService.SchemeInt.Primary, dynamicColorService.SchemeMaui.Primary.ToInt());
Assert.IsNotNull(_application.Resources[Schemes.Keys.Primary]);
}

[TestMethod]
public void EnableDynamicColor_SeedColorAvailable()
{
IOptions<DynamicColorOptions> options = CreateOptions();

ISeedColorService seedColorService = new MockSeedColorService(unchecked((int)0xFFc07d52));
DynamicColorService dynamicColorService = new(options, seedColorService, _application, _preferences);

dynamicColorService.Initialize(null);

Assert.AreEqual(unchecked((int)0xFFc07d52), dynamicColorService.Seed);
Assert.AreEqual(unchecked((int)0xFF96490A), dynamicColorService.SchemeInt.Primary);
Assert.AreEqual(dynamicColorService.SchemeInt.Primary, dynamicColorService.SchemeMaui.Primary.ToInt());
Assert.IsNotNull(_application.Resources[Schemes.Keys.Primary]);
}

private static IOptions<DynamicColorOptions> CreateOptions()
{
DynamicColorOptions options = new();
return Options.Create(options);
}

private static IOptions<DynamicColorOptions> CreateOptions(Action<DynamicColorOptions> configure)
{
DynamicColorOptions options = new();
configure(options);
return Options.Create(options);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<UseMaui>true</UseMaui>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Options" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.8" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.8" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\MaterialColorUtilities.Maui\MaterialColorUtilities.Maui.csproj" />
</ItemGroup>

</Project>
33 changes: 33 additions & 0 deletions MaterialColorUtilities.Maui.Tests/MockPreferences.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Text.Json;

namespace MaterialColorUtilities.Maui.Tests;

public class MockPreferences : IPreferences
{
private readonly Dictionary<string, string> _container = new();

public bool ContainsKey(string key, string? sharedName = null)
{
return _container.ContainsKey(key);
}

public void Remove(string key, string? sharedName = null)
{
_container.Remove(key);
}

public void Clear(string? sharedName = null)
{
_container.Clear();
}

public void Set<T>(string key, T value, string? sharedName = null)
{
_container[key] = JsonSerializer.Serialize(value);
}

public T Get<T>(string key, T defaultValue, string? sharedName = null)
{
return JsonSerializer.Deserialize<T>(_container[key]) ?? throw new InvalidOperationException();
}
}
14 changes: 14 additions & 0 deletions MaterialColorUtilities.Maui.Tests/MockSeedColorService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace MaterialColorUtilities.Maui.Tests;

public class MockSeedColorService : ISeedColorService
{
public MockSeedColorService() { }

public MockSeedColorService(int? seedColor)
{
SeedColor = seedColor;
}

public int? SeedColor { get; }
public event Action? OnSeedColorChanged { add { } remove { } }
}
1 change: 1 addition & 0 deletions MaterialColorUtilities.Maui.Tests/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Microsoft.VisualStudio.TestTools.UnitTesting;
27 changes: 21 additions & 6 deletions MaterialColorUtilities.Maui/DynamicColorOptions.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
namespace MaterialColorUtilities.Maui;

public class DynamicColorOptions
public sealed class DynamicColorOptions
{
/// <summary>
/// Will be used if a dynamic accent color is not available.
/// When true, updates to Application.UserAppTheme will be stored using Preferences
/// and reapplied when the app restarts. Defaults to true.
/// </summary>
public int FallbackSeed { get; set; } = unchecked((int)0xff4285F4); // Google Blue
public bool RememberIsDark { get; set; } = true;

/// <summary>
/// Determines whether to generate colors as application resources.
/// Set to false when using a custom color scheme source.
/// </summary>
/// <remarks>
/// Can be updated at runtime using DynamicColorService.IsEnabled.
/// </remarks>
public bool EnableTheming { get; set; } = true;

/// <summary>
/// Whether to use wallpaper/accent color based dynamic theming.
/// Will be used if a dynamic seed color is not available or dynamic theming is disabled.
/// </summary>
public int FallbackSeed { get; set; } = unchecked((int)0xff4285F4); // Google Blue

/// <summary>
/// Whether to use wallpaper/accent color based dynamic theming. Defaults to true.
/// </summary>
/// <remarks>
/// When set to <see langword="false"/>, <see cref="FallbackSeed"/> will be used as seed,
/// even on platforms that expose an accent color.
/// </remarks>
public bool UseDynamicColor { get; set; } = true;
}
public bool EnableDynamicColor { get; set; } = true;
}
168 changes: 0 additions & 168 deletions MaterialColorUtilities.Maui/DynamicColorService.Android.cs

This file was deleted.

Loading

0 comments on commit a705549

Please sign in to comment.