Skip to content

Commit

Permalink
[History] Cleanup in history viewmodel, properly unbind from previous…
Browse files Browse the repository at this point in the history
… document
  • Loading branch information
BAndysc committed Nov 1, 2018
1 parent d962e0c commit 0d141c2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 43 deletions.
74 changes: 32 additions & 42 deletions WDE.HistoryWindow/ViewModels/HistoryViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,21 @@
using WDE.Common.Events;
using WDE.Common.History;
using Prism.Ioc;
using WDE.Common.Managers;

namespace WDE.HistoryWindow.ViewModels
{
internal class Valuea
internal class HistoryEvent
{
public string Name { get; set; }
public bool Aaaa { get; set; }
public bool IsFromFuture { get; set; }
}

internal class HistoryViewModel : BindableBase
{
public ObservableCollection<Valuea> Items { get; set; } = new ObservableCollection<Valuea>();
public ObservableCollection<HistoryEvent> Items { get; set; } = new ObservableCollection<HistoryEvent>();

private IDocument previousDocument;

private string _title;
public string Title
Expand All @@ -31,56 +34,43 @@ public string Title
set { SetProperty(ref _title, value); }
}

~HistoryViewModel()
{

}

public HistoryViewModel(IEventAggregator eventAggregator)
{
eventAggregator.GetEvent<EventActiveDocumentChanged>().Subscribe(doc =>
{
if (doc.History == null)
return;
Items.Clear();

doc.History.Past.CollectionChanged += (sender, e) =>
if (previousDocument != null)
{
Items.Clear();
foreach (var past in doc.History.Past)
{
Items.Add(new Valuea() { Name = past.GetDescription(), Aaaa = false });
}
previousDocument.History.Past.CollectionChanged -= HistoryCollectionChanged;
previousDocument.History.Future.CollectionChanged -= HistoryCollectionChanged;
previousDocument = null;
}

foreach (var past in doc.History.Future)
{
Items.Add(new Valuea() { Name = past.GetDescription(), Aaaa = true });
}
};
doc.History.Future.CollectionChanged += (sender, e) =>
{
Items.Clear();
foreach (var past in doc.History.Past)
{
Items.Add(new Valuea() { Name = past.GetDescription(), Aaaa = false });
}
if (doc == null || doc.History == null)
return;

foreach (var past in doc.History.Future)
{
Items.Add(new Valuea() { Name = past.GetDescription(), Aaaa = true });
}
};
previousDocument = doc;

Items.Clear();
foreach (var past in doc.History.Past)
{
Items.Add(new Valuea() { Name = past.GetDescription(), Aaaa = false });
}
doc.History.Past.CollectionChanged += HistoryCollectionChanged;
doc.History.Future.CollectionChanged += HistoryCollectionChanged;

foreach (var past in doc.History.Future)
{
Items.Add(new Valuea() { Name = past.GetDescription(), Aaaa = true });
}
HistoryCollectionChanged(null, null);
});
}

private void HistoryCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
Items.Clear();
foreach (var past in previousDocument.History.Past)
{
Items.Add(new HistoryEvent() { Name = past.GetDescription(), IsFromFuture = false });
}

foreach (var past in previousDocument.History.Future)
{
Items.Add(new HistoryEvent() { Name = past.GetDescription(), IsFromFuture = true });
}
}
}
}
2 changes: 1 addition & 1 deletion WDE.HistoryWindow/Views/HistoryView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<TextBlock.Style>
<Style TargetType="TextBlock" BasedOn="{StaticResource firstStyle}">
<Style.Triggers>
<DataTrigger Binding="{Binding Aaaa}" Value="true">
<DataTrigger Binding="{Binding IsFromFuture}" Value="true">
<Setter Property="Foreground" Value="Gray"/>
</DataTrigger>
</Style.Triggers>
Expand Down

0 comments on commit 0d141c2

Please sign in to comment.