2014-01-18 18:17:08 -08:00
|
|
|
package org.thoughtcrime.securesms.util;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2015-11-05 10:41:43 -08:00
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.support.annotation.Nullable;
|
2014-01-18 18:17:08 -08:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
2015-11-05 10:41:43 -08:00
|
|
|
import android.widget.BaseAdapter;
|
2014-01-18 18:17:08 -08:00
|
|
|
import android.widget.ImageButton;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.R;
|
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
2015-11-05 10:41:43 -08:00
|
|
|
import org.whispersystems.libaxolotl.util.guava.Optional;
|
|
|
|
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
public class SelectedRecipientsAdapter extends BaseAdapter {
|
|
|
|
@NonNull private Context context;
|
|
|
|
@Nullable private OnRecipientDeletedListener onRecipientDeletedListener;
|
|
|
|
@NonNull private List<RecipientWrapper> recipients;
|
|
|
|
|
|
|
|
public SelectedRecipientsAdapter(@NonNull Context context) {
|
|
|
|
this(context, Collections.<Recipient>emptyList());
|
|
|
|
}
|
2014-01-18 18:17:08 -08:00
|
|
|
|
2015-11-05 10:41:43 -08:00
|
|
|
public SelectedRecipientsAdapter(@NonNull Context context,
|
|
|
|
@NonNull Collection<Recipient> existingRecipients)
|
|
|
|
{
|
|
|
|
this.context = context;
|
|
|
|
this.recipients = wrapExistingMembers(existingRecipients);
|
|
|
|
}
|
2014-01-18 18:17:08 -08:00
|
|
|
|
2015-11-05 10:41:43 -08:00
|
|
|
public void add(@NonNull Recipient recipient, boolean isPush) {
|
|
|
|
if (!find(recipient).isPresent()) {
|
|
|
|
RecipientWrapper wrapper = new RecipientWrapper(recipient, true, isPush);
|
|
|
|
this.recipients.add(0, wrapper);
|
|
|
|
notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
}
|
2014-01-18 18:17:08 -08:00
|
|
|
|
2015-11-05 10:41:43 -08:00
|
|
|
public Optional<RecipientWrapper> find(@NonNull Recipient recipient) {
|
|
|
|
RecipientWrapper found = null;
|
|
|
|
for (RecipientWrapper wrapper : recipients) {
|
|
|
|
if (wrapper.getRecipient().equals(recipient)) found = wrapper;
|
|
|
|
}
|
|
|
|
return Optional.fromNullable(found);
|
|
|
|
}
|
2014-01-18 18:17:08 -08:00
|
|
|
|
2015-11-05 10:41:43 -08:00
|
|
|
public void remove(@NonNull Recipient recipient) {
|
|
|
|
Optional<RecipientWrapper> match = find(recipient);
|
|
|
|
if (match.isPresent()) {
|
|
|
|
recipients.remove(match.get());
|
|
|
|
notifyDataSetChanged();
|
|
|
|
}
|
2014-01-18 18:17:08 -08:00
|
|
|
}
|
|
|
|
|
2015-11-05 10:41:43 -08:00
|
|
|
public Set<Recipient> getRecipients() {
|
|
|
|
final Set<Recipient> recipientSet = new HashSet<>(recipients.size());
|
|
|
|
for (RecipientWrapper wrapper : recipients) {
|
|
|
|
recipientSet.add(wrapper.getRecipient());
|
|
|
|
}
|
|
|
|
return recipientSet;
|
2014-01-18 18:17:08 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-11-05 10:41:43 -08:00
|
|
|
public int getCount() {
|
|
|
|
return recipients.size();
|
|
|
|
}
|
2014-01-18 18:17:08 -08:00
|
|
|
|
2015-11-05 10:41:43 -08:00
|
|
|
public boolean hasNonPushMembers() {
|
|
|
|
for (RecipientWrapper wrapper : recipients) {
|
|
|
|
if (!wrapper.isPush()) return true;
|
2014-01-18 18:17:08 -08:00
|
|
|
}
|
2015-11-05 10:41:43 -08:00
|
|
|
return false;
|
|
|
|
}
|
2014-01-18 18:17:08 -08:00
|
|
|
|
2015-11-05 10:41:43 -08:00
|
|
|
@Override
|
|
|
|
public Object getItem(int position) {
|
|
|
|
return recipients.get(position);
|
|
|
|
}
|
2014-01-18 18:17:08 -08:00
|
|
|
|
2015-11-05 10:41:43 -08:00
|
|
|
@Override
|
|
|
|
public long getItemId(int position) {
|
|
|
|
return position;
|
|
|
|
}
|
2014-01-18 18:17:08 -08:00
|
|
|
|
2015-11-05 10:41:43 -08:00
|
|
|
@Override
|
|
|
|
public View getView(final int position, View v, final ViewGroup parent) {
|
|
|
|
if (v == null) {
|
|
|
|
v = LayoutInflater.from(context).inflate(R.layout.selected_recipient_list_item, parent, false);
|
|
|
|
}
|
2014-01-18 18:17:08 -08:00
|
|
|
|
2015-11-05 10:41:43 -08:00
|
|
|
final RecipientWrapper rw = (RecipientWrapper)getItem(position);
|
|
|
|
final Recipient p = rw.getRecipient();
|
|
|
|
final boolean modifiable = rw.isModifiable();
|
|
|
|
|
|
|
|
TextView name = (TextView) v.findViewById(R.id.name);
|
|
|
|
TextView phone = (TextView) v.findViewById(R.id.phone);
|
|
|
|
ImageButton delete = (ImageButton) v.findViewById(R.id.delete);
|
|
|
|
|
|
|
|
name.setText(p.getName());
|
|
|
|
phone.setText(p.getNumber());
|
|
|
|
delete.setVisibility(modifiable ? View.VISIBLE : View.GONE);
|
|
|
|
delete.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View view) {
|
|
|
|
if (onRecipientDeletedListener != null) {
|
|
|
|
onRecipientDeletedListener.onRecipientDeleted(recipients.get(position).getRecipient());
|
2014-02-20 15:41:52 -08:00
|
|
|
}
|
2014-01-18 18:17:08 -08:00
|
|
|
}
|
2015-11-05 10:41:43 -08:00
|
|
|
});
|
2014-01-18 18:17:08 -08:00
|
|
|
|
|
|
|
return v;
|
2014-02-12 17:17:33 -08:00
|
|
|
}
|
|
|
|
|
2015-11-05 10:41:43 -08:00
|
|
|
private static List<RecipientWrapper> wrapExistingMembers(Collection<Recipient> recipients) {
|
|
|
|
final LinkedList<RecipientWrapper> wrapperList = new LinkedList<>();
|
|
|
|
for (Recipient recipient : recipients) {
|
|
|
|
wrapperList.add(new RecipientWrapper(recipient, false, true));
|
|
|
|
}
|
|
|
|
return wrapperList;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setOnRecipientDeletedListener(@Nullable OnRecipientDeletedListener listener) {
|
2014-02-12 17:17:33 -08:00
|
|
|
onRecipientDeletedListener = listener;
|
|
|
|
}
|
2014-01-18 18:17:08 -08:00
|
|
|
|
2014-02-12 17:17:33 -08:00
|
|
|
public interface OnRecipientDeletedListener {
|
2015-11-05 10:41:43 -08:00
|
|
|
void onRecipientDeleted(Recipient recipient);
|
2014-01-18 18:17:08 -08:00
|
|
|
}
|
2014-02-20 15:41:52 -08:00
|
|
|
|
|
|
|
public static class RecipientWrapper {
|
|
|
|
private final Recipient recipient;
|
2015-11-05 10:41:43 -08:00
|
|
|
private final boolean modifiable;
|
|
|
|
private final boolean push;
|
|
|
|
|
|
|
|
public RecipientWrapper(final @NonNull Recipient recipient,
|
|
|
|
final boolean modifiable,
|
|
|
|
final boolean push)
|
|
|
|
{
|
|
|
|
this.recipient = recipient;
|
2014-02-20 15:41:52 -08:00
|
|
|
this.modifiable = modifiable;
|
2015-11-05 10:41:43 -08:00
|
|
|
this.push = push;
|
2014-02-20 15:41:52 -08:00
|
|
|
}
|
|
|
|
|
2015-11-05 10:41:43 -08:00
|
|
|
public @NonNull Recipient getRecipient() {
|
2014-02-20 15:41:52 -08:00
|
|
|
return recipient;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isModifiable() {
|
|
|
|
return modifiable;
|
|
|
|
}
|
2015-11-05 10:41:43 -08:00
|
|
|
|
|
|
|
public boolean isPush() {
|
|
|
|
return push;
|
|
|
|
}
|
2014-02-20 15:41:52 -08:00
|
|
|
}
|
2014-01-18 18:17:08 -08:00
|
|
|
}
|