Improve: package sort algorithms
This commit is contained in:
parent
3341084f1c
commit
67d1040113
@ -18,16 +18,14 @@ import android.widget.Toast
|
|||||||
import com.github.wrdlbrnft.sortedlistadapter.SortedListAdapter
|
import com.github.wrdlbrnft.sortedlistadapter.SortedListAdapter
|
||||||
import io.neoterm.R
|
import io.neoterm.R
|
||||||
import io.neoterm.backend.TerminalSession
|
import io.neoterm.backend.TerminalSession
|
||||||
import io.neoterm.component.pm.PackageComponent
|
import io.neoterm.component.pm.*
|
||||||
import io.neoterm.component.pm.Source
|
|
||||||
import io.neoterm.component.pm.SourceManager
|
|
||||||
import io.neoterm.component.pm.SourceHelper
|
|
||||||
import io.neoterm.frontend.component.ComponentManager
|
import io.neoterm.frontend.component.ComponentManager
|
||||||
import io.neoterm.frontend.config.NeoPreference
|
import io.neoterm.frontend.config.NeoPreference
|
||||||
import io.neoterm.frontend.config.NeoTermPath
|
import io.neoterm.frontend.config.NeoTermPath
|
||||||
import io.neoterm.frontend.floating.TerminalDialog
|
import io.neoterm.frontend.floating.TerminalDialog
|
||||||
import io.neoterm.ui.pm.adapter.PackageAdapter
|
import io.neoterm.ui.pm.adapter.PackageAdapter
|
||||||
import io.neoterm.ui.pm.model.PackageModel
|
import io.neoterm.ui.pm.model.PackageModel
|
||||||
|
import io.neoterm.ui.pm.utils.StringDistance
|
||||||
import io.neoterm.utils.PackageUtils
|
import io.neoterm.utils.PackageUtils
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -232,18 +230,25 @@ class PackageManagerActivity : AppCompatActivity(), SearchView.OnQueryTextListen
|
|||||||
}.start()
|
}.start()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun filter(models: List<PackageModel>, query: String?): List<PackageModel> {
|
private fun sortDistance(models: List<PackageModel>, query: String,
|
||||||
val filteredModelList = ArrayList<PackageModel>()
|
mapper: (NeoPackageInfo) -> String): List<Pair<PackageModel, Int>> {
|
||||||
if (query != null) {
|
return models
|
||||||
val lowerCaseQuery = query.toLowerCase()
|
.map({
|
||||||
for (model in models) {
|
Pair(it, StringDistance.distance(mapper(it.packageInfo).toLowerCase(), query.toLowerCase()))
|
||||||
val name = model.packageInfo.packageName!!.toLowerCase()
|
})
|
||||||
val desc = model.packageInfo.description!!.toLowerCase()
|
.sortedWith(Comparator { l, r -> r.second.compareTo(l.second) })
|
||||||
if (name.contains(lowerCaseQuery) || desc.contains(lowerCaseQuery)) {
|
.toList()
|
||||||
filteredModelList.add(model)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun filter(models: List<PackageModel>, query: String): List<PackageModel> {
|
||||||
|
val filteredModelList = mutableListOf<PackageModel>()
|
||||||
|
val prepared = models.filter {
|
||||||
|
it.packageInfo.packageName!!.contains(query, true)
|
||||||
|
|| it.packageInfo.description!!.contains(query, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sortDistance(prepared, query, { it.packageName!! }).mapTo(filteredModelList, { it.first })
|
||||||
|
sortDistance(prepared, query, { it.description!! }).mapTo(filteredModelList, { it.first })
|
||||||
return filteredModelList
|
return filteredModelList
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -252,10 +257,12 @@ class PackageManagerActivity : AppCompatActivity(), SearchView.OnQueryTextListen
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun onQueryTextChange(text: String?): Boolean {
|
override fun onQueryTextChange(text: String?): Boolean {
|
||||||
|
if (text != null) {
|
||||||
val filteredModelList = filter(models, text)
|
val filteredModelList = filter(models, text)
|
||||||
adapter.edit()
|
adapter.edit()
|
||||||
.replaceAll(filteredModelList)
|
.replaceAll(filteredModelList)
|
||||||
.commit()
|
.commit()
|
||||||
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
38
app/src/main/java/io/neoterm/ui/pm/utils/StringDistance.java
Normal file
38
app/src/main/java/io/neoterm/ui/pm/utils/StringDistance.java
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
package io.neoterm.ui.pm.utils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author kiva
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class StringDistance {
|
||||||
|
public static int distance(String source, String target) {
|
||||||
|
char[] sources = source.toCharArray();
|
||||||
|
char[] targets = target.toCharArray();
|
||||||
|
int sourceLen = sources.length;
|
||||||
|
int targetLen = targets.length;
|
||||||
|
|
||||||
|
int[][] d = new int[sourceLen + 1][targetLen + 1];
|
||||||
|
for (int i = 0; i <= sourceLen; i++) {
|
||||||
|
d[i][0] = i;
|
||||||
|
}
|
||||||
|
for (int i = 0; i <= targetLen; i++) {
|
||||||
|
d[0][i] = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 1; i <= sourceLen; i++) {
|
||||||
|
for (int j = 1; j <= targetLen; j++) {
|
||||||
|
if (sources[i - 1] == targets[j - 1]) {
|
||||||
|
d[i][j] = d[i - 1][j - 1];
|
||||||
|
} else {
|
||||||
|
int insert = d[i][j - 1] + 1;
|
||||||
|
int delete = d[i - 1][j] + 1;
|
||||||
|
int replace = d[i - 1][j - 1] + 1;
|
||||||
|
d[i][j] = Math.min(insert, delete) > Math.min(delete, replace)
|
||||||
|
? Math.min(delete, replace)
|
||||||
|
: Math.min(insert, delete);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return d[sourceLen][targetLen];
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user