Skip to content

Commit

Permalink
Prioritize normal icon issuer matches over inverse matches
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
alexbakker committed Jan 20, 2024
1 parent 566bcac commit bfbb3ef
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 18 deletions.
49 changes: 37 additions & 12 deletions app/src/main/java/com/beemdevelopment/aegis/icons/IconPack.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

public class IconPack {
private UUID _uuid;
Expand Down Expand Up @@ -59,9 +58,21 @@ public List<Icon> getSuggestedIcons(String issuer) {
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
Expand Down Expand Up @@ -162,15 +173,24 @@ public String getCategory() {
return _category;
}

public List<String> getIssuers() {
return Collections.unmodifiableList(_issuers);
}
private MatchType getMatchFor(String issuer) {
String lowerEntryIssuer = issuer.toLowerCase();

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;
}

public boolean isSuggestedFor(String issuer) {
String lowerIssuer = issuer.toLowerCase();
return getIssuers().stream()
.map(String::toLowerCase)
.anyMatch(is -> is.contains(lowerIssuer) || lowerIssuer.contains(is));
return null;
}

public static Icon fromJson(JSONObject obj) throws JSONException {
Expand All @@ -188,4 +208,9 @@ public static Icon fromJson(JSONObject obj) throws JSONException {
return new Icon(filename, name, category, issuers);
}
}

private enum MatchType {
NORMAL,
INVERSE
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
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;
Expand Down Expand Up @@ -96,11 +95,7 @@ public void setQuery(@Nullable String query) {
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();
}
}
Expand Down

0 comments on commit bfbb3ef

Please sign in to comment.