From b444306c9bd24cb21f34ff2f61c71af60daf052a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albert=20Rag=C3=A1ny-N=C3=A9meth?= Date: Sun, 23 Oct 2022 20:38:38 +0200 Subject: [PATCH] Enable nullable reference types (#27) --- .../DynamicColorService.cs | 15 +++-- .../MaterialColorUtilities.Maui.csproj | 1 + .../SeedColorService.Android.cs | 30 ++++----- .../SeedColorService.Mac.cs | 4 +- .../SeedColorService.Windows.cs | 2 +- .../MaterialColorUtilities.csproj | 2 +- .../Quantize/QuantizerWsmeans.cs | 2 + .../Quantize/QuantizerWu.cs | 2 + MaterialColorUtilities/Schemes/Scheme.cs | 65 ++++++++++--------- 9 files changed, 65 insertions(+), 58 deletions(-) diff --git a/MaterialColorUtilities.Maui/DynamicColorService.cs b/MaterialColorUtilities.Maui/DynamicColorService.cs index c06b0cf..5646f37 100644 --- a/MaterialColorUtilities.Maui/DynamicColorService.cs +++ b/MaterialColorUtilities.Maui/DynamicColorService.cs @@ -18,6 +18,7 @@ public DynamicColorService(IOptions options, ISeedColorServ } } +// TODO: Rename to MaterialColorService public class DynamicColorService< [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TCorePalette, @@ -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!; /// /// When the seed is set, it is stored using Preferences and will be reapplied the next time the app is launched. @@ -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) @@ -206,7 +207,7 @@ private void Update() .Where(m => m.Name == nameof(Scheme.ConvertTo)) .ToList()[0] .MakeGenericMethod(typeof(Color)) - .Invoke(SchemeInt, new object[] { (Func)Color.FromUint }); + .Invoke(SchemeInt, new object[] { (Func)Color.FromUint })!; } #if PLATFORM @@ -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); } @@ -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)!; } } diff --git a/MaterialColorUtilities.Maui/MaterialColorUtilities.Maui.csproj b/MaterialColorUtilities.Maui/MaterialColorUtilities.Maui.csproj index c192300..18cf85c 100644 --- a/MaterialColorUtilities.Maui/MaterialColorUtilities.Maui.csproj +++ b/MaterialColorUtilities.Maui/MaterialColorUtilities.Maui.csproj @@ -8,6 +8,7 @@ true true enable + enable 14.2 14.0 diff --git a/MaterialColorUtilities.Maui/SeedColorService.Android.cs b/MaterialColorUtilities.Maui/SeedColorService.Android.cs index 026fd4b..3f88cac 100644 --- a/MaterialColorUtilities.Maui/SeedColorService.Android.cs +++ b/MaterialColorUtilities.Maui/SeedColorService.Android.cs @@ -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) { @@ -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 => @@ -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 { @@ -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; } @@ -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(); } @@ -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(); } @@ -177,20 +177,20 @@ private async void CheckWallpaper() /// Requires permission 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]; diff --git a/MaterialColorUtilities.Maui/SeedColorService.Mac.cs b/MaterialColorUtilities.Maui/SeedColorService.Mac.cs index 59f8c50..2f4eadc 100644 --- a/MaterialColorUtilities.Maui/SeedColorService.Mac.cs +++ b/MaterialColorUtilities.Maui/SeedColorService.Mac.cs @@ -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, @@ -36,5 +36,5 @@ public uint? SeedColor } } - public event Action OnSeedColorChanged; + public event Action? OnSeedColorChanged; } \ No newline at end of file diff --git a/MaterialColorUtilities.Maui/SeedColorService.Windows.cs b/MaterialColorUtilities.Maui/SeedColorService.Windows.cs index 41fcc9e..49ac2ab 100644 --- a/MaterialColorUtilities.Maui/SeedColorService.Windows.cs +++ b/MaterialColorUtilities.Maui/SeedColorService.Windows.cs @@ -21,5 +21,5 @@ public uint? SeedColor } } - public event Action OnSeedColorChanged; + public event Action? OnSeedColorChanged; } \ No newline at end of file diff --git a/MaterialColorUtilities/MaterialColorUtilities.csproj b/MaterialColorUtilities/MaterialColorUtilities.csproj index 9ba1fe2..8e499e4 100644 --- a/MaterialColorUtilities/MaterialColorUtilities.csproj +++ b/MaterialColorUtilities/MaterialColorUtilities.csproj @@ -4,7 +4,7 @@ netstandard2.0;netstandard2.1;net6.0 10 enable - disable + enable true 0.1.0 diff --git a/MaterialColorUtilities/Quantize/QuantizerWsmeans.cs b/MaterialColorUtilities/Quantize/QuantizerWsmeans.cs index 7031b7a..68fc04d 100644 --- a/MaterialColorUtilities/Quantize/QuantizerWsmeans.cs +++ b/MaterialColorUtilities/Quantize/QuantizerWsmeans.cs @@ -13,6 +13,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#nullable disable + namespace MaterialColorUtilities.Quantize; /// diff --git a/MaterialColorUtilities/Quantize/QuantizerWu.cs b/MaterialColorUtilities/Quantize/QuantizerWu.cs index d674c29..f27bd3e 100644 --- a/MaterialColorUtilities/Quantize/QuantizerWu.cs +++ b/MaterialColorUtilities/Quantize/QuantizerWu.cs @@ -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; diff --git a/MaterialColorUtilities/Schemes/Scheme.cs b/MaterialColorUtilities/Schemes/Scheme.cs index a663535..fadc590 100644 --- a/MaterialColorUtilities/Schemes/Scheme.cs +++ b/MaterialColorUtilities/Schemes/Scheme.cs @@ -21,38 +21,38 @@ namespace MaterialColorUtilities.Schemes; /// The type of the named colors. public class Scheme { - 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!; /// /// Converts the Scheme into a new one with a different color type. @@ -66,6 +66,7 @@ public class Scheme /// isn't nested inside another class /// and has a generic type parameter for at least the color type. /// + // TODO: Rename to Convert public Scheme ConvertTo(Func convert) { return ConvertTo(convert, new());