From 13ac3f576c8fa7fbdca9ede57f81b0c596da9466 Mon Sep 17 00:00:00 2001 From: Andreas Huber Date: Fri, 16 Jun 2017 14:37:53 +0200 Subject: [PATCH] Fixes #47 --- Lawo.EmberPlusSharp/Model/MatrixBase`1.cs | 63 +++++++++++++---------- 1 file changed, 35 insertions(+), 28 deletions(-) diff --git a/Lawo.EmberPlusSharp/Model/MatrixBase`1.cs b/Lawo.EmberPlusSharp/Model/MatrixBase`1.cs index 2a0a98ae..5523cf90 100644 --- a/Lawo.EmberPlusSharp/Model/MatrixBase`1.cs +++ b/Lawo.EmberPlusSharp/Model/MatrixBase`1.cs @@ -61,15 +61,15 @@ public IReadOnlyList Labels /// public IReadOnlyList Targets { - get { return this.targets; } - private set { this.SetValue(ref this.targets, value); } + get => this.targets; + private set => this.SetSignals(ref this.targets, value, this.sources); } /// public IReadOnlyList Sources { - get { return this.sources; } - private set { this.SetValue(ref this.sources, value); } + get => this.sources; + private set => this.SetSignals(ref this.sources, value, this.targets); } /// @@ -82,12 +82,22 @@ public IReadOnlyDictionary> Connections private set { - if (this.SetValue(ref this.connections, value)) + this.SetValue(ref this.connections, value); + + foreach (var connection in this.connections) { - foreach (var connection in this.connections) - { - connection.Value.CollectionChanged += (s, e) => this.OnSourcesChanged(connection.Key); - } + connection.Value.CollectionChanged += (s, e) => this.OnSourcesChanged(connection.Key); + } + + if (this.MaximumTotalConnects == 0) + { + this.MaximumTotalConnects = + GetMaximumTotalConnects(this.type, this.Targets.Count, this.Sources.Count); + } + + if (this.MaximumConnectsPerTarget == 0) + { + this.MaximumConnectsPerTarget = this.type == MatrixType.NToN ? this.Sources.Count : 1; } } } @@ -120,7 +130,6 @@ internal sealed override bool WriteRequest(EmberWriter writer, IStreamedParamete internal sealed override RetrievalState ReadContents(EmberReader reader, ElementType actualType) { this.AssertElementType(ElementType.Matrix, actualType); - var type = MatrixType.OneToN; var addressingMode = MatrixAddressingMode.Linear; while (reader.Read() && (reader.InnerNumber != InnerNumber.EndContainer)) @@ -131,7 +140,7 @@ internal sealed override RetrievalState ReadContents(EmberReader reader, Element this.Description = reader.AssertAndReadContentsAsString(); break; case GlowMatrixContents.Type.OuterNumber: - type = this.ReadEnum(reader, GlowMatrixContents.Type.Name); + this.type = this.ReadEnum(reader, GlowMatrixContents.Type.Name); break; case GlowMatrixContents.AddressingMode.OuterNumber: addressingMode = @@ -170,22 +179,6 @@ internal sealed override RetrievalState ReadContents(EmberReader reader, Element } } - if ((this.Targets != null) && (this.Sources != null)) - { - this.Connections = this.Targets.ToDictionary(i => i, i => new ObservableCollection()); - - if (this.MaximumTotalConnects == 0) - { - this.MaximumTotalConnects = - GetMaximumTotalConnects(type, this.Targets.Count, this.Sources.Count); - } - - if (this.MaximumConnectsPerTarget == 0) - { - this.MaximumConnectsPerTarget = type == MatrixType.NToN ? this.Sources.Count : 1; - } - } - return this.RetrievalState; } @@ -207,7 +200,7 @@ internal sealed override RetrievalState ReadAdditionalField(EmberReader reader, GlowTarget.InnerNumber, GlowTarget.Number.OuterNumber, GlowTarget.Number.Name); - this.Connections = this.Targets.ToDictionary(i => i, i => new ObservableCollection()); + break; case GlowMatrix.Sources.OuterNumber: reader.AssertInnerNumber(GlowSourceCollection.InnerNumber); @@ -365,6 +358,7 @@ private static void Insert(ObservableCollection existingSources, int[] sour } private readonly HashSet targetsWithChangedConnections = new HashSet(); + private MatrixType type = MatrixType.OneToN; private int maximumTotalConnects; private int maximumConnectsPerTarget; private IReadOnlyList parametersLocation; @@ -403,6 +397,19 @@ private enum ConnectionDisposition Locked } + private void SetSignals(ref IReadOnlyList field, IReadOnlyList signals, IReadOnlyList other) + { + if (field?.SequenceEqual(signals) != true) + { + this.SetValue(ref field, signals); + + if (other != null) + { + this.Connections = this.targets.ToDictionary(i => i, i => new ObservableCollection()); + } + } + } + private void OnSourcesChanged(int target) { if (!this.isProviderChangeInProgress)