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