Skip to content

Commit

Permalink
Enable nullable reference types (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
albi005 committed Oct 23, 2022
1 parent 7e4d949 commit b444306
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 58 deletions.
15 changes: 8 additions & 7 deletions MaterialColorUtilities.Maui/DynamicColorService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public DynamicColorService(IOptions<DynamicColorOptions> options, ISeedColorServ
}
}

// TODO: Rename to MaterialColorService
public class DynamicColorService<
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
TCorePalette,
Expand Down Expand Up @@ -124,9 +125,9 @@ public uint Seed
}
}

public TCorePalette CorePalette { get; protected set; }
public TSchemeInt SchemeInt { get; protected set; }
public TSchemeMaui SchemeMaui { get; protected set; }
public TCorePalette CorePalette { get; protected set; } = null!;
public TSchemeInt SchemeInt { get; protected set; } = null!;
public TSchemeMaui SchemeMaui { get; protected set; } = null!;

/// <summary>
/// When the seed is set, it is stored using Preferences and will be reapplied the next time the app is launched.
Expand All @@ -139,7 +140,7 @@ public void ForgetSeed()
}

// Called by MauiAppBuilder.Build()
public virtual void Initialize(IServiceProvider services)
public virtual void Initialize(IServiceProvider? services)
{
if (_preferences.ContainsKey(IsDarkKey))
_application.UserAppTheme = _preferences.Get(IsDarkKey, false)
Expand Down Expand Up @@ -206,7 +207,7 @@ private void Update()
.Where(m => m.Name == nameof(Scheme<int>.ConvertTo))
.ToList()[0]
.MakeGenericMethod(typeof(Color))
.Invoke(SchemeInt, new object[] { (Func<uint, Color>)Color.FromUint });
.Invoke(SchemeInt, new object[] { (Func<uint, Color>)Color.FromUint })!;
}

#if PLATFORM
Expand All @@ -221,7 +222,7 @@ protected virtual void Apply()
foreach (PropertyInfo property in typeof(TSchemeMaui).GetProperties())
{
string key = property.Name;
Color value = (Color)property.GetValue(SchemeMaui);
Color value = (Color)property.GetValue(SchemeMaui)!;
_appResources[key] = value;
_appResources[key + "Brush"] = new SolidColorBrush(value);
}
Expand All @@ -236,6 +237,6 @@ protected virtual void Apply()
// TODO: Replace with using empty constructor and method call
private static TCorePalette CreateCorePalette(uint seed)
{
return (TCorePalette)Activator.CreateInstance(typeof(TCorePalette), seed, false);
return (TCorePalette)Activator.CreateInstance(typeof(TCorePalette), seed, false)!;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<UseMaui>true</UseMaui>
<SingleProject>true</SingleProject>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">14.2</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'maccatalyst'">14.0</SupportedOSPlatformVersion>
Expand Down
30 changes: 15 additions & 15 deletions MaterialColorUtilities.Maui/SeedColorService.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ public class SeedColorService : ISeedColorService

private readonly LifecycleEventService _lifecycleEventService;
private readonly IPreferences _preferences;
private readonly WallpaperManager _wallpaperManager = WallpaperManager.GetInstance(Platform.AppContext);
private readonly WallpaperManager _wallpaperManager = WallpaperManager.GetInstance(Platform.AppContext)!;

private bool _hasInitialized;
private int? _wallpaperId;
private uint? _SeedColorCache;
private uint? _seedColorCache;

public SeedColorService(ILifecycleEventService lifecycleEventService, IPreferences preferences)
{
Expand All @@ -44,7 +44,7 @@ private void EnsureInitialized()
if (_preferences.ContainsKey(SeedColorCacheKey))
{
_wallpaperId = _preferences.Get(WallpaperIdKey, 0);
_SeedColorCache = _preferences.Get(SeedColorCacheKey, 0U);
_seedColorCache = _preferences.Get(SeedColorCacheKey, 0U);
}

_lifecycleEventService.AddAndroid(a =>
Expand All @@ -71,11 +71,11 @@ private void EnsureInitialized()
>= 31 => GuessAndroid12Seed(),
>= 27 => GetAndroid8PrimaryWallpaperColor(),
#pragma warning restore CA1416
>= 24 => _SeedColorCache,
>= 24 => _seedColorCache,
_ => null
};

private event Action OnSeedColorChanged;
private event Action? OnSeedColorChanged;

event Action ISeedColorService.OnSeedColorChanged
{
Expand Down Expand Up @@ -121,7 +121,7 @@ event Action ISeedColorService.OnSeedColorChanged
if (id == Android.Resource.Color.SystemAccent1500)
{
// If Primary50 didn't change, return
if (color == _wallpaperId) return _SeedColorCache;
if (color == _wallpaperId) return _seedColorCache;
_wallpaperId = color;
}

Expand All @@ -133,14 +133,14 @@ event Action ISeedColorService.OnSeedColorChanged
}
}

_SeedColorCache = closestColor;
_seedColorCache = closestColor;
return closestColor;
}

