Skip to content
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
114 changes: 100 additions & 14 deletions WFInfo/AutoAddViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Globalization;
using System.Linq;

namespace WFInfo
{
public class AutoAddViewModel : INPC
{
private ObservableCollection<AutoAddSingleItem> _itemList;
private double _totalPlatinum;
private int _totalDucats;

public ObservableCollection<AutoAddSingleItem> ItemList
public ObservableCollection<AutoAddSingleItem> ItemList
{
get => _itemList;
private set
Expand All @@ -18,29 +23,73 @@ private set
RaisePropertyChanged();
}
}

public double TotalPlatinum
{
get => _totalPlatinum;
private set
{
_totalPlatinum = value;
RaisePropertyChanged();
}
}
public int TotalDucats
{
get => _totalDucats;
private set
{
_totalDucats = value;
RaisePropertyChanged();
}
}

public AutoAddViewModel()
{
_itemList = new ObservableCollection<AutoAddSingleItem>();
_itemList.CollectionChanged += CollectionChanged;
}

public void addItem(AutoAddSingleItem item)
{
item.PropertyChanged += ItemChanged;
_itemList.Add(item);
RaisePropertyChanged();
}

public void removeItem(AutoAddSingleItem item)
{
item.PropertyChanged -= ItemChanged;
_itemList.Remove(item);
RaisePropertyChanged();
}

private void ItemChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(AutoAddSingleItem.PlatinumValue) || e.PropertyName == nameof(AutoAddSingleItem.DucatValue))
{
RecalculateTotals();
}
}

private void CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
RecalculateTotals();
}

private void RecalculateTotals()
{
TotalPlatinum = _itemList.Sum(item => item.PlatinumValue);
TotalDucats = _itemList.Sum(item => item.DucatValue);
}
}

public class AutoAddSingleItem : INPC
{
public AutoAddViewModel _parent;

private const NumberStyles style = NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands | NumberStyles.AllowExponent;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The necessity of this confused me a bit, but apparently the overload of double.TryParse that accepts a CultureInfo without NumberStyles doesn't exist in our .NET version.

private ObservableCollection<string> _rewardOptions;
private string _activeOption;
private double _platinumValue;
private int _ducatValue;

public ObservableCollection<string> RewardOptions
{
Expand All @@ -52,14 +101,41 @@ private set
}
}

private string _activeOption;
public string ActiveOption
{
get => _activeOption;
set
{
_activeOption = value;
RaisePropertyChanged();
if (_activeOption != value)
{
_activeOption = value;
UpdateValues();
RaisePropertyChanged();
}
}
}
public double PlatinumValue
{
get => _platinumValue;
private set
{
if (_platinumValue != value)
{
_platinumValue = value;
RaisePropertyChanged();
}
}
}
public int DucatValue
{
get => _ducatValue;
private set
{
if (_ducatValue != value)
{
_ducatValue = value;
RaisePropertyChanged();
}
}
}

Expand All @@ -69,21 +145,31 @@ public string ActiveOption

public AutoAddSingleItem(List<string> options, int activeIndex, AutoAddViewModel parent)
{

RewardOptions = new ObservableCollection<string>(options);
activeIndex = Math.Min(RewardOptions.Count - 1, activeIndex);
if (activeIndex >= 0 && options != null)
{
ActiveOption = options[activeIndex];
} else
{
ActiveOption = "";
}
ActiveOption = activeIndex >= 0 ? options[activeIndex] : "";
_parent = parent;
Remove = new SimpleCommand(() => RemoveFromParent());
Increment = new SimpleCommand(() => AddCount(true));
}

private void UpdateValues()
{
if (!string.IsNullOrEmpty(ActiveOption))
{
JObject job = (JObject)Main.dataBase.marketData.GetValue(ActiveOption);
string plat = job["plat"].ToObject<string>();
string ducats = job["ducats"].ToObject<string>();
PlatinumValue = double.TryParse(plat, style, CultureInfo.InvariantCulture, out double platValue) ? platValue : 0;
DucatValue = int.TryParse(ducats, style, CultureInfo.InvariantCulture, out int ducatValue) ? ducatValue : 0;
}
else
{
PlatinumValue = 0;
DucatValue = 0;
}
}

public void AddCount(bool save)
{
//get item count, increment, save
Expand Down
20 changes: 15 additions & 5 deletions WFInfo/AutoCount.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@
BorderBrush="#FF0F0F0F"
HorizontalAlignment="Stretch"
ItemsSource="{Binding RewardOptions}"
SelectedItem="{Binding ActiveOption, Mode=TwoWay}"
SelectedItem="{Binding ActiveOption, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Margin="5,3" Template="{DynamicResource ComboBoxTemplate}" Style="{DynamicResource ComboBoxStyle1}">
</ComboBox>
</DataTemplate>
Expand Down Expand Up @@ -195,16 +195,26 @@
</ScrollViewer>
<Grid Grid.Row="2">
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" MinWidth="100" />
<ColumnDefinition Width="Auto" MinWidth="100" />
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Content="Save all" Foreground="#506464" FontSize="14" Style="{StaticResource Label_Button}">

<StackPanel Grid.Column="0" Orientation="Horizontal" HorizontalAlignment="Center">
<Label Content="Total:" Foreground="#506464" FontSize="14" />
<Image x:Name="platImage1" Width="20" Source="Resources/plat.gif" Height="20" />
<Label Content="{Binding viewModel.TotalPlatinum}" ContentStringFormat="{}{0:0.##}" Foreground="#506464" FontSize="14" />
<Image x:Name="firstDucatImage1" Width="20" Source="Resources/ducat_w.gif" Height="20" />
<Label Content="{Binding viewModel.TotalDucats}" Foreground="#506464" FontSize="14" />
</StackPanel>

<Label Grid.Column="1" Content="Save all" Foreground="#506464" FontSize="14" Style="{StaticResource Label_Button}">
<Label.InputBindings>
<MouseBinding MouseAction="LeftClick" Command="{Binding IncrementAll}" />
</Label.InputBindings>
</Label>

<Label Grid.Column="1" Content="Dismiss all" Foreground="#506464" FontSize="14" Style="{StaticResource Label_Button}">
<Label Grid.Column="2" Content="Dismiss all" Foreground="#506464" FontSize="14" Style="{StaticResource Label_Button}">
<Label.InputBindings>
<MouseBinding MouseAction="LeftClick" Command="{Binding RemoveAll}" />
</Label.InputBindings>
Expand Down