mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-04-26 03:40:56 +01:00
Group member dialog update.
This commit is contained in:
committed by
Greyson Parrelli
parent
d05a71c8fe
commit
28bbfd88b2
@@ -0,0 +1,35 @@
|
||||
package org.thoughtcrime.securesms.groups.ui;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import org.thoughtcrime.securesms.recipients.Recipient;
|
||||
|
||||
public abstract class GroupMemberEntry {
|
||||
|
||||
private @Nullable Runnable onClick;
|
||||
|
||||
private GroupMemberEntry() {
|
||||
}
|
||||
|
||||
public void setOnClick(@NonNull Runnable onClick) {
|
||||
this.onClick = onClick;
|
||||
}
|
||||
|
||||
public @Nullable Runnable getOnClick() {
|
||||
return onClick;
|
||||
}
|
||||
|
||||
public static class FullMember extends GroupMemberEntry {
|
||||
|
||||
private final Recipient member;
|
||||
|
||||
public FullMember(@NonNull Recipient member) {
|
||||
this.member = member;
|
||||
}
|
||||
|
||||
public Recipient getMember() {
|
||||
return member;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
package org.thoughtcrime.securesms.groups.ui;
|
||||
|
||||
import android.content.Context;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import org.thoughtcrime.securesms.R;
|
||||
import org.thoughtcrime.securesms.components.AvatarImageView;
|
||||
import org.thoughtcrime.securesms.recipients.Recipient;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
final class GroupMemberListAdapter extends RecyclerView.Adapter<GroupMemberListAdapter.ViewHolder> {
|
||||
|
||||
private static final int FULL_MEMBER = 0;
|
||||
|
||||
private final ArrayList<GroupMemberEntry> data = new ArrayList<>();
|
||||
|
||||
void updateData(@NonNull Collection<? extends GroupMemberEntry> recipients) {
|
||||
data.clear();
|
||||
data.addAll(recipients);
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
||||
switch (viewType) {
|
||||
case FULL_MEMBER:
|
||||
return new FullMemberViewHolder(LayoutInflater.from(parent.getContext())
|
||||
.inflate(R.layout.group_recipient_list_item,
|
||||
parent, false));
|
||||
default:
|
||||
throw new AssertionError();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
|
||||
holder.bind(data.get(position));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemViewType(int position) {
|
||||
GroupMemberEntry groupMemberEntry = data.get(position);
|
||||
|
||||
if (groupMemberEntry instanceof GroupMemberEntry.FullMember) {
|
||||
return FULL_MEMBER;
|
||||
}
|
||||
|
||||
throw new AssertionError();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return data.size();
|
||||
}
|
||||
|
||||
static abstract class ViewHolder extends RecyclerView.ViewHolder {
|
||||
|
||||
final Context context;
|
||||
private final AvatarImageView avatar;
|
||||
private final TextView recipient;
|
||||
final PopupMenuView popupMenu;
|
||||
|
||||
ViewHolder(@NonNull View itemView) {
|
||||
super(itemView);
|
||||
|
||||
context = itemView.getContext();
|
||||
avatar = itemView.findViewById(R.id.recipient_avatar);
|
||||
recipient = itemView.findViewById(R.id.recipient_name);
|
||||
popupMenu = itemView.findViewById(R.id.popupMenu);
|
||||
}
|
||||
|
||||
void bindRecipient(@NonNull Recipient recipient) {
|
||||
String displayName = recipient.isLocalNumber() ? context.getString(R.string.GroupMembersDialog_you)
|
||||
: recipient.getDisplayName(itemView.getContext());
|
||||
bindImageAndText(recipient, displayName);
|
||||
}
|
||||
|
||||
void bindImageAndText(@NonNull Recipient recipient, @NonNull String displayText) {
|
||||
this.recipient.setText(displayText);
|
||||
this.avatar.setRecipient(recipient);
|
||||
}
|
||||
|
||||
void bind(@NonNull GroupMemberEntry memberEntry) {
|
||||
Runnable onClick = memberEntry.getOnClick();
|
||||
View.OnClickListener onClickListener = v -> { if (onClick != null) onClick.run(); };
|
||||
|
||||
this.avatar.setOnClickListener(onClickListener);
|
||||
this.recipient.setOnClickListener(onClickListener);
|
||||
}
|
||||
}
|
||||
|
||||
final static class FullMemberViewHolder extends ViewHolder {
|
||||
|
||||
FullMemberViewHolder(@NonNull View itemView) {
|
||||
super(itemView);
|
||||
}
|
||||
|
||||
@Override
|
||||
void bind(@NonNull GroupMemberEntry memberEntry) {
|
||||
super.bind(memberEntry);
|
||||
|
||||
GroupMemberEntry.FullMember fullMember = (GroupMemberEntry.FullMember) memberEntry;
|
||||
|
||||
bindRecipient(fullMember.getMember());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package org.thoughtcrime.securesms.groups.ui;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.TypedArray;
|
||||
import android.util.AttributeSet;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import org.thoughtcrime.securesms.R;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
public final class GroupMemberListView extends RecyclerView {
|
||||
|
||||
private final GroupMemberListAdapter membersAdapter = new GroupMemberListAdapter();
|
||||
private int maxHeight;
|
||||
|
||||
public GroupMemberListView(@NonNull Context context) {
|
||||
super(context);
|
||||
initialize(context, null);
|
||||
}
|
||||
|
||||
public GroupMemberListView(@NonNull Context context, @Nullable AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
initialize(context, attrs);
|
||||
}
|
||||
|
||||
public GroupMemberListView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyle) {
|
||||
super(context, attrs, defStyle);
|
||||
initialize(context, attrs);
|
||||
}
|
||||
|
||||
private void initialize(@NonNull Context context, @Nullable AttributeSet attrs) {
|
||||
setHasFixedSize(true);
|
||||
setLayoutManager(new LinearLayoutManager(context));
|
||||
setAdapter(membersAdapter);
|
||||
|
||||
if (attrs != null) {
|
||||
TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.GroupMemberListView, 0, 0);
|
||||
try {
|
||||
maxHeight = typedArray.getDimensionPixelSize(R.styleable.GroupMemberListView_maxHeight, 0);
|
||||
} finally {
|
||||
typedArray.recycle();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void setMembers(@NonNull Collection<? extends GroupMemberEntry> recipients) {
|
||||
membersAdapter.updateData(recipients);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onMeasure(int widthSpec, int heightSpec) {
|
||||
if (maxHeight > 0) {
|
||||
heightSpec = MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.AT_MOST);
|
||||
}
|
||||
super.onMeasure(widthSpec, heightSpec);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package org.thoughtcrime.securesms.groups.ui;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.MenuInflater;
|
||||
import android.view.View;
|
||||
|
||||
import androidx.annotation.IdRes;
|
||||
import androidx.annotation.MenuRes;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.widget.PopupMenu;
|
||||
|
||||
import org.thoughtcrime.securesms.R;
|
||||
|
||||
public final class PopupMenuView extends View {
|
||||
|
||||
private @MenuRes int menu;
|
||||
private @Nullable ItemClick callback;
|
||||
|
||||
public PopupMenuView(Context context) {
|
||||
super(context);
|
||||
init();
|
||||
}
|
||||
|
||||
public PopupMenuView(Context context, @Nullable AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
init();
|
||||
}
|
||||
|
||||
public PopupMenuView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
|
||||
super(context, attrs, defStyleAttr);
|
||||
init();
|
||||
}
|
||||
|
||||
private void init() {
|
||||
setBackgroundResource(R.drawable.ic_more_vert_24);
|
||||
|
||||
setOnClickListener(v -> {
|
||||
if (callback != null) {
|
||||
PopupMenu popup = new PopupMenu(getContext(), v);
|
||||
MenuInflater inflater = popup.getMenuInflater();
|
||||
|
||||
inflater.inflate(menu, popup.getMenu());
|
||||
|
||||
popup.setOnMenuItemClickListener(item -> callback.onItemClick(item.getItemId()));
|
||||
popup.show();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void setMenu(@MenuRes int menu, @NonNull ItemClick callback) {
|
||||
this.menu = menu;
|
||||
this.callback = callback;
|
||||
}
|
||||
|
||||
public interface ItemClick {
|
||||
boolean onItemClick(@IdRes int menuItemId);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user