[SupportedOSPlatform("android27.0")]
private uint? GetAndroid8PrimaryWallpaperColor()
{
WallpaperColors colors = _wallpaperManager.GetWallpaperColors((int)WallpaperManagerFlags.System);
WallpaperColors? colors = _wallpaperManager.GetWallpaperColors((int)WallpaperManagerFlags.System);
return (uint?)colors?.PrimaryColor.ToArgb();
}

Expand All @@ -160,14 +160,14 @@ private async void CheckWallpaper()

_wallpaperId = wallpaperId;

_SeedColorCache = await Task.Run(QuantizeWallpaper);
_seedColorCache = await Task.Run(QuantizeWallpaper);

_preferences.Set(WallpaperIdKey, wallpaperId);

if (_SeedColorCache == null)
if (_seedColorCache == null)
_preferences.Remove(SeedColorCacheKey);
else
_preferences.Set(SeedColorCacheKey, (int)_SeedColorCache);
_preferences.Set(SeedColorCacheKey, (int)_seedColorCache);
OnSeedColorChanged?.Invoke();
}

Expand All @@ -177,20 +177,20 @@ private async void CheckWallpaper()
/// <remarks>Requires permission <see cref="Permissions.StorageRead"/></remarks>
private uint? QuantizeWallpaper()
{
uint[] pixels = GetWallpaperPixels();
uint[]? pixels = GetWallpaperPixels();
if (pixels == null) return null;
return ImageUtils.ColorsFromImage(pixels)[0];
}

private uint[] GetWallpaperPixels()
private uint[]? GetWallpaperPixels()
{
Drawable drawable = _wallpaperManager.Drawable;
Drawable? drawable = _wallpaperManager.Drawable;
if (drawable is not BitmapDrawable bitmapDrawable || bitmapDrawable.Bitmap == null) return null;
Bitmap bitmap = bitmapDrawable.Bitmap;
if (bitmap.Height * bitmap.Width > 112 * 112)
{
Size optimalSize = CalculateOptimalSize(bitmap.Width, bitmap.Height);
bitmap = Bitmap.CreateScaledBitmap(bitmap, optimalSize.Width, optimalSize.Height, false);
bitmap = Bitmap.CreateScaledBitmap(bitmap, optimalSize.Width, optimalSize.Height, false)!;
}

int[] pixels = new int[bitmap!.ByteCount / 4];
Expand Down
4 changes: 2 additions & 2 deletions MaterialColorUtilities.Maui/SeedColorService.Mac.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public uint? SeedColor
{
get
{
UIColor accentColor = _dummyButton.TintColor;
UIColor? accentColor = _dummyButton.TintColor;
if (accentColor == null) return null;
accentColor.GetRGBA(
out NFloat r,
Expand All @@ -36,5 +36,5 @@ public uint? SeedColor
}
}

public event Action OnSeedColorChanged;
public event Action? OnSeedColorChanged;
}
2 changes: 1 addition & 1 deletion MaterialColorUtilities.Maui/SeedColorService.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ public uint? SeedColor
}
}

public event Action OnSeedColorChanged;
public event Action? OnSeedColorChanged;
}
2 changes: 1 addition & 1 deletion MaterialColorUtilities/MaterialColorUtilities.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0</TargetFrameworks>
<LangVersion>10</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>
<Nullable>enable</Nullable>

