Prioritize normal icon issuer matches over inverse matches

Icon packs may have very generic issuers for their icons (like [aegis-simple-icons](https://github.com/alexbakker/aegis-simple-icons)).
For example, this causes the icon assigning view to suggest the "C" icon for every
entry that contains a "c".

This patch addresses that by giving inverse matches (where the entry
issuer contains the icon issuer) a lower position in the suggested icons
list.
This commit is contained in:
Alexander Bakker 2024-01-20 14:16:33 +01:00
parent 566bcac3e0
commit bfbb3ef2c4
2 changed files with 38 additions and 18 deletions

View file

@ -18,7 +18,6 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
public class IconPack {
private UUID _uuid;
@ -59,9 +58,21 @@ public class IconPack {
return new ArrayList<>();
}
return _icons.stream()
.filter(i -> i.isSuggestedFor(issuer))
.collect(Collectors.toList());
List<Icon> icons = new ArrayList<>();
for (Icon icon : _icons) {
MatchType matchType = icon.getMatchFor(issuer);
if (matchType != null) {
// Inverse matches (entry issuer contains icon name) are less likely
// to be good, so position them at the end of the list.
if (matchType.equals(MatchType.NORMAL)) {
icons.add(0, icon);
} else if (matchType.equals(MatchType.INVERSE)) {
icons.add(icon);
}
}
}
return icons;
}
@Nullable
@ -162,15 +173,24 @@ public class IconPack {
return _category;
}
public List<String> getIssuers() {
return Collections.unmodifiableList(_issuers);
}
private MatchType getMatchFor(String issuer) {
String lowerEntryIssuer = issuer.toLowerCase();
public boolean isSuggestedFor(String issuer) {
String lowerIssuer = issuer.toLowerCase();
return getIssuers().stream()
.map(String::toLowerCase)
.anyMatch(is -> is.contains(lowerIssuer) || lowerIssuer.contains(is));
boolean inverseMatch = false;
for (String is : _issuers) {
String lowerIconIssuer = is.toLowerCase();
if (lowerIconIssuer.contains(lowerEntryIssuer)) {
return MatchType.NORMAL;
}
if (lowerEntryIssuer.contains(lowerIconIssuer)) {
inverseMatch = true;
}
}
if (inverseMatch) {
return MatchType.INVERSE;
}
return null;
}
public static Icon fromJson(JSONObject obj) throws JSONException {
@ -188,4 +208,9 @@ public class IconPack {
return new Icon(filename, name, category, issuers);
}
}
private enum MatchType {
NORMAL,
INVERSE
}
}

View file

@ -18,7 +18,6 @@ import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
public class IconAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final Context _context;
@ -96,11 +95,7 @@ public class IconAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
if (_query == null) {
loadIcons(_pack, false);
} else {
_icons = _pack.getIcons().stream()
.filter(i -> i.isSuggestedFor(query))
.collect(Collectors.toList());
Collections.sort(_icons, Comparator.comparing(IconPack.Icon::getName));
_icons = _pack.getSuggestedIcons(query);
notifyDataSetChanged();
}
}