Skip to content

Commit

Permalink
Merge pull request #32 from NeverMorewd/replay-navigation
Browse files Browse the repository at this point in the history
fix onnavigatedfrom
  • Loading branch information
NeverMorewd authored Nov 12, 2024
2 parents 999a77f + 87372da commit 5b83193
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 31 deletions.
22 changes: 15 additions & 7 deletions src/Lemon.ModuleNavigation.Avaloniaui/Regions/AvaloniauiRegion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ namespace Lemon.ModuleNavigation.Avaloniaui.Regions
public abstract class AvaloniauiRegion : IRegion
{
private readonly Dictionary<string, IView> _viewCache;
private readonly ConcurrentItem<(IView, INavigationAware)> _current;
public AvaloniauiRegion()
{
_viewCache = [];
_current = new();
RegionTemplate = GetDataTemplate();
}
public abstract string Name
Expand Down Expand Up @@ -41,24 +43,30 @@ private IDataTemplate GetDataTemplate()
{
return default;
}
if (context.RequestNew || !_viewCache.TryGetValue(context.ViewName, out IView? view))
if (context.RequestNew || !_viewCache.TryGetValue(context.TargetViewName, out IView? view))
{
view = context.ServiceProvider.GetRequiredKeyedService<IView>(context.ViewName);
view = context.ServiceProvider.GetRequiredKeyedService<IView>(context.TargetViewName);

var viewFullName = view.GetType().FullName;

context.Uri = new Uri($"avares://{viewFullName}.axaml");
var viewModel = context.ServiceProvider.GetRequiredKeyedService<INavigationAware>(context.ViewName);
viewModel.OnNavigatedTo(context);
if (viewModel.IsNavigationTarget(context))
var navigationAware = context.ServiceProvider.GetRequiredKeyedService<INavigationAware>(context.TargetViewName);
if (_current.TryTakeData(out (IView, INavigationAware) data))
{
view.DataContext = viewModel;
data.Item2.OnNavigatedFrom(context);
}
if (navigationAware.IsNavigationTarget(context))
{
view.DataContext = navigationAware;
navigationAware.OnNavigatedTo(context);
_current.SetData((view, navigationAware));
}
else
{
return default;
}
_viewCache.TryAdd(context.ViewName, view);

_viewCache.TryAdd(context.TargetViewName, view);
}
return view as Control;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public override void Activate(NavigationContext target)
{
if(Content is NavigationContext current)
{
if (target.ViewName == current.ViewName)
if (target.TargetViewName == current.TargetViewName)
{
return;
}
Expand Down
12 changes: 6 additions & 6 deletions src/Lemon.ModuleNavigation.Sample/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public MainViewModel(IEnumerable<IModule> modules,
_dialogService = dialogService;
_regionManager = regionManager;
// default view
_navigationService.NavigateToView("ContentRegion", "ViewAlpha", false);
_navigationService.RequestViewNavigation("ContentRegion", "ViewAlpha", false);
Modules = new ObservableCollection<IModule>(modules);
ToViewCommand = ReactiveCommand.Create<string>(content =>
{
Expand All @@ -42,10 +42,10 @@ public MainViewModel(IEnumerable<IModule> modules,
requestNew = true;

}
_navigationService.NavigateToView("ContentRegion", viewName, requestNew);
_navigationService.NavigateToView("TabRegion", viewName, requestNew);
_navigationService.NavigateToView("ItemsRegion", viewName, requestNew);
_navigationService.NavigateToView("TransitioningContentRegion", viewName, requestNew);
_navigationService.RequestViewNavigation("ContentRegion", viewName, requestNew);
_navigationService.RequestViewNavigation("TabRegion", viewName, requestNew);
_navigationService.RequestViewNavigation("ItemsRegion", viewName, requestNew);
_navigationService.RequestViewNavigation("TransitioningContentRegion", viewName, requestNew);
});
ToDialogCommand = ReactiveCommand.Create<string>(content =>
{
Expand Down Expand Up @@ -78,7 +78,7 @@ public MainViewModel(IEnumerable<IModule> modules,
});
_regionManager.Subscribe<NavigationContext>(n =>
{
_logger.LogDebug($"Request to : {n.RegionName}.{n.ViewName}");
_logger.LogDebug($"Request to : {n.RegionName}.{n.TargetViewName}");
});
_regionManager.Subscribe<IRegion>(r =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ namespace Lemon.ModuleNavigation.Abstracts
public interface IViewNavigationService
{
IDisposable BindingViewNavigationHandler(IViewNavigationHandler handler);
void NavigateToView(string regionName,
void RequestViewNavigation(string regionName,
string viewKey,
bool requestNew = false);
void NavigateToView(string regionName,
void RequestViewNavigation(string regionName,
string viewKey,
NavigationParameters parameters,
bool requestNew = false);
Expand Down
36 changes: 36 additions & 0 deletions src/Lemon.ModuleNavigation/ConcurrentItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Collections.Concurrent;

namespace Lemon.ModuleNavigation
{
public class ConcurrentItem<T>
{
private const int SIZE = 1;
private readonly BlockingCollection<T> _collection = new(SIZE);
public ConcurrentItem() { }

public void SetData(T data)
{
if (_collection.Count == SIZE)
{
_collection.Take();
}
_collection.Add(data);
}

public T TakeData()
{
return _collection.Take();
}

public bool TryTakeData(out T? data)
{
return _collection.TryTake(out data);
}

public bool WaitForData(TimeSpan timeSpan, out T? data)
{
return _collection.TryTake(out data, timeSpan);
}
}

}
10 changes: 5 additions & 5 deletions src/Lemon.ModuleNavigation/Core/NavigationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal NavigationContext(string viewName,
bool requestNew,
NavigationParameters? navigationParameters)
{
ViewName = viewName;
TargetViewName = viewName;
Parameters = navigationParameters;
RequestNew = requestNew;
RegionName = regionName;
Expand All @@ -18,7 +18,7 @@ internal NavigationContext(string viewName,
}
public static ViewNameComparer ViewNameComparer => new();
public static StrictComparer StrictComparer => new();
public string ViewName
public string TargetViewName
{
get;
private set;
Expand Down Expand Up @@ -50,7 +50,7 @@ public IServiceProvider ServiceProvider
}
public override string ToString()
{
return ViewName;
return TargetViewName;
}
}
public class ViewNameComparer : IEqualityComparer<NavigationContext>
Expand All @@ -59,12 +59,12 @@ public bool Equals(NavigationContext? x, NavigationContext? y)
{
if (x == null && y == null) return true;
if (x == null || y == null) return false;
return x.ViewName == y.ViewName;
return x.TargetViewName == y.TargetViewName;
}

public int GetHashCode(NavigationContext obj)
{
return obj.ViewName.GetHashCode();
return obj.TargetViewName.GetHashCode();
}
}
public class StrictComparer : IEqualityComparer<NavigationContext>
Expand Down
6 changes: 4 additions & 2 deletions src/Lemon.ModuleNavigation/Core/NavigationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public class NavigationService : INavigationService
{
private readonly List<IModuleNavigationHandler> _handlers = [];
private readonly List<IViewNavigationHandler> _viewHandlers = [];

// reserve only one for now.
private readonly ConcurrentStack<(IModule module, NavigationParameters parameter)> _bufferModule = [];
private readonly ConcurrentStack<(string moduleName, NavigationParameters parameter)> _bufferModuleName = [];
private readonly ConcurrentStack<(string regionName, string viewName, bool requestNew)> _bufferViewName = [];
Expand Down Expand Up @@ -36,7 +38,7 @@ public void RequestModuleNavigate(string moduleName, NavigationParameters parame
}
_bufferModuleName.Push((moduleName, parameters));
}
public void NavigateToView(string regionName,
public void RequestViewNavigation(string regionName,
string viewKey,
bool requestNew = false)
{
Expand Down Expand Up @@ -86,7 +88,7 @@ IDisposable IViewNavigationService.BindingViewNavigationHandler(IViewNavigationH
});
}

public void NavigateToView(string regionName,
public void RequestViewNavigation(string regionName,
string viewKey,
NavigationParameters parameters,
bool requestNew = false)
Expand Down
2 changes: 1 addition & 1 deletion src/Lemon.ModuleNavigation/Internals/ConcurrentSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Lemon.ModuleNavigation.Internals
{
internal class ConcurrentSet<T> : IEnumerable<T> where T : notnull
internal sealed class ConcurrentSet<T> : IEnumerable<T> where T : notnull
{
private readonly ConcurrentDictionary<T, byte> _dict = new();

Expand Down
2 changes: 1 addition & 1 deletion src/Lemon.ModuleNavigation/Internals/DisposableAction.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Lemon.ModuleNavigation.Internals
{
internal class DisposableAction : IDisposable
internal sealed class DisposableAction : IDisposable
{
private readonly Action _action;
private int _disposed;
Expand Down
8 changes: 2 additions & 6 deletions src/Lemon.ModuleNavigation/Parameters/BaseParameters.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lemon.ModuleNavigation.Parameters
{
Expand Down Expand Up @@ -55,7 +51,7 @@ public IEnumerable<T> GetValues<T>(string key) where T : notnull
}
public bool TryGetValue<T>(string key, out T? value) where T : notnull
{
return _entries.TryGetValue<T>(key, out value);
return _entries.TryGetValue(key, out value);
}
IEnumerator IEnumerable.GetEnumerator()
{
Expand Down

0 comments on commit 5b83193

Please sign in to comment.