Skip to content

Commit

Permalink
Merge pull request #40 from NeverMorewd/bugfix-modulenavi
Browse files Browse the repository at this point in the history
Bugfix modulenavi
  • Loading branch information
NeverMorewd authored Feb 9, 2025
2 parents f85e35e + c95897a commit a559216
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ private static void SetBinding(Control control,
{
return null;
}
return navigationHandler.ModuleManager.CreateView(m) as Control;
return navigationHandler.ModuleManager.GetOrCreateView(m, regionName) as Control;
});
}
else if (control is ItemsControl itemsControl)
Expand Down Expand Up @@ -248,7 +248,7 @@ private static void SetBinding(Control control,
{
return null;
}
return navigationHandler.ModuleManager.CreateView(m) as Control;
return navigationHandler.ModuleManager.GetOrCreateView(m, regionName) as Control;
});
}
else if (control is ContentControl contentControl)
Expand Down
8 changes: 8 additions & 0 deletions src/Lemon.ModuleNavigation/Abstracts/IModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ public Type ViewModelType
{
get;
}
public IView? View
{
get;
}
public IModuleNavigationAware? ViewModel
{
get;
}
public void Initialize();
}
}
32 changes: 27 additions & 5 deletions src/Lemon.ModuleNavigation/Core/ModuleManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Lemon.ModuleNavigation.Abstracts;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Concurrent;
using System.Collections.ObjectModel;
using System.ComponentModel;
Expand All @@ -11,11 +10,15 @@ namespace Lemon.ModuleNavigation.Core
public class ModuleManager : IModuleManager, INotifyPropertyChanged
{
private readonly ConcurrentDictionary<string, IModule> _modulesCache;
private readonly ConcurrentDictionary<(string, string), IView> _regionCache;
private readonly ConcurrentDictionary<(string RegionName, string ModuleKey), IView> _regionCache;
private readonly IServiceProvider _serviceProvider;
public ModuleManager(IEnumerable<IModule> modules, IServiceProvider serviceProvider)
private readonly IRegionManager _regionManager;
public ModuleManager(IEnumerable<IModule> modules,
IRegionManager regionManager,
IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
_regionManager = regionManager;
_regionCache = [];
_modulesCache = new ConcurrentDictionary<string, IModule>(modules.ToDictionary(m => m.Key, m => m));
Modules = _modulesCache.Values;
Expand Down Expand Up @@ -85,7 +88,6 @@ public void RequestNavigate(IModule module, NavigationParameters parameters)
ActiveModules.Add(module);
}
}

///TODO:Consider an async implementation
module.Initialize();
module.IsActivated = true;
Expand All @@ -111,10 +113,30 @@ public IView GetOrCreateView(IModule module, string regionName)
}
else
{
var view = CreateView(module);
IView view;
if (!IsRenderedOnAnyRegion(module.Key))
{
view = module.View!;
}
else
{
view = CreateView(module);
}
_regionCache.TryAdd((regionName, module.Key), view);
return view;
}
}

private bool IsRenderedOnAnyRegion(string moduleKey)
{
if (!_regionCache.IsEmpty)
{
foreach (var item in _regionCache)
{
return (item.Key.ModuleKey == moduleKey);
}
}
return false;
}
}
}

0 comments on commit a559216

Please sign in to comment.