-
Notifications
You must be signed in to change notification settings - Fork 776
Expand file tree
/
Copy pathViewModelLocator.cs
More file actions
440 lines (382 loc) · 18.2 KB
/
ViewModelLocator.cs
File metadata and controls
440 lines (382 loc) · 18.2 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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
#if XFORMS
namespace Caliburn.Micro.Xamarin.Forms
#else
namespace Caliburn.Micro
#endif
{
using System;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows;
using System.Collections.Generic;
#if WINDOWS_UWP
using Windows.UI.Xaml;
#endif
#if XFORMS
using UIElement = global::Xamarin.Forms.Element;
#endif
/// <summary>
/// A strategy for determining which view model to use for a given view.
/// </summary>
public static class ViewModelLocator
{
#if ANDROID
const string DefaultViewSuffix = "Activity";
#elif IOS
const string DefaultViewSuffix = "ViewController";
#else
const string DefaultViewSuffix = "View";
#endif
static readonly ILog Log = LogManager.GetLog(typeof(ViewModelLocator));
//These fields are used for configuring the default type mappings. They can be changed using ConfigureTypeMappings().
static string defaultSubNsViews;
static string defaultSubNsViewModels;
static string defaultSubNsDesignViewModels;
static bool useNameSuffixesInMappings;
static string nameFormat;
static string viewModelSuffix;
static readonly List<string> ViewSuffixList = new List<string>();
static bool includeViewSuffixInVmNames;
///<summary>
/// Used to transform names.
///</summary>
public static readonly NameTransformer NameTransformer = new NameTransformer();
/// <summary>
/// The name of the capture group used as a marker for rules that return interface types
/// </summary>
public static string InterfaceCaptureGroupName = "isinterface";
static ViewModelLocator() {
var configuration = new TypeMappingConfiguration();
#if ANDROID
configuration.DefaultSubNamespaceForViews = "Activities";
configuration.ViewSuffixList.Add("Activity");
configuration.IncludeViewSuffixInViewModelNames = false;
#elif IOS
configuration.DefaultSubNamespaceForViews = "ViewControllers";
configuration.ViewSuffixList.Add("ViewController");
configuration.IncludeViewSuffixInViewModelNames = false;
#endif
ConfigureTypeMappings(configuration);
}
/// <summary>
/// Specifies how type mappings are created, including default type mappings. Calling this method will
/// clear all existing name transformation rules and create new default type mappings according to the
/// configuration.
/// </summary>
/// <param name="config">An instance of TypeMappingConfiguration that provides the settings for configuration</param>
public static void ConfigureTypeMappings(TypeMappingConfiguration config)
{
if (String.IsNullOrEmpty(config.DefaultSubNamespaceForViews))
{
throw new ArgumentException("DefaultSubNamespaceForViews field cannot be blank.");
}
if (String.IsNullOrEmpty(config.DefaultSubNamespaceForViewModels))
{
throw new ArgumentException("DefaultSubNamespaceForViewModels field cannot be blank.");
}
if (Execute.InDesignMode && String.IsNullOrEmpty(config.DefaultSubNamespaceForDesignViewModels))
{
throw new ArgumentException("DefaultSubNamespaceForDesignViewModels field cannot be blank.");
}
if (String.IsNullOrEmpty(config.NameFormat))
{
throw new ArgumentException("NameFormat field cannot be blank.");
}
NameTransformer.Clear();
ViewSuffixList.Clear();
defaultSubNsViews = config.DefaultSubNamespaceForViews;
defaultSubNsViewModels = config.DefaultSubNamespaceForViewModels;
defaultSubNsDesignViewModels = config.DefaultSubNamespaceForDesignViewModels;
nameFormat = config.NameFormat;
useNameSuffixesInMappings = config.UseNameSuffixesInMappings;
viewModelSuffix = config.ViewModelSuffix;
ViewSuffixList.AddRange(config.ViewSuffixList);
includeViewSuffixInVmNames = config.IncludeViewSuffixInViewModelNames;
SetAllDefaults();
}
private static void SetAllDefaults()
{
if (useNameSuffixesInMappings)
{
//Add support for all view suffixes
ViewSuffixList.Apply(AddDefaultTypeMapping);
}
else
{
AddSubNamespaceMapping(defaultSubNsViews, defaultSubNsViewModels);
if (Execute.InDesignMode) AddSubNamespaceMapping(defaultSubNsDesignViewModels, defaultSubNsViews);
}
}
/// <summary>
/// Adds a default type mapping using the standard namespace mapping convention
/// </summary>
/// <param name="viewSuffix">Suffix for type name. Should be "View" or synonym of "View". (Optional)</param>
public static void AddDefaultTypeMapping(string viewSuffix = DefaultViewSuffix)
{
if (!useNameSuffixesInMappings)
{
return;
}
//Check for <Namespace>.<BaseName><ViewSuffix> construct
AddNamespaceMapping(String.Empty, String.Empty, viewSuffix);
//Check for <Namespace>.Views.<NameSpace>.<BaseName><ViewSuffix> construct
AddSubNamespaceMapping(defaultSubNsViews, defaultSubNsViewModels, viewSuffix);
//Check for <Namespace>.Views.<NameSpace>.<BaseName><ViewSuffix> construct
if (Execute.InDesignMode) AddSubNamespaceMapping(defaultSubNsViews, defaultSubNsDesignViewModels, viewSuffix);
}
/// <summary>
/// Adds a standard type mapping based on namespace RegEx replace and filter patterns
/// </summary>
/// <param name="nsSourceReplaceRegEx">RegEx replace pattern for source namespace</param>
/// <param name="nsSourceFilterRegEx">RegEx filter pattern for source namespace</param>
/// <param name="nsTargetsRegEx">Array of RegEx replace values for target namespaces</param>
/// <param name="viewSuffix">Suffix for type name. Should be "View" or synonym of "View". (Optional)</param>
public static void AddTypeMapping(string nsSourceReplaceRegEx, string nsSourceFilterRegEx, string[] nsTargetsRegEx, string viewSuffix = DefaultViewSuffix)
{
var replist = new List<string>();
Action<string> func;
const string basegrp = "${basename}";
var interfacegrp = "${" + InterfaceCaptureGroupName + "}";
if (useNameSuffixesInMappings)
{
if (viewModelSuffix.Contains(viewSuffix) || !includeViewSuffixInVmNames)
{
var nameregex = String.Format(nameFormat, basegrp, viewModelSuffix);
func = t =>
{
replist.Add(t + "I" + nameregex + interfacegrp);
replist.Add(t + "I" + basegrp + interfacegrp);
replist.Add(t + nameregex);
replist.Add(t + basegrp);
};
}
else
{
var nameregex = String.Format(nameFormat, basegrp, "${suffix}" + viewModelSuffix);
func = t =>
{
replist.Add(t + "I" + nameregex + interfacegrp);
replist.Add(t + nameregex);
};
}
}
else
{
func = t =>
{
replist.Add(t + "I" + basegrp + interfacegrp);
replist.Add(t + basegrp);
};
}
nsTargetsRegEx.ToList().Apply(t => func(t));
string suffix = useNameSuffixesInMappings ? viewSuffix : String.Empty;
var srcfilterregx = String.IsNullOrEmpty(nsSourceFilterRegEx)
? null
: String.Concat(nsSourceFilterRegEx, String.Format(nameFormat, RegExHelper.NameRegEx, suffix), "$");
var rxbase = RegExHelper.GetNameCaptureGroup("basename");
var rxsuffix = RegExHelper.GetCaptureGroup("suffix", suffix);
//Add a dummy capture group -- place after the "$" so it can never capture anything
var rxinterface = RegExHelper.GetCaptureGroup(InterfaceCaptureGroupName, String.Empty);
NameTransformer.AddRule(
String.Concat(nsSourceReplaceRegEx, String.Format(nameFormat, rxbase, rxsuffix), "$", rxinterface),
replist.ToArray(),
srcfilterregx
);
}
/// <summary>
/// Adds a standard type mapping based on namespace RegEx replace and filter patterns
/// </summary>
/// <param name="nsSourceReplaceRegEx">RegEx replace pattern for source namespace</param>
/// <param name="nsSourceFilterRegEx">RegEx filter pattern for source namespace</param>
/// <param name="nsTargetRegEx">RegEx replace value for target namespace</param>
/// <param name="viewSuffix">Suffix for type name. Should be "View" or synonym of "View". (Optional)</param>
public static void AddTypeMapping(string nsSourceReplaceRegEx, string nsSourceFilterRegEx, string nsTargetRegEx, string viewSuffix = DefaultViewSuffix)
{
AddTypeMapping(nsSourceReplaceRegEx, nsSourceFilterRegEx, new[] { nsTargetRegEx }, viewSuffix);
}
/// <summary>
/// Adds a standard type mapping based on simple namespace mapping
/// </summary>
/// <param name="nsSource">Namespace of source type</param>
/// <param name="nsTargets">Namespaces of target type as an array</param>
/// <param name="viewSuffix">Suffix for type name. Should be "View" or synonym of "View". (Optional)</param>
public static void AddNamespaceMapping(string nsSource, string[] nsTargets, string viewSuffix = DefaultViewSuffix)
{
//need to terminate with "." in order to concatenate with type name later
var nsencoded = RegExHelper.NamespaceToRegEx(nsSource + ".");
//Start pattern search from beginning of string ("^")
//unless original string was blank (i.e. special case to indicate "append target to source")
if (!String.IsNullOrEmpty(nsSource))
{
nsencoded = "^" + nsencoded;
}
//Capture namespace as "origns" in case we need to use it in the output in the future
var nsreplace = RegExHelper.GetCaptureGroup("origns", nsencoded);
var nsTargetsRegEx = nsTargets.Select(t => t + ".").ToArray();
AddTypeMapping(nsreplace, null, nsTargetsRegEx, viewSuffix);
}
/// <summary>
/// Adds a standard type mapping based on simple namespace mapping
/// </summary>
/// <param name="nsSource">Namespace of source type</param>
/// <param name="nsTarget">Namespace of target type</param>
/// <param name="viewSuffix">Suffix for type name. Should be "View" or synonym of "View". (Optional)</param>
public static void AddNamespaceMapping(string nsSource, string nsTarget, string viewSuffix = DefaultViewSuffix)
{
AddNamespaceMapping(nsSource, new[] { nsTarget }, viewSuffix);
}
/// <summary>
/// Adds a standard type mapping by substituting one subnamespace for another
/// </summary>
/// <param name="nsSource">Subnamespace of source type</param>
/// <param name="nsTargets">Subnamespaces of target type as an array</param>
/// <param name="viewSuffix">Suffix for type name. Should be "View" or synonym of "View". (Optional)</param>
public static void AddSubNamespaceMapping(string nsSource, string[] nsTargets, string viewSuffix = DefaultViewSuffix)
{
//need to terminate with "." in order to concatenate with type name later
var nsencoded = RegExHelper.NamespaceToRegEx(nsSource + ".");
string rxbeforetgt, rxaftersrc, rxaftertgt;
string rxbeforesrc = rxbeforetgt = rxaftersrc = rxaftertgt = String.Empty;
if (!String.IsNullOrEmpty(nsSource))
{
if (!nsSource.StartsWith("*"))
{
rxbeforesrc = RegExHelper.GetNamespaceCaptureGroup("nsbefore");
rxbeforetgt = @"${nsbefore}";
}
if (!nsSource.EndsWith("*"))
{
rxaftersrc = RegExHelper.GetNamespaceCaptureGroup("nsafter");
rxaftertgt = "${nsafter}";
}
}
var rxmid = RegExHelper.GetCaptureGroup("subns", nsencoded);
var nsreplace = String.Concat(rxbeforesrc, rxmid, rxaftersrc);
var nsTargetsRegEx = nsTargets.Select(t => String.Concat(rxbeforetgt, t, ".", rxaftertgt)).ToArray();
AddTypeMapping(nsreplace, null, nsTargetsRegEx, viewSuffix);
}
/// <summary>
/// Adds a standard type mapping by substituting one subnamespace for another
/// </summary>
/// <param name="nsSource">Subnamespace of source type</param>
/// <param name="nsTarget">Subnamespace of target type</param>
/// <param name="viewSuffix">Suffix for type name. Should be "View" or synonym of "View". (Optional)</param>
public static void AddSubNamespaceMapping(string nsSource, string nsTarget, string viewSuffix = DefaultViewSuffix)
{
AddSubNamespaceMapping(nsSource, new[] { nsTarget }, viewSuffix);
}
/// <summary>
/// Makes a type name into an interface name.
/// </summary>
/// <param name = "typeName">The part.</param>
/// <returns></returns>
public static string MakeInterface(string typeName)
{
var suffix = string.Empty;
if (typeName.Contains("[["))
{
//generic type
var genericParStart = typeName.IndexOf("[[");
suffix = typeName.Substring(genericParStart);
typeName = typeName.Remove(genericParStart);
}
var index = typeName.LastIndexOf(".");
return typeName.Insert(index + 1, "I") + suffix;
}
/// <summary>
/// Transforms a View type name into all of its possible ViewModel type names. Accepts a flag
/// to include or exclude interface types.
/// </summary>
/// <returns>Enumeration of transformed names</returns>
/// <remarks>Arguments:
/// typeName = The name of the View type being resolved to its companion ViewModel.
/// includeInterfaces = Flag to indicate if interface types are included
/// </remarks>
public static Func<string, bool, IEnumerable<string>> TransformName = (typeName, includeInterfaces) =>
{
Func<string, string> getReplaceString;
if (includeInterfaces)
{
getReplaceString = r => r;
}
else
{
var interfacegrpregex = @"\${" + InterfaceCaptureGroupName + @"}$";
getReplaceString = r => Regex.IsMatch(r, interfacegrpregex) ? String.Empty : r;
}
return NameTransformer.Transform(typeName, getReplaceString).Where(n => n != String.Empty);
};
/// <summary>
/// Determines the view model type based on the specified view type.
/// </summary>
/// <returns>The view model type.</returns>
/// <remarks>
/// Pass the view type and receive a view model type. Pass true for the second parameter to search for interfaces.
/// </remarks>
public static Func<Type, bool, Type> LocateTypeForViewType = (viewType, searchForInterface) =>
{
var typeName = viewType.FullName;
var viewModelTypeList = TransformName(typeName, searchForInterface).ToList();
var viewModelType = AssemblySource.FindTypeByNames(viewModelTypeList);
if (viewModelType == null)
{
Log.Warn("View Model not found. Searched: {0}.", string.Join(", ", viewModelTypeList.ToArray()));
}
return viewModelType;
};
/// <summary>
/// Locates the view model for the specified view type.
/// </summary>
/// <returns>The view model.</returns>
/// <remarks>
/// Pass the view type as a parameter and receive a view model instance.
/// </remarks>
public static Func<Type, object> LocateForViewType = viewType =>
{
var viewModelType = LocateTypeForViewType(viewType, false);
if (viewModelType != null)
{
var viewModel = IoC.GetInstance(viewModelType, null);
if (viewModel != null)
{
return viewModel;
}
}
viewModelType = LocateTypeForViewType(viewType, true);
return viewModelType != null
? IoC.GetInstance(viewModelType, null)
: null;
};
/// <summary>
/// Locates the view model for the specified view instance.
/// </summary>
/// <returns>The view model.</returns>
/// <remarks>
/// Pass the view instance as a parameters and receive a view model instance.
/// </remarks>
public static Func<object, object> LocateForView = view =>
{
if (view == null)
{
return null;
}
#if ANDROID || IOS
return LocateForViewType(view.GetType());
#elif XFORMS
var frameworkElement = view as UIElement;
if (frameworkElement != null && frameworkElement.BindingContext != null)
{
return frameworkElement.BindingContext;
}
return LocateForViewType(view.GetType());
#else
var frameworkElement = view as FrameworkElement;
if (frameworkElement != null && frameworkElement.DataContext != null)
{
return frameworkElement.DataContext;
}
return LocateForViewType(view.GetType());
#endif
};
}
}