From 1089d838db015c765066c202b330bc5346697e89 Mon Sep 17 00:00:00 2001 From: Naveen Date: Tue, 22 Aug 2023 18:04:08 +0530 Subject: [PATCH] Avoid modifying items while list is being updated `performFiltering` is always called on a worker thread. UI should only be updated in `publishResults`. --- .../adapters/AutoCompleteTextViewAdapter.kt | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/AutoCompleteTextViewAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/AutoCompleteTextViewAdapter.kt index c4562f46..8538d7b4 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/AutoCompleteTextViewAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/AutoCompleteTextViewAdapter.kt @@ -51,24 +51,27 @@ class AutoCompleteTextViewAdapter(val activity: SimpleActivity, val contacts: Ar override fun performFiltering(constraint: CharSequence?): FilterResults { val filterResults = FilterResults() if (constraint != null) { - resultList.clear() + val results = mutableListOf() val searchString = constraint.toString().normalizeString() contacts.forEach { if (it.doesContainPhoneNumber(searchString) || it.name.contains(searchString, true)) { - resultList.add(it) + results.add(it) } } - resultList.sortWith(compareBy { !it.name.startsWith(searchString, true) }) + results.sortWith(compareBy { !it.name.startsWith(searchString, true) }) - filterResults.values = resultList - filterResults.count = resultList.size + filterResults.values = results + filterResults.count = results.size } return filterResults } override fun publishResults(constraint: CharSequence?, results: FilterResults?) { - if ((results?.count ?: -1) > 0) { + if (results != null && results.count > 0) { + resultList.clear() + @Suppress("UNCHECKED_CAST") + resultList.addAll(results.values as List) notifyDataSetChanged() } else { notifyDataSetInvalidated()