Skip to content

Commit

Permalink
chore: updating for modern C# syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
dansiegel committed Feb 24, 2024
1 parent ae26f82 commit 22629b3
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 38 deletions.
10 changes: 10 additions & 0 deletions src/Maui/Prism.Maui/AppModel/IPageLifecycleAware.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
namespace Prism.AppModel;

/// <summary>
/// Interface that defines lifecycle events for pages.
/// </summary>
public interface IPageLifecycleAware
{
/// <summary>
/// Called when the page is appearing.
/// </summary>
void OnAppearing();

/// <summary>
/// Called when the page is disappearing.
/// </summary>
void OnDisappearing();
}
5 changes: 5 additions & 0 deletions src/Maui/Prism.Maui/Behaviors/PageLifeCycleAwareBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@

namespace Prism.Behaviors;

/// <summary>
/// Provides lifecycle events for <see cref="Page"/> and <see cref="IPageLifecycleAware"/> ViewModels.
/// </summary>
public class PageLifeCycleAwareBehavior : BehaviorBase<Page>
{
/// <inheritdoc />
protected override void OnAttachedTo(Page bindable)
{
base.OnAttachedTo(bindable);
bindable.Appearing += OnAppearing;
bindable.Disappearing += OnDisappearing;
}

/// <inheritdoc />
protected override void OnDetachingFrom(Page bindable)
{
base.OnDetachingFrom(bindable);
Expand Down
2 changes: 2 additions & 0 deletions src/Maui/Prism.Maui/Behaviors/PageScopeBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ namespace Prism.Behaviors;
/// </summary>
public sealed class PageScopeBehavior : BehaviorBase<Page>
{
/// <inheritdoc />
protected override void OnAttachedTo(Page page)
{
base.OnAttachedTo(page);
// Ensure the scope gets created and NavigationService is created
Navigation.Xaml.Navigation.GetNavigationService(page);
}

/// <inheritdoc />
protected override void OnDetachingFrom(Page page)
{
base.OnDetachingFrom(page);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace Prism.Behaviors;

/// <summary>
/// Provides <see cref="IActiveAware"/> support for the children of the <see cref="TabbedPage"/>
/// </summary>
public class TabbedPageActiveAwareBehavior : MultiPageActiveAwareBehavior<Page>
{
}
15 changes: 13 additions & 2 deletions src/Maui/Prism.Maui/Dialogs/DialogViewRegistry.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
using Prism.Ioc;
using Prism.Mvvm;
using Prism.Mvvm;

namespace Prism.Dialogs;

/// <summary>
/// Implementation of a view registry specifically for dialog views.
/// </summary>
public class DialogViewRegistry : ViewRegistryBase, IDialogViewRegistry
{
/// <summary>
/// Initializes a new instance of the <see cref="DialogViewRegistry"/> class with the specified view registrations.
/// </summary>
/// <param name="registrations">The collection of view registrations to manage.</param>
public DialogViewRegistry(IEnumerable<ViewRegistration> registrations)
: base(ViewType.Dialog, registrations)
{
}

/// <summary>
/// Configures a dialog view with the specified context and container provider.
/// </summary>
/// <param name="bindable">The bindable object representing the dialog view to configure.</param>
/// <param name="container">The container provider to use for resolving dependencies.</param>
protected override void ConfigureView(BindableObject bindable, IContainerProvider container)
{
}
Expand Down
3 changes: 3 additions & 0 deletions src/Maui/Prism.Maui/Dialogs/IDialogViewRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Prism.Dialogs;

/// <summary>
/// Interface that defines a registry for managing views specific to dialogs.
/// </summary>
public interface IDialogViewRegistry : IViewRegistry
{
}
9 changes: 9 additions & 0 deletions src/Maui/Prism.Maui/Dialogs/KnownDialogParameters.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@

namespace Prism.Dialogs;

/// <summary>
/// Provides well-known parameter names used for configuring dialogs.
/// </summary>
public static class KnownDialogParameters
{
/// <summary>
/// Parameter name used to control whether a dialog should close when the background is tapped.
/// </summary>
public const string CloseOnBackgroundTapped = "closeOnBackgroundTapped";

/// <summary>
/// Parameter name used to pass additional custom parameters to a dialog.
/// </summary>
public const string XamlParam = "xamlParam";
}
7 changes: 2 additions & 5 deletions src/Maui/Prism.Maui/Dialogs/Xaml/DialogLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,8 @@ public static void SetMask(BindableObject bindable, View value) =>
/// <returns>True if a mask is used for the dialog, false if not, or null if not explicitly set.</returns>
public static bool? GetUseMask(BindableObject bindable)
{
var value = bindable.GetValue(UseMaskProperty);
if (value is bool boolean)
return boolean;

return true; // Default to using a mask if not explicitly set
// Default to using a mask if not explicitly set
return bindable.GetValue(UseMaskProperty) is bool boolean ? boolean : true;
}

/// <summary>
Expand Down
12 changes: 5 additions & 7 deletions src/Prism.Core/Common/MulticastExceptionHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

Expand Down Expand Up @@ -62,17 +62,15 @@ public async Task HandleAsync(Exception exception, object? parameter = null)
return;

// Get Invoke() method of the delegate
var invokeMethod = multicastDelegate.GetType().GetMethod("Invoke");

if (invokeMethod == null)
throw new InvalidOperationException($"Could not find Invoke() method for delegate of type {multicastDelegate.GetType().Name}");
var invokeMethod = multicastDelegate.GetType().GetMethod("Invoke")
?? throw new InvalidOperationException($"Could not find Invoke() method for delegate of type {multicastDelegate.GetType().Name}");

var parameters = invokeMethod.GetParameters();
var arguments = parameters.Length switch
{
0 => Array.Empty<object?>(),
1 => typeof(Exception).IsAssignableFrom(parameters[0].ParameterType) ? new object?[] { exception } : new object?[] { parameter },
2 => typeof(Exception).IsAssignableFrom(parameters[0].ParameterType) ? new object?[] { exception, parameter } : new object?[] { parameter, exception },
1 => typeof(Exception).IsAssignableFrom(parameters[0].ParameterType) ? [exception] : [parameter],
2 => typeof(Exception).IsAssignableFrom(parameters[0].ParameterType) ? [exception, parameter] : [parameter, exception],
_ => throw new InvalidOperationException($"Handler of type {multicastDelegate.GetType().Name} is not supported", exception)
};

Expand Down
37 changes: 13 additions & 24 deletions src/Prism.Core/Mvvm/ViewRegistryBase{TBaseView}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,27 +99,27 @@ private IEnumerable<Type> GetCandidates(Type viewModelType)

names = names.Where(x => !x.EndsWith("PagePage")).ToList();

var namespaces = _registryType switch
string[] namespaces = _registryType switch
{
ViewType.Page => new[]
{
ViewType.Page =>
[
viewModelType.Namespace.Replace("ViewModels", "Views"),
viewModelType.Namespace.Replace("ViewModels", "Pages")
},
ViewType.Region => new[]
{
],
ViewType.Region =>
[
viewModelType.Namespace.Replace("ViewModels", "Views"),
viewModelType.Namespace.Replace("ViewModels", "Regions")
},
ViewType.Dialog => new[]
{
],
ViewType.Dialog =>
[
viewModelType.Namespace.Replace("ViewModels", "Views"),
viewModelType.Namespace.Replace("ViewModels", "Dialogs")
},
_ => new[]
{
],
_ =>
[
viewModelType.Namespace.Replace("ViewModels", "Views"),
}
]
};
var candidates = namespaces.Select(@namespace => names.Select(name => $"{@namespace}.{name}"))
Expand Down Expand Up @@ -209,15 +209,4 @@ protected ViewRegistration GetRegistration(string name) =>
/// <param name="view"></param>
/// <param name="container"></param>
protected abstract void SetContainerProvider(TBaseView view, IContainerProvider container);

//public static Type GetPageType(string name)
//{
// var registrations = _registrations.Where(x => x.Name == name);
// if (!registrations.Any())
// throw new KeyNotFoundException(name);
// if (registrations.Count() > 1)
// throw new InvalidOperationException(string.Format(Resources.MultipleViewsRegisteredForNavigationKey, name, string.Join(", ", registrations.Select(x => x.View.FullName))));

// return registrations.First().View;
//}
}

0 comments on commit 22629b3

Please sign in to comment.