Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize EDEngineer #715

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 30 additions & 9 deletions EDEngineer/Views/CommanderViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,15 @@ public ICollectionView FilterView(MainWindowViewModel parentViewModel, Kind kind
e.Accepted = ((entry.Data.Kind & kind) == entry.Data.Kind || entry.Data.Kind == Kind.Unknown) &&
(parentViewModel.MaterialSubkindFilter == null || entry.Data.Kind == Kind.Data || parentViewModel.MaterialSubkindFilter == entry.Data.Subkind) &&
(parentViewModel.ShowZeroes || entry.Count != 0) &&
(parentViewModel.ShowFull || entry.Count < entry.Data.MaximumCapacity) &&
(!parentViewModel.ShowOnlyForFavorites || favoritedBlueprints.Any(b => b.Ingredients.Any(i => i.Entry == entry)));
};

parentViewModel.PropertyChanged += (o, e) =>
{
if (e.PropertyName == nameof(parentViewModel.ShowZeroes) || e.PropertyName == nameof(parentViewModel.ShowOnlyForFavorites))
if (e.PropertyName == nameof(parentViewModel.ShowZeroes) ||
e.PropertyName == nameof(parentViewModel.ShowFull) ||
e.PropertyName == nameof(parentViewModel.ShowOnlyForFavorites))
{
source.View.Refresh();
}
Expand Down Expand Up @@ -242,7 +245,6 @@ private void LoadBlueprints(ILanguage languages, IEnumerable<Blueprint> blueprin
if (Settings.Default.Favorites.Contains($"{blueprint}"))
{
Settings.Default.Favorites.Remove($"{blueprint}");
Settings.Default.Save();
}
}
else if (Settings.Default.Favorites.Contains($"{blueprint}"))
Expand All @@ -251,7 +253,6 @@ private void LoadBlueprints(ILanguage languages, IEnumerable<Blueprint> blueprin
favoritedBlueprints.Add(blueprint);
Settings.Default.Favorites.Remove($"{blueprint}");
Settings.Default.Favorites.Add(text);
Settings.Default.Save();
}

if (Settings.Default.Ignored.Contains(text))
Expand All @@ -261,15 +262,13 @@ private void LoadBlueprints(ILanguage languages, IEnumerable<Blueprint> blueprin
if (Settings.Default.Ignored.Contains($"{blueprint}"))
{
Settings.Default.Ignored.Remove($"{blueprint}");
Settings.Default.Save();
}
}
else if (Settings.Default.Ignored.Contains($"{blueprint}"))
{
blueprint.Ignored = true;
Settings.Default.Ignored.Remove($"{blueprint}");
Settings.Default.Ignored.Add(text);
Settings.Default.Save();
}

blueprint.ShoppingListCount = Settings.Default.ShoppingList.Cast<string>().Count(l => l == text);
Expand Down Expand Up @@ -316,11 +315,15 @@ private void LoadBlueprints(ILanguage languages, IEnumerable<Blueprint> blueprin
Settings.Default.ShoppingList.Add(text);
}

Settings.Default.Save();
if (!importingShoppingList)
{
Settings.Default.Save();
}
}
};
}

Settings.Default.Save();
Filters = new BlueprintFilters(languages, State.Blueprints);

ShoppingList = new ShoppingListViewModel(State.Cargo, State.Blueprints, languages);
Expand Down Expand Up @@ -403,15 +406,20 @@ public void ShoppingListChange(Blueprint blueprint, int i)
{
blueprint.ShoppingListCount += i;

OnPropertyChanged(nameof(ShoppingList));
OnPropertyChanged(nameof(ShoppingListItem));
// Don't bother UI when there is import in progress.
if (!importingShoppingList)
{
OnPropertyChanged(nameof(ShoppingList));
OnPropertyChanged(nameof(ShoppingListItem));
}
}
}

