fix rare concurrent modification in muc user search

This commit is contained in:
Daniel Gultsch 2023-10-31 10:43:53 +01:00
parent 4e5d65b183
commit fba7721cd5
No known key found for this signature in database
GPG key ID: F43D18AD2A0982C2

View file

@ -13,8 +13,13 @@ import android.view.inputmethod.InputMethodManager;
import android.widget.EditText; import android.widget.EditText;
import android.widget.Toast; import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil; import androidx.databinding.DataBindingUtil;
import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;
import com.google.common.collect.Ordering;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Locale; import java.util.Locale;
@ -56,30 +61,38 @@ public class MucUsersActivity extends XmppActivity implements XmppConnectionServ
private void loadAndSubmitUsers() { private void loadAndSubmitUsers() {
if (mConversation != null) { if (mConversation != null) {
allUsers = mConversation.getMucOptions().getUsers(); allUsers = mConversation.getMucOptions().getUsers();
Collections.sort(allUsers);
submitFilteredList(mSearchEditText != null ? mSearchEditText.getText().toString() : null); submitFilteredList(mSearchEditText != null ? mSearchEditText.getText().toString() : null);
} }
} }
private void submitFilteredList(String search) { private void submitFilteredList(final String search) {
if (TextUtils.isEmpty(search)) { if (TextUtils.isEmpty(search)) {
userAdapter.submitList(allUsers); userAdapter.submitList(Ordering.natural().immutableSortedCopy(allUsers));
} else { } else {
final String needle = search.toLowerCase(Locale.getDefault()); final String needle = search.toLowerCase(Locale.getDefault());
ArrayList<MucOptions.User> filtered = new ArrayList<>(); userAdapter.submitList(
for(MucOptions.User user : allUsers) { Ordering.natural()
.immutableSortedCopy(
Collections2.filter(
this.allUsers,
user -> {
final String name = user.getName(); final String name = user.getName();
final Contact contact = user.getContact(); final Contact contact = user.getContact();
if (name != null && name.toLowerCase(Locale.getDefault()).contains(needle) || contact != null && contact.getDisplayName().toLowerCase(Locale.getDefault()).contains(needle)) { return name != null
filtered.add(user); && name.toLowerCase(
} Locale.getDefault())
} .contains(needle)
userAdapter.submitList(filtered); || contact != null
&& contact.getDisplayName()
.toLowerCase(
Locale.getDefault())
.contains(needle);
})));
} }
} }
@Override @Override
public boolean onContextItemSelected(MenuItem item) { public boolean onContextItemSelected(@NonNull MenuItem item) {
if (!MucDetailsContextMenuHelper.onContextItemSelected(item, userAdapter.getSelectedUser(), this)) { if (!MucDetailsContextMenuHelper.onContextItemSelected(item, userAdapter.getSelectedUser(), this)) {
return super.onContextItemSelected(item); return super.onContextItemSelected(item);
} }