mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-13 11:19:04 +01:00
Refactor contact lists, their adapters and items
This commit is contained in:
@@ -4,8 +4,6 @@ import android.content.Context;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.CheckBox;
|
||||
import android.widget.TextView;
|
||||
|
||||
import org.briarproject.R;
|
||||
import org.briarproject.android.contact.BaseContactListAdapter;
|
||||
@@ -14,16 +12,12 @@ import org.briarproject.api.contact.ContactId;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
import static android.view.View.GONE;
|
||||
import static android.view.View.VISIBLE;
|
||||
|
||||
class ContactSelectorAdapter
|
||||
extends BaseContactListAdapter<ContactSelectorAdapter.SelectableContactHolder> {
|
||||
class ContactSelectorAdapter extends
|
||||
BaseContactListAdapter<SelectableContactItem, SelectableContactHolder> {
|
||||
|
||||
ContactSelectorAdapter(Context context,
|
||||
OnItemClickListener listener) {
|
||||
|
||||
super(context, listener);
|
||||
OnContactClickListener<SelectableContactItem> listener) {
|
||||
super(context, SelectableContactItem.class, listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -31,66 +25,17 @@ class ContactSelectorAdapter
|
||||
int i) {
|
||||
View v = LayoutInflater.from(ctx).inflate(
|
||||
R.layout.list_item_selectable_contact, viewGroup, false);
|
||||
|
||||
return new SelectableContactHolder(v);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(SelectableContactHolder ui, int position) {
|
||||
super.onBindViewHolder(ui, position);
|
||||
|
||||
SelectableContactListItem item =
|
||||
(SelectableContactListItem) getItemAt(position);
|
||||
if (item == null) return;
|
||||
|
||||
if (item.isSelected()) {
|
||||
ui.checkBox.setChecked(true);
|
||||
} else {
|
||||
ui.checkBox.setChecked(false);
|
||||
}
|
||||
|
||||
if (item.isDisabled()) {
|
||||
// we share this forum already with that contact
|
||||
ui.layout.setEnabled(false);
|
||||
ui.shared.setVisibility(VISIBLE);
|
||||
grayOutItem(ui, true);
|
||||
} else {
|
||||
ui.layout.setEnabled(true);
|
||||
ui.shared.setVisibility(GONE);
|
||||
grayOutItem(ui, false);
|
||||
}
|
||||
}
|
||||
|
||||
Collection<ContactId> getSelectedContactIds() {
|
||||
Collection<ContactId> selected = new ArrayList<>();
|
||||
|
||||
for (int i = 0; i < items.size(); i++) {
|
||||
SelectableContactListItem item =
|
||||
(SelectableContactListItem) items.get(i);
|
||||
SelectableContactItem item = items.get(i);
|
||||
if (item.isSelected()) selected.add(item.getContact().getId());
|
||||
}
|
||||
|
||||
return selected;
|
||||
}
|
||||
|
||||
static class SelectableContactHolder
|
||||
extends BaseContactListAdapter.BaseContactHolder {
|
||||
|
||||
private final CheckBox checkBox;
|
||||
private final TextView shared;
|
||||
|
||||
private SelectableContactHolder(View v) {
|
||||
super(v);
|
||||
|
||||
checkBox = (CheckBox) v.findViewById(R.id.checkBox);
|
||||
shared = (TextView) v.findViewById(R.id.infoView);
|
||||
}
|
||||
}
|
||||
|
||||
private void grayOutItem(SelectableContactHolder ui, boolean gray) {
|
||||
float alpha = gray ? 0.25f : 1f;
|
||||
ui.avatar.setAlpha(alpha);
|
||||
ui.name.setAlpha(alpha);
|
||||
ui.checkBox.setAlpha(alpha);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,16 +15,14 @@ import android.view.ViewGroup;
|
||||
|
||||
import org.briarproject.R;
|
||||
import org.briarproject.android.ActivityComponent;
|
||||
import org.briarproject.android.contact.BaseContactListAdapter;
|
||||
import org.briarproject.android.contact.ContactListItem;
|
||||
import org.briarproject.android.contact.BaseContactListAdapter.OnContactClickListener;
|
||||
import org.briarproject.android.fragment.BaseFragment;
|
||||
import org.briarproject.android.view.BriarRecyclerView;
|
||||
import org.briarproject.api.contact.Contact;
|
||||
import org.briarproject.api.contact.ContactId;
|
||||
import org.briarproject.api.contact.ContactManager;
|
||||
import org.briarproject.api.db.DbException;
|
||||
import org.briarproject.api.identity.IdentityManager;
|
||||
import org.briarproject.api.identity.LocalAuthor;
|
||||
import org.briarproject.api.plugins.ConnectionRegistry;
|
||||
import org.briarproject.api.sync.GroupId;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -41,8 +39,8 @@ import static org.briarproject.android.sharing.ShareActivity.getContactsFromIds;
|
||||
import static org.briarproject.android.sharing.ShareActivity.getContactsFromIntegers;
|
||||
import static org.briarproject.api.sharing.SharingConstants.GROUP_ID;
|
||||
|
||||
public class ContactSelectorFragment extends BaseFragment implements
|
||||
BaseContactListAdapter.OnItemClickListener {
|
||||
public class ContactSelectorFragment extends BaseFragment
|
||||
implements OnContactClickListener<SelectableContactItem> {
|
||||
|
||||
public static final String TAG = ContactSelectorFragment.class.getName();
|
||||
private static final Logger LOG = Logger.getLogger(TAG);
|
||||
@@ -56,7 +54,7 @@ public class ContactSelectorFragment extends BaseFragment implements
|
||||
@Inject
|
||||
volatile ContactManager contactManager;
|
||||
@Inject
|
||||
volatile IdentityManager identityManager;
|
||||
volatile ConnectionRegistry connectionRegistry;
|
||||
|
||||
private volatile GroupId groupId;
|
||||
private volatile ContactSelectorListener listener;
|
||||
@@ -115,7 +113,6 @@ public class ContactSelectorFragment extends BaseFragment implements
|
||||
selectedContacts = getContactsFromIntegers(intContacts);
|
||||
}
|
||||
}
|
||||
|
||||
return contentView;
|
||||
}
|
||||
|
||||
@@ -170,8 +167,8 @@ public class ContactSelectorFragment extends BaseFragment implements
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onItemClick(View view, ContactListItem item) {
|
||||
((SelectableContactListItem) item).toggleSelected();
|
||||
public void onItemClick(View view, SelectableContactItem item) {
|
||||
item.toggleSelected();
|
||||
adapter.notifyItemChanged(adapter.findItemPosition(item), item);
|
||||
|
||||
updateMenuItem();
|
||||
@@ -183,18 +180,19 @@ public class ContactSelectorFragment extends BaseFragment implements
|
||||
public void run() {
|
||||
try {
|
||||
long now = System.currentTimeMillis();
|
||||
List<ContactListItem> contacts = new ArrayList<>();
|
||||
|
||||
List<SelectableContactItem> contacts =
|
||||
new ArrayList<>();
|
||||
for (Contact c : contactManager.getActiveContacts()) {
|
||||
LocalAuthor localAuthor = identityManager
|
||||
.getLocalAuthor(c.getLocalAuthorId());
|
||||
// is this contact online?
|
||||
boolean connected =
|
||||
connectionRegistry.isConnected(c.getId());
|
||||
// was this contact already selected?
|
||||
boolean selected = selection != null &&
|
||||
selection.contains(c.getId());
|
||||
// do we have already some sharing with that contact?
|
||||
boolean disabled = listener.isDisabled(groupId, c);
|
||||
contacts.add(new SelectableContactListItem(c,
|
||||
localAuthor, groupId, selected, disabled));
|
||||
contacts.add(new SelectableContactItem(c, connected,
|
||||
selected, disabled));
|
||||
}
|
||||
long duration = System.currentTimeMillis() - now;
|
||||
if (LOG.isLoggable(INFO))
|
||||
@@ -208,7 +206,8 @@ public class ContactSelectorFragment extends BaseFragment implements
|
||||
});
|
||||
}
|
||||
|
||||
private void displayContacts(final List<ContactListItem> contacts) {
|
||||
private void displayContacts(
|
||||
final List<SelectableContactItem> contacts) {
|
||||
listener.runOnUiThreadUnlessDestroyed(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
package org.briarproject.android.sharing;
|
||||
|
||||
import android.support.annotation.UiThread;
|
||||
import android.view.View;
|
||||
import android.widget.CheckBox;
|
||||
import android.widget.TextView;
|
||||
|
||||
import org.briarproject.R;
|
||||
import org.briarproject.android.contact.BaseContactListAdapter.OnContactClickListener;
|
||||
import org.briarproject.android.contact.ContactItemViewHolder;
|
||||
import org.briarproject.api.nullsafety.NotNullByDefault;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import static android.view.View.GONE;
|
||||
import static android.view.View.VISIBLE;
|
||||
|
||||
@UiThread
|
||||
@NotNullByDefault
|
||||
class SelectableContactHolder
|
||||
extends ContactItemViewHolder<SelectableContactItem> {
|
||||
|
||||
private final CheckBox checkBox;
|
||||
private final TextView shared;
|
||||
|
||||
SelectableContactHolder(View v) {
|
||||
super(v);
|
||||
checkBox = (CheckBox) v.findViewById(R.id.checkBox);
|
||||
shared = (TextView) v.findViewById(R.id.infoView);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void bind(SelectableContactItem item, @Nullable
|
||||
OnContactClickListener<SelectableContactItem> listener) {
|
||||
super.bind(item, listener);
|
||||
|
||||
if (item.isSelected()) {
|
||||
checkBox.setChecked(true);
|
||||
} else {
|
||||
checkBox.setChecked(false);
|
||||
}
|
||||
|
||||
if (item.isDisabled()) {
|
||||
// we share this forum already with that contact
|
||||
layout.setEnabled(false);
|
||||
shared.setVisibility(VISIBLE);
|
||||
grayOutItem(true);
|
||||
} else {
|
||||
layout.setEnabled(true);
|
||||
shared.setVisibility(GONE);
|
||||
grayOutItem(false);
|
||||
}
|
||||
}
|
||||
|
||||
private void grayOutItem(boolean gray) {
|
||||
float alpha = gray ? 0.25f : 1f;
|
||||
avatar.setAlpha(alpha);
|
||||
name.setAlpha(alpha);
|
||||
checkBox.setAlpha(alpha);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package org.briarproject.android.sharing;
|
||||
|
||||
import org.briarproject.android.contact.ContactItem;
|
||||
import org.briarproject.api.contact.Contact;
|
||||
import org.briarproject.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import javax.annotation.concurrent.NotThreadSafe;
|
||||
|
||||
@NotThreadSafe
|
||||
@NotNullByDefault
|
||||
public class SelectableContactItem extends ContactItem {
|
||||
|
||||
private boolean selected, disabled;
|
||||
|
||||
public SelectableContactItem(Contact contact, boolean connected,
|
||||
boolean selected, boolean disabled) {
|
||||
super(contact, connected);
|
||||
this.selected = selected;
|
||||
this.disabled = disabled;
|
||||
}
|
||||
|
||||
public boolean isSelected() {
|
||||
return selected;
|
||||
}
|
||||
|
||||
public void toggleSelected() {
|
||||
selected = !selected;
|
||||
}
|
||||
|
||||
public boolean isDisabled() {
|
||||
return disabled;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
package org.briarproject.android.sharing;
|
||||
|
||||
import android.support.annotation.UiThread;
|
||||
|
||||
import org.briarproject.android.contact.ContactListItem;
|
||||
import org.briarproject.api.clients.MessageTracker.GroupCount;
|
||||
import org.briarproject.api.contact.Contact;
|
||||
import org.briarproject.api.identity.LocalAuthor;
|
||||
import org.briarproject.api.sync.GroupId;
|
||||
|
||||
// This class is NOT thread-safe
|
||||
public class SelectableContactListItem extends ContactListItem {
|
||||
|
||||
private boolean selected, disabled;
|
||||
|
||||
public SelectableContactListItem(Contact contact, LocalAuthor localAuthor,
|
||||
GroupId groupId, boolean selected, boolean disabled) {
|
||||
|
||||
super(contact, localAuthor, false, groupId, new GroupCount(0, 0, 0));
|
||||
|
||||
this.selected = selected;
|
||||
this.disabled = disabled;
|
||||
}
|
||||
|
||||
public void setSelected(boolean selected) {
|
||||
this.selected = selected;
|
||||
}
|
||||
|
||||
public boolean isSelected() {
|
||||
return selected;
|
||||
}
|
||||
|
||||
public void toggleSelected() {
|
||||
selected = !selected;
|
||||
}
|
||||
|
||||
public boolean isDisabled() {
|
||||
return disabled;
|
||||
}
|
||||
}
|
||||
@@ -7,13 +7,11 @@ import android.view.MenuItem;
|
||||
|
||||
import org.briarproject.R;
|
||||
import org.briarproject.android.BriarActivity;
|
||||
import org.briarproject.android.contact.ContactListItem;
|
||||
import org.briarproject.android.contact.ContactItem;
|
||||
import org.briarproject.android.view.BriarRecyclerView;
|
||||
import org.briarproject.api.clients.MessageTracker.GroupCount;
|
||||
import org.briarproject.api.contact.Contact;
|
||||
import org.briarproject.api.db.DbException;
|
||||
import org.briarproject.api.identity.IdentityManager;
|
||||
import org.briarproject.api.identity.LocalAuthor;
|
||||
import org.briarproject.api.plugins.ConnectionRegistry;
|
||||
import org.briarproject.api.sync.GroupId;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -36,7 +34,7 @@ abstract class SharingStatusActivity extends BriarActivity {
|
||||
|
||||
// Fields that are accessed from background threads must be volatile
|
||||
@Inject
|
||||
volatile IdentityManager identityManager;
|
||||
volatile ConnectionRegistry connectionRegistry;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
@@ -109,13 +107,11 @@ abstract class SharingStatusActivity extends BriarActivity {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
List<ContactListItem> contactItems = new ArrayList<>();
|
||||
List<ContactItem> contactItems = new ArrayList<>();
|
||||
for (Contact c : getSharedBy()) {
|
||||
LocalAuthor localAuthor = identityManager
|
||||
.getLocalAuthor(c.getLocalAuthorId());
|
||||
ContactListItem item =
|
||||
new ContactListItem(c, localAuthor, false,
|
||||
groupId, new GroupCount(0, 0, 0));
|
||||
boolean isConnected =
|
||||
connectionRegistry.isConnected(c.getId());
|
||||
ContactItem item = new ContactItem(c, isConnected);
|
||||
contactItems.add(item);
|
||||
}
|
||||
displaySharedBy(contactItems);
|
||||
@@ -127,7 +123,7 @@ abstract class SharingStatusActivity extends BriarActivity {
|
||||
});
|
||||
}
|
||||
|
||||
private void displaySharedBy(final List<ContactListItem> contacts) {
|
||||
private void displaySharedBy(final List<ContactItem> contacts) {
|
||||
runOnUiThreadUnlessDestroyed(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
@@ -142,13 +138,11 @@ abstract class SharingStatusActivity extends BriarActivity {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
List<ContactListItem> contactItems = new ArrayList<>();
|
||||
List<ContactItem> contactItems = new ArrayList<>();
|
||||
for (Contact c : getSharedWith()) {
|
||||
LocalAuthor localAuthor = identityManager
|
||||
.getLocalAuthor(c.getLocalAuthorId());
|
||||
ContactListItem item =
|
||||
new ContactListItem(c, localAuthor, false,
|
||||
groupId, new GroupCount(0, 0, 0));
|
||||
boolean isConnected =
|
||||
connectionRegistry.isConnected(c.getId());
|
||||
ContactItem item = new ContactItem(c, isConnected);
|
||||
contactItems.add(item);
|
||||
}
|
||||
displaySharedWith(contactItems);
|
||||
@@ -160,7 +154,7 @@ abstract class SharingStatusActivity extends BriarActivity {
|
||||
});
|
||||
}
|
||||
|
||||
private void displaySharedWith(final List<ContactListItem> contacts) {
|
||||
private void displaySharedWith(final List<ContactItem> contacts) {
|
||||
runOnUiThreadUnlessDestroyed(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
@@ -7,20 +7,22 @@ import android.view.ViewGroup;
|
||||
|
||||
import org.briarproject.R;
|
||||
import org.briarproject.android.contact.BaseContactListAdapter;
|
||||
import org.briarproject.android.contact.ContactItem;
|
||||
import org.briarproject.android.contact.ContactItemViewHolder;
|
||||
|
||||
class SharingStatusAdapter
|
||||
extends BaseContactListAdapter<BaseContactListAdapter.BaseContactHolder> {
|
||||
class SharingStatusAdapter extends
|
||||
BaseContactListAdapter<ContactItem, ContactItemViewHolder<ContactItem>> {
|
||||
|
||||
SharingStatusAdapter(Context context) {
|
||||
super(context, null);
|
||||
super(context, ContactItem.class, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaseContactHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
|
||||
public ContactItemViewHolder<ContactItem> onCreateViewHolder(
|
||||
ViewGroup viewGroup, int i) {
|
||||
View v = LayoutInflater.from(viewGroup.getContext()).inflate(
|
||||
R.layout.list_item_contact_small, viewGroup, false);
|
||||
|
||||
return new BaseContactHolder(v);
|
||||
return new ContactItemViewHolder<>(v);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user