Updated search to not update the entire thing

This commit is contained in:
ottoptj 2024-05-20 21:53:38 +03:00
commit faea9c95e3
3 changed files with 20 additions and 18 deletions

View file

@ -75,6 +75,7 @@ class AppMenuActivity : AppCompatActivity(), AppMenuAdapter.OnItemClickListener,
recyclerView.layoutManager = LinearLayoutManager(this)
installedApps = getInstalledApps()
filteredApps = mutableListOf()
filteredApps.addAll(installedApps)
val newApps = mutableListOf<Pair<LauncherActivityInfo, Pair<UserHandle, Int>>>()
newApps.addAll(installedApps)
adapter = AppMenuAdapter(this@AppMenuActivity, newApps, this, this,this, menuMode)
@ -155,21 +156,24 @@ class AppMenuActivity : AppCompatActivity(), AppMenuAdapter.OnItemClickListener,
private fun filterItems(query: String?) {
CoroutineScope(Dispatchers.Default).launch {
val cleanQuery = query?.clean()
filteredApps.clear()
val newFilteredApps = mutableListOf<Pair<LauncherActivityInfo, Pair<UserHandle, Int>>>()
if (cleanQuery.isNullOrEmpty()) {
filteredApps.addAll(installedApps)
newFilteredApps.addAll(installedApps)
} else {
installedApps.forEachIndexed {index, it ->
val cleanItemText = sharedPreferenceManager.getAppName(this@AppMenuActivity, it.first.applicationInfo.packageName, it.second.second, it.first.applicationInfo.loadLabel(packageManager)).toString().clean()
if (cleanItemText.contains(cleanQuery, ignoreCase = true)) {
filteredApps.add(it)
newFilteredApps.add(it)
}
}
}
val changes = detectChanges(filteredApps, newFilteredApps)
withContext(Dispatchers.Main) {
adapter.updateAllApps(filteredApps)
applyChanges(changes, newFilteredApps)
}
filteredApps = newFilteredApps
}
}
@ -198,6 +202,7 @@ class AppMenuActivity : AppCompatActivity(), AppMenuAdapter.OnItemClickListener,
override fun onStop() {
super.onStop()
job.cancel()
//finish()
}
@ -228,16 +233,10 @@ class AppMenuActivity : AppCompatActivity(), AppMenuAdapter.OnItemClickListener,
private fun detectChanges(oldList: List<Pair<LauncherActivityInfo, Pair<UserHandle, Int>>>, newList: List<Pair<LauncherActivityInfo, Pair<UserHandle, Int>>>): List<Change> {
val changes = mutableListOf<Change>()
val removalChanges = mutableListOf<Change>()
val oldSet = oldList.map { Pair(it.first.applicationInfo.packageName, it.second.second) }.toSet()
val newSet = newList.map { Pair(it.first.applicationInfo.packageName, it.second.second) }.toSet()
// Detect removals
oldList.forEachIndexed { index, oldItem ->
if (!newSet.contains(Pair(oldItem.first.applicationInfo.packageName, oldItem.second.second))) {
changes.add(Change(ChangeType.REMOVE, index))
}
}
// Detect insertions
newList.forEachIndexed { index, newItem ->
if (!oldSet.contains(Pair(newItem.first.applicationInfo.packageName, newItem.second.second))) {
@ -245,6 +244,13 @@ class AppMenuActivity : AppCompatActivity(), AppMenuAdapter.OnItemClickListener,
}
}
// Detect removals
oldList.forEachIndexed { index, oldItem ->
if (!newSet.contains(Pair(oldItem.first.applicationInfo.packageName, oldItem.second.second))) {
removalChanges.add(Change(ChangeType.REMOVE, index))
}
}
// Detect updates
oldList.forEachIndexed { index, oldItem ->
if (newSet.contains(Pair(oldItem.first.applicationInfo.packageName, oldItem.second.second))) {
@ -255,6 +261,8 @@ class AppMenuActivity : AppCompatActivity(), AppMenuAdapter.OnItemClickListener,
}
}
changes.addAll(removalChanges.reversed())
return changes
}

View file

@ -137,11 +137,4 @@ class AppMenuAdapter(
fun updateApp(position: Int, app: Pair<LauncherActivityInfo, Pair<UserHandle, Int>>) {
apps[position] = app
}
@SuppressLint("NotifyDataSetChanged")
fun updateAllApps(newApps: List<Pair<LauncherActivityInfo, Pair<UserHandle, Int>>>) {
apps.clear()
apps.addAll(newApps)
notifyDataSetChanged()
}
}

View file

@ -55,6 +55,7 @@
android:drawablePadding="8dp"
android:editTextColor="#f3f3f3"
android:hint="Search..."
android:singleLine="true"
android:textAppearance="@android:style/TextAppearance.DeviceDefault"
android:textSize="25sp" />