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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Avalonia" Version="11.0.0-preview4" />
|
||||
<PackageReference Include="FluentAvaloniaUI" Version="2.0.0-preview4" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Toolkit.Foundation\Toolkit.Foundation.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,10 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Toolkit.Foundation
|
||||
{
|
||||
public interface IEventParameter
|
||||
{
|
||||
List<object> GetValues(EventArgs args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace Kromek.Framework.Core.Extensions
|
||||
{
|
||||
public interface INavigationConfirmation
|
||||
{
|
||||
bool CanConfirm();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
|
||||
namespace Kromek.Framework.Core.Extensions
|
||||
{
|
||||
public interface INavigationConfirmationAsync
|
||||
{
|
||||
Task<bool> CanConfirmAsync();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace Toolkit.Foundation
|
||||
{
|
||||
public interface INavigationRoute
|
||||
{
|
||||
void Add(string name, object route);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace Toolkit.Foundation
|
||||
{
|
||||
public interface INavigationRouteDescriptor
|
||||
{
|
||||
object Route { get; }
|
||||
|
||||
string? Name { get; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Toolkit.Foundation
|
||||
{
|
||||
public interface INavigationRouteDescriptorCollection : IList<INavigationRouteDescriptor>
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace Toolkit.Foundation
|
||||
{
|
||||
public interface INavigationRouter : IInitializer
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Toolkit.Foundation
|
||||
{
|
||||
public interface IParameter
|
||||
{
|
||||
string? Key { get; }
|
||||
|
||||
KeyValuePair<string, object>? GetValue(object target);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
using System;
|
||||
|
||||
namespace Toolkit.Foundation
|
||||
{
|
||||
public record Navigate
|
||||
{
|
||||
public Navigate(string name, params object?[] parameters)
|
||||
{
|
||||
Name = name;
|
||||
Parameters = parameters;
|
||||
}
|
||||
|
||||
public Navigate(Type type, params object?[] parameters)
|
||||
{
|
||||
Type = type;
|
||||
Parameters = parameters;
|
||||
}
|
||||
|
||||
public Type? Type { get; }
|
||||
|
||||
public object? Route { get; init; }
|
||||
|
||||
public string? Name { get; }
|
||||
|
||||
public string? FriendlyName { get; init; }
|
||||
|
||||
public object?[] Parameters { get; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace Toolkit.Foundation
|
||||
{
|
||||
public record NavigateBack(object Route);
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using CommunityToolkit.Mvvm.Messaging;
|
||||
|
||||
namespace Toolkit.Foundation
|
||||
{
|
||||
public class NavigateHandler : IRecipient<Navigate>
|
||||
{
|
||||
private readonly IMessenger messenger;
|
||||
|
||||
public NavigateHandler(IMessenger messenger)
|
||||
{
|
||||
this.messenger = messenger;
|
||||
}
|
||||
|
||||
public void Receive(Navigate request)
|
||||
{
|
||||
messenger.Send(request);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
namespace Toolkit.Foundation
|
||||
{
|
||||
public class Navigated<TContent, TDataContext> where TContent : class where TDataContext : class
|
||||
{
|
||||
public Navigated()
|
||||
{
|
||||
}
|
||||
|
||||
public Navigated(TContent content, TDataContext dataContext, IDictionary<string, object>? parameters = null)
|
||||
{
|
||||
Content = content;
|
||||
DataContext = dataContext;
|
||||
Parameters = parameters;
|
||||
}
|
||||
|
||||
public TContent? Content { get; }
|
||||
|
||||
public TDataContext? DataContext { get; }
|
||||
|
||||
public IDictionary<string, object>? Parameters { get; }
|
||||
}
|
||||
|
||||
public class Navigated
|
||||
{
|
||||
public static Navigated<TTemplate, TDataTemplate> Create<TTemplate, TDataTemplate>(TTemplate content, TDataTemplate dataContext, IDictionary<string, object>? parameters = null) where TTemplate : class where TDataTemplate : class
|
||||
{
|
||||
return new Navigated<TTemplate, TDataTemplate>(content, dataContext, parameters);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
namespace Toolkit.Foundation
|
||||
{
|
||||
public record NavigationRouteDescriptor : INavigationRouteDescriptor
|
||||
{
|
||||
public NavigationRouteDescriptor(string name, object route)
|
||||
{
|
||||
Name = name;
|
||||
Route = route;
|
||||
}
|
||||
|
||||
public string Name { get; }
|
||||
|
||||
public object Route { get; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace Toolkit.Foundation
|
||||
{
|
||||
public class NavigationRouteDescriptorCollection : List<INavigationRouteDescriptor>, INavigationRouteDescriptorCollection
|
||||
{
|
||||
public NavigationRouteDescriptorCollection(IEnumerable<INavigationRouteDescriptor> collection) : base(collection)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
+7
-1
@@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.4.33110.190
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Toolkit.Foundation", "Toolkit.Foundation\Toolkit.Foundation.csproj", "{A3332A95-DF10-4A3F-A500-18513BF1EE2E}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Toolkit.Foundation", "Toolkit.Foundation\Toolkit.Foundation.csproj", "{A3332A95-DF10-4A3F-A500-18513BF1EE2E}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Toolkit.Foundation.Avalonia", "Toolkit.Foundation.Avalonia\Toolkit.Foundation.Avalonia.csproj", "{31217E1F-8F2D-468C-A9DC-839EE9B6F9D4}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
@@ -15,6 +17,10 @@ Global
|
||||
{A3332A95-DF10-4A3F-A500-18513BF1EE2E}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{A3332A95-DF10-4A3F-A500-18513BF1EE2E}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{A3332A95-DF10-4A3F-A500-18513BF1EE2E}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{31217E1F-8F2D-468C-A9DC-839EE9B6F9D4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{31217E1F-8F2D-468C-A9DC-839EE9B6F9D4}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{31217E1F-8F2D-468C-A9DC-839EE9B6F9D4}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{31217E1F-8F2D-468C-A9DC-839EE9B6F9D4}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
||||
Reference in New Issue
Block a user