Skip to content

Commit

Permalink
Make tree entries count sum up all children
Browse files Browse the repository at this point in the history
Fixes Goz3rr#15
  • Loading branch information
virusek20 committed Mar 30, 2019
1 parent 78005ca commit 64211ca
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions SatisfactorySaveEditor/Converter/SaveNodeItemToStringConverter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Windows.Data;
using SatisfactorySaveEditor.Model;

Expand All @@ -11,21 +13,46 @@ public object Convert(object value, Type targetType, object parameter, CultureIn
{
if (!(value is SaveObjectModel saveNodeItem)) return string.Empty;

var totalCount = Traverse(saveNodeItem.Items, obj => obj.Items).Count(obj => obj.Items.Count == 0);
var count = saveNodeItem.Items.Count;
string formatString;

switch (count)
{
case 0:
return $"{saveNodeItem.Title}";
case 1:
return $"{saveNodeItem.Title} (1 entry)";
formatString = $"{saveNodeItem.Title} (1 entry, ";
break;
default:
formatString = $"{saveNodeItem.Title} ({count} entries, ";
break;
}

return $"{saveNodeItem.Title} ({count} entries)";
switch (totalCount)
{
case 1:
return formatString + $"{totalCount} object)";
default:
return formatString + $"{totalCount} objects)";
}
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}

public static IEnumerable<T> Traverse<T>(IEnumerable<T> items, Func<T, IEnumerable<T>> childSelector)
{
var stack = new Stack<T>(items);

while (stack.Any())
{
var next = stack.Pop();
yield return next;
foreach (var child in childSelector(next)) stack.Push(child);
}
}
}
}

0 comments on commit 64211ca

Please sign in to comment.