Skip to content

Commit

Permalink
Filters: Extract ReleaseFilterPanel.
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoltanar committed Dec 6, 2023
1 parent c10f5f5 commit 0e58488
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 103 deletions.
7 changes: 7 additions & 0 deletions Happy Reader/Happy Reader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@
</Compile>
<Compile Include="View\RadioButtonCheckedConverter.cs" />
<Compile Include="Model\SavedData.cs" />
<Compile Include="View\ReleaseFilterPanel.xaml.cs">
<DependentUpon>ReleaseFilterPanel.xaml</DependentUpon>
</Compile>
<Compile Include="View\Tabs\AddEntriesTab.xaml.cs">
<DependentUpon>AddEntriesTab.xaml</DependentUpon>
</Compile>
Expand Down Expand Up @@ -320,6 +323,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="View\ReleaseFilterPanel.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="View\Tabs\AddEntriesTab.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand Down
164 changes: 62 additions & 102 deletions Happy Reader/View/FiltersPane.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,17 @@ namespace Happy_Reader.View
{
public partial class FiltersPane : UserControl
{

// ReSharper disable once NotAccessedField.Local
private FiltersViewModel ViewModel => (FiltersViewModel)DataContext;
private AutoCompleteBox _traitOrTagControl;
private DockPanel _languageDockPanel;
private Grid _releaseMonthGrid;
private readonly ReleaseFilterPanel _releaseMonthGrid = new();
private readonly LangRelease _langRelease = new();
private readonly ReleaseMonthFilter _releaseMonth = new();

public FiltersPane()
{
InitializeComponent();
CreateLangReleaseFilter();
CreateReleaseMonthGrid();
}

private void CreateLangReleaseFilter()
Expand All @@ -44,34 +41,6 @@ private void CreateLangReleaseFilter()
languageTextBox.DataContext = _langRelease;
}

private void CreateReleaseMonthGrid()
{
var grid = new Grid { DataContext = _releaseMonth };
var relativeCheckBox = new CheckBox() { Content = "Relative", DataContext = _releaseMonth };
relativeCheckBox.SetBinding(ToggleButton.IsCheckedProperty, new Binding(nameof(ReleaseMonthFilter.Relative)) { NotifyOnSourceUpdated = true });
relativeCheckBox.Checked += UpdateReleaseMonthFilter;
relativeCheckBox.Unchecked += UpdateReleaseMonthFilter;
relativeCheckBox.SourceUpdated += UpdateReleaseMonthFilter;
var yearTextBox = new LabeledTextBox { Label = "Year", DataContext = _releaseMonth };
yearTextBox.SetBinding(LabeledTextBox.TextProperty,
new Binding(nameof(ReleaseMonthFilter.Year)) { NotifyOnSourceUpdated = true });
yearTextBox.SourceUpdated += UpdateReleaseMonthFilter;
var monthTextBox = new LabeledTextBox { Label = "Month", DataContext = _releaseMonth };
monthTextBox.SetBinding(LabeledTextBox.TextProperty,
new Binding(nameof(ReleaseMonthFilter.Month)) { NotifyOnSourceUpdated = true });
monthTextBox.SourceUpdated += UpdateReleaseMonthFilter;
grid.ColumnDefinitions.Add(new ColumnDefinition());
grid.ColumnDefinitions.Add(new ColumnDefinition());
grid.ColumnDefinitions.Add(new ColumnDefinition());
grid.Children.Add(relativeCheckBox);
Grid.SetColumn(relativeCheckBox, 0);
grid.Children.Add(yearTextBox);
Grid.SetColumn(yearTextBox, 1);
grid.Children.Add(monthTextBox);
Grid.SetColumn(monthTextBox, 2);
_releaseMonthGrid = grid;
}

private void CreateLangReleaseCheckBox(string content, string tooltip, string property)
{
var checkBox = new CheckBox { Content = content, ToolTip = tooltip };
Expand Down Expand Up @@ -123,74 +92,71 @@ private void FilterTypeChanged(object sender, SelectionChangedEventArgs e)
};
bindingProperty = Selector.SelectedValueProperty;
}
else
else if (type == typeof(bool)) control = new TextBlock(new System.Windows.Documents.Run("Use Exclude check box"));
else if (type == typeof(string))
{
if (type == typeof(bool)) control = new TextBlock(new System.Windows.Documents.Run("Use Exclude check box"));
else if (type == typeof(string))
{
control = new TextBox();
bindingProperty = TextBox.TextProperty;
}
else if (type == typeof(int))
{
control = new TextBox();
bindingProperty = TextBox.TextProperty;
control.PreviewTextInput += NumberValidationTextBox;
}
else if (type == typeof(DumpFiles.WrittenTag))
{
_traitOrTagControl = new AutoCompleteBox() { ItemFilter = DumpfileFilter, ItemsSource = DumpFiles.GetAllTags() };
control = _traitOrTagControl;
bindingProperty = AutoCompleteBox.SelectedItemProperty;
}
else if (type == typeof(DumpFiles.WrittenTrait))
{
_traitOrTagControl = new AutoCompleteBox()
{
ItemFilter = DumpfileFilter,
HorizontalAlignment = HorizontalAlignment.Stretch,
Margin = new Thickness(130 + 5, 2, 2, 2),
};
var traitRootItems = StaticMethods.GetEnumValues(typeof(DumpFiles.RootTrait));
traitRootItems[0].Content = "Any";
traitRootItems[0].Tag = (DumpFiles.RootTrait)(-1);
var rootControl = new ComboBox
{
ItemsSource = traitRootItems,
SelectedValuePath = nameof(Tag),
HorizontalAlignment = HorizontalAlignment.Left,
Width = 130
};
rootControl.SelectionChanged += RootControl_SelectionChanged;
rootControl.SelectedIndex = 0;
FilterValuesGrid.Children.Add(rootControl);
control = _traitOrTagControl;
bindingProperty = AutoCompleteBox.SelectedItemProperty;
}
else if (type == typeof(LangRelease))
{
control = _languageDockPanel;
bindingProperty = DataContextProperty;
}
else if (type == typeof(ListedProducer))
{
_traitOrTagControl = new AutoCompleteBox() { ItemFilter = ProducerBoxFilter, ItemsSource = StaticHelpers.LocalDatabase.Producers };
control = _traitOrTagControl;
bindingProperty = AutoCompleteBox.SelectedItemProperty;
}
else if (type == typeof(StaffItem) || type == typeof(VnSeiyuu))
control = new TextBox();
bindingProperty = TextBox.TextProperty;
}
else if (type == typeof(int))
{
control = new TextBox();
bindingProperty = TextBox.TextProperty;
control.PreviewTextInput += NumberValidationTextBox;
}
else if (type == typeof(DumpFiles.WrittenTag))
{
_traitOrTagControl = new AutoCompleteBox() { ItemFilter = DumpfileFilter, ItemsSource = DumpFiles.GetAllTags() };
control = _traitOrTagControl;
bindingProperty = AutoCompleteBox.SelectedItemProperty;
}
else if (type == typeof(DumpFiles.WrittenTrait))
{
_traitOrTagControl = new AutoCompleteBox()
{
_traitOrTagControl = new AutoCompleteBox { ItemFilter = StaffItemFilter, ItemsSource = StaticHelpers.LocalDatabase.StaffAliases };
control = _traitOrTagControl;
bindingProperty = AutoCompleteBox.SelectedItemProperty;
}
else if (type == typeof(ReleaseMonthFilter))
ItemFilter = DumpfileFilter,
HorizontalAlignment = HorizontalAlignment.Stretch,
Margin = new Thickness(130 + 5, 2, 2, 2),
};
var traitRootItems = StaticMethods.GetEnumValues(typeof(DumpFiles.RootTrait));
traitRootItems[0].Content = "Any";
traitRootItems[0].Tag = (DumpFiles.RootTrait)(-1);
var rootControl = new ComboBox
{
control = _releaseMonthGrid;
bindingProperty = DataContextProperty;
}
else control = new TextBlock(new System.Windows.Documents.Run(type.ToString()));
ItemsSource = traitRootItems,
SelectedValuePath = nameof(Tag),
HorizontalAlignment = HorizontalAlignment.Left,
Width = 130
};
rootControl.SelectionChanged += RootControl_SelectionChanged;
rootControl.SelectedIndex = 0;
FilterValuesGrid.Children.Add(rootControl);
control = _traitOrTagControl;
bindingProperty = AutoCompleteBox.SelectedItemProperty;
}
else if (type == typeof(LangRelease))
{
control = _languageDockPanel;
bindingProperty = DataContextProperty;
}
else if (type == typeof(ListedProducer))
{
_traitOrTagControl = new AutoCompleteBox() { ItemFilter = ProducerBoxFilter, ItemsSource = StaticHelpers.LocalDatabase.Producers };
control = _traitOrTagControl;
bindingProperty = AutoCompleteBox.SelectedItemProperty;
}
else if (type == typeof(StaffItem) || type == typeof(VnSeiyuu))
{
_traitOrTagControl = new AutoCompleteBox { ItemFilter = StaffItemFilter, ItemsSource = StaticHelpers.LocalDatabase.StaffAliases };
control = _traitOrTagControl;
bindingProperty = AutoCompleteBox.SelectedItemProperty;
}
else if (type == typeof(ReleaseMonthFilter))
{
control = _releaseMonthGrid;
_releaseMonthGrid.ViewModel = ViewModel.NewFilter;
}
else control = new TextBlock(new System.Windows.Documents.Run(type.ToString()));
if (bindingProperty != null) control.SetBinding(bindingProperty, valueBinding);
FilterValuesGrid.Children.Add(control);
}
Expand All @@ -201,12 +167,6 @@ private void UpdateLangReleaseFilter(object sender, RoutedEventArgs e)
ViewModel.NewFilter.Value = _langRelease;
}

