Skip to content

Commit

Permalink
chore: enable nullability for core mvvm
Browse files Browse the repository at this point in the history
  • Loading branch information
RLittlesII committed May 14, 2024
1 parent c333750 commit a12ff18
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 105 deletions.
12 changes: 6 additions & 6 deletions src/Prism.Core/Mvvm/BindableBase.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;

#nullable enable
namespace Prism.Mvvm
{
/// <summary>
Expand All @@ -13,7 +12,7 @@ public abstract class BindableBase : INotifyPropertyChanged
/// <summary>
/// Occurs when a property value changes.
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
public event PropertyChangedEventHandler? PropertyChanged;

/// <summary>
/// Checks if a property already matches a desired value. Sets the property and
Expand All @@ -27,7 +26,7 @@ public abstract class BindableBase : INotifyPropertyChanged
/// support CallerMemberName.</param>
/// <returns>True if the value was changed, false if the existing value matched the
/// desired value.</returns>
protected virtual bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
protected virtual bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string? propertyName = null)
{
if (EqualityComparer<T>.Default.Equals(storage, value)) return false;

Expand All @@ -50,7 +49,8 @@ protected virtual bool SetProperty<T>(ref T storage, T value, [CallerMemberName]
/// <param name="onChanged">Action that is called after the property value has been changed.</param>
/// <returns>True if the value was changed, false if the existing value matched the
/// desired value.</returns>
protected virtual bool SetProperty<T>(ref T storage, T value, Action onChanged, [CallerMemberName] string propertyName = null)
protected virtual bool SetProperty<T>(ref T storage, T value, Action? onChanged,
[CallerMemberName] string? propertyName = null)
{
if (EqualityComparer<T>.Default.Equals(storage, value)) return false;

Expand All @@ -67,7 +67,7 @@ protected virtual bool SetProperty<T>(ref T storage, T value, Action onChanged,
/// <param name="propertyName">Name of the property used to notify listeners. This
/// value is optional and can be provided automatically when invoked from compilers
/// that support <see cref="CallerMemberNameAttribute"/>.</param>
protected void RaisePropertyChanged([CallerMemberName] string propertyName = null)
protected void RaisePropertyChanged([CallerMemberName] string? propertyName = null)
{
OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
}
Expand Down
21 changes: 6 additions & 15 deletions src/Prism.Core/Mvvm/ErrorsContainer.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

#nullable enable
namespace Prism.Mvvm
{
/// <summary>
Expand Down Expand Up @@ -53,10 +51,7 @@ public bool HasErrors
/// Returns all the errors in the container.
/// </summary>
/// <returns>The dictionary of errors per property.</returns>
public Dictionary<string, List<T>> GetErrors()
{
return validationResults;
}
public Dictionary<string, List<T>> GetErrors() => validationResults;

/// <summary>
/// Gets the validation errors for a specified property.
Expand All @@ -65,9 +60,8 @@ public Dictionary<string, List<T>> GetErrors()
/// <returns>The validation errors of type <typeparamref name="T"/> for the property.</returns>
public IEnumerable<T> GetErrors(string propertyName)
{
var localPropertyName = propertyName ?? string.Empty;
List<T> currentValidationResults = null;
if (this.validationResults.TryGetValue(localPropertyName, out currentValidationResults))
var localPropertyName = propertyName;
if (this.validationResults.TryGetValue(localPropertyName, out var currentValidationResults))
{
return currentValidationResults;
}
Expand Down Expand Up @@ -110,10 +104,7 @@ public void ClearErrors<TProperty>(Expression<Func<TProperty>> propertyExpressio
/// <example>
/// container.ClearErrors("SomeProperty");
/// </example>
public void ClearErrors(string propertyName)
{
this.SetErrors(propertyName, new List<T>());
}
public void ClearErrors(string propertyName) => this.SetErrors(propertyName, new List<T>());

/// <summary>
/// Sets the validation errors for the specified property.
Expand All @@ -140,7 +131,7 @@ public void SetErrors(string propertyName, IEnumerable<T> newValidationResults)
{
var localPropertyName = propertyName ?? string.Empty;
var hasCurrentValidationResults = this.validationResults.ContainsKey(localPropertyName);
var hasNewValidationResults = newValidationResults != null && newValidationResults.Count() > 0;
var hasNewValidationResults = newValidationResults.Count() > 0;

if (hasCurrentValidationResults || hasNewValidationResults)
{
Expand Down
6 changes: 1 addition & 5 deletions src/Prism.Core/Mvvm/IViewRegistry.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using Prism.Ioc;

namespace Prism.Mvvm;
namespace Prism.Mvvm;

/// <summary>
/// Provides an abstraction layer for ViewRegistration that can be mocked
Expand Down
4 changes: 1 addition & 3 deletions src/Prism.Core/Mvvm/PropertySupport.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@


using System;
using System.Linq.Expressions;
using System.Reflection;
using Prism.Properties;

#nullable enable
namespace Prism.Mvvm
{
///<summary>
Expand Down
5 changes: 2 additions & 3 deletions src/Prism.Core/Mvvm/ViewCreationException.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;

#nullable enable
namespace Prism.Mvvm;

/// <summary>
Expand All @@ -23,7 +22,7 @@ public ViewCreationException(string viewName, ViewType viewType)
/// <param name="viewName">The name of the view that failed to create.</param>
/// <param name="viewType">The type of view that failed to create (Page, Region, or Dialog).</param>
/// <param name="innerException">The inner exception that caused the view creation to fail.</param>
public ViewCreationException(string viewName, ViewType viewType, Exception innerException)
public ViewCreationException(string viewName, ViewType viewType, Exception? innerException)
: base($"Unable to create {viewType} '{viewName}'.", innerException)
{
ViewName = viewName;
Expand Down
6 changes: 3 additions & 3 deletions src/Prism.Core/Mvvm/ViewModelCreationException.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using System;
using System.ComponentModel;
using System.ComponentModel;

#nullable enable
namespace Prism.Mvvm;

/// <summary>
/// Exception thrown when an error occurs during ViewModel creation.
/// </summary>
public class ViewModelCreationException : Exception
{
private static Func<object, string> _viewNameDelegate = null;
private static Func<object, string>? _viewNameDelegate;

/// <summary>
/// Gets the name of the view associated with the exception, based on platform-specific logic.
Expand Down
37 changes: 14 additions & 23 deletions src/Prism.Core/Mvvm/ViewModelLocationProvider.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Reflection;

#nullable enable
namespace Prism.Mvvm
{
/// <summary>
Expand Down Expand Up @@ -52,24 +49,24 @@ public static void Reset()
/// <summary>
/// ViewModelFactory that provides the View instance and ViewModel type as parameters.
/// </summary>
static Func<object, Type, object> _defaultViewModelFactoryWithViewParameter;
static Func<object, Type, object>? _defaultViewModelFactoryWithViewParameter;

/// <summary>
/// Default view type to view model type resolver, assumes the view model is in same assembly as the view type, but in the "ViewModels" namespace.
/// </summary>
static Func<Type, Type> _defaultViewTypeToViewModelTypeResolver = DefaultViewTypeToViewModel;
static Func<Type, Type?> _defaultViewTypeToViewModelTypeResolver = DefaultViewTypeToViewModel;

private static Type DefaultViewTypeToViewModel(Type viewType)
private static Type? DefaultViewTypeToViewModel(Type viewType)
{
var viewName = viewType.FullName;
viewName = viewName.Replace(".Views.", ".ViewModels.");
viewName = viewName?.Replace(".Views.", ".ViewModels.");
var viewAssemblyName = viewType.GetTypeInfo().Assembly.FullName;
var suffix = viewName.EndsWith("View") ? "Model" : "ViewModel";
var suffix = viewName != null && viewName.EndsWith("View") ? "Model" : "ViewModel";
var viewModelName = string.Format(CultureInfo.InvariantCulture, "{0}{1}, {2}", viewName, suffix, viewAssemblyName);
return Type.GetType(viewModelName);
}

static Func<object, Type> _defaultViewToViewModelTypeResolver = view => null;
static Func<object, Type?> _defaultViewToViewModelTypeResolver = view => null;

/// <summary>
/// Sets the default view model factory.
Expand All @@ -93,7 +90,7 @@ public static void SetDefaultViewModelFactory(Func<object, Type, object> viewMod
/// Sets the default view type to view model type resolver.
/// </summary>
/// <param name="viewTypeToViewModelTypeResolver">The view type to view model type resolver.</param>
public static void SetDefaultViewTypeToViewModelTypeResolver(Func<Type, Type> viewTypeToViewModelTypeResolver)
public static void SetDefaultViewTypeToViewModelTypeResolver(Func<Type, Type?> viewTypeToViewModelTypeResolver)
{
_defaultViewTypeToViewModelTypeResolver = viewTypeToViewModelTypeResolver;
}
Expand All @@ -102,7 +99,7 @@ public static void SetDefaultViewTypeToViewModelTypeResolver(Func<Type, Type> vi
/// Sets the default ViewModel Type Resolver given the View instance. This can be used to evaluate the View for
/// custom attributes or Attached Properties to determine the ViewModel Type.
/// </summary>
public static void SetDefaultViewToViewModelTypeResolver(Func<object, Type> viewToViewModelTypeResolver) =>
public static void SetDefaultViewToViewModelTypeResolver(Func<object, Type?> viewToViewModelTypeResolver) =>
_defaultViewToViewModelTypeResolver = viewToViewModelTypeResolver;

/// <summary>
Expand All @@ -114,7 +111,7 @@ public static void SetDefaultViewToViewModelTypeResolver(Func<object, Type> view
public static void AutoWireViewModelChanged(object view, Action<object, object> setDataContextCallback)
{
// Try mappings first
object viewModel = GetViewModelForView(view);
object? viewModel = GetViewModelForView(view);

// try to use ViewModel type
if (viewModel == null)
Expand Down Expand Up @@ -144,30 +141,24 @@ public static void AutoWireViewModelChanged(object view, Action<object, object>
/// </summary>
/// <param name="view">The view that the view model wants.</param>
/// <returns>The ViewModel that corresponds to the view passed as a parameter.</returns>
private static object GetViewModelForView(object view)
private static object? GetViewModelForView(object view)
{
var viewKey = view.GetType().ToString();

// Mapping of view models base on view type (or instance) goes here
if (_factories.ContainsKey(viewKey))
return _factories[viewKey]();

return null;
return _factories.ContainsKey(viewKey) ? _factories[viewKey]() : null;
}

/// <summary>
/// Gets the ViewModel type for the specified view.
/// </summary>
/// <param name="view">The View that the ViewModel wants.</param>
/// <returns>The ViewModel type that corresponds to the View.</returns>
private static Type GetViewModelTypeForView(Type view)
private static Type? GetViewModelTypeForView(Type view)
{
var viewKey = view.ToString();

if (_typeFactories.ContainsKey(viewKey))
return _typeFactories[viewKey];

return null;
return _typeFactories.ContainsKey(viewKey) ? _typeFactories[viewKey] : null;
}

/// <summary>
Expand Down
4 changes: 1 addition & 3 deletions src/Prism.Core/Mvvm/ViewRegistration.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;

namespace Prism.Mvvm;
namespace Prism.Mvvm;

/// <summary>
/// Represents information about a registered view.
Expand Down
Loading

0 comments on commit a12ff18

Please sign in to comment.