2019-09-23 23:20:43 +02:00
|
|
|
package org.mercury_im.messenger.ui.roster.contacts;
|
2019-04-14 18:12:04 +02:00
|
|
|
|
2019-06-21 03:45:33 +02:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
2019-04-14 18:12:04 +02:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
2019-09-25 22:50:52 +02:00
|
|
|
import android.widget.ImageView;
|
2019-04-14 18:12:04 +02:00
|
|
|
import android.widget.TextView;
|
|
|
|
|
2019-06-29 19:06:07 +02:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
|
2019-06-21 03:45:33 +02:00
|
|
|
import org.jxmpp.jid.EntityBareJid;
|
2019-05-13 03:19:17 +02:00
|
|
|
import org.mercury_im.messenger.R;
|
2019-11-03 06:33:52 +01:00
|
|
|
import org.mercury_im.messenger.xmpp.model.ContactModel;
|
2019-06-21 03:45:33 +02:00
|
|
|
import org.mercury_im.messenger.ui.chat.ChatActivity;
|
2019-09-02 01:06:17 +02:00
|
|
|
import org.mercury_im.messenger.ui.util.AbstractRecyclerViewAdapter;
|
2019-06-30 13:37:36 +02:00
|
|
|
import org.mercury_im.messenger.util.ColorUtil;
|
2019-04-14 18:12:04 +02:00
|
|
|
|
2019-09-02 01:06:17 +02:00
|
|
|
import java.util.Objects;
|
2019-04-14 18:12:04 +02:00
|
|
|
|
2019-10-28 14:56:56 +01:00
|
|
|
import butterknife.BindView;
|
|
|
|
import butterknife.ButterKnife;
|
|
|
|
|
2019-09-23 23:20:43 +02:00
|
|
|
public class ContactListRecyclerViewAdapter
|
2019-10-28 14:56:56 +01:00
|
|
|
extends AbstractRecyclerViewAdapter<ContactModel, ContactListRecyclerViewAdapter.RosterItemViewHolder> {
|
2019-04-14 18:12:04 +02:00
|
|
|
|
2019-09-23 23:20:43 +02:00
|
|
|
public ContactListRecyclerViewAdapter() {
|
2019-09-02 01:06:17 +02:00
|
|
|
super(new ContactDiffCallback());
|
2019-04-14 18:12:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@NonNull
|
|
|
|
@Override
|
2019-06-20 17:20:23 +02:00
|
|
|
public RosterItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
2019-06-21 03:45:33 +02:00
|
|
|
return new RosterItemViewHolder(parent.getContext(), LayoutInflater.from(parent.getContext())
|
2019-09-23 23:20:43 +02:00
|
|
|
.inflate(R.layout.list_item_contact, parent, false));
|
2019-04-14 18:12:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-06-20 17:20:23 +02:00
|
|
|
public void onBindViewHolder(@NonNull RosterItemViewHolder holder, int position) {
|
2019-10-28 14:56:56 +01:00
|
|
|
ContactModel model = getModelAt(position);
|
2019-06-20 17:20:23 +02:00
|
|
|
holder.bind(model);
|
2019-04-14 18:12:04 +02:00
|
|
|
}
|
|
|
|
|
2019-06-20 17:20:23 +02:00
|
|
|
public class RosterItemViewHolder extends RecyclerView.ViewHolder {
|
2019-06-03 01:16:49 +02:00
|
|
|
|
2019-06-20 17:20:23 +02:00
|
|
|
private View view;
|
2019-06-03 01:16:49 +02:00
|
|
|
|
2019-06-24 01:41:17 +02:00
|
|
|
@BindView(R.id.roster_entry__jid)
|
2019-06-03 01:16:49 +02:00
|
|
|
TextView jidView;
|
2019-04-14 18:12:04 +02:00
|
|
|
|
2019-09-01 02:55:19 +02:00
|
|
|
@BindView(R.id.roster_entry__name)
|
|
|
|
TextView nameView;
|
2019-04-14 18:12:04 +02:00
|
|
|
|
2019-06-24 01:41:17 +02:00
|
|
|
@BindView(R.id.roster_entry__avatar)
|
2019-09-25 22:50:52 +02:00
|
|
|
ImageView avatarView;
|
2019-06-24 01:41:17 +02:00
|
|
|
|
2019-06-21 03:45:33 +02:00
|
|
|
Context context;
|
|
|
|
|
|
|
|
public RosterItemViewHolder(Context context, View itemView) {
|
2019-04-14 18:12:04 +02:00
|
|
|
super(itemView);
|
2019-06-21 03:45:33 +02:00
|
|
|
this.context = context;
|
2019-06-20 17:20:23 +02:00
|
|
|
this.view = itemView;
|
2019-06-24 01:41:17 +02:00
|
|
|
ButterKnife.bind(this, view);
|
2019-06-03 01:16:49 +02:00
|
|
|
}
|
|
|
|
|
2019-10-28 14:56:56 +01:00
|
|
|
void bind(ContactModel contactModel) {
|
|
|
|
String name = contactModel.getRostername();
|
2019-09-01 02:55:19 +02:00
|
|
|
nameView.setText(name != null ? name : contactModel.getEntity().getJid().getLocalpart().asUnescapedString());
|
2019-06-21 03:45:33 +02:00
|
|
|
EntityBareJid jid = contactModel.getEntity().getJid();
|
|
|
|
jidView.setText(jid.toString());
|
2019-09-25 22:50:52 +02:00
|
|
|
avatarView.setColorFilter(ColorUtil.consistentColor(jid.toString()));
|
2019-09-02 01:06:17 +02:00
|
|
|
view.setOnClickListener(view -> {
|
|
|
|
|
|
|
|
Intent intent = new Intent(context, ChatActivity.class);
|
2019-09-30 02:28:22 +02:00
|
|
|
intent.putExtra(ChatActivity.EXTRA_JID, jid.toString());
|
2019-10-28 14:56:56 +01:00
|
|
|
intent.putExtra(ChatActivity.EXTRA_ACCOUNT, contactModel.getEntity().getAccount().getId());
|
2019-09-02 01:06:17 +02:00
|
|
|
|
|
|
|
context.startActivity(intent);
|
2019-06-21 03:45:33 +02:00
|
|
|
});
|
2019-04-14 18:12:04 +02:00
|
|
|
}
|
|
|
|
}
|
2019-09-02 01:06:17 +02:00
|
|
|
|
2019-10-28 14:56:56 +01:00
|
|
|
private static class ContactDiffCallback extends AbstractDiffCallback<ContactModel> {
|
2019-09-02 01:06:17 +02:00
|
|
|
|
2019-09-13 09:17:59 +02:00
|
|
|
ContactDiffCallback() {
|
2019-09-02 01:06:17 +02:00
|
|
|
super(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-10-28 14:56:56 +01:00
|
|
|
public boolean areItemsTheSame(ContactModel oldItem, ContactModel newItem) {
|
2019-09-02 01:06:17 +02:00
|
|
|
return oldItem.getId() == newItem.getId();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-10-28 14:56:56 +01:00
|
|
|
public boolean areContentsTheSame(ContactModel oldItem, ContactModel newItem) {
|
2019-09-02 01:06:17 +02:00
|
|
|
return areItemsTheSame(oldItem, newItem) &&
|
2019-10-28 14:56:56 +01:00
|
|
|
Objects.equals(oldItem.getRostername(), newItem.getRostername());
|
2019-09-02 01:06:17 +02:00
|
|
|
}
|
|
|
|
}
|
2019-04-14 18:12:04 +02:00
|
|
|
}
|