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-05-13 03:19:17 +02:00
|
|
|
import org.mercury_im.messenger.R;
|
2019-12-06 15:52:50 +01:00
|
|
|
import org.mercury_im.messenger.entity.contact.Peer;
|
2019-12-21 16:47:41 +01:00
|
|
|
import org.mercury_im.messenger.ui.avatar.AvatarDrawable;
|
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-12-06 15:52:50 +01:00
|
|
|
extends AbstractRecyclerViewAdapter<Peer, 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-12-06 15:52:50 +01:00
|
|
|
Peer peer = getItemAt(position);
|
|
|
|
holder.bind(peer);
|
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-12-06 15:52:50 +01:00
|
|
|
void bind(Peer contact) {
|
2019-12-21 16:47:41 +01:00
|
|
|
String name = contact.getDisplayName();
|
2019-12-06 15:52:50 +01:00
|
|
|
String address = contact.getAddress();
|
2019-12-21 16:47:41 +01:00
|
|
|
nameView.setText(name);
|
2019-12-06 15:52:50 +01:00
|
|
|
jidView.setText(address);
|
2019-12-21 16:47:41 +01:00
|
|
|
avatarView.setImageDrawable(new AvatarDrawable(name, address));
|
2019-09-02 01:06:17 +02:00
|
|
|
view.setOnClickListener(view -> {
|
|
|
|
|
|
|
|
Intent intent = new Intent(context, ChatActivity.class);
|
2019-12-06 15:52:50 +01:00
|
|
|
intent.putExtra(ChatActivity.EXTRA_JID, address);
|
2019-12-21 16:30:14 +01:00
|
|
|
intent.putExtra(ChatActivity.EXTRA_ACCOUNT, contact.getAccount().getId().toString());
|
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-12-06 15:52:50 +01:00
|
|
|
private static class ContactDiffCallback extends AbstractDiffCallback<Peer> {
|
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-12-06 15:52:50 +01:00
|
|
|
public boolean areItemsTheSame(Peer oldItem, Peer newItem) {
|
2019-09-02 01:06:17 +02:00
|
|
|
return oldItem.getId() == newItem.getId();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-12-06 15:52:50 +01:00
|
|
|
public boolean areContentsTheSame(Peer oldItem, Peer newItem) {
|
2019-09-02 01:06:17 +02:00
|
|
|
return areItemsTheSame(oldItem, newItem) &&
|
2019-12-06 15:52:50 +01:00
|
|
|
Objects.equals(oldItem.getName(), newItem.getName());
|
2019-09-02 01:06:17 +02:00
|
|
|
}
|
|
|
|
}
|
2019-04-14 18:12:04 +02:00
|
|
|
}
|