Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
NeverMorewd committed Dec 9, 2024
2 parents d1f1cb5 + 423b6d1 commit c0a619b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 43 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dotnet-desktop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [windows-2022, ubuntu-22.04, macos-12]
os: [windows-2022, ubuntu-22.04, macos-13]
steps:
- uses: actions/checkout@v3
- name: Setup .NET 8
Expand Down
77 changes: 36 additions & 41 deletions src/Lemon.ModuleNavigation.Avaloniaui/Regions/AvaloniauiRegion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,73 +3,68 @@
using Lemon.ModuleNavigation.Abstracts;
using Lemon.ModuleNavigation.Core;
using Microsoft.Extensions.DependencyInjection;
using System.Collections.Concurrent;
using System.Collections.ObjectModel;

namespace Lemon.ModuleNavigation.Avaloniaui.Regions
{
public abstract class AvaloniauiRegion : IRegion
{
private readonly Dictionary<string, IView> _viewCache;
private readonly ConcurrentItem<(IView, INavigationAware)> _current;
private readonly ConcurrentDictionary<string, IView> _viewCache = new();
private readonly ConcurrentItem<(IView View, INavigationAware NavigationAware)> _current = new();

public AvaloniauiRegion()
{
_viewCache = [];
_current = new();
RegionTemplate = GetDataTemplate();
}
public abstract string Name
{
get;
}
public abstract ObservableCollection<NavigationContext> Contexts
{
get;
}
public IDataTemplate? RegionTemplate
{
get;
set;
RegionTemplate = CreateRegionDataTemplate();
}

public abstract void Activate(NavigationContext target);
public abstract string Name { get; }
public abstract ObservableCollection<NavigationContext> Contexts { get; }

public IDataTemplate? RegionTemplate { get; set; }

public abstract void Activate(NavigationContext target);
public abstract void DeActivate(NavigationContext target);

private IDataTemplate GetDataTemplate()
private IDataTemplate CreateRegionDataTemplate()
{
return new FuncDataTemplate<NavigationContext>((context, np) =>
{
if (context == null)
{
return default;
}
if (context.RequestNew || !_viewCache.TryGetValue(context.TargetViewName, out IView? view))
{
view = context.ServiceProvider.GetRequiredKeyedService<IView>(context.TargetViewName);
return null;

var viewFullName = view.GetType().FullName;
bool needNewView = context.RequestNew ||
!_viewCache.TryGetValue(context.TargetViewName, out IView? view);

context.Uri = new Uri($"avares://{viewFullName}.axaml");
if (needNewView)
{
// Create new view and navigation aware instance
view = context.ServiceProvider.GetRequiredKeyedService<IView>(context.TargetViewName);
var navigationAware = context.ServiceProvider.GetRequiredKeyedService<INavigationAware>(context.TargetViewName);
if (_current.TryTakeData(out (IView, INavigationAware) data))
{
data.Item2.OnNavigatedFrom(context);
}
if (navigationAware.IsNavigationTarget(context))
{
view.DataContext = navigationAware;
navigationAware.OnNavigatedTo(context);
_current.SetData((view, navigationAware));
}
else

// Handle previous navigation
if (_current.TryTakeData(out var previousData))
{
return default;
previousData.NavigationAware.OnNavigatedFrom(context);
}

// Validate navigation target
if (!navigationAware.IsNavigationTarget(context))
return null;

// Setup new view
view.DataContext = navigationAware;
navigationAware.OnNavigatedTo(context);
_current.SetData((view, navigationAware));
_viewCache.TryAdd(context.TargetViewName, view);
}
else
{
view = _viewCache[context.TargetViewName];
}

return view as Control;
});
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public override void Activate(NavigationContext target)
{
if(Content is NavigationContext current)
{
if (target.TargetViewName == current.TargetViewName)
if (target.TargetViewName == current.TargetViewName
&& !target.RequestNew)
{
return;
}
Expand Down

0 comments on commit c0a619b

Please sign in to comment.