MaxItemCount moved to HistoryFilter

This commit is contained in:
Jaex 2021-07-25 05:55:01 +03:00
parent c13c0afec7
commit 27e1bf9514
2 changed files with 41 additions and 56 deletions

View file

@ -88,7 +88,7 @@ public HistoryForm(string historyPath, HistorySettings settings, Action<string>
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<HistoryItem> 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)

View file

@ -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<HistoryItem> ApplyFilter(IEnumerable<HistoryItem> historyItems)
{
FileName = filename;
}
public HistoryItem[] ApplyFilter(HistoryItem[] historyItems)
{
if (!IsFiltered)
{
return historyItems;
}
IEnumerable<HistoryItem> 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;
}
}
}