-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathRegion.cs
More file actions
145 lines (131 loc) · 4.13 KB
/
Region.cs
File metadata and controls
145 lines (131 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using Avalonia.Controls;
using Avalonia.Controls.Templates;
using Lemon.ModuleNavigation.Abstractions;
using Lemon.ModuleNavigation.Core;
using Microsoft.Extensions.DependencyInjection;
using System.Collections.Concurrent;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
namespace Lemon.ModuleNavigation.Avaloniaui.Regions;
public abstract class Region : IRegion
{
public Region(string name)
{
Name = name;
Current = new();
ViewCache = [];
Contexts = [];
RegionContentTemplate = CreateRegionDataTemplate();
Contexts.CollectionChanged += Contexts_CollectionChanged;
}
protected ConcurrentItem<(IView View, INavigationAware NavigationAware)> Current
{
get;
}
public string Name
{
get;
}
protected ConcurrentDictionary<NavigationContext, IView> ViewCache
{
get;
}
protected ConcurrentDictionary<string, IView> ViewNameCache
{
get
{
return new(Contexts
.GroupBy(kv => kv.ViewName)
.Select(group => new KeyValuePair<string, IView>(
group.Key,
group.Last().View!)));
}
}
public ObservableCollection<NavigationContext> Contexts
{
get;
}
public IDataTemplate? RegionContentTemplate
{
get;
}
public virtual void ScrollIntoView(int index)
{
throw new NotImplementedException();
}
public virtual void ScrollIntoView(NavigationContext item)
{
throw new NotImplementedException();
}
public abstract void Activate(NavigationContext target);
public abstract void DeActivate(string viewName);
public abstract void DeActivate(NavigationContext target);
protected IView? ResolveView(NavigationContext context)
{
var view = context.View;
INavigationAware? navigationAware = null;
if (view is null)
{
view = context.ServiceProvider.GetRequiredKeyedService<IView>(context.ViewName);
navigationAware = context.ServiceProvider.GetRequiredKeyedService<INavigationAware>(context.ViewName);
view.DataContext = navigationAware;
if (navigationAware is ICanUnload canUnloadNavigationAware)
{
canUnloadNavigationAware.RequestUnload += () =>
{
DeActivate(context);
};
}
context.View = view;
ViewCache.AddOrUpdate(context, view, (key, value) => view);
}
else
{
navigationAware = view.DataContext as INavigationAware
?? context.ServiceProvider.GetRequiredKeyedService<INavigationAware>(context.ViewName);
}
if (Current.TryTakeData(out var previousData))
{
previousData.NavigationAware.OnNavigatedFrom(context);
}
navigationAware.OnNavigatedTo(context);
Current.SetData((view, navigationAware));
context.Alias = navigationAware.Alias;
return view;
}
protected virtual void WhenContextsAdded(IEnumerable<NavigationContext> contexts)
{
}
protected virtual void WhenContextsRemoved(IEnumerable<NavigationContext> contexts)
{
}
private void Contexts_CollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Add)
{
if (e.NewItems is not null)
{
WhenContextsAdded(e.NewItems.Cast<NavigationContext>());
}
}
if (e.Action == NotifyCollectionChangedAction.Remove)
{
if (e.OldItems is not null)
{
WhenContextsRemoved(e.OldItems.Cast<NavigationContext>());
}
}
}
private IDataTemplate CreateRegionDataTemplate()
{
return new FuncDataTemplate<NavigationContext>((context, np) =>
{
if (context is null)
{
return null;
}
var view = ResolveView(context);
return view as Control;
});
}
}