public void ImportShoppingList()
{
if (Helpers.TryRetrieveShoppingList(out var shoppingListItems))
{
importingShoppingList = true;
var blueprints = State.Blueprints;

if (shoppingListItems != null && shoppingListItems.Count > 0)
Expand All @@ -436,6 +444,7 @@ public void ImportShoppingList()
break;
case MessageBoxResult.Cancel:
// User pressed Cancel button so skip out of Import
importingShoppingList = false;
return;
}

Expand All @@ -444,7 +453,7 @@ public void ImportShoppingList()
}

RefreshShoppingList();

importingShoppingList = false;
}
}

Expand Down Expand Up @@ -481,14 +490,26 @@ public void ExportShoppingList()

public void ClearShoppingList()
{
var importingShoppingListOld = importingShoppingList;
importingShoppingList = true;

foreach (var tuple in ShoppingList.Composition.ToList())
{
ShoppingListChange(tuple.Item1, tuple.Item2 * -1);
}

importingShoppingList = importingShoppingListOld;

if (!importingShoppingList)
{
RefreshShoppingList();
}
}

public int ShoppingListItem => 0;

public bool importingShoppingList { get; private set; }

public override string ToString()
{
return $"CMDR {CommanderName}";
Expand Down
1 change: 1 addition & 0 deletions EDEngineer/Views/MainWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public MainWindow()
{
if (e.PropertyName == "ShowOnlyForFavorites" ||
e.PropertyName == "ShowZeroes" ||
e.PropertyName == "ShowFull" ||
e.PropertyName == "CurrentCommander" ||
e.PropertyName == "MaterialSubkindFilter" ||
e.PropertyName == "IngredientsGrouped")
Expand Down
15 changes: 12 additions & 3 deletions EDEngineer/Views/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<ToggleButton HorizontalAlignment="Stretch"
FontSize="16"
Expand All @@ -498,29 +499,37 @@
FontSize="16"
FontFamily="/Resources/Fonts/#Euro Caps"
Grid.Column="1"
ToolTip="{Binding Languages.CurrentLanguage, Converter={x:Static localization:Languages.Instance}, ConverterParameter='Show capped entries?'}"
IsChecked="{Binding ShowFull, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<iconPacks:PackIconMaterial Kind="Texture" />
</ToggleButton>
<ToggleButton HorizontalAlignment="Stretch"
FontSize="16"
FontFamily="/Resources/Fonts/#Euro Caps"
Grid.Column="2"
ToolTip="{Binding Languages.CurrentLanguage, Converter={x:Static localization:Languages.Instance}, ConverterParameter='Show only for favorites?'}"
IsChecked="{Binding ShowOnlyForFavorites, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<iconPacks:PackIconMaterial Kind="Star" Foreground="{StaticResource EliteOrangeBrush}" />
</ToggleButton>
<ToggleButton HorizontalAlignment="Stretch"
FontSize="16"
FontFamily="/Resources/Fonts/#Euro Caps"
Grid.Column="2"
Grid.Column="3"
ToolTip="{Binding Languages.CurrentLanguage, Converter={x:Static localization:Languages.Instance}, ConverterParameter='Show information about resources?'}"
IsChecked="{Binding ShowOriginIcons, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<iconPacks:PackIconMaterial Kind="InformationOutline" Foreground="{StaticResource EliteOrangeBrush}" />
</ToggleButton>
<ToggleButton HorizontalAlignment="Stretch"
FontSize="16"
FontFamily="/Resources/Fonts/#Euro Caps"
Grid.Column="3"
Grid.Column="4"
ToolTip="{Binding Languages.CurrentLanguage, Converter={x:Static localization:Languages.Instance}, ConverterParameter='Group ingredients?'}"
IsChecked="{Binding IngredientsGrouped, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<iconPacks:PackIconMaterial Kind="PackageVariantClosed" Foreground="{StaticResource EliteOrangeBrush}" />
</ToggleButton>
<Border BorderBrush="{StaticResource EliteOrangeBrush}"
BorderThickness="2"
Grid.Column="4">
Grid.Column="5">
<ComboBox ItemsSource="{Binding Comparers}"
SelectedItem="{Binding CurrentComparer, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
HorizontalContentAlignment="Center"
Expand Down
10 changes: 10 additions & 0 deletions EDEngineer/Views/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ private static CommanderAggregation GetAggregation()
}

private bool showZeroes = true;
private bool showFull = true;
private bool showOnlyForFavorites;
private bool showOriginIcons = true;
private bool ingredientsGrouped;
Expand All @@ -204,6 +205,15 @@ public bool ShowZeroes
OnPropertyChanged();
}
}
public bool ShowFull
{
get => showFull;
set
{
showFull = value;
OnPropertyChanged();
}
}

