2018-04-26 17:03:54 -07:00
|
|
|
package org.thoughtcrime.securesms.contactshare;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.support.annotation.Nullable;
|
|
|
|
import android.support.v7.widget.LinearLayoutManager;
|
|
|
|
import android.support.v7.widget.RecyclerView;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
import com.bumptech.glide.load.engine.DiskCacheStrategy;
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.R;
|
|
|
|
import org.thoughtcrime.securesms.components.AvatarImageView;
|
|
|
|
import org.thoughtcrime.securesms.mms.GlideRequests;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Locale;
|
|
|
|
|
2018-05-10 11:31:38 -07:00
|
|
|
import static org.thoughtcrime.securesms.contactshare.Contact.*;
|
|
|
|
|
2018-04-26 17:03:54 -07:00
|
|
|
public class ContactShareEditAdapter extends RecyclerView.Adapter<ContactShareEditAdapter.ContactEditViewHolder> {
|
|
|
|
|
|
|
|
private final GlideRequests glideRequests;
|
2018-05-10 11:31:38 -07:00
|
|
|
private final Locale locale;
|
|
|
|
private final EventListener eventListener;
|
2018-04-26 17:03:54 -07:00
|
|
|
private final List<Contact> contacts;
|
|
|
|
|
2018-05-10 11:31:38 -07:00
|
|
|
ContactShareEditAdapter(@NonNull GlideRequests glideRequests, @NonNull Locale locale, @NonNull EventListener eventListener) {
|
2018-04-26 17:03:54 -07:00
|
|
|
this.glideRequests = glideRequests;
|
2018-05-10 11:31:38 -07:00
|
|
|
this.locale = locale;
|
|
|
|
this.eventListener = eventListener;
|
2018-04-26 17:03:54 -07:00
|
|
|
this.contacts = new ArrayList<>();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ContactEditViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
2018-07-03 17:16:50 -07:00
|
|
|
return new ContactEditViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_editable_contact, parent, false),
|
|
|
|
locale,
|
|
|
|
glideRequests);
|
2018-04-26 17:03:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBindViewHolder(ContactEditViewHolder holder, int position) {
|
2018-07-03 17:16:50 -07:00
|
|
|
holder.bind(position, contacts.get(position), eventListener);
|
2018-04-26 17:03:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getItemCount() {
|
|
|
|
return contacts.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
void setContacts(@Nullable List<Contact> contacts) {
|
|
|
|
this.contacts.clear();
|
|
|
|
|
|
|
|
if (contacts != null) {
|
|
|
|
this.contacts.addAll(contacts);
|
|
|
|
}
|
|
|
|
|
|
|
|
notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
static class ContactEditViewHolder extends RecyclerView.ViewHolder {
|
|
|
|
|
|
|
|
private final TextView name;
|
2018-05-10 11:31:38 -07:00
|
|
|
private final View nameEditButton;
|
2018-04-26 17:03:54 -07:00
|
|
|
private final ContactFieldAdapter fieldAdapter;
|
|
|
|
|
2018-07-03 17:16:50 -07:00
|
|
|
ContactEditViewHolder(View itemView, @NonNull Locale locale, @NonNull GlideRequests glideRequests) {
|
2018-04-26 17:03:54 -07:00
|
|
|
super(itemView);
|
|
|
|
|
2018-05-10 11:31:38 -07:00
|
|
|
this.name = itemView.findViewById(R.id.editable_contact_name);
|
|
|
|
this.nameEditButton = itemView.findViewById(R.id.editable_contact_name_edit_button);
|
2018-07-03 17:16:50 -07:00
|
|
|
this.fieldAdapter = new ContactFieldAdapter(locale, glideRequests, true);
|
2018-04-26 17:03:54 -07:00
|
|
|
|
|
|
|
RecyclerView fields = itemView.findViewById(R.id.editable_contact_fields);
|
|
|
|
fields.setLayoutManager(new LinearLayoutManager(itemView.getContext()));
|
|
|
|
fields.getLayoutManager().setAutoMeasureEnabled(true);
|
|
|
|
fields.setAdapter(fieldAdapter);
|
|
|
|
}
|
|
|
|
|
2018-07-03 17:16:50 -07:00
|
|
|
void bind(int position, @NonNull Contact contact, @NonNull EventListener eventListener) {
|
2018-04-26 17:03:54 -07:00
|
|
|
Context context = itemView.getContext();
|
|
|
|
|
|
|
|
name.setText(ContactUtil.getDisplayName(contact));
|
2018-05-10 11:31:38 -07:00
|
|
|
nameEditButton.setOnClickListener(v -> eventListener.onNameEditClicked(position, contact.getName()));
|
2018-07-03 17:16:50 -07:00
|
|
|
fieldAdapter.setFields(context, contact.getAvatar(), contact.getPhoneNumbers(), contact.getEmails(), contact.getPostalAddresses());
|
2018-04-26 17:03:54 -07:00
|
|
|
}
|
|
|
|
}
|
2018-05-10 11:31:38 -07:00
|
|
|
|
|
|
|
interface EventListener {
|
|
|
|
void onNameEditClicked(int position, @NonNull Name name);
|
|
|
|
}
|
2018-04-26 17:03:54 -07:00
|
|
|
}
|