From 00c550abffeacf831ddd44bf0715c491295cf7c0 Mon Sep 17 00:00:00 2001 From: Zoltanar Date: Sun, 10 Dec 2023 17:13:15 +0000 Subject: [PATCH] Filters: Extended Release Month filter to replace former release date filter. --- Happy Reader/Model/VnFilters/GeneralFilter.cs | 1 - .../Model/VnFilters/ReleaseMonthFilter.cs | 71 +++++++++++++++---- Happy Reader/View/FiltersPane.xaml.cs | 1 - Happy Reader/View/ReleaseFilterPanel.xaml | 28 ++++++-- Happy Reader/View/ReleaseFilterPanel.xaml.cs | 34 ++------- 5 files changed, 85 insertions(+), 50 deletions(-) diff --git a/Happy Reader/Model/VnFilters/GeneralFilter.cs b/Happy Reader/Model/VnFilters/GeneralFilter.cs index 43d935e..7bc0eb0 100644 --- a/Happy Reader/Model/VnFilters/GeneralFilter.cs +++ b/Happy Reader/Model/VnFilters/GeneralFilter.cs @@ -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; diff --git a/Happy Reader/Model/VnFilters/ReleaseMonthFilter.cs b/Happy Reader/Model/VnFilters/ReleaseMonthFilter.cs index 952c0fa..7755bab 100644 --- a/Happy Reader/Model/VnFilters/ReleaseMonthFilter.cs +++ b/Happy Reader/Model/VnFilters/ReleaseMonthFilter.cs @@ -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:))(?-?\d{1,4}):(?-?\d{1,2})"); + private static readonly Regex ParseRegex = new(@"(?(|Relative:))(?Y-?\d{1,4})(?M-?\d{1,2})?(?D-?\d{1,2})?(?;(?(|Relative:))(?Y-?\d{1,4})(?M-?\d{1,2})?(?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; } } \ No newline at end of file diff --git a/Happy Reader/View/FiltersPane.xaml.cs b/Happy Reader/View/FiltersPane.xaml.cs index 30b7589..d33b53e 100644 --- a/Happy Reader/View/FiltersPane.xaml.cs +++ b/Happy Reader/View/FiltersPane.xaml.cs @@ -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 diff --git a/Happy Reader/View/ReleaseFilterPanel.xaml b/Happy Reader/View/ReleaseFilterPanel.xaml index cf7eac0..430f012 100644 --- a/Happy Reader/View/ReleaseFilterPanel.xaml +++ b/Happy Reader/View/ReleaseFilterPanel.xaml @@ -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"> - - - + d:DataContext="{d:DesignInstance happyReader:ReleaseMonthFilter}"> + + + + + + + + + + + + + + + + + + + + + + diff --git a/Happy Reader/View/ReleaseFilterPanel.xaml.cs b/Happy Reader/View/ReleaseFilterPanel.xaml.cs index d0219dd..7e87226 100644 --- a/Happy Reader/View/ReleaseFilterPanel.xaml.cs +++ b/Happy Reader/View/ReleaseFilterPanel.xaml.cs @@ -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 { @@ -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;