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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using Tango.Core;
using Tango.Core.Commands;
using Tango.PPC.Common;
using Tango.PPC.Common.Modules;
using Tango.PPC.Common.Navigation;
using Tango.PPC.UI.Views;
using Tango.SharedUI.Controls;
namespace Tango.PPC.UI.Navigation
{
/// <summary>
/// Represents the default PPC navigation manager.
/// </summary>
/// <seealso cref="Tango.PPC.Common.Navigation.INavigationManager" />
public class DefaultNavigationManager : ExtendedObject, INavigationManager
{
private event Action<Object, Object> NavigationCycleCompleted;
private IPPCModuleLoader _moduleLoader;
private Object _currentVM;
private String _lastFullPath;
private bool _preventHistory;
private Stack<String> _navigationHistory;
private IPPCModule _currentModule;
/// <summary>
/// Gets or sets the current module.
/// </summary>
public IPPCModule CurrentModule
{
get { return _currentModule; }
private set { _currentModule = value; RaisePropertyChangedAuto(); }
}
/// <summary>
/// Navigates to the previous view.
/// </summary>
public RelayCommand NavigateBackCommand { get; private set; }
/// <summary>
/// Navigates to the specified full path in command parameter.
/// </summary>
public RelayCommand<String> NavigateToCommand { get; private set; }
/// <summary>
/// Initializes a new instance of the <see cref="DefaultNavigationManager"/> class.
/// </summary>
/// <param name="moduleLoader">The module loader.</param>
public DefaultNavigationManager(IPPCModuleLoader moduleLoader)
{
_navigationHistory = new Stack<String>();
_moduleLoader = moduleLoader;
NavigateToCommand = new RelayCommand<string>(async (x) => await NavigateTo(x));
NavigateBackCommand = new RelayCommand(async () => await NavigateBack());
}
/// <summary>
/// Navigates to the specified PPC view.
/// </summary>
/// <param name="view">The view.</param>
public Task<bool> NavigateTo(NavigationView view, bool pushToHistory = true)
{
if (view == NavigationView.HomeModule)
{
_navigationHistory.Clear();
_lastFullPath = null;
var firstModule = _moduleLoader.UserModules.FirstOrDefault();
var moduleAtt = firstModule.GetType().GetCustomAttribute<PPCModuleAttribute>();
if (moduleAtt != null)
{
return NavigateTo(firstModule.GetType(), pushToHistory, moduleAtt.HomeViewName);
}
else
{
return NavigateTo(firstModule.GetType(), pushToHistory);
}
}
else
{
MainView.Instance.NavigationControl.NavigateTo(view.ToString());
return Task.FromResult(true);
}
}
/// <summary>
/// Navigates to the specified module.
/// </summary>
/// <typeparam name="T"></typeparam>
public Task<bool> NavigateTo<T>(bool pushToHistory = true) where T : IPPCModule
{
return NavigateTo(typeof(T));
}
/// <summary>
/// Navigates to the specified module using the view path (e.g MainView.JobsView).
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="viewPath">The view path.</param>
public Task<bool> NavigateTo<T>(string viewPath, bool pushToHistory = true) where T : IPPCModule
{
return NavigateTo<T>(pushToHistory, viewPath.Split('.'));
}
/// <summary>
/// Navigates to the specified module using the view path (e.g MainView,JobsView).
/// This method makes it easy to do stuff like NavigateTo(nameof(MainView),nameof(JobsView));
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="viewPath">The view path.</param>
public Task<bool> NavigateTo<T>(bool pushToHistory = true, params String[] viewPath) where T : IPPCModule
{
return NavigateTo(typeof(T), pushToHistory, viewPath);
}
/// <summary>
/// Navigates to the specified module and view by full path (e.g Jobs.JobsView).
/// </summary>
/// <param name="fullPath">The full path.</param>
public async Task<bool> NavigateTo(String fullPath, bool pushToHistory = true)
{
String[] path = fullPath.Split('.');
var module = _moduleLoader.UserModules.SingleOrDefault(x => x.GetType().Name == path[0] || x.Name == path[0]);
if (path.Length == 1 && path[0] == CurrentModule.Name) return true;
var fromVM = _currentVM;
if (_currentVM != null && _currentVM is INavigationBlocker)
{
if (!await (_currentVM as INavigationBlocker).OnNavigateOutRequest())
{
return false;
}
}
if (pushToHistory && _lastFullPath != null && !_preventHistory)
{
_navigationHistory.Push(_lastFullPath);
RaisePropertyChanged(nameof(CanNavigateBack));
}
_lastFullPath = fullPath;
MainView.Instance.NavigationControl.NavigateTo(NavigationView.LayoutView.ToString());
var navigationControl = LayoutView.Instance.NavigationControl;
CurrentModule = module;
var moduleView = navigationControl.NavigateTo(module.Name);
_currentVM = moduleView.DataContext;
if (path.Length > 1)
{
var moduleNavigation = moduleView.FindChildOffline<NavigationControl>();
moduleNavigation.RegisterForLoadedOrNow(async (x, e) =>
{
foreach (var view in path.Skip(1))
{
await Task.Delay(100);
var v = moduleNavigation.NavigateTo(view);
_currentVM = v.DataContext;
if (view != path.Last())
{
moduleNavigation = v.FindChildOffline<NavigationControl>();
}
}
NavigationCycleCompleted?.Invoke(fromVM, _currentVM);
});
}
return true;
}
private Task<bool> NavigateTo(Type moduleType, bool pushToHistory = true, params String[] viewPath)
{
if (viewPath != null && viewPath.Length > 0)
{
return NavigateTo(moduleType.Name + "." + String.Join(".", viewPath), pushToHistory);
}
else
{
return NavigateTo(moduleType.Name, pushToHistory);
}
}
public Task<TResult> NavigateForResult<TModule, TView, TResult, TObject>(TObject obj, bool pushToHistory = true)
where TModule : IPPCModule
{
TaskCompletionSource<TResult> source = new TaskCompletionSource<TResult>();
var fromVM = _currentVM;
Object toVM = null;
Action<Object, Object> handler = null;
handler = (from, to) =>
{
if (toVM == null)
{
toVM = to;
if (toVM is INavigationResultProvider<TResult, TObject>)
{
(toVM as INavigationResultProvider<TResult, TObject>).OnNavigationObjectReceived(obj);
}
}
else
{
if (to == fromVM && from == toVM)
{
if (from is INavigationResultProvider<TResult, TObject>)
{
source.SetResult((from as INavigationResultProvider<TResult, TObject>).GetNavigationResult());
}
}
NavigationCycleCompleted -= handler;
}
};
NavigationCycleCompleted += handler;
NavigateTo<TModule>(typeof(TView).Name, pushToHistory);
return source.Task;
}
/// <summary>
/// Gets a value indicating whether the navigation system is able to navigate to the previous view.
/// </summary>
public bool CanNavigateBack
{
get { return _navigationHistory.Count > 0; }
}
/// <summary>
/// Navigates to the previous view if <see cref="P:Tango.PPC.Common.Navigation.INavigationManager.CanNavigateBack" /> is true.
/// </summary>
public async Task<bool> NavigateBack()
{
String first = _navigationHistory.Pop();
_preventHistory = true;
if (await NavigateTo(first))
{
RaisePropertyChanged(nameof(CanNavigateBack));
_preventHistory = false;
return true;
}
else
{
_navigationHistory.Push(first);
_preventHistory = false;
RaisePropertyChanged(nameof(CanNavigateBack));
return false;
}
}
}
}
|