mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-20 22:59:54 +01:00
Merge branch '200-duplicate-entries-in-contactlist' into 'master'
Fix contacts duplicating itself when pressing back button in conversation view. Normally, this should be handled by the `SortedList` in the `RecyclerView`, but it isn't for some reason. Instead of spending way too much time on finding out why, I am clearing the adapter instead (on each `activity.onResume()`) as it was done before. Closes #200 See merge request !42
This commit is contained in:
@@ -3,6 +3,7 @@ package org.briarproject.android.contact;
|
|||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.support.design.widget.FloatingActionButton;
|
import android.support.design.widget.FloatingActionButton;
|
||||||
|
import android.support.v7.util.SortedList;
|
||||||
import android.support.v7.widget.LinearLayoutManager;
|
import android.support.v7.widget.LinearLayoutManager;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
|
||||||
@@ -134,7 +135,21 @@ public class ContactListActivity extends BriarActivity
|
|||||||
runOnUiThread(new Runnable() {
|
runOnUiThread(new Runnable() {
|
||||||
public void run() {
|
public void run() {
|
||||||
if (contacts.size() > 0) {
|
if (contacts.size() > 0) {
|
||||||
adapter.addAll(contacts);
|
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 == SortedList.INVALID_POSITION) {
|
||||||
|
adapter.add(contact);
|
||||||
|
} else {
|
||||||
|
adapter.updateItem(position, contact);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
adapter.addAll(contacts);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// no contacts to display, make sure progress bar is hidden
|
// no contacts to display, make sure progress bar is hidden
|
||||||
list.showData();
|
list.showData();
|
||||||
|
|||||||
@@ -60,13 +60,25 @@ public class ContactListAdapter
|
|||||||
@Override
|
@Override
|
||||||
public boolean areItemsTheSame(ContactListItem c1,
|
public boolean areItemsTheSame(ContactListItem c1,
|
||||||
ContactListItem c2) {
|
ContactListItem c2) {
|
||||||
return c1.getContact().getId().equals(c2.getContact().getId());
|
return c1.getContact().getId()
|
||||||
|
.equals(c2.getContact().getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean areContentsTheSame(ContactListItem c1,
|
public boolean areContentsTheSame(ContactListItem c1,
|
||||||
ContactListItem c2) {
|
ContactListItem c2) {
|
||||||
return c1.equals(c2);
|
// check for all properties that influence visual
|
||||||
|
// representation of contact
|
||||||
|
if (c1.isConnected() != c2.isConnected()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (c1.getUnreadCount() != c2.getUnreadCount()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (c1.getTimestamp() != c2.getTimestamp()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
private Context ctx;
|
private Context ctx;
|
||||||
@@ -144,7 +156,8 @@ public class ContactListAdapter
|
|||||||
}
|
}
|
||||||
|
|
||||||
public ContactListItem getItem(int position) {
|
public ContactListItem getItem(int position) {
|
||||||
if (position == -1 || contacts.size() <= position) {
|
if (position == SortedList.INVALID_POSITION ||
|
||||||
|
contacts.size() <= position) {
|
||||||
return null; // Not found
|
return null; // Not found
|
||||||
}
|
}
|
||||||
return contacts.get(position);
|
return contacts.get(position);
|
||||||
@@ -163,13 +176,17 @@ public class ContactListAdapter
|
|||||||
return null; // Not found
|
return null; // Not found
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int findItemPosition(ContactListItem item) {
|
||||||
|
return contacts.indexOf(item);
|
||||||
|
}
|
||||||
|
|
||||||
public int findItemPosition(ContactId c) {
|
public int findItemPosition(ContactId c) {
|
||||||
int count = getItemCount();
|
int count = getItemCount();
|
||||||
for (int i = 0; i < count; i++) {
|
for (int i = 0; i < count; i++) {
|
||||||
ContactListItem item = getItem(i);
|
ContactListItem item = getItem(i);
|
||||||
if (item.getContact().getId().equals(c)) return i;
|
if (item.getContact().getId().equals(c)) return i;
|
||||||
}
|
}
|
||||||
return -1; // Not found
|
return SortedList.INVALID_POSITION; // Not found
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addAll(final List<ContactListItem> contacts) {
|
public void addAll(final List<ContactListItem> contacts) {
|
||||||
|
|||||||
Reference in New Issue
Block a user