2019-03-31 15:12:01 +00:00
|
|
|
package eu.siacs.conversations.ui.adapter;
|
|
|
|
|
2019-09-21 08:51:05 +00:00
|
|
|
import android.app.Activity;
|
2021-01-18 17:26:46 +00:00
|
|
|
import androidx.databinding.DataBindingUtil;
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.recyclerview.widget.ListAdapter;
|
|
|
|
import androidx.recyclerview.widget.DiffUtil;
|
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
2019-03-31 15:12:01 +00:00
|
|
|
import android.text.TextUtils;
|
2019-09-21 08:51:05 +00:00
|
|
|
import android.view.ContextMenu;
|
2019-03-31 15:12:01 +00:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
|
2019-05-02 18:09:43 +00:00
|
|
|
import java.util.Locale;
|
|
|
|
|
2019-03-31 15:12:01 +00:00
|
|
|
import eu.siacs.conversations.R;
|
|
|
|
import eu.siacs.conversations.databinding.SearchResultItemBinding;
|
2019-11-02 08:43:37 +00:00
|
|
|
import eu.siacs.conversations.entities.Room;
|
2019-09-21 08:51:05 +00:00
|
|
|
import eu.siacs.conversations.ui.XmppActivity;
|
2019-03-31 15:12:01 +00:00
|
|
|
import eu.siacs.conversations.ui.util.AvatarWorkerTask;
|
2020-05-15 15:06:16 +00:00
|
|
|
import eu.siacs.conversations.xmpp.Jid;
|
2019-03-31 15:12:01 +00:00
|
|
|
|
2019-11-02 08:43:37 +00:00
|
|
|
public class ChannelSearchResultAdapter extends ListAdapter<Room, ChannelSearchResultAdapter.ViewHolder> implements View.OnCreateContextMenuListener {
|
2019-03-31 15:12:01 +00:00
|
|
|
|
2019-11-02 08:43:37 +00:00
|
|
|
private static final DiffUtil.ItemCallback<Room> DIFF = new DiffUtil.ItemCallback<Room>() {
|
2019-03-31 15:12:01 +00:00
|
|
|
@Override
|
2019-11-02 08:43:37 +00:00
|
|
|
public boolean areItemsTheSame(@NonNull Room a, @NonNull Room b) {
|
2019-04-24 11:25:54 +00:00
|
|
|
return a.address != null && a.address.equals(b.address);
|
2019-03-31 15:12:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-11-02 08:43:37 +00:00
|
|
|
public boolean areContentsTheSame(@NonNull Room a, @NonNull Room b) {
|
2019-03-31 15:12:01 +00:00
|
|
|
return a.equals(b);
|
|
|
|
}
|
|
|
|
};
|
2019-09-21 08:51:05 +00:00
|
|
|
private OnChannelSearchResultSelected listener;
|
2019-11-02 08:43:37 +00:00
|
|
|
private Room current;
|
2019-03-31 15:12:01 +00:00
|
|
|
|
|
|
|
public ChannelSearchResultAdapter() {
|
|
|
|
super(DIFF);
|
|
|
|
}
|
|
|
|
|
|
|
|
@NonNull
|
|
|
|
@Override
|
|
|
|
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
|
2019-05-02 18:09:43 +00:00
|
|
|
return new ViewHolder(DataBindingUtil.inflate(LayoutInflater.from(viewGroup.getContext()), R.layout.search_result_item, viewGroup, false));
|
2019-03-31 15:12:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int position) {
|
2019-11-02 08:43:37 +00:00
|
|
|
final Room searchResult = getItem(position);
|
2019-03-31 15:12:01 +00:00
|
|
|
viewHolder.binding.name.setText(searchResult.getName());
|
|
|
|
final String description = searchResult.getDescription();
|
2019-05-02 18:09:43 +00:00
|
|
|
final String language = searchResult.getLanguage();
|
2019-03-31 15:12:01 +00:00
|
|
|
if (TextUtils.isEmpty(description)) {
|
|
|
|
viewHolder.binding.description.setVisibility(View.GONE);
|
|
|
|
} else {
|
|
|
|
viewHolder.binding.description.setText(description);
|
|
|
|
viewHolder.binding.description.setVisibility(View.VISIBLE);
|
|
|
|
}
|
2019-05-02 18:09:43 +00:00
|
|
|
if (language == null || language.length() != 2) {
|
|
|
|
viewHolder.binding.language.setVisibility(View.GONE);
|
|
|
|
} else {
|
|
|
|
viewHolder.binding.language.setText(language.toUpperCase(Locale.ENGLISH));
|
|
|
|
viewHolder.binding.language.setVisibility(View.VISIBLE);
|
|
|
|
}
|
2019-09-16 12:20:15 +00:00
|
|
|
final Jid room = searchResult.getRoom();
|
|
|
|
viewHolder.binding.room.setText(room != null ? room.asBareJid().toString() : "");
|
2019-03-31 15:12:01 +00:00
|
|
|
AvatarWorkerTask.loadAvatar(searchResult, viewHolder.binding.avatar, R.dimen.avatar);
|
2019-09-21 08:51:05 +00:00
|
|
|
final View root = viewHolder.binding.getRoot();
|
|
|
|
root.setTag(searchResult);
|
|
|
|
root.setOnClickListener(v -> listener.onChannelSearchResult(searchResult));
|
|
|
|
root.setOnCreateContextMenuListener(this);
|
2019-03-31 15:12:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setOnChannelSearchResultSelectedListener(OnChannelSearchResultSelected listener) {
|
|
|
|
this.listener = listener;
|
|
|
|
}
|
|
|
|
|
2019-11-02 08:43:37 +00:00
|
|
|
public Room getCurrent() {
|
2019-09-21 08:51:05 +00:00
|
|
|
return this.current;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
|
|
|
|
final Activity activity = XmppActivity.find(v);
|
|
|
|
final Object tag = v.getTag();
|
2019-11-02 08:43:37 +00:00
|
|
|
if (activity != null && tag instanceof Room) {
|
2019-09-21 08:51:05 +00:00
|
|
|
activity.getMenuInflater().inflate(R.menu.channel_item_context, menu);
|
2019-11-02 08:43:37 +00:00
|
|
|
this.current = (Room) tag;
|
2019-09-21 08:51:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public interface OnChannelSearchResultSelected {
|
2019-11-02 08:43:37 +00:00
|
|
|
void onChannelSearchResult(Room result);
|
2019-09-21 08:51:05 +00:00
|
|
|
}
|
2019-03-31 15:12:01 +00:00
|
|
|
|
|
|
|
public static class ViewHolder extends RecyclerView.ViewHolder {
|
|
|
|
|
|
|
|
private final SearchResultItemBinding binding;
|
|
|
|
|
|
|
|
private ViewHolder(SearchResultItemBinding binding) {
|
|
|
|
super(binding.getRoot());
|
|
|
|
this.binding = binding;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|