Skip to content

Commit

Permalink
Filters: Extended Release Month filter to replace former release date…
Browse files Browse the repository at this point in the history
… filter.
  • Loading branch information
Zoltanar committed Dec 10, 2023
1 parent 0e58488 commit 00c550a
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 50 deletions.
1 change: 0 additions & 1 deletion Happy Reader/Model/VnFilters/GeneralFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
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
71 changes: 57 additions & 14 deletions Happy Reader/Model/VnFilters/ReleaseMonthFilter.cs
Original file line number Diff line number Diff line change
@@ -1,43 +1,86 @@
using System;
using System.Text.RegularExpressions;

namespace Happy_Reader.Model.VnFilters;
namespace Happy_Reader;

public class ReleaseMonthFilter
{
private static readonly Regex ParseRegex = new(@"(?<relative>(|Relative:))(?<year>-?\d{1,4}):(?<month>-?\d{1,2})");
private static readonly Regex ParseRegex = new(@"(?<relative>(|Relative:))(?<year>Y-?\d{1,4})(?<month>M-?\d{1,2})?(?<day>D-?\d{1,2})?(?<between>;(?<torelative>(|Relative:))(?<toyear>Y-?\d{1,4})(?<tomonth>M-?\d{1,2})?(?<today>D-?\d{1,2})?)?");

public bool Relative { get; set; }
public int Year { get; set; }
public int Month { get; set; }
public int? Month { get; set; }
public int? Day { get; set; }
public bool Between { get; set; }
public bool ToRelative { get; set; }
public int ToYear { get; set; }
public int? ToMonth { get; set; }
public int? ToDay { get; set; }

public bool IsInReleaseMonth(DateTime dateTime)
{
var now = DateTime.UtcNow;
if (Relative)
var fromOrOn = GetDateTime(true, Relative, Year, Month, Day);
if (!Between)
{
var yearDiff = dateTime.Year - now.Year;
if (yearDiff != Year) return false;
var monthDiff = dateTime.Month - now.Month;
return monthDiff == Month;
if (dateTime.Year != fromOrOn.Year) return false;
return (!Month.HasValue || dateTime.Month == fromOrOn.Month) && (!Day.HasValue || dateTime.Day == fromOrOn.Day);
}
if (dateTime.Year != Year) return false;
return dateTime.Month == Month;
var to = GetDateTime(false, ToRelative, ToYear, ToMonth, ToDay);
return dateTime >= fromOrOn && dateTime <= to;
}

public override string ToString() => $"{(Relative ? "Relative:" : string.Empty)}{Year:0000}:{Month:00}";
private static DateTime GetDateTime(bool from, bool relative, int year, int? month, int? day)
{
DateTime dateTime;
if (!relative)
{
var actualMonth = month ?? (from ? 1 : 12);
dateTime = new DateTime(year, actualMonth, day ?? (from ? 1 : DateTime.DaysInMonth(year, actualMonth)));
}
else
{
dateTime = DateTime.Today;
dateTime = dateTime.AddYears(year);
dateTime = month.HasValue ? dateTime.AddMonths(month.Value) : new DateTime(dateTime.Year, from ? 1 : 12, 1);
dateTime = day.HasValue ? dateTime.AddDays(day.Value) : new DateTime(dateTime.Year, dateTime.Month, from ? 1 : DateTime.DaysInMonth(dateTime.Year, dateTime.Month));
}
return dateTime;

}

public override string ToString()
{
var result = $"{(Relative ? "Relative:" : string.Empty)}Y{Year:0000}";
if (Month.HasValue) result += $"M{Month:00}";
if (Day.HasValue) result += $"D{Day:00}";
if (Between)
{
result += $";{(ToRelative ? "Relative:" : string.Empty)}Y{ToYear:0000}";
if (ToMonth.HasValue) result += $"M{ToMonth:00}";
if (ToDay.HasValue) result += $"D{ToDay:00}";
}
return result;
}

public static bool TryParse(string value, out ReleaseMonthFilter releaseMonthFilter)
{
releaseMonthFilter = default;
if (string.IsNullOrEmpty(value)) return false;
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)
Year = int.Parse(match.Groups["year"].Value.Substring(1)),
Month = match.Groups["month"].Success ? int.Parse(match.Groups["month"].Value.Substring(1)) : null,
Day = match.Groups["day"].Success ? int.Parse(match.Groups["day"].Value.Substring(1)) : null
};
if (!match.Groups["between"].Success) return true;
releaseMonthFilter.Between = true;
releaseMonthFilter.ToRelative = match.Groups["torelative"].Value != string.Empty;
releaseMonthFilter.ToYear = int.Parse(match.Groups["toyear"].Value.Substring(1));
releaseMonthFilter.ToMonth = match.Groups["tomonth"].Success ? int.Parse(match.Groups["tomonth"].Value.Substring(1)) : null;
releaseMonthFilter.ToDay = match.Groups["today"].Success ? int.Parse(match.Groups["today"].Value.Substring(1)) : null;
return true;
}
}
1 change: 0 additions & 1 deletion Happy Reader/View/FiltersPane.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
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 Down
28 changes: 24 additions & 4 deletions Happy Reader/View/ReleaseFilterPanel.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,29 @@
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"
xmlns:happyReader="clr-namespace:Happy_Reader"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid x:Name="MainGrid">

