Skip to content

Commit

Permalink
Merge pull request #43 from 1nf0rmagician/fix/entryEditorLists
Browse files Browse the repository at this point in the history
Fix EntryEditor
  • Loading branch information
Toxantron authored Mar 25, 2022
2 parents df066e3 + 8020e96 commit 102542f
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/Moryx.Controls/EntryEditor/EntryViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ public EntryViewModel(Entry entry)
ValueType = Entry.Value.Type;
UnitType = Entry.Value.UnitType;
SubEntries = new ObservableCollection<EntryViewModel>(Entry.SubEntries.Select(e => new EntryViewModel(e)));

_preEditSubEntries = new ObservableCollection<EntryViewModel>(SubEntries);
_preEditValue = Entry.Value.Current;

UpdateParent();
}
Expand Down Expand Up @@ -205,8 +208,6 @@ public void BeginEdit()
public void EndEdit()
{
SubEntries.EndEdit();
_preEditValue = Entry.Value.Current;
_preEditSubEntries = new ObservableCollection<EntryViewModel>(SubEntries);
CopyToModel();
}

Expand All @@ -223,10 +224,10 @@ private void CopyToModel()
/// <inheritdoc />
public void CancelEdit()
{
SubEntries.CancelEdit();
Value = _preEditValue;
Entry.SubEntries = _preEditSubEntries.Select(vm => vm.Entry).ToList();
Value = _preEditValue;
CopyFromModel();
SubEntries.CancelEdit();
}

private void CopyFromModel()
Expand All @@ -242,13 +243,13 @@ private void CopyFromModel()
private void MergeIntoViewModelCollection(ObservableCollection<EntryViewModel> entryViewModels, IList<Entry> updated)
{
// Remove those without identifiert or not existing in the updated collection
var removed = entryViewModels.Where(vm => vm.Entry.Identifier == "" || updated.All(e => e.Identifier != vm.Entry.Identifier)).ToList();
var removed = entryViewModels.Where(vm => vm.Entry.Identifier == "" || updated.All(e => e != vm.Entry)).ToList();
foreach (var obj in removed)
entryViewModels.Remove(obj);

foreach (var updatedEntry in updated)
{
var match = entryViewModels.FirstOrDefault(vm => vm.Entry.Identifier == updatedEntry.Identifier);
var match = entryViewModels.FirstOrDefault(vm => vm.Entry == updatedEntry);
if (match is null) // Add new Entry
entryViewModels.Add(new EntryViewModel(updatedEntry));
else // Update Entry
Expand Down

0 comments on commit 102542f

Please sign in to comment.