<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>0.1.0</Version>
Expand Down
2 changes: 2 additions & 0 deletions MaterialColorUtilities/Quantize/QuantizerWsmeans.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#nullable disable

namespace MaterialColorUtilities.Quantize;

/// <summary>
Expand Down
2 changes: 2 additions & 0 deletions MaterialColorUtilities/Quantize/QuantizerWu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#nullable disable

using MaterialColorUtilities.Utils;

namespace MaterialColorUtilities.Quantize;
Expand Down
65 changes: 33 additions & 32 deletions MaterialColorUtilities/Schemes/Scheme.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,38 +21,38 @@ namespace MaterialColorUtilities.Schemes;
/// <typeparam name="TColor">The type of the named colors.</typeparam>
public class Scheme<TColor>
{
public TColor Primary { get; set; }
public TColor OnPrimary { get; set; }
public TColor PrimaryContainer { get; set; }
public TColor OnPrimaryContainer { get; set; }
public TColor Secondary { get; set; }
public TColor OnSecondary { get; set; }
public TColor SecondaryContainer { get; set; }
public TColor OnSecondaryContainer { get; set; }
public TColor Tertiary { get; set; }
public TColor OnTertiary { get; set; }
public TColor TertiaryContainer { get; set; }
public TColor OnTertiaryContainer { get; set; }
public TColor Error { get; set; }
public TColor OnError { get; set; }
public TColor ErrorContainer { get; set; }
public TColor OnErrorContainer { get; set; }
public TColor Background { get; set; }
public TColor OnBackground { get; set; }
public TColor Surface { get; set; }
public TColor OnSurface { get; set; }
public TColor SurfaceVariant { get; set; }
public TColor OnSurfaceVariant { get; set; }
public TColor Outline { get; set; }
public TColor Shadow { get; set; }
public TColor InverseSurface { get; set; }
public TColor InverseOnSurface { get; set; }
public TColor InversePrimary { get; set; }
public TColor Surface1 { get; set; }
public TColor Surface2 { get; set; }
public TColor Surface3 { get; set; }
public TColor Surface4 { get; set; }
public TColor Surface5 { get; set; }
public TColor Primary { get; set; } = default!;
public TColor OnPrimary { get; set; } = default!;
public TColor PrimaryContainer { get; set; } = default!;
public TColor OnPrimaryContainer { get; set; } = default!;
public TColor Secondary { get; set; } = default!;
public TColor OnSecondary { get; set; } = default!;
public TColor SecondaryContainer { get; set; } = default!;
public TColor OnSecondaryContainer { get; set; } = default!;
public TColor Tertiary { get; set; } = default!;
public TColor OnTertiary { get; set; } = default!;
public TColor TertiaryContainer { get; set; } = default!;
public TColor OnTertiaryContainer { get; set; } = default!;
public TColor Error { get; set; } = default!;
public TColor OnError { get; set; } = default!;
public TColor ErrorContainer { get; set; } = default!;
public TColor OnErrorContainer { get; set; } = default!;
public TColor Background { get; set; } = default!;
public TColor OnBackground { get; set; } = default!;
public TColor Surface { get; set; } = default!;
public TColor OnSurface { get; set; } = default!;
public TColor SurfaceVariant { get; set; } = default!;
public TColor OnSurfaceVariant { get; set; } = default!;
public TColor Outline { get; set; } = default!;
public TColor Shadow { get; set; } = default!;
public TColor InverseSurface { get; set; } = default!;
public TColor InverseOnSurface { get; set; } = default!;
public TColor InversePrimary { get; set; } = default!;
public TColor Surface1 { get; set; } = default!;
public TColor Surface2 { get; set; } = default!;
public TColor Surface3 { get; set; } = default!;
public TColor Surface4 { get; set; } = default!;
public TColor Surface5 { get; set; } = default!;

/// <summary>
/// Converts the Scheme into a new one with a different color type.
Expand All @@ -66,6 +66,7 @@ public class Scheme<TColor>
/// isn't nested inside another class
/// and has a generic type parameter for at least the color type.
/// </remarks>
// TODO: Rename to Convert
public Scheme<TResult> ConvertTo<TResult>(Func<TColor, TResult> convert)
{
return ConvertTo(convert, new());
Expand Down

0 comments on commit b444306

Please sign in to comment.