</Grid>
d:DataContext="{d:DesignInstance happyReader:ReleaseMonthFilter}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<CheckBox Grid.Row="0" Grid.Column="0" Content="Relative" IsChecked="{Binding Relative, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged}" SourceUpdated="UpdateReleaseMonthFilter" TargetUpdated="UpdateReleaseMonthFilter" VerticalContentAlignment="Center"/>
<local:LabeledTextBox Grid.Row="0" Grid.Column="1" Label="Year" LabelWidth="35" Text="{Binding Year, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged}" SourceUpdated="UpdateReleaseMonthFilter" TargetUpdated="UpdateReleaseMonthFilter"/>
<local:LabeledTextBox Grid.Row="0" Grid.Column="2" Label="Month" LabelWidth="50" Text="{Binding Month, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged, TargetNullValue=''}" SourceUpdated="UpdateReleaseMonthFilter" TargetUpdated="UpdateReleaseMonthFilter"/>
<local:LabeledTextBox Grid.Row="0" Grid.Column="3" Label="Day" LabelWidth="50" Text="{Binding Day, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged, TargetNullValue=''}" SourceUpdated="UpdateReleaseMonthFilter" TargetUpdated="UpdateReleaseMonthFilter"/>
<CheckBox Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="4" Content="Between" IsChecked="{Binding Between}" HorizontalAlignment="Center"/>
<CheckBox Grid.Row="2" Grid.Column="0" IsEnabled="{Binding Between}" Content="Relative" IsChecked="{Binding ToRelative, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged}" SourceUpdated="UpdateReleaseMonthFilter" TargetUpdated="UpdateReleaseMonthFilter" VerticalContentAlignment="Center"/>
<local:LabeledTextBox Grid.Row="2" Grid.Column="1" IsEnabled="{Binding Between}" Label="Year" LabelWidth="35" Text="{Binding ToYear, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged}" SourceUpdated="UpdateReleaseMonthFilter" TargetUpdated="UpdateReleaseMonthFilter"/>
<local:LabeledTextBox Grid.Row="2" Grid.Column="2" IsEnabled="{Binding Between}" Label="Month" LabelWidth="50" Text="{Binding ToMonth, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged, TargetNullValue=''}" SourceUpdated="UpdateReleaseMonthFilter" TargetUpdated="UpdateReleaseMonthFilter"/>
<local:LabeledTextBox Grid.Row="2" Grid.Column="3" IsEnabled="{Binding Between}" Label="Day" LabelWidth="50" Text="{Binding ToDay, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged, TargetNullValue=''}" SourceUpdated="UpdateReleaseMonthFilter" TargetUpdated="UpdateReleaseMonthFilter"/>
</Grid>
</UserControl>
34 changes: 4 additions & 30 deletions Happy Reader/View/ReleaseFilterPanel.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using Happy_Reader.Model.VnFilters;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Controls;
using System.Windows.Data;
namespace Happy_Reader.View
{
Expand All @@ -13,33 +10,10 @@ public partial class ReleaseFilterPanel : UserControl
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);
DataContext = _releaseMonth;
}

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

0 comments on commit 00c550a

Please sign in to comment.