Skip to content

Commit

Permalink
Add Mod filter function: show same Guid (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
kuule72 authored Feb 18, 2021
1 parent fd73d63 commit c40529e
Show file tree
Hide file tree
Showing 11 changed files with 958 additions and 520 deletions.
1 change: 1 addition & 0 deletions src/KKManager.Core/KKManager.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@
<AutoGen>True</AutoGen>
<DesignTimeSharedInput>True</DesignTimeSharedInput>
</Compile>
<Compile Include="Util\ModMatchFilter.cs" />
<Compile Include="Util\TimeSpanUtils.cs" />
<Compile Include="Util\Extensions.cs" />
<Compile Include="Util\FileSize.cs" />
Expand Down
10 changes: 5 additions & 5 deletions src/KKManager.Core/Util/ListTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,16 @@ bool SearchStringIsEmpty()
PutSearchText();
};

var textMatchFilter = new TextMatchFilter(listView);
textMatchFilter.StringComparison = StringComparison.OrdinalIgnoreCase;
listView.AdditionalFilter = textMatchFilter;
var matchFilter = new ModMatchFilter(listView);
matchFilter.StringComparison = StringComparison.OrdinalIgnoreCase;
listView.AdditionalFilter = matchFilter;
listView.UseFiltering = true;
searchBox.TextChanged += (sender, args) =>
{
if (SearchStringIsEmpty())
textMatchFilter.ContainsStrings = null;
matchFilter.ContainsStrings = null;
else
textMatchFilter.ContainsStrings = new[] { searchBox.Text.Trim() };
matchFilter.ContainsStrings = new[] { searchBox.Text.Trim() };
listView.UpdateColumnFiltering();
};
}
Expand Down
141 changes: 141 additions & 0 deletions src/KKManager.Core/Util/ModMatchFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BrightIdeasSoftware;

namespace KKManager.Util
{
public class ModMatchFilter : IModelFilter
{
public StringComparison StringComparison
{
get { return textMatchFilter.StringComparison; }
set { textMatchFilter.StringComparison = value; }
}

public IEnumerable<string> ContainsStrings
{
get { return _ContainsStrings; }
set { SetContainsStrings(value); }
}

public ObjectListView ListView { get { return textMatchFilter.ListView; } }

IEnumerable<string> _ContainsStrings;
TextMatchFilter textMatchFilter;
HashSet<Data.ModInfoBase> sameMods;
string sameCol;
bool isMod;
bool isFilterCol;


public ModMatchFilter(ObjectListView olv)
{
textMatchFilter = new TextMatchFilter(olv);
sameMods = new HashSet<Data.ModInfoBase>();
}

public bool Filter(object modelObject)
{
bool textMatch = textMatchFilter.Filter(modelObject);

if (isMod && isFilterCol && textMatch)
{
return sameMods.Contains(modelObject);
}
return textMatch;
}


private void SetContainsStrings(IEnumerable<string> strs)
{
_ContainsStrings = strs;
sameCol = null;
isFilterCol = false;
isMod = false;
sameMods.Clear();
if (strs == null)
{
textMatchFilter.ContainsStrings = null;
return;
}

foreach (var item in ListView.Objects)
{
isMod = item is Data.ModInfoBase;
break;
}


List<string> textStrs = new List<string>();
foreach (var item in strs)
{
string words = item;
if (item.Contains("same:guid")) // same:guid
{
words = item.Replace("same:guid", "");
sameCol = "guid";
}
else if (item.Contains("same:name")) // same:name
{
words = item.Replace("same:name", "");
sameCol = "name";
}
textStrs.Add(words.Trim());
}

isFilterCol = !string.IsNullOrWhiteSpace(sameCol);
textMatchFilter.ContainsStrings = textStrs;

if (isMod)
{
var mods = new List<Data.ModInfoBase>(500);
foreach (var item in ListView.Objects)
{
mods.Add(item as Data.ModInfoBase);
}
FilterSameColumn(mods);
}
}


private IEnumerable FilterSameColumn(IEnumerable<Data.ModInfoBase> modelObjects)
{
IEnumerable<IGrouping<string, Data.ModInfoBase>> filterList;

switch (sameCol)
{
case "guid":
filterList = from r in modelObjects
group r by r.Guid into g
where g.Count() > 1
select g;
break;
case "name":
filterList = from r in modelObjects
group r by r.Name into g
where g.Count() > 1
select g;
break;
default:
return modelObjects;
}

sameMods.Clear();
foreach (var item in filterList)
{
foreach (var mod in item)
{
sameMods.Add(mod);
}
}

return sameMods;
}


}
}
45 changes: 31 additions & 14 deletions src/KKManager/Windows/Content/PluginsWindow.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions src/KKManager/Windows/Content/PluginsWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,5 +191,27 @@ private void toolStripButtonOpenDir_Click(object sender, EventArgs e)
MessageBox.Show(ex.Message, "Failed to start application", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

private void toolStripButtonSameGuid_Click(object sender, EventArgs e)
{
const string SearchText = "Search...";
const string SameGuidText = "same:guid";

var searchString = toolStripTextBoxSearch.Text;
searchString = searchString.Replace(SearchText, "").Trim();

if (searchString.ToLower().Contains(SameGuidText))
{
searchString = searchString.ToLower().Replace(SameGuidText, "");
toolStripButtonSameGuid.BackColor = SystemColors.Control;
}
else
{
searchString = searchString.ToLower().Replace(SameGuidText, "");
searchString = SameGuidText + " " + searchString;
toolStripButtonSameGuid.BackColor = Color.Green;
}
toolStripTextBoxSearch.Text = searchString;
}
}
}
Loading

0 comments on commit c40529e

Please sign in to comment.