Skip to content

Commit

Permalink
Filters: Added ReleaseMonth filter.
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoltanar committed Nov 28, 2023
1 parent 9df91e1 commit 7d3a5af
Show file tree
Hide file tree
Showing 6 changed files with 227 additions and 120 deletions.
1 change: 1 addition & 0 deletions Happy Reader/Happy Reader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@
<Compile Include="Model\VnFilters\GeneralFilter.cs" />
<Compile Include="Model\VnFilters\IFilter.cs" />
<Compile Include="Model\VnFilters\GeneralMultiFilter.cs" />
<Compile Include="Model\VnFilters\ReleaseMonthFilter.cs" />
<Compile Include="Model\VolumeMixer.cs" />
<Compile Include="MouseEventExtArgs.cs" />
<Compile Include="ViewModel\CharactersTabViewModel.cs" />
Expand Down
27 changes: 23 additions & 4 deletions Happy Reader/Model/VnFilters/GeneralFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Happy_Apps_Core;
using Happy_Apps_Core.DataAccess;
using Happy_Apps_Core.Database;
using Happy_Reader.Model.VnFilters;
using JetBrains.Annotations;
using Newtonsoft.Json;

Expand Down Expand Up @@ -50,18 +51,26 @@ public string StringValue
set
{
_stringValue = value;
if (Type is GeneralFilterType.ReleaseMonth)
{
ReleaseMonthFilter.TryParse(value, out var releaseMonth);
Value = releaseMonth;
}
if (int.TryParse(value, out int intValue)) Value = intValue;
if (bool.TryParse(value, out bool boolValue)) Value = boolValue;
if (Type is GeneralFilterType.OriginalLanguage or GeneralFilterType.Language) _langRelease = GetLangRelease();
else if (bool.TryParse(value, out bool boolValue)) Value = boolValue;
else if (Type is GeneralFilterType.OriginalLanguage or GeneralFilterType.Language) _langRelease = GetLangRelease();
}
}

[JsonIgnore]
public int IntValue { get; set; }

[JsonIgnore]
public ReleaseMonthFilter ReleaseMonthValue { get; set; }

