Skip to content
This repository was archived by the owner on May 4, 2023. It is now read-only.

Commit a37c047

Browse files
committed
Rework search engine and add a pertinence system. (Fix #36)
1 parent 96f4eb3 commit a37c047

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

app/src/main/java/com/fox2code/mmm/MainActivity.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ private void cardIconifyUpdate() {
167167
theme.resolveAttribute(backgroundAttr, value, true);
168168
this.searchCard.setCardBackgroundColor(value.data);
169169
this.searchCard.setAlpha(iconified ? 0.70F : 1F);
170+
this.moduleViewListBuilder.setFooterPx(this.searchCard.getHeight());
170171
}
171172

172173
@Override

app/src/main/java/com/fox2code/mmm/ModuleHolder.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public final class ModuleHolder implements Comparable<ModuleHolder> {
2727
public final int footerPx;
2828
public LocalModuleInfo moduleInfo;
2929
public RepoModule repoModule;
30+
public int filterLevel;
3031

3132
public ModuleHolder(String moduleId) {
3233
this.moduleId = Objects.requireNonNull(moduleId);
@@ -209,19 +210,25 @@ public int compare(ModuleHolder o1, ModuleHolder o2) {
209210
UPDATABLE(R.string.updatable, true) {
210211
@Override
211212
public int compare(ModuleHolder o1, ModuleHolder o2) {
213+
int cmp = Integer.compare(o1.filterLevel, o2.filterLevel);
214+
if (cmp != 0) return cmp;
212215
return Long.compare(o2.repoModule.lastUpdated, o1.repoModule.lastUpdated);
213216
}
214217
},
215218
INSTALLED(R.string.installed, true) {
216219
@Override
217220
public int compare(ModuleHolder o1, ModuleHolder o2) {
221+
int cmp = Integer.compare(o1.filterLevel, o2.filterLevel);
222+
if (cmp != 0) return cmp;
218223
return o1.getMainModuleName().compareTo(o2.getMainModuleName());
219224
}
220225
},
221226
SPECIAL_NOTIFICATIONS(R.string.loading, true),
222227
INSTALLABLE(R.string.online_repo, true) {
223228
@Override
224229
public int compare(ModuleHolder o1, ModuleHolder o2) {
230+
int cmp = Integer.compare(o1.filterLevel, o2.filterLevel);
231+
if (cmp != 0) return cmp;
225232
return Long.compare(o2.repoModule.lastUpdated, o1.repoModule.lastUpdated);
226233
}
227234
},

app/src/main/java/com/fox2code/mmm/ModuleViewListBuilder.java

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class ModuleViewListBuilder {
3131
@NonNull
3232
private String query = "";
3333
private boolean noUpdate;
34+
private int footerPx;
3435

3536
public ModuleViewListBuilder(Activity activity) {
3637
this.activity = activity;
@@ -100,7 +101,8 @@ public void applyTo(RecyclerView moduleList, ModuleViewAdapter moduleViewAdapter
100101
try {
101102
synchronized (this.updateLock) {
102103
// Build start
103-
moduleHolders = new ArrayList<>();
104+
moduleHolders = new ArrayList<>(Math.min(64,
105+
this.mappedModuleHolders.size() + 5));
104106
int special = 0;
105107
Iterator<NotificationType> notificationTypeIterator = this.notifications.iterator();
106108
while (notificationTypeIterator.hasNext()) {
@@ -133,6 +135,9 @@ public void applyTo(RecyclerView moduleList, ModuleViewAdapter moduleViewAdapter
133135
}
134136
}
135137
Collections.sort(moduleHolders, ModuleHolder::compareTo);
138+
if (this.footerPx != 0) { // Footer is always last
139+
moduleHolders.add(new ModuleHolder(this.footerPx));
140+
}
136141
Log.i(TAG, "Got " + moduleHolders.size() + " entries!");
137142
// Build end
138143
}
@@ -198,10 +203,27 @@ public void refreshNotificationsUI(ModuleViewAdapter moduleViewAdapter) {
198203
}
199204

200205
private boolean matchFilter(ModuleHolder moduleHolder) {
201-
if (this.query.isEmpty()) return true;
202206
ModuleInfo moduleInfo = moduleHolder.getMainModuleInfo();
203-
return moduleInfo.id.toLowerCase(Locale.ROOT).contains(this.query) ||
204-
moduleInfo.name.toLowerCase(Locale.ROOT).contains(this.query);
207+
String query = this.query;
208+
String idLw = moduleInfo.id.toLowerCase(Locale.ROOT);
209+
String nameLw = moduleInfo.name.toLowerCase(Locale.ROOT);
210+
String authorLw = moduleInfo.author.toLowerCase(Locale.ROOT);
211+
if (query.isEmpty() || query.equals(idLw) ||
212+
query.equals(nameLw) || query.equals(authorLw)) {
213+
moduleHolder.filterLevel = 0; // Lower = better
214+
return true;
215+
}
216+
if (idLw.contains(query) || nameLw.contains(query)) {
217+
moduleHolder.filterLevel = 1;
218+
return true;
219+
}
220+
if (authorLw.contains(query) || (moduleInfo.description != null &&
221+
moduleInfo.description.toLowerCase(Locale.ROOT).contains(query))) {
222+
moduleHolder.filterLevel = 2;
223+
return true;
224+
}
225+
moduleHolder.filterLevel = 3;
226+
return false;
205227
}
206228

207229
private static void notifySizeChanged(ModuleViewAdapter moduleViewAdapter,
@@ -242,4 +264,13 @@ public boolean setQueryChange(String query) {
242264
}
243265
return true;
244266
}
267+
268+
public void setFooterPx(int footerPx) {
269+
if (this.footerPx != footerPx) {
270+
synchronized (this.updateLock) {
271+
this.footerPx = footerPx;
272+
}
273+
this.noUpdate = false;
274+
}
275+
}
245276
}

0 commit comments

Comments
 (0)