public Subkind? MaterialSubkindFilter
{
Expand Down
44 changes: 29 additions & 15 deletions EDEngineer/Views/ShoppingListViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,25 @@ public class ShoppingListViewModel : INotifyPropertyChanged, IShoppingList
private readonly StateCargo stateCargo;
private readonly ILanguage languages;
private readonly List<IGrouping<Tuple<string, string>, Blueprint>> blueprints;
public List<Blueprint> list = null;

public ShoppingListViewModel(StateCargo stateCargo, List<Blueprint> blueprints, ILanguage languages)
{
this.blueprints = blueprints.GroupBy(b => Tuple.Create(b.Type, b.BlueprintName)).ToList();
this.stateCargo = stateCargo;
this.languages = languages;
list = this.ToList();

foreach (var blueprint in blueprints)
{
blueprint.PropertyChanged += (o, e) =>
{
if (e.PropertyName == "ShoppingListCount")
{
list = this.ToList();
}
};
}

foreach (var ingredientsValue in stateCargo.Ingredients)
{
Expand All @@ -44,7 +57,7 @@ protected virtual void OnPropertyChanged([CallerMemberName] string propertyName
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

public List<Blueprint> List => this.ToList();
public List<Blueprint> List => list;
public ILanguage Languages => this.languages;

public List<Tuple<Blueprint, int>> Composition
Expand Down Expand Up @@ -205,36 +218,37 @@ public bool SynchronizeWithLogs
}

public Dictionary<EntryData, int> Deduction =>
this.ToList()
.FirstOrDefault()
list.FirstOrDefault()
?.Ingredients
.ToDictionary(i => i.Entry.Data, i => i.Size);

public Dictionary<Entry, int> MissingIngredients =>
this.ToList()
.FirstOrDefault()
list.FirstOrDefault()
?.Ingredients
.Where(i => i.Size - i.Entry.Count > 0)
.ToDictionary(i => i.Entry, i => i.Size - i.Entry.Count);

public IEnumerator<Blueprint> GetEnumerator()
{
var ingredients = blueprints
.SelectMany(b => b)
.SelectMany(b => Enumerable.Repeat(b, b.ShoppingListCount))
.SelectMany(b => b.Ingredients)
.ToList();
var ingredients = new Dictionary<Entry, int>();
foreach (var b in blueprints.SelectMany(b => b).Where(b => b.ShoppingListCount > 0))
{
foreach (var ingredient in b.Ingredients)
{
if (!ingredients.ContainsKey(ingredient.Entry))
{
ingredients[ingredient.Entry] = 0;
}
ingredients[ingredient.Entry] += ingredient.Size * b.ShoppingListCount;
}
}

if (!ingredients.Any())
{
yield break;
}

var composition = ingredients.GroupBy(i => i.Entry.Data.Name)
.Select(
i =>
new BlueprintIngredient(i.First().Entry,
i.Sum(c => c.Size)))
var composition = ingredients.Select(i => new BlueprintIngredient(i.Key, i.Value))
.OrderBy(i => i.Entry.Count - i.Size > 0 ? 1 : 0)
.ThenByDescending(i => i.Entry.Data.Subkind)
.ThenBy(i => i.Entry.Data.Kind)
Expand Down