Add Navigations
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Primitives;
|
||||
|
||||
namespace Toolkit.Foundation.Avalonia
|
||||
{
|
||||
public class ContentControlHandler : NavigationRouteHandler<ContentControl>
|
||||
{
|
||||
public override void Receive(NavigationRouteRequest<ContentControl> message)
|
||||
{
|
||||
if (message.Template is TemplatedControl control)
|
||||
{
|
||||
control.DataContext = message.Data;
|
||||
message.Target.Content = control;
|
||||
}
|
||||
|
||||
message.Reply(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
using FluentAvalonia.UI.Controls;
|
||||
using Kromek.Framework.Core.Extensions;
|
||||
using Toolkit.Foundation.Avalonia;
|
||||
|
||||
namespace Kromek.Framework.Avalonia
|
||||
{
|
||||
public class ContentDialogHandler : NavigationRouteHandler<ContentDialog>
|
||||
{
|
||||
public override async void Receive(NavigationRouteRequest<ContentDialog> message)
|
||||
{
|
||||
if (message.Template is ContentDialog contentDialog)
|
||||
{
|
||||
async void HandleButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
|
||||
{
|
||||
ContentDialogButtonClickDeferral defferal = args.GetDeferral();
|
||||
|
||||
if (sender.DataContext is INavigationConfirmationAsync confirmationAsync)
|
||||
{
|
||||
if (!await confirmationAsync.CanConfirmAsync())
|
||||
{
|
||||
args.Cancel = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (sender.DataContext is INavigationConfirmation confirmation)
|
||||
{
|
||||
if (!confirmation.CanConfirm())
|
||||
{
|
||||
args.Cancel = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!args.Cancel)
|
||||
{
|
||||
contentDialog.SecondaryButtonClick -= HandleButtonClick;
|
||||
contentDialog.PrimaryButtonClick -= HandleButtonClick;
|
||||
contentDialog.CloseButtonClick -= HandleButtonClick;
|
||||
}
|
||||
|
||||
defferal.Complete();
|
||||
}
|
||||
|
||||
contentDialog.SecondaryButtonClick += HandleButtonClick;
|
||||
contentDialog.PrimaryButtonClick += HandleButtonClick;
|
||||
contentDialog.CloseButtonClick += HandleButtonClick;
|
||||
|
||||
contentDialog.DataContext = message.Data;
|
||||
await contentDialog.ShowAsync();
|
||||
|
||||
message.Reply(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
using Avalonia.Controls.Primitives;
|
||||
using FluentAvalonia.UI.Controls;
|
||||
using FluentAvalonia.UI.Navigation;
|
||||
using Toolkit.Foundation.Avalonia;
|
||||
|
||||
namespace Kromek.Framework.Avalonia
|
||||
{
|
||||
public class FrameHandler : NavigationRouteHandler<Frame>
|
||||
{
|
||||
public override async void Receive(NavigationRouteRequest<Frame> message)
|
||||
{
|
||||
message.Target.NavigationPageFactory = new NavigationPageFactory();
|
||||
|
||||
TaskCompletionSource<bool> completionSource = new();
|
||||
if (message.Template is TemplatedControl content)
|
||||
{
|
||||
void HandleNavigated(object sender, NavigationEventArgs args)
|
||||
{
|
||||
message.Target.Navigated -= HandleNavigated;
|
||||
if (message.Target.Content is TemplatedControl control)
|
||||
{
|
||||
control.DataContext = message.Data;
|
||||
completionSource.SetResult(true);
|
||||
}
|
||||
}
|
||||
|
||||
message.Target.Navigated += HandleNavigated;
|
||||
message.Target.NavigateFromObject(content);
|
||||
}
|
||||
|
||||
bool result = await completionSource.Task;
|
||||
message.Reply(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using FluentAvalonia.UI.Controls;
|
||||
using Avalonia.Controls;
|
||||
|
||||
namespace Toolkit.Foundation.Avalonia
|
||||
{
|
||||
internal class NavigationPageFactory : INavigationPageFactory
|
||||
{
|
||||
public IControl? GetPage(Type srcType)
|
||||
{
|
||||
return default;
|
||||
}
|
||||
|
||||
public IControl GetPageFromObject(object target)
|
||||
{
|
||||
return (IControl)target;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
using Avalonia.Controls.Primitives;
|
||||
using Avalonia.Interactivity;
|
||||
|
||||
namespace Toolkit.Foundation.Avalonia
|
||||
{
|
||||
public class NavigationRoute : INavigationRoute
|
||||
{
|
||||
private readonly INavigationRouteDescriptorCollection routes;
|
||||
|
||||
public NavigationRoute(INavigationRouteDescriptorCollection routes)
|
||||
{
|
||||
this.routes = routes;
|
||||
}
|
||||
|
||||
public void Add(string name, object route)
|
||||
{
|
||||
if (route is TemplatedControl control)
|
||||
{
|
||||
void HandleUnloaded(object? sender, RoutedEventArgs args)
|
||||
{
|
||||
if (routes.FirstOrDefault(x => x.Route == sender) is INavigationRouteDescriptor descriptor)
|
||||
{
|
||||
routes.Remove(descriptor);
|
||||
}
|
||||
}
|
||||
|
||||
control.Unloaded += HandleUnloaded;
|
||||
}
|
||||
|
||||
if (routes.FirstOrDefault(x => x.Name == name) is INavigationRouteDescriptor descriptor)
|
||||
{
|
||||
routes.Remove(descriptor);
|
||||
}
|
||||
|
||||
routes.Add(new NavigationRouteDescriptor(name, route));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
using Avalonia.Controls.Primitives;
|
||||
using CommunityToolkit.Mvvm.Messaging;
|
||||
|
||||
namespace Toolkit.Foundation.Avalonia
|
||||
{
|
||||
public abstract class NavigationRouteHandler<TTarget> : IRecipient<NavigationRouteRequest<TTarget>> where TTarget : TemplatedControl
|
||||
{
|
||||
public abstract void Receive(NavigationRouteRequest<TTarget> message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
using Avalonia.Controls.Primitives;
|
||||
using CommunityToolkit.Mvvm.Messaging.Messages;
|
||||
|
||||
namespace Toolkit.Foundation.Avalonia
|
||||
{
|
||||
public class NavigationRouteRequest<TTarget> : AsyncRequestMessage<bool> where TTarget : TemplatedControl
|
||||
{
|
||||
public NavigationRouteRequest(TTarget target, object? data, object? template, IDictionary<string, object>? parameters = null)
|
||||
{
|
||||
Target = target;
|
||||
Data = data;
|
||||
Template = template;
|
||||
Parameters = parameters;
|
||||
}
|
||||
|
||||
public TTarget Target { get; }
|
||||
|
||||
public object? Data { get; }
|
||||
|
||||
public object? Template { get; }
|
||||
|
||||
public IDictionary<string, object>? Parameters { get; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Primitives;
|
||||
using CommunityToolkit.Mvvm.Messaging;
|
||||
using FluentAvalonia.UI.Controls;
|
||||
|
||||
namespace Toolkit.Foundation.Avalonia
|
||||
{
|
||||
public class NavigationRouter : INavigationRouter
|
||||
{
|
||||
private readonly INavigationRouteDescriptorCollection descriptors;
|
||||
private readonly IMessenger messenger;
|
||||
private readonly INamedDataTemplateFactory namedDataTemplateFactory;
|
||||
private readonly INamedTemplateFactory namedTemplateFactory;
|
||||
private readonly ITemplateDescriptorProvider templateDescriptorProvider;
|
||||
private readonly ITemplateFactory templateFactory;
|
||||
private readonly ITypedDataTemplateFactory typedDataTemplateFactory;
|
||||
|
||||
public NavigationRouter(ITemplateDescriptorProvider templateDescriptorProvider,
|
||||
ITemplateFactory templateFactory,
|
||||
INamedTemplateFactory namedTemplateFactory,
|
||||
INamedDataTemplateFactory namedDataTemplateFactory,
|
||||
ITypedDataTemplateFactory typedDataTemplateFactory,
|
||||
IMessenger messenger,
|
||||
INavigationRouteDescriptorCollection descriptors)
|
||||
{
|
||||
this.templateDescriptorProvider = templateDescriptorProvider;
|
||||
this.templateFactory = templateFactory;
|
||||
this.namedTemplateFactory = namedTemplateFactory;
|
||||
this.namedDataTemplateFactory = namedDataTemplateFactory;
|
||||
this.typedDataTemplateFactory = typedDataTemplateFactory;
|
||||
this.messenger = messenger;
|
||||
this.descriptors = descriptors;
|
||||
}
|
||||
|
||||
public Task InitializeAsync()
|
||||
{
|
||||
messenger.Register<Navigate>(this, (sender, args) => OnNavigate(args));
|
||||
messenger.Register<NavigateBack>(this, (sender, args) => OnNavigateBack(args));
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private async void OnNavigate(Navigate args)
|
||||
{
|
||||
object? data = null;
|
||||
object? template = null;
|
||||
|
||||
Dictionary<string, object> keyedParameters = new();
|
||||
List<object> parameters = new();
|
||||
|
||||
foreach (object? parameter in args.Parameters)
|
||||
{
|
||||
if (parameter is not null)
|
||||
{
|
||||
if (parameter is KeyValuePair<string, object> keyed)
|
||||
{
|
||||
keyedParameters.Add(keyed.Key, keyed.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
parameters.Add(parameter);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (args.Name is { Length: > 0 } name)
|
||||
{
|
||||
data = namedDataTemplateFactory.Create(name, parameters.ToArray());
|
||||
template = namedTemplateFactory.Create(name);
|
||||
}
|
||||
|
||||
if (args.Type is Type type)
|
||||
{
|
||||
data = typedDataTemplateFactory.Create(type, parameters.ToArray());
|
||||
template = templateFactory.Create(data);
|
||||
}
|
||||
|
||||
if (template is not null)
|
||||
{
|
||||
bool navigated = false;
|
||||
if (template is ContentDialog contentDialog)
|
||||
{
|
||||
navigated = await messenger.Send(new NavigationRouteRequest<ContentDialog>(contentDialog, data, template, keyedParameters));
|
||||
}
|
||||
else
|
||||
{
|
||||
if (descriptors.FirstOrDefault(x => args.Route is string { } name && name == x.Name) is NavigationRouteDescriptor descriptor)
|
||||
{
|
||||
switch (descriptor.Route)
|
||||
{
|
||||
case Frame frame:
|
||||
navigated = await messenger.Send(new NavigationRouteRequest<Frame>(frame, data, template, keyedParameters));
|
||||
break;
|
||||
case ContentControl contentControl:
|
||||
navigated = await messenger.Send(new NavigationRouteRequest<ContentControl>(contentControl, data, template, keyedParameters));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (navigated)
|
||||
{
|
||||
messenger.Send((Navigated)Navigated.Create((dynamic?)template, (dynamic?)data, keyedParameters));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (descriptors.FirstOrDefault(x => args.Route is string { } name && name == x.Name) is NavigationRouteDescriptor descriptor)
|
||||
{
|
||||
if (descriptor.Route is ContentControl contentControl)
|
||||
{
|
||||
contentControl.Content = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnNavigateBack(NavigateBack args)
|
||||
{
|
||||
if (descriptors.FirstOrDefault(x => args.Route is string { } name && name == x.Name) is NavigationRouteDescriptor descriptor)
|
||||
{
|
||||
if (descriptor.Route is ContentControl { Content: TemplatedControl content })
|
||||
{
|
||||
if (content.DataContext is IDisposable disposable)
|
||||
{
|
||||
disposable.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
if (descriptor.Route is Frame frame)
|
||||
{
|
||||
frame.GoBack();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user