diff --git a/ShareX.HistoryLib/Forms/HistoryForm.cs b/ShareX.HistoryLib/Forms/HistoryForm.cs index 55b6f6316..07fdad728 100644 --- a/ShareX.HistoryLib/Forms/HistoryForm.cs +++ b/ShareX.HistoryLib/Forms/HistoryForm.cs @@ -88,7 +88,7 @@ public HistoryForm(string historyPath, HistorySettings settings, Action private void RefreshHistoryItems() { allHistoryItems = GetHistoryItems(); - ApplyFilter(); + ApplyFilterAdvanced(); } private void OutputStats(HistoryItem[] historyItems) @@ -163,7 +163,28 @@ private HistoryItem[] GetHistoryItems() return historyItems.ToArray(); } - private void ApplyFilter() + private void ApplyFilter(HistoryFilter filter) + { + if (allHistoryItems != null && allHistoryItems.Length > 0) + { + IEnumerable historyItems = filter.ApplyFilter(allHistoryItems); + + AddHistoryItems(historyItems.ToArray()); + } + } + + private void ApplyFilterSimple() + { + HistoryFilter filter = new HistoryFilter() + { + FileName = tstbSearch.Text, + MaxItemCount = Settings.MaxItemCount + }; + + ApplyFilter(filter); + } + + private void ApplyFilterAdvanced() { HistoryFilter filter = new HistoryFilter() { @@ -175,34 +196,13 @@ private void ApplyFilter() FilterType = cbTypeFilter.Checked, Type = cbTypeFilterSelection.Text, FilterHost = cbHostFilter.Checked, - Host = cbHostFilterSelection.Text + Host = cbHostFilterSelection.Text, + MaxItemCount = Settings.MaxItemCount }; ApplyFilter(filter); } - private void ApplyFilterSimple() - { - HistoryFilter filter = new HistoryFilter(tstbSearch.Text); - - ApplyFilter(filter); - } - - private void ApplyFilter(HistoryFilter filter) - { - if (allHistoryItems != null && allHistoryItems.Length > 0) - { - HistoryItem[] historyItems = filter.ApplyFilter(allHistoryItems); - - if (Settings.MaxItemCount > 0 && historyItems.Length > Settings.MaxItemCount) - { - historyItems = historyItems.Take(Settings.MaxItemCount).ToArray(); - } - - AddHistoryItems(historyItems); - } - } - private void AddHistoryItems(HistoryItem[] historyItems) { Cursor = Cursors.WaitCursor; @@ -382,7 +382,7 @@ private void txtFilenameFilter_KeyDown(object sender, KeyEventArgs e) { e.Handled = true; e.SuppressKeyPress = true; - ApplyFilter(); + ApplyFilterAdvanced(); txtFilenameFilter.Focus(); } } @@ -393,14 +393,14 @@ private void txtURLFilter_KeyDown(object sender, KeyEventArgs e) { e.Handled = true; e.SuppressKeyPress = true; - ApplyFilter(); + ApplyFilterAdvanced(); txtURLFilter.Focus(); } } private void btnApplyFilters_Click(object sender, EventArgs e) { - ApplyFilter(); + ApplyFilterAdvanced(); } private void btnRemoveFilters_Click(object sender, EventArgs e) diff --git a/ShareX.HistoryLib/HistoryFilter.cs b/ShareX.HistoryLib/HistoryFilter.cs index 5939624d9..74b0a41a8 100644 --- a/ShareX.HistoryLib/HistoryFilter.cs +++ b/ShareX.HistoryLib/HistoryFilter.cs @@ -43,64 +43,49 @@ public class HistoryFilter public bool FilterHost { get; set; } public string Host { get; set; } + public int MaxItemCount { get; set; } public bool SearchInTags { get; set; } = true; - public bool IsFiltered - { - get - { - return !string.IsNullOrEmpty(FileName) || !string.IsNullOrEmpty(URL) || FilterDate || - (FilterType && !string.IsNullOrEmpty(Type)) || (FilterHost && !string.IsNullOrEmpty(Host)); - } - } - public HistoryFilter() { } - public HistoryFilter(string filename) + public IEnumerable ApplyFilter(IEnumerable historyItems) { - FileName = filename; - } - - public HistoryItem[] ApplyFilter(HistoryItem[] historyItems) - { - if (!IsFiltered) - { - return historyItems; - } - - IEnumerable result = historyItems.AsEnumerable(); - if (FilterType && !string.IsNullOrEmpty(Type)) { - result = result.Where(x => !string.IsNullOrEmpty(x.Type) && x.Type.Equals(Type, StringComparison.InvariantCultureIgnoreCase)); + historyItems = historyItems.Where(x => !string.IsNullOrEmpty(x.Type) && x.Type.Equals(Type, StringComparison.InvariantCultureIgnoreCase)); } if (FilterHost && !string.IsNullOrEmpty(Host)) { - result = result.Where(x => !string.IsNullOrEmpty(x.Host) && x.Host.Contains(Host, StringComparison.InvariantCultureIgnoreCase)); + historyItems = historyItems.Where(x => !string.IsNullOrEmpty(x.Host) && x.Host.Contains(Host, StringComparison.InvariantCultureIgnoreCase)); } if (!string.IsNullOrEmpty(FileName)) { string pattern = Regex.Escape(FileName).Replace("\\?", ".").Replace("\\*", ".*"); Regex regex = new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant); - result = result.Where(x => (x.FileName != null && regex.IsMatch(x.FileName)) || + historyItems = historyItems.Where(x => (x.FileName != null && regex.IsMatch(x.FileName)) || (SearchInTags && x.Tags != null && x.Tags.Any(tag => regex.IsMatch(tag.Value)))); } if (!string.IsNullOrEmpty(URL)) { - result = result.Where(x => x.URL != null && x.URL.Contains(URL, StringComparison.InvariantCultureIgnoreCase)); + historyItems = historyItems.Where(x => x.URL != null && x.URL.Contains(URL, StringComparison.InvariantCultureIgnoreCase)); } if (FilterDate) { - result = result.Where(x => x.DateTime.Date >= FromDate && x.DateTime.Date <= ToDate); + historyItems = historyItems.Where(x => x.DateTime.Date >= FromDate && x.DateTime.Date <= ToDate); } - return result.ToArray(); + if (MaxItemCount > 0) + { + historyItems = historyItems.Take(MaxItemCount); + } + + return historyItems; } } } \ No newline at end of file