Skip to content

Commit

Permalink
Modify view models to support applying reordered configuration.
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikgolda committed May 30, 2019
1 parent 69c51d1 commit 5e0ddb5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

namespace Soloplan.WhatsON.GUI.Common.SubjectTreeView
{
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Input;
Expand Down Expand Up @@ -78,18 +80,32 @@ public void Init(IGrouping<string, SubjectConfiguration> subjectGroup)
this.SubjectViewModels.Remove(noLongerPresentSubjectViewModel);
}

foreach (var subjectViewModel in this.SubjectViewModels)
{
log.Debug("Updating viewmodel for {subjectConfiguration}", new { Identifier = subjectViewModel.Identifier, Name = subjectViewModel.Name });
var config = subjectGroup.FirstOrDefault(configurationSubject => subjectViewModel.Identifier == configurationSubject.Identifier);
subjectViewModel.Init(config);
}

var addedIds = new List<Guid>();
foreach (var newSubject in newSubjects)
{
log.Debug("Adding new subject {subjectConfiguration}", new { Identifier = newSubject.Identifier, Name = newSubject.Name });
addedIds.Add(newSubject.Identifier);
this.CreateViewModelForSubjectConfiguration(newSubject);
}

int index = 0;
foreach (var config in subjectGroup)
{
log.Debug("Updating viewmodel for {subjectConfiguration}", new { Identifier = config.Identifier, Name = config.Name });
var subjectViewModel = this.SubjectViewModels.FirstOrDefault(model => model.Identifier == config.Identifier);
if (!addedIds.Contains(config.Identifier))
{
subjectViewModel.Init(config);
}

var oldIndex = this.SubjectViewModels.IndexOf(subjectViewModel);
if (oldIndex != index)
{
this.SubjectViewModels.Move(oldIndex, index);
}

index++;
}
}

private void CreateViewModelForSubjectConfiguration(SubjectConfiguration subjectConfiguration)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ public void Update(ApplicationConfiguration configuration)

log.Debug("Initializing {name}", nameof(SubjectTreeViewModel));
var grouping = this.ParseConfiguration(configuration).ToList();
int index = 0;
foreach (var group in grouping)
{
log.Debug("Applying settings for group {group}", group.Key);
Expand All @@ -175,9 +176,18 @@ public void Update(ApplicationConfiguration configuration)
{
log.Debug("{model} doesn't exist, creating...", nameof(SubjectGroupViewModel));
subjectGroupViewModel = new SubjectGroupViewModel();
this.SubjectGroups.Add(subjectGroupViewModel);
this.SubjectGroups.Insert(index, subjectGroupViewModel);
}
else
{
var oldIndex = this.SubjectGroups.IndexOf(subjectGroupViewModel);
if (oldIndex != index)
{
this.SubjectGroups.Move(oldIndex, index);
}
}

index++;
subjectGroupViewModel.Init(group);
}

Expand Down

0 comments on commit 5e0ddb5

Please sign in to comment.