Browse Source

Exclude persistent views in designer mode

pull/165/head
Ionite 1 year ago
parent
commit
dfeb13e0f4
No known key found for this signature in database
  1. 59
      StabilityMatrix.Avalonia/ViewLocator.cs

59
StabilityMatrix.Avalonia/ViewLocator.cs

@ -12,29 +12,27 @@ namespace StabilityMatrix.Avalonia;
public class ViewLocator : IDataTemplate, INavigationPageFactory public class ViewLocator : IDataTemplate, INavigationPageFactory
{ {
private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
/*/// <summary> /*/// <summary>
/// Weak Dictionary of (DataContext, View) pairs to keep the view and layout alive /// Weak Dictionary of (DataContext, View) pairs to keep the view and layout alive
/// </summary> /// </summary>
private static readonly ConditionalWeakTable<object, Control> PersistentViewCache = new();*/ private static readonly ConditionalWeakTable<object, Control> PersistentViewCache = new();*/
/// <inheritdoc /> /// <inheritdoc />
public Control Build(object? data) public Control Build(object? data)
{ {
if (data is null) throw new ArgumentNullException(nameof(data)); if (data is null)
throw new ArgumentNullException(nameof(data));
var type = data.GetType(); var type = data.GetType();
if (Attribute.GetCustomAttribute(type, typeof(ViewAttribute)) is ViewAttribute viewAttr) if (Attribute.GetCustomAttribute(type, typeof(ViewAttribute)) is ViewAttribute viewAttr)
{ {
var viewType = viewAttr.ViewType; var viewType = viewAttr.ViewType;
return GetView(viewType, data, viewAttr.IsPersistent); return GetView(viewType, data, viewAttr.IsPersistent);
} }
return new TextBlock return new TextBlock { Text = "View Model Not Found: " + data.GetType().FullName };
{
Text = "View Model Not Found: " + data.GetType().FullName
};
} }
private Control GetView(Type viewType) private Control GetView(Type viewType)
@ -43,32 +41,36 @@ public class ViewLocator : IDataTemplate, INavigationPageFactory
{ {
return view; return view;
} }
return new TextBlock return new TextBlock { Text = "View Not Found: " + viewType.FullName };
{
Text = "View Not Found: " + viewType.FullName
};
} }
private Control GetView(Type viewType, object context, bool persistent) private Control GetView(Type viewType, object context, bool persistent)
{ {
// Disregard persistent settings in design mode
if (Design.IsDesignMode)
{
persistent = false;
}
if (persistent) if (persistent)
{ {
// Check assignable from IPersistentViewProvider // Check assignable from IPersistentViewProvider
if (context is not IPersistentViewProvider persistentViewProvider) if (context is not IPersistentViewProvider persistentViewProvider)
{ {
throw new InvalidOperationException( throw new InvalidOperationException(
$"View {viewType.Name} is marked as persistent but does not implement IPersistentViewProvider"); $"View {viewType.Name} is marked as persistent but does not implement IPersistentViewProvider"
);
} }
// Try get from context // Try get from context
if (persistentViewProvider.AttachedPersistentView is { } view) if (persistentViewProvider.AttachedPersistentView is { } view)
{ {
Logger.Trace("Got persistent view {ViewType} from context", viewType.Name); Logger.Trace("Got persistent view {ViewType} from context", viewType.Name);
return view; return view;
} }
// Otherwise get from service provider // Otherwise get from service provider
if (App.Services.GetService(viewType) is Control newView) if (App.Services.GetService(viewType) is Control newView)
{ {
@ -86,13 +88,10 @@ public class ViewLocator : IDataTemplate, INavigationPageFactory
return view; return view;
} }
} }
return new TextBlock return new TextBlock { Text = "View Not Found: " + viewType.FullName };
{
Text = "View Not Found: " + viewType.FullName
};
} }
/// <inheritdoc /> /// <inheritdoc />
public bool Match(object? data) public bool Match(object? data)
{ {
@ -102,8 +101,10 @@ public class ViewLocator : IDataTemplate, INavigationPageFactory
/// <inheritdoc /> /// <inheritdoc />
public Control? GetPage(Type srcType) public Control? GetPage(Type srcType)
{ {
if (Attribute.GetCustomAttribute(srcType, typeof(ViewAttribute)) is not ViewAttribute if (
viewAttr) Attribute.GetCustomAttribute(srcType, typeof(ViewAttribute))
is not ViewAttribute viewAttr
)
{ {
throw new InvalidOperationException("View not found for " + srcType.FullName); throw new InvalidOperationException("View not found for " + srcType.FullName);
} }
@ -111,15 +112,17 @@ public class ViewLocator : IDataTemplate, INavigationPageFactory
// Get new view // Get new view
var view = GetView(viewAttr.ViewType); var view = GetView(viewAttr.ViewType);
view.DataContext ??= App.Services.GetService(srcType); view.DataContext ??= App.Services.GetService(srcType);
return view; return view;
} }
/// <inheritdoc /> /// <inheritdoc />
public Control GetPageFromObject(object target) public Control GetPageFromObject(object target)
{ {
if (Attribute.GetCustomAttribute(target.GetType(), typeof(ViewAttribute)) is not if (
ViewAttribute viewAttr) Attribute.GetCustomAttribute(target.GetType(), typeof(ViewAttribute))
is not ViewAttribute viewAttr
)
{ {
throw new InvalidOperationException("View not found for " + target.GetType().FullName); throw new InvalidOperationException("View not found for " + target.GetType().FullName);
} }

Loading…
Cancel
Save