Skip to content

Commit

Permalink
fix(settings): fix NullReferenceException by checking that GMD is loa…
Browse files Browse the repository at this point in the history
…ded and ask user to launch game if they try to search without GMD
  • Loading branch information
DevilPepper committed May 9, 2021
1 parent 4f44b83 commit dea48df
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 16 deletions.
11 changes: 11 additions & 0 deletions ItemBoxTracker/Converter/CountToString.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using HunterPie.UI.Infrastructure.Converters;

namespace MHWItemBoxTracker.Converter {
public class CountToString : GenericValueConverter<int, string> {
public string Zero { get; set; }
public string More { get; set; }
public override string Convert(int value, object _) {
return value > 0 ? More : Zero;
}
}
}
5 changes: 3 additions & 2 deletions ItemBoxTracker/GUI/AutoSuggestBox.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
<Popup
x:Name="suggestionsPopup"
Visibility="{Binding IsOpen, ElementName=suggestionsPopup, Converter={convert:BooleanToVisibilityConverter}}"
MinWidth="{Binding ActualWidth}"
Width="{Binding ActualWidth}"
MinHeight="30"
MaxHeight="150"
StaysOpen="False"
Expand All @@ -114,8 +114,9 @@

<TextBlock
x:Name="noResults"
TextWrapping="Wrap"
Background="{x:Null}"
Text="No results..."
Text="{Binding NoResultsText}"
VerticalAlignment="Center"
Padding="5 0"
Foreground="#ffbfbfbf" />
Expand Down
12 changes: 12 additions & 0 deletions ItemBoxTracker/GUI/AutoSuggestBox.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ public string Placeholder {
get { return (string)GetValue(PlaceholderProperty); }
set { SetValue(PlaceholderProperty, value); }
}

public static readonly DependencyProperty NoResultsProperty = DependencyProperty.Register(
"NoResultsText", typeof(string), typeof(AutoSuggestBox), new PropertyMetadata("No Results..."));
/// <value>
/// DependencyProperty <c>NoResultsText</c>
/// is the text to show when there are no results.
/// Defaults to "No Results..."
/// </value>
public string NoResultsText {
get { return (string)GetValue(NoResultsProperty); }
set { SetValue(NoResultsProperty, value); }
}
public static readonly DependencyProperty SelectionProperty = DependencyProperty.Register(
"Selection", typeof(object), typeof(AutoSuggestBox), new PropertyMetadata(null));
/// <value>
Expand Down
1 change: 0 additions & 1 deletion ItemBoxTracker/GUI/ItemDataTemplate.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
Padding="5 5"
TextWrapping="Wrap"
Text="{Binding ItemId, Converter={convert:ItemIdToDescription}}" />
<!-- <TextBlock Foreground="#ffbfbfbf" Text="{Binding ItemId, Converter={StaticResource dec2hex}}" /> -->
</StackPanel>
</StackPanel.ToolTip>

Expand Down
26 changes: 14 additions & 12 deletions ItemBoxTracker/GUI/Settings.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,20 @@ private void OnLoaded(object sender, RoutedEventArgs e) {
}

private void LoadItems() {
var items = new ObservableCollection<ItemViewModel>(Enumerable
.Range(0, GMD.Items.gValuesOffsets.Length / 2)
.Select(id => new ItemViewModel {
ItemId = id,
Name = GMD.GetItemNameById(id)
})
.Where(item => item.Name != null)
.ToList());

vm.Items.Clear();
foreach (var item in items) {
vm.Items.Add(item);
if (GMD.Items.gValuesOffsets != null) {
var items = new ObservableCollection<ItemViewModel>(Enumerable
.Range(0, GMD.Items.gValuesOffsets.Length / 2)
.Select(id => new ItemViewModel {
ItemId = id,
Name = GMD.GetItemNameById(id)
})
.Where(item => item.Name != null)
.ToList());

vm.Items.Clear();
foreach (var item in items) {
vm.Items.Add(item);
}
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion ItemBoxTracker/GUI/SettingsTab.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
xmlns:vm="clr-namespace:MHWItemBoxTracker.ViewModels"
xmlns:src="clr-namespace:HunterPie.GUI;assembly=HunterPie.UI"
xmlns:Custom="clr-namespace:HunterPie.GUIControls.Custom_Controls;assembly=HunterPie.UI"
xmlns:convert="clr-namespace:MHWItemBoxTracker.Converter"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Expand All @@ -20,7 +21,7 @@
</ResourceDictionary>
</UserControl.Resources>

<StackPanel>
<StackPanel x:Name="settingsTab">


<Grid HorizontalAlignment="Center">
Expand Down Expand Up @@ -94,6 +95,7 @@
Content="" />
<local:AutoSuggestBox
Placeholder="Search items..."
NoResultsText="{Binding DataContext.Items.Count, RelativeSource={RelativeSource AncestorType=UserControl}, Converter={convert:CountToString Zero=Please launch the game to enable this search and then reload this screen once HunterPie has loaded the strings, More=No Results...}}"
Selection="{Binding DataContext, RelativeSource={RelativeSource AncestorType=StackPanel}, Mode=TwoWay}"
ItemTemplate="{StaticResource ItemDataTemplate}"
OnTextChanged="OnTextChanged"
Expand Down
1 change: 1 addition & 0 deletions ItemBoxTracker/ItemBoxTracker.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@

<Compile Include="Controller\ItemBoxTracker.cs" />

<Compile Include="Converter\CountToString.cs" />
<Compile Include="Converter\ItemIdToDescription.cs" />

<Compile Include="Service\ConfigService.cs" />
Expand Down

0 comments on commit dea48df

Please sign in to comment.