mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-15 12:19:54 +01:00
Update contact list when contacts are deleted. #227
Also removed unnecessary adapter notifications in various places.
This commit is contained in:
@@ -36,7 +36,6 @@ import java.util.logging.Logger;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static android.support.v7.util.SortedList.INVALID_POSITION;
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
|
||||
@@ -91,7 +90,6 @@ public class ContactListActivity extends BriarActivity
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
eventBus.addListener(this);
|
||||
|
||||
loadContacts();
|
||||
}
|
||||
|
||||
@@ -133,26 +131,9 @@ public class ContactListActivity extends BriarActivity
|
||||
private void displayContacts(final List<ContactListItem> contacts) {
|
||||
runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
if (contacts.size() > 0) {
|
||||
if (adapter.getItemCount() > 0) {
|
||||
// update existing items rather than just adding them,
|
||||
// because different timestamps in added items change
|
||||
// sorting criteria and cause duplicates
|
||||
for (ContactListItem contact : contacts) {
|
||||
int position = adapter.findItemPosition(contact);
|
||||
if (position == INVALID_POSITION) {
|
||||
adapter.add(contact);
|
||||
} else {
|
||||
adapter.updateItem(position, contact);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
adapter.addAll(contacts);
|
||||
}
|
||||
} else {
|
||||
// no contacts to display, make sure progress bar is hidden
|
||||
list.showData();
|
||||
}
|
||||
adapter.clear();
|
||||
if (contacts.size() == 0) list.showData();
|
||||
else adapter.addAll(contacts);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -217,10 +198,9 @@ public class ContactListActivity extends BriarActivity
|
||||
private void removeItem(final ContactId c) {
|
||||
runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
ContactListItem item = adapter.findItem(c);
|
||||
if (item != null) {
|
||||
adapter.remove(item);
|
||||
}
|
||||
int position = adapter.findItemPosition(c);
|
||||
ContactListItem item = adapter.getItem(position);
|
||||
if (item != null) adapter.remove(item);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ import static android.support.v7.util.SortedList.INVALID_POSITION;
|
||||
public class ContactListAdapter
|
||||
extends RecyclerView.Adapter<ContactListAdapter.ContactHolder> {
|
||||
|
||||
private SortedList<ContactListItem> contacts =
|
||||
private final SortedList<ContactListItem> contacts =
|
||||
new SortedList<ContactListItem>(ContactListItem.class,
|
||||
new SortedList.Callback<ContactListItem>() {
|
||||
@Override
|
||||
@@ -141,11 +141,7 @@ public class ContactListAdapter
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return contacts == null ? 0 : contacts.size();
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return contacts == null || contacts.size() == 0;
|
||||
return contacts.size();
|
||||
}
|
||||
|
||||
public ContactListItem getItem(int position) {
|
||||
@@ -159,15 +155,6 @@ public class ContactListAdapter
|
||||
contacts.updateItemAt(position, item);
|
||||
}
|
||||
|
||||
public ContactListItem findItem(ContactId c) {
|
||||
int count = getItemCount();
|
||||
for (int i = 0; i < count; i++) {
|
||||
ContactListItem item = getItem(i);
|
||||
if (item.getContact().getId().equals(c)) return item;
|
||||
}
|
||||
return null; // Not found
|
||||
}
|
||||
|
||||
public int findItemPosition(ContactListItem item) {
|
||||
return contacts.indexOf(item);
|
||||
}
|
||||
@@ -181,16 +168,16 @@ public class ContactListAdapter
|
||||
return INVALID_POSITION; // Not found
|
||||
}
|
||||
|
||||
public void addAll(final List<ContactListItem> contacts) {
|
||||
public void addAll(List<ContactListItem> contacts) {
|
||||
this.contacts.addAll(contacts);
|
||||
}
|
||||
|
||||
public void add(final ContactListItem contact) {
|
||||
this.contacts.add(contact);
|
||||
public void add(ContactListItem contact) {
|
||||
contacts.add(contact);
|
||||
}
|
||||
|
||||
public void remove(final ContactListItem contact) {
|
||||
this.contacts.remove(contact);
|
||||
public void remove(ContactListItem contact) {
|
||||
contacts.remove(contact);
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
|
||||
@@ -14,6 +14,8 @@ import org.briarproject.R;
|
||||
import org.briarproject.api.messaging.PrivateMessageHeader;
|
||||
import org.briarproject.util.StringUtils;
|
||||
|
||||
import static android.support.v7.util.SortedList.INVALID_POSITION;
|
||||
|
||||
class ConversationAdapter extends
|
||||
RecyclerView.Adapter<ConversationAdapter.MessageHolder> {
|
||||
|
||||
@@ -21,7 +23,7 @@ class ConversationAdapter extends
|
||||
private static final int MSG_IN = 1;
|
||||
private static final int MSG_IN_UNREAD = 2;
|
||||
|
||||
private SortedList<ConversationItem> messages =
|
||||
private final SortedList<ConversationItem> messages =
|
||||
new SortedList<ConversationItem>(ConversationItem.class,
|
||||
new SortedList.Callback<ConversationItem>() {
|
||||
@Override
|
||||
@@ -145,14 +147,13 @@ class ConversationAdapter extends
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return messages == null ? 0 : messages.size();
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return messages == null || messages.size() == 0;
|
||||
return messages.size();
|
||||
}
|
||||
|
||||
public ConversationItem getItem(int position) {
|
||||
if (position == INVALID_POSITION || messages.size() <= position) {
|
||||
return null; // Not found
|
||||
}
|
||||
return messages.get(position);
|
||||
}
|
||||
|
||||
@@ -168,10 +169,6 @@ class ConversationAdapter extends
|
||||
this.messages.add(message);
|
||||
}
|
||||
|
||||
public void remove(final ConversationItem message) {
|
||||
this.messages.remove(message);
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
this.messages.beginBatchedUpdates();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user