Skip to content

Commit

Permalink
Merge pull request #41 from 1nf0rmagician/hotfix/compatibilityIssue
Browse files Browse the repository at this point in the history
Fix edit-mode dependent sync of subentries
  • Loading branch information
Toxantron authored Jan 6, 2022
2 parents e93a31a + 58220df commit ff8ac25
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.2.0
3.2.1
3 changes: 2 additions & 1 deletion src/Moryx.Controls.Demo/ViewModels/EntryEditorViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ public EntryEditorViewModel()

private void EndEdit(object obj)
{
throw new NotImplementedException();
IsEditMode = false;
EntryViewModels.EndEdit();
}

private void CancelEdit(object obj)
Expand Down
19 changes: 17 additions & 2 deletions src/Moryx.Controls/EntryEditor/EntryViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
Expand All @@ -21,6 +22,7 @@ public class EntryViewModel : INotifyPropertyChanged, IEditableObject
#region Fields and Properties
private ObservableCollection<EntryViewModel> _subEntries;
private string _preEditValue;
private ObservableCollection<EntryViewModel> _preEditSubEntries;

/// <summary>
/// The entry of this view model
Expand Down Expand Up @@ -62,7 +64,7 @@ public class EntryViewModel : INotifyPropertyChanged, IEditableObject
/// </summary>
public bool IsReadOnly => Entry.Value.IsReadOnly;

// TODO: AL6 Remove direct access to Model and the helper variable _preEditValue from BeginEdit, CancelEdit and EndEdit methods
// TODO: AL6 Remove direct access to Model (insert in CopyFrom/ToModel) and the helper variable _preEditValue from BeginEdit, CancelEdit and EndEdit methods
/// <summary>
/// Current value of <see cref="Entry"/>
/// </summary>
Expand Down Expand Up @@ -90,6 +92,7 @@ public ObservableCollection<string> PossibleValues
}
}

// TODO: AL6 Remove direct sync to Model (insert in CopyFrom/ToModel) and the helper variable _preEditSubEntries from BeginEdit, CancelEdit and EndEdit methods
/// <summary>
/// Child instances of this <see cref="EntryViewModel"/>
/// </summary>
Expand All @@ -98,10 +101,20 @@ public ObservableCollection<EntryViewModel> SubEntries
get => _subEntries;
set
{
if (_subEntries is not null)
_subEntries.CollectionChanged -= SyncSubEntries;
_subEntries = value;
_subEntries.CollectionChanged += SyncSubEntries;
OnPropertyChanged();
}
}

private void SyncSubEntries(object sender, NotifyCollectionChangedEventArgs args)
{
if (args.Action is NotifyCollectionChangedAction.Move)
return;
Entry.SubEntries = SubEntries.Select(vm => vm.Entry).ToList();
}
#endregion

#region Constructors
Expand Down Expand Up @@ -185,13 +198,15 @@ public void BeginEdit()
{
SubEntries.BeginEdit();
_preEditValue = Entry.Value.Current;
_preEditSubEntries = new ObservableCollection<EntryViewModel>(SubEntries);
}

/// <inheritdoc />
public void EndEdit()
{
SubEntries.EndEdit();
_preEditValue = Entry.Value.Current;
_preEditSubEntries = new ObservableCollection<EntryViewModel>(SubEntries);
CopyToModel();
}

Expand All @@ -201,7 +216,6 @@ private void CopyToModel()
Entry.Value.Current = Value;
Entry.Value.Type = ValueType;
Entry.Value.UnitType = UnitType;
Entry.SubEntries = SubEntries.Select(vm => vm.Entry).ToList();

UpdateParent();
}
Expand All @@ -211,6 +225,7 @@ public void CancelEdit()
{
SubEntries.CancelEdit();
Value = _preEditValue;
Entry.SubEntries = _preEditSubEntries.Select(vm => vm.Entry).ToList();
CopyFromModel();
}

Expand Down

0 comments on commit ff8ac25

Please sign in to comment.