private void UpdateReleaseMonthFilter(object sender, RoutedEventArgs e)
{
if (ViewModel == null) return;
ViewModel.NewFilter.Value = _releaseMonth;
}

private void RootControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count == 0) return;
Expand Down
12 changes: 12 additions & 0 deletions Happy Reader/View/ReleaseFilterPanel.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<UserControl x:Class="Happy_Reader.View.ReleaseFilterPanel"
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"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Happy_Reader.View"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid x:Name="MainGrid">

</Grid>
</UserControl>
48 changes: 48 additions & 0 deletions Happy Reader/View/ReleaseFilterPanel.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using Happy_Reader.Model.VnFilters;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
namespace Happy_Reader.View
{
public partial class ReleaseFilterPanel : UserControl
{
private readonly ReleaseMonthFilter _releaseMonth = new();
public IFilter ViewModel { get; set; }

public ReleaseFilterPanel()
{
InitializeComponent();
//MainGrid.DataContext = _releaseMonth;
var grid = MainGrid;
var relativeCheckBox = new CheckBox() { Content = "Relative", DataContext = _releaseMonth };
relativeCheckBox.SetBinding(ToggleButton.IsCheckedProperty, new Binding(nameof(ReleaseMonthFilter.Relative)) { NotifyOnSourceUpdated = true });
relativeCheckBox.Checked += UpdateReleaseMonthFilter;
relativeCheckBox.Unchecked += UpdateReleaseMonthFilter;
relativeCheckBox.SourceUpdated += UpdateReleaseMonthFilter;
var yearTextBox = new LabeledTextBox { Label = "Year", DataContext = _releaseMonth };
yearTextBox.SetBinding(LabeledTextBox.TextProperty,
new Binding(nameof(ReleaseMonthFilter.Year)) { NotifyOnSourceUpdated = true });
yearTextBox.SourceUpdated += UpdateReleaseMonthFilter;
var monthTextBox = new LabeledTextBox { Label = "Month", DataContext = _releaseMonth };
monthTextBox.SetBinding(LabeledTextBox.TextProperty,
new Binding(nameof(ReleaseMonthFilter.Month)) { NotifyOnSourceUpdated = true });
monthTextBox.SourceUpdated += UpdateReleaseMonthFilter;
grid.ColumnDefinitions.Add(new ColumnDefinition());
grid.ColumnDefinitions.Add(new ColumnDefinition());
grid.ColumnDefinitions.Add(new ColumnDefinition());
grid.Children.Add(relativeCheckBox);
Grid.SetColumn(relativeCheckBox, 0);
grid.Children.Add(yearTextBox);
Grid.SetColumn(yearTextBox, 1);
grid.Children.Add(monthTextBox);
Grid.SetColumn(monthTextBox, 2);
}

private void UpdateReleaseMonthFilter(object sender, RoutedEventArgs e)
{
if (ViewModel == null) return;
ViewModel.Value = _releaseMonth;
}
}
}
1 change: 0 additions & 1 deletion Happy Reader/ViewModel/CharactersTabViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public override async Task Initialize()
OnPropertyChanged(nameof(ProducerList));
LocalDatabase.SetCharactersAttachedVisualNovels();
SelectedFilterIndex = 0;
//await RefreshTiles();
}

protected override Func<IDataItem<int>, double?> GetSuggestion { get; } = i => ((CharacterItem)i).TraitScore;
Expand Down

0 comments on commit 0e58488

Please sign in to comment.