Rework search engine and add a pertinence system. (Fix #36)

pull/42/head
Fox2Code 2 years ago
parent 96f4eb3862
commit a37c047edb

@ -167,6 +167,7 @@ public class MainActivity extends CompatActivity implements SwipeRefreshLayout.O
theme.resolveAttribute(backgroundAttr, value, true);
this.searchCard.setCardBackgroundColor(value.data);
this.searchCard.setAlpha(iconified ? 0.70F : 1F);
this.moduleViewListBuilder.setFooterPx(this.searchCard.getHeight());
}
@Override

@ -27,6 +27,7 @@ public final class ModuleHolder implements Comparable<ModuleHolder> {
public final int footerPx;
public LocalModuleInfo moduleInfo;
public RepoModule repoModule;
public int filterLevel;
public ModuleHolder(String moduleId) {
this.moduleId = Objects.requireNonNull(moduleId);
@ -209,12 +210,16 @@ public final class ModuleHolder implements Comparable<ModuleHolder> {
UPDATABLE(R.string.updatable, true) {
@Override
public int compare(ModuleHolder o1, ModuleHolder o2) {
int cmp = Integer.compare(o1.filterLevel, o2.filterLevel);
if (cmp != 0) return cmp;
return Long.compare(o2.repoModule.lastUpdated, o1.repoModule.lastUpdated);
}
},
INSTALLED(R.string.installed, true) {
@Override
public int compare(ModuleHolder o1, ModuleHolder o2) {
int cmp = Integer.compare(o1.filterLevel, o2.filterLevel);
if (cmp != 0) return cmp;
return o1.getMainModuleName().compareTo(o2.getMainModuleName());
}
},
@ -222,6 +227,8 @@ public final class ModuleHolder implements Comparable<ModuleHolder> {
INSTALLABLE(R.string.online_repo, true) {
@Override
public int compare(ModuleHolder o1, ModuleHolder o2) {
int cmp = Integer.compare(o1.filterLevel, o2.filterLevel);
if (cmp != 0) return cmp;
return Long.compare(o2.repoModule.lastUpdated, o1.repoModule.lastUpdated);
}
},

@ -31,6 +31,7 @@ public class ModuleViewListBuilder {
@NonNull
private String query = "";
private boolean noUpdate;
private int footerPx;
public ModuleViewListBuilder(Activity activity) {
this.activity = activity;
@ -100,7 +101,8 @@ public class ModuleViewListBuilder {
try {
synchronized (this.updateLock) {
// Build start
moduleHolders = new ArrayList<>();
moduleHolders = new ArrayList<>(Math.min(64,
this.mappedModuleHolders.size() + 5));
int special = 0;
Iterator<NotificationType> notificationTypeIterator = this.notifications.iterator();
while (notificationTypeIterator.hasNext()) {
@ -133,6 +135,9 @@ public class ModuleViewListBuilder {
}
}
Collections.sort(moduleHolders, ModuleHolder::compareTo);
if (this.footerPx != 0) { // Footer is always last
moduleHolders.add(new ModuleHolder(this.footerPx));
}
Log.i(TAG, "Got " + moduleHolders.size() + " entries!");
// Build end
}
@ -198,10 +203,27 @@ public class ModuleViewListBuilder {
}
private boolean matchFilter(ModuleHolder moduleHolder) {
if (this.query.isEmpty()) return true;
ModuleInfo moduleInfo = moduleHolder.getMainModuleInfo();
return moduleInfo.id.toLowerCase(Locale.ROOT).contains(this.query) ||
moduleInfo.name.toLowerCase(Locale.ROOT).contains(this.query);
String query = this.query;
String idLw = moduleInfo.id.toLowerCase(Locale.ROOT);
String nameLw = moduleInfo.name.toLowerCase(Locale.ROOT);
String authorLw = moduleInfo.author.toLowerCase(Locale.ROOT);
if (query.isEmpty() || query.equals(idLw) ||
query.equals(nameLw) || query.equals(authorLw)) {
moduleHolder.filterLevel = 0; // Lower = better
return true;
}
if (idLw.contains(query) || nameLw.contains(query)) {
moduleHolder.filterLevel = 1;
return true;
}
if (authorLw.contains(query) || (moduleInfo.description != null &&
moduleInfo.description.toLowerCase(Locale.ROOT).contains(query))) {
moduleHolder.filterLevel = 2;
return true;
}
moduleHolder.filterLevel = 3;
return false;
}
private static void notifySizeChanged(ModuleViewAdapter moduleViewAdapter,
@ -242,4 +264,13 @@ public class ModuleViewListBuilder {
}
return true;
}
public void setFooterPx(int footerPx) {
if (this.footerPx != footerPx) {
synchronized (this.updateLock) {
this.footerPx = footerPx;
}
this.noUpdate = false;
}
}
}

Loading…
Cancel
Save