Skip to content

Commit

Permalink
updates no change support
Browse files Browse the repository at this point in the history
  • Loading branch information
eglauko committed Nov 29, 2023
1 parent 2af0e36 commit cb6bc13
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 20 deletions.
4 changes: 4 additions & 0 deletions RoyalCode.Yasamen/RoyalCode.Yasamen.Forms/FieldBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,10 @@ public override Task SetParametersAsync(ParameterView parameters)
throw new InvalidOperationException(
$"{GetType()} does not support changing the {nameof(ModelContext<object>)} dynamically.");
}
else
{
changeSupport!.HasCurrentValue(Value);
}

if (ModelContext is null)
throw new InvalidOperationException(
Expand Down
26 changes: 14 additions & 12 deletions RoyalCode.Yasamen/RoyalCode.Yasamen.Forms/Support/ChangeSupport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public sealed class ChangeSupport
private List<IPropertyChangeListener>? listeners;
private bool initialized;
private object? initialValue;
private object? currentValue;
private bool notifying;

internal ChangeSupport(string name,
Expand Down Expand Up @@ -66,7 +67,7 @@ public ChangeSupportListener<TProperty> OnChanged<TProperty>(PropertyChangedHand
throw new ArgumentNullException(nameof(handler));

var listener = new InternalListener<TProperty>(handler);
listeners ??= new();
listeners ??= [];
listeners.Add(listener);

var supportListener = new ChangeSupportListener<TProperty>(() =>
Expand All @@ -92,7 +93,7 @@ public ChangeSupportListener OnAnyChanged(AnyPropertyChangedHandler handler)
throw new ArgumentNullException(nameof(handler));

var listener = new InternalListener(handler);
listeners ??= new();
listeners ??= [];
listeners.Add(listener);

return new ChangeSupportListener(() =>
Expand All @@ -109,7 +110,7 @@ public void Include<TValue, TIncludedProperty>(
var includedChangeSupport = collection.GetChangeSupport(changeSupportName);
var include = new IncludeChangeSupport<TValue, TIncludedProperty>(includedChangeSupport, includedHandler);

includes ??= new();
includes ??= [];
includes.Add(include);
}

Expand All @@ -131,23 +132,21 @@ internal void Initialize<TValue>(FieldIdentifier identifier, TValue initialValue
Identifier = identifier;
FieldType = typeof(TValue);
this.initialValue = initialValue;
currentValue = initialValue;

InitializeIncludes(identifier, initialValue);
}

internal void InitializeIncludes<TValue>(FieldIdentifier identifier, TValue initialValue)
internal void HasCurrentValue<TValue>(TValue value)
{
includes?.ForEach(i => i.Initialize(identifier, initialValue));
parentPropertyChangeSupport?.GetChangeSupport(Name).InitializeIncludes(identifier, initialValue);
if (Identifier is not null && currentValue is TValue previousValue && !Equals(previousValue, value))
PropertyHasChanged(Identifier.Value, previousValue, value);
}

//TODO: ver se isso é necessário.
internal void SetIdentifier(FieldIdentifier identifier)
internal void InitializeIncludes<TValue>(FieldIdentifier identifier, TValue initialValue)
{
if (!initialized)
throw new InvalidOperationException($"The {nameof(ChangeSupport)} must be initialized");

Identifier = identifier;
includes?.ForEach(i => i.Initialize(identifier, initialValue));
parentPropertyChangeSupport?.GetChangeSupport(Name).InitializeIncludes(identifier, initialValue);
}

internal void Reset()
Expand All @@ -156,6 +155,7 @@ internal void Reset()
Identifier = default;
FieldType = null;
initialValue = default;
currentValue = default;

includes?.ForEach(i => i.Reset());

Expand All @@ -169,6 +169,8 @@ internal void PropertyHasChanged<TProperty>(FieldIdentifier fieldIdentifier, TPr

notifying = true;

currentValue = newValue;

listeners?.ForEach(l =>
{
if (l is IPropertyChangeListener<TProperty> typedListener)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace RoyalCode.Yasamen.Forms.Support;
/// A collection of <see cref="ChangeSupport"/>.
/// </para>
/// <para>
/// This class manages the <see cref="ChangeSupport"/> and its lifecycle.
/// This class manages the <see cref="ChangeSupport"/> and its life-cycle.
/// </para>
/// </summary>
public sealed class ChangeSupportCollection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ namespace RoyalCode.Yasamen.Forms.Support;
public sealed class PropertyChangeSupport
{
private readonly ChangeSupportCollection changeSupports;
private readonly PropertyChangeSupport? parent;
private Dictionary<string, object>? properties;
private PropertyChangeSupport? parent;

/// <summary>
/// Creates new <see cref="PropertyChangeSupport"/> without parent.
Expand Down Expand Up @@ -81,11 +81,12 @@ public void PropertyHasChanged<TProperty>(FieldIdentifier fieldIdentifier, TProp

public PropertySupported<TValue> Property<TValue>(string name)
{
properties ??= new();
properties ??= [];
if (properties.TryGetValue(name, out var obj))
{
if (obj is not PropertySupported<TValue> supported)
throw new InvalidOperationException($"The property with name '{name}' has other type them ('{typeof(TValue).Name}')");
throw new InvalidOperationException(
$"The property with name '{name}' has other type them ('{typeof(TValue).Name}')");

return supported;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,5 @@ public static void AddDataServices(this IServiceCollection services)
{
services.AddScoped<IDataServicesProvider, DataServicesProvider>();
services.AddScoped(typeof(FinderPerformerService<,>));

}
}
6 changes: 3 additions & 3 deletions RoyalCode.Yasamen/version.targets
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>

<Version>0.1.3.3</Version>
<AssemblyVersion>0.1.3.3</AssemblyVersion>
<FileVersion>0.1.3.3</FileVersion>
<Version>0.1.3.4</Version>
<AssemblyVersion>0.1.3.4</AssemblyVersion>
<FileVersion>0.1.3.4</FileVersion>

</PropertyGroup>
<ItemGroup>
Expand Down

0 comments on commit cb6bc13

Please sign in to comment.