From bfbb3ef2c47a86504c43289a44b347bd173bd74e Mon Sep 17 00:00:00 2001 From: Alexander Bakker Date: Sat, 20 Jan 2024 14:16:33 +0100 Subject: [PATCH] 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. --- .../beemdevelopment/aegis/icons/IconPack.java | 49 ++++++++++++++----- .../aegis/ui/views/IconAdapter.java | 7 +-- 2 files changed, 38 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/com/beemdevelopment/aegis/icons/IconPack.java b/app/src/main/java/com/beemdevelopment/aegis/icons/IconPack.java index 9120a4e3..5f87ca55 100644 --- a/app/src/main/java/com/beemdevelopment/aegis/icons/IconPack.java +++ b/app/src/main/java/com/beemdevelopment/aegis/icons/IconPack.java @@ -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 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 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 + } } diff --git a/app/src/main/java/com/beemdevelopment/aegis/ui/views/IconAdapter.java b/app/src/main/java/com/beemdevelopment/aegis/ui/views/IconAdapter.java index a61d7e1e..55043679 100644 --- a/app/src/main/java/com/beemdevelopment/aegis/ui/views/IconAdapter.java +++ b/app/src/main/java/com/beemdevelopment/aegis/ui/views/IconAdapter.java @@ -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 { private final Context _context; @@ -96,11 +95,7 @@ public class IconAdapter extends RecyclerView.Adapter { 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(); } }