[JsonIgnore]
public object Value
{
{
set
{
if (value != null && value.GetType().IsEnum)
Expand Down Expand Up @@ -106,6 +115,10 @@ public object Value
IntValue = 0;
_stringValue = null;
break;
case ReleaseMonthFilter releaseMonthValue:
ReleaseMonthValue = releaseMonthValue;
_stringValue = releaseMonthValue.ToString();
break;
default:
IntValue = 0;
_stringValue = value.ToString();
Expand Down Expand Up @@ -174,6 +187,8 @@ private Func<IDataItem<int>, bool> GetFunction()
return i => (GetVisualNovel(i, out var vn) && vn.UserVN != null) != Exclude;
case GeneralFilterType.ReleaseDate:
return i => (GetVisualNovel(i, out var vn) && DateFunctionFromString(vn.ReleaseDate)) != Exclude;
case GeneralFilterType.ReleaseMonth:
return i => (GetVisualNovel(i, out var vn) && ReleaseMonthValue.IsInReleaseMonth(vn.ReleaseDate)) != Exclude;
case GeneralFilterType.HasAnime:
return i => (GetVisualNovel(i, out var vn) && vn.HasAnime) != Exclude;
case GeneralFilterType.SuggestionScore:
Expand Down Expand Up @@ -389,6 +404,7 @@ public override string ToString()
case GeneralFilterType.CharacterGender:
case GeneralFilterType.Name:
case GeneralFilterType.VNID:
case GeneralFilterType.ReleaseMonth:
return $"{result} {StringValue}";
case GeneralFilterType.Length:
return $"{result} - {(StringValue == null ? "None" : ((LengthFilterEnum)IntValue).GetDescription())}";
Expand Down Expand Up @@ -449,7 +465,7 @@ private LangRelease GetLangRelease()
{
return _langRelease = JsonConvert.DeserializeObject<LangRelease>(_stringValue) ?? new LangRelease();
}
catch(JsonReaderException)
catch (JsonReaderException)
{
var langRelease = new LangRelease
{
Expand Down Expand Up @@ -522,5 +538,8 @@ public enum GeneralFilterType
VNID = 27,
[Description("Newly Added"), TypeConverter(typeof(bool))]
NewlyAdded = 28,
[Description("Release Month"), TypeConverter(typeof(ReleaseMonthFilter))]
ReleaseMonth = 29,

}
}
43 changes: 43 additions & 0 deletions Happy Reader/Model/VnFilters/ReleaseMonthFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Text.RegularExpressions;

namespace Happy_Reader.Model.VnFilters;

public class ReleaseMonthFilter
{
private static readonly Regex ParseRegex = new(@"(?<relative>(|Relative:))(?<year>-?\d{1,4}):(?<month>-?\d{1,2})");

public bool Relative { get; set; }
public int Year { get; set; }
public int Month { get; set; }

public bool IsInReleaseMonth(DateTime dateTime)
{
var now = DateTime.UtcNow;
if (Relative)
{
var yearDiff = dateTime.Year - now.Year;
if (yearDiff != Year) return false;
var monthDiff = dateTime.Month - now.Month;
return monthDiff == Month;
}
if (dateTime.Year != Year) return false;
return dateTime.Month == Month;
}

public override string ToString() => $"{(Relative ? "Relative:" : string.Empty)}{Year:0000}:{Month:00}";

public static bool TryParse(string value, out ReleaseMonthFilter releaseMonthFilter)
{
releaseMonthFilter = default;
var match = ParseRegex.Match(value);
if (!match.Success) return false;
releaseMonthFilter = new ReleaseMonthFilter
{
Relative = match.Groups["relative"].Value != string.Empty,
Year = int.Parse(match.Groups["year"].Value),
Month = int.Parse(match.Groups["month"].Value)
};
return true;
}
}
55 changes: 49 additions & 6 deletions Happy Reader/View/FiltersPane.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Windows.Input;
using Happy_Apps_Core;
using Happy_Apps_Core.Database;
using Happy_Reader.Model.VnFilters;
using Happy_Reader.ViewModel;

namespace Happy_Reader.View
Expand All @@ -17,12 +18,15 @@ public partial class FiltersPane : UserControl
private FiltersViewModel ViewModel => (FiltersViewModel)DataContext;
private AutoCompleteBox _traitOrTagControl;
private DockPanel _languageDockPanel;
private LangRelease _langRelease = new LangRelease();
private Grid _releaseMonthGrid;
private readonly LangRelease _langRelease = new();
private readonly ReleaseMonthFilter _releaseMonth = new();

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

private void CreateLangReleaseFilter()
Expand All @@ -40,6 +44,34 @@ 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 @@ -70,7 +102,7 @@ private void SaveOrGroup(object sender, RoutedEventArgs e)
var isPermanent = parent == PermanentFilterGroupBox;
ViewModel.SaveOrGroup(isPermanent);
}

private void FilterTypeChanged(object sender, SelectionChangedEventArgs e)
{
if (ViewModel.SelectedFilterIndex < 0) return;
Expand Down Expand Up @@ -138,7 +170,7 @@ private void FilterTypeChanged(object sender, SelectionChangedEventArgs e)
else if (type == typeof(LangRelease))
{
control = _languageDockPanel;
bindingProperty = DockPanel.DataContextProperty;
bindingProperty = DataContextProperty;
}
else if (type == typeof(ListedProducer))
{
Expand All @@ -152,18 +184,29 @@ private void FilterTypeChanged(object sender, SelectionChangedEventArgs e)
control = _traitOrTagControl;
bindingProperty = AutoCompleteBox.SelectedItemProperty;
}
else if (type == typeof(ReleaseMonthFilter))
{
control = _releaseMonthGrid;
bindingProperty = DataContextProperty;
}
else control = new TextBlock(new System.Windows.Documents.Run(type.ToString()));
}
if (bindingProperty != null) control.SetBinding(bindingProperty, valueBinding);
FilterValuesGrid.Children.Add(control);
}

private void UpdateLangReleaseFilter(object sender, RoutedEventArgs e)
private void UpdateLangReleaseFilter(object sender, RoutedEventArgs e)
{
if (ViewModel == null) return;
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 Expand Up @@ -210,11 +253,11 @@ private void PreviewDeleteFilter(object sender, CanExecuteRoutedEventArgs e)
{
var grid = (DataGrid)sender;
if (e.Command != DataGrid.DeleteCommand) return;
if(grid.SelectedItems.Count == 0) return;
if (grid.SelectedItems.Count == 0) return;
var message = "Are you sure you wish to delete ";
if (grid.SelectedItems.Count > 1) message += $"{grid.SelectedItems.Count} filters?";
else message += $"filter '{((CustomFilter)grid.SelectedItems[0]).Name}' ?";
if (MessageBox.Show(message,StaticHelpers.ClientName, MessageBoxButton.YesNo) != MessageBoxResult.Yes) e.Handled = true;
if (MessageBox.Show(message, StaticHelpers.ClientName, MessageBoxButton.YesNo) != MessageBoxResult.Yes) e.Handled = true;
}

private void Row_DoubleClick(object sender, MouseButtonEventArgs e)
Expand Down
1 change: 1 addition & 0 deletions Happy Reader/View/Tabs/DatabaseTab.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ private void OpenVNPanel(MouseEventArgs e, FrameworkElement element, bool switch

private void ScrollToTop()
{
if (VisualNovelItems.Items.Count == 0) return;
var firstItem = VisualNovelItems.Items[0];
if(firstItem == null) return;
VisualNovelItems.ScrollIntoView(firstItem);
Expand Down
Loading

0 comments on commit 7d3a5af

Please sign in to comment.