mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-18 13:49:53 +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 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.INFO;
|
||||||
import static java.util.logging.Level.WARNING;
|
import static java.util.logging.Level.WARNING;
|
||||||
|
|
||||||
@@ -91,7 +90,6 @@ public class ContactListActivity extends BriarActivity
|
|||||||
public void onResume() {
|
public void onResume() {
|
||||||
super.onResume();
|
super.onResume();
|
||||||
eventBus.addListener(this);
|
eventBus.addListener(this);
|
||||||
|
|
||||||
loadContacts();
|
loadContacts();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -133,26 +131,9 @@ public class ContactListActivity extends BriarActivity
|
|||||||
private void displayContacts(final List<ContactListItem> contacts) {
|
private void displayContacts(final List<ContactListItem> contacts) {
|
||||||
runOnUiThread(new Runnable() {
|
runOnUiThread(new Runnable() {
|
||||||
public void run() {
|
public void run() {
|
||||||
if (contacts.size() > 0) {
|
adapter.clear();
|
||||||
if (adapter.getItemCount() > 0) {
|
if (contacts.size() == 0) list.showData();
|
||||||
// update existing items rather than just adding them,
|
else adapter.addAll(contacts);
|
||||||
// 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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -217,10 +198,9 @@ public class ContactListActivity extends BriarActivity
|
|||||||
private void removeItem(final ContactId c) {
|
private void removeItem(final ContactId c) {
|
||||||
runOnUiThread(new Runnable() {
|
runOnUiThread(new Runnable() {
|
||||||
public void run() {
|
public void run() {
|
||||||
ContactListItem item = adapter.findItem(c);
|
int position = adapter.findItemPosition(c);
|
||||||
if (item != null) {
|
ContactListItem item = adapter.getItem(position);
|
||||||
adapter.remove(item);
|
if (item != null) adapter.remove(item);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ import static android.support.v7.util.SortedList.INVALID_POSITION;
|
|||||||
public class ContactListAdapter
|
public class ContactListAdapter
|
||||||
extends RecyclerView.Adapter<ContactListAdapter.ContactHolder> {
|
extends RecyclerView.Adapter<ContactListAdapter.ContactHolder> {
|
||||||
|
|
||||||
private SortedList<ContactListItem> contacts =
|
private final SortedList<ContactListItem> contacts =
|
||||||
new SortedList<ContactListItem>(ContactListItem.class,
|
new SortedList<ContactListItem>(ContactListItem.class,
|
||||||
new SortedList.Callback<ContactListItem>() {
|
new SortedList.Callback<ContactListItem>() {
|
||||||
@Override
|
@Override
|
||||||
@@ -141,11 +141,7 @@ public class ContactListAdapter
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getItemCount() {
|
public int getItemCount() {
|
||||||
return contacts == null ? 0 : contacts.size();
|
return contacts.size();
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isEmpty() {
|
|
||||||
return contacts == null || contacts.size() == 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ContactListItem getItem(int position) {
|
public ContactListItem getItem(int position) {
|
||||||
@@ -159,15 +155,6 @@ public class ContactListAdapter
|
|||||||
contacts.updateItemAt(position, item);
|
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) {
|
public int findItemPosition(ContactListItem item) {
|
||||||
return contacts.indexOf(item);
|
return contacts.indexOf(item);
|
||||||
}
|
}
|
||||||
@@ -181,16 +168,16 @@ public class ContactListAdapter
|
|||||||
return INVALID_POSITION; // Not found
|
return INVALID_POSITION; // Not found
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addAll(final List<ContactListItem> contacts) {
|
public void addAll(List<ContactListItem> contacts) {
|
||||||
this.contacts.addAll(contacts);
|
this.contacts.addAll(contacts);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void add(final ContactListItem contact) {
|
public void add(ContactListItem contact) {
|
||||||
this.contacts.add(contact);
|
contacts.add(contact);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void remove(final ContactListItem contact) {
|
public void remove(ContactListItem contact) {
|
||||||
this.contacts.remove(contact);
|
contacts.remove(contact);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void clear() {
|
public void clear() {
|
||||||
|
|||||||
@@ -14,6 +14,8 @@ import org.briarproject.R;
|
|||||||
import org.briarproject.api.messaging.PrivateMessageHeader;
|
import org.briarproject.api.messaging.PrivateMessageHeader;
|
||||||
import org.briarproject.util.StringUtils;
|
import org.briarproject.util.StringUtils;
|
||||||
|
|
||||||
|
import static android.support.v7.util.SortedList.INVALID_POSITION;
|
||||||
|
|
||||||
class ConversationAdapter extends
|
class ConversationAdapter extends
|
||||||
RecyclerView.Adapter<ConversationAdapter.MessageHolder> {
|
RecyclerView.Adapter<ConversationAdapter.MessageHolder> {
|
||||||
|
|
||||||
@@ -21,7 +23,7 @@ class ConversationAdapter extends
|
|||||||
private static final int MSG_IN = 1;
|
private static final int MSG_IN = 1;
|
||||||
private static final int MSG_IN_UNREAD = 2;
|
private static final int MSG_IN_UNREAD = 2;
|
||||||
|
|
||||||
private SortedList<ConversationItem> messages =
|
private final SortedList<ConversationItem> messages =
|
||||||
new SortedList<ConversationItem>(ConversationItem.class,
|
new SortedList<ConversationItem>(ConversationItem.class,
|
||||||
new SortedList.Callback<ConversationItem>() {
|
new SortedList.Callback<ConversationItem>() {
|
||||||
@Override
|
@Override
|
||||||
@@ -145,14 +147,13 @@ class ConversationAdapter extends
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getItemCount() {
|
public int getItemCount() {
|
||||||
return messages == null ? 0 : messages.size();
|
return messages.size();
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isEmpty() {
|
|
||||||
return messages == null || messages.size() == 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ConversationItem getItem(int position) {
|
public ConversationItem getItem(int position) {
|
||||||
|
if (position == INVALID_POSITION || messages.size() <= position) {
|
||||||
|
return null; // Not found
|
||||||
|
}
|
||||||
return messages.get(position);
|
return messages.get(position);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -168,10 +169,6 @@ class ConversationAdapter extends
|
|||||||
this.messages.add(message);
|
this.messages.add(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void remove(final ConversationItem message) {
|
|
||||||
this.messages.remove(message);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void clear() {
|
public void clear() {
|
||||||
this.messages.beginBatchedUpdates();
|
this.messages.beginBatchedUpdates();
|
||||||
|
|
||||||
|
|||||||
@@ -111,7 +111,6 @@ implements EventListener, OnItemClickListener {
|
|||||||
for (ForumContacts f : available)
|
for (ForumContacts f : available)
|
||||||
adapter.add(new AvailableForumsItem(f));
|
adapter.add(new AvailableForumsItem(f));
|
||||||
adapter.sort(AvailableForumsItemComparator.INSTANCE);
|
adapter.sort(AvailableForumsItemComparator.INSTANCE);
|
||||||
adapter.notifyDataSetChanged();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -220,7 +220,6 @@ public class ForumActivity extends BriarActivity implements EventListener,
|
|||||||
// Scroll to the bottom
|
// Scroll to the bottom
|
||||||
list.setSelection(adapter.getCount() - 1);
|
list.setSelection(adapter.getCount() - 1);
|
||||||
}
|
}
|
||||||
adapter.notifyDataSetChanged();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -180,7 +180,6 @@ public class ForumListActivity extends BriarActivity
|
|||||||
available.setVisibility(GONE);
|
available.setVisibility(GONE);
|
||||||
loading.setVisibility(VISIBLE);
|
loading.setVisibility(VISIBLE);
|
||||||
adapter.clear();
|
adapter.clear();
|
||||||
adapter.notifyDataSetChanged();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -197,7 +196,6 @@ public class ForumListActivity extends BriarActivity
|
|||||||
// Add a new item
|
// Add a new item
|
||||||
adapter.add(new ForumListItem(f, headers));
|
adapter.add(new ForumListItem(f, headers));
|
||||||
adapter.sort(ForumListItemComparator.INSTANCE);
|
adapter.sort(ForumListItemComparator.INSTANCE);
|
||||||
adapter.notifyDataSetChanged();
|
|
||||||
selectFirstUnread();
|
selectFirstUnread();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -298,7 +296,6 @@ public class ForumListActivity extends BriarActivity
|
|||||||
ForumListItem item = findForum(g);
|
ForumListItem item = findForum(g);
|
||||||
if (item != null) {
|
if (item != null) {
|
||||||
adapter.remove(item);
|
adapter.remove(item);
|
||||||
adapter.notifyDataSetChanged();
|
|
||||||
if (adapter.isEmpty()) {
|
if (adapter.isEmpty()) {
|
||||||
empty.setVisibility(VISIBLE);
|
empty.setVisibility(VISIBLE);
|
||||||
list.setVisibility(GONE);
|
list.setVisibility(GONE);
|
||||||
|
|||||||
@@ -192,7 +192,6 @@ implements OnItemSelectedListener, OnClickListener {
|
|||||||
for (LocalAuthor a : localAuthors)
|
for (LocalAuthor a : localAuthors)
|
||||||
adapter.add(new LocalAuthorItem(a));
|
adapter.add(new LocalAuthorItem(a));
|
||||||
adapter.sort(LocalAuthorItemComparator.INSTANCE);
|
adapter.sort(LocalAuthorItemComparator.INSTANCE);
|
||||||
adapter.notifyDataSetChanged();
|
|
||||||
int count = adapter.getCount();
|
int count = adapter.getCount();
|
||||||
for (int i = 0; i < count; i++) {
|
for (int i = 0; i < count; i++) {
|
||||||
LocalAuthorItem item = adapter.getItem(i);
|
LocalAuthorItem item = adapter.getItem(i);
|
||||||
|
|||||||
@@ -1,17 +1,5 @@
|
|||||||
package org.briarproject.android.identity;
|
package org.briarproject.android.identity;
|
||||||
|
|
||||||
import static android.text.TextUtils.TruncateAt.END;
|
|
||||||
import static org.briarproject.android.identity.LocalAuthorItem.ANONYMOUS;
|
|
||||||
import static org.briarproject.android.identity.LocalAuthorItem.NEW;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.Comparator;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.briarproject.R;
|
|
||||||
import org.briarproject.android.util.LayoutUtils;
|
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
@@ -19,6 +7,18 @@ import android.widget.BaseAdapter;
|
|||||||
import android.widget.SpinnerAdapter;
|
import android.widget.SpinnerAdapter;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
|
import org.briarproject.R;
|
||||||
|
import org.briarproject.android.util.LayoutUtils;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static android.text.TextUtils.TruncateAt.END;
|
||||||
|
import static org.briarproject.android.identity.LocalAuthorItem.ANONYMOUS;
|
||||||
|
import static org.briarproject.android.identity.LocalAuthorItem.NEW;
|
||||||
|
|
||||||
public class LocalAuthorSpinnerAdapter extends BaseAdapter
|
public class LocalAuthorSpinnerAdapter extends BaseAdapter
|
||||||
implements SpinnerAdapter {
|
implements SpinnerAdapter {
|
||||||
|
|
||||||
@@ -33,10 +33,12 @@ implements SpinnerAdapter {
|
|||||||
|
|
||||||
public void add(LocalAuthorItem item) {
|
public void add(LocalAuthorItem item) {
|
||||||
list.add(item);
|
list.add(item);
|
||||||
|
notifyDataSetChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void clear() {
|
public void clear() {
|
||||||
list.clear();
|
list.clear();
|
||||||
|
notifyDataSetChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getCount() {
|
public int getCount() {
|
||||||
@@ -94,5 +96,6 @@ implements SpinnerAdapter {
|
|||||||
|
|
||||||
public void sort(Comparator<LocalAuthorItem> comparator) {
|
public void sort(Comparator<LocalAuthorItem> comparator) {
|
||||||
Collections.sort(list, comparator);
|
Collections.sort(list, comparator);
|
||||||
|
notifyDataSetChanged();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -66,7 +66,6 @@ implements OnItemSelectedListener, OnClickListener {
|
|||||||
adapter.clear();
|
adapter.clear();
|
||||||
for (LocalAuthor a : authors) adapter.add(new LocalAuthorItem(a));
|
for (LocalAuthor a : authors) adapter.add(new LocalAuthorItem(a));
|
||||||
adapter.sort(LocalAuthorItemComparator.INSTANCE);
|
adapter.sort(LocalAuthorItemComparator.INSTANCE);
|
||||||
adapter.notifyDataSetChanged();
|
|
||||||
// If a local author has been selected, select it again
|
// If a local author has been selected, select it again
|
||||||
AuthorId localAuthorId = container.getLocalAuthorId();
|
AuthorId localAuthorId = container.getLocalAuthorId();
|
||||||
if (localAuthorId == null) return;
|
if (localAuthorId == null) return;
|
||||||
|
|||||||
Reference in New Issue
Block a user