1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +00:00

Data model paths - Added events for validating/invalidating

List condition - Reinitialize when path validates/invalidates
Conditions - Don't save with invalid paths
Core - Fixed render exception on data model expansion deactivate
This commit is contained in:
SpoinkyNL 2020-10-10 11:03:45 +02:00
parent d46a610e23
commit 69ae42c039
5 changed files with 82 additions and 6 deletions

View File

@ -13,6 +13,7 @@ namespace Artemis.Core
public class DataModelConditionList : DataModelConditionPart
{
private bool _disposed;
private bool _reinitializing;
/// <summary>
/// Creates a new instance of the <see cref="DataModelConditionList" /> class
@ -83,6 +84,7 @@ namespace Artemis.Core
ListPath?.Dispose();
ListPath = path != null ? new DataModelPath(path) : null;
SubscribeToListPath();
// Remove the old root group that was tied to the old data model
while (Children.Any())
@ -143,6 +145,10 @@ namespace Artemis.Core
internal override void Save()
{
// Don't save an invalid state
if (ListPath != null && !ListPath.IsValid)
return;
// Target list
ListPath?.Save();
Entity.ListPath = ListPath?.Entity;
@ -164,6 +170,9 @@ namespace Artemis.Core
internal void Initialize()
{
while (Children.Any())
RemoveChild(Children[0]);
if (Entity.ListPath == null)
return;
@ -175,6 +184,7 @@ namespace Artemis.Core
return;
ListPath = listPath;
SubscribeToListPath();
if (ListPath.IsValid)
{
ListType = listType.GetGenericArguments()[0];
@ -187,7 +197,7 @@ namespace Artemis.Core
}
// There should only be one child and it should be a group
if (Entity.Children.SingleOrDefault() is DataModelConditionGroupEntity rootGroup)
if (Entity.Children.FirstOrDefault() is DataModelConditionGroupEntity rootGroup)
{
AddChild(new DataModelConditionGroup(this, rootGroup));
}
@ -197,6 +207,39 @@ namespace Artemis.Core
AddChild(new DataModelConditionGroup(this));
}
}
private void SubscribeToListPath()
{
if (ListPath == null) return;
ListPath.PathValidated += ListPathOnPathValidated;
ListPath.PathInvalidated += ListPathOnPathInvalidated;
}
#region Event handlers
private void ListPathOnPathValidated(object? sender, EventArgs e)
{
if (_reinitializing)
return;
_reinitializing = true;
ListPath?.Dispose();
Initialize();
_reinitializing = false;
}
private void ListPathOnPathInvalidated(object? sender, EventArgs e)
{
if (_reinitializing)
return;
_reinitializing = true;
ListPath?.Dispose();
Initialize();
_reinitializing = false;
}
#endregion
}
/// <summary>

View File

@ -205,6 +205,10 @@ namespace Artemis.Core
internal override void Save()
{
// Don't save an invalid state
if (LeftPath != null && !LeftPath.IsValid || RightPath != null && !RightPath.IsValid)
return;
Entity.PredicateType = (int) PredicateType;
LeftPath?.Save();

View File

@ -14,9 +14,9 @@ namespace Artemis.Core
/// </summary>
public class DataModelPath : IStorageModel, IDisposable
{
private bool _disposed;
private readonly LinkedList<DataModelPathSegment> _segments;
private Expression<Func<object, object>>? _accessorLambda;
private bool _disposed;
/// <summary>
/// Creates a new instance of the <see cref="DataModelPath" /> class pointing directly to the target
@ -59,7 +59,7 @@ namespace Artemis.Core
/// <param name="dataModelPath">The path to base the new instance on</param>
public DataModelPath(DataModelPath dataModelPath)
{
if (dataModelPath == null)
if (dataModelPath == null)
throw new ArgumentNullException(nameof(dataModelPath));
Target = dataModelPath.Target;
@ -188,6 +188,8 @@ namespace Artemis.Core
_accessorLambda = null;
Accessor = null;
OnPathInvalidated();
}
internal void Initialize()
@ -239,6 +241,9 @@ namespace Artemis.Core
),
parameter
);
if (IsValid)
OnPathValidated();
}
private void SubscribeToDataModelStore()
@ -307,5 +312,29 @@ namespace Artemis.Core
}
#endregion
#region Events
/// <summary>
/// Occurs whenever the path becomes invalid
/// </summary>
public event EventHandler PathInvalidated;
/// <summary>
/// Occurs whenever the path becomes valid
/// </summary>
public event EventHandler PathValidated;
protected virtual void OnPathValidated()
{
PathValidated?.Invoke(this, EventArgs.Empty);
}
protected virtual void OnPathInvalidated()
{
PathInvalidated?.Invoke(this, EventArgs.Empty);
}
#endregion
}
}

View File

@ -166,8 +166,8 @@ namespace Artemis.Core.Services
_frameStopWatch.Restart();
lock (_dataModelExpansions)
{
// Update all active modules
foreach (BaseDataModelExpansion dataModelExpansion in _dataModelExpansions)
// Update all active modules, check Enabled status because it may go false before before the _dataModelExpansions list is updated
foreach (BaseDataModelExpansion dataModelExpansion in _dataModelExpansions.Where(e => e.Enabled))
dataModelExpansion.Update(args.DeltaTime);
}

View File

@ -146,7 +146,7 @@
<materialDesign:PackIcon Kind="Add" Width="18" Height="18" />
</Button>
<ItemsControl Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2" ItemsSource="{Binding Items}">
<ItemsControl Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2" ItemsSource="{Binding Items, IsAsync=True}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<ContentControl s:View.Model="{Binding}" VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" IsTabStop="False" />