GitNex/app/src/main/java/org/mian/gitnex/adapters/LabelsAdapter.java
qwerty287 6a84e4d897 Extend CI workflows (#1155)
Extend CI workflows (-> https://codeberg.org/gitnex/GitNex/issues/1099)
	1. pull translations and push them again on every pull
       @mmarif this needs the secrets and `BOT_TOKEN`
       the code for this comes from tea4j-autodeploy
    2. add a **template** for a check workflow which only does some static checks right now. It is disabled because it requires some investigation and discussion on how we should deal with these issues. The checks are currently (if we would enable it):
    	1. check code style and formatting according to the project file in `.idea/`
        2. make sure every Java file has an `@author` annotation

I would wait until you reviewed with enabling them because we have to reformat all the files which results in changes with a few thousand changed lines (tried this once).

Closes https://codeberg.org/gitnex/GitNex/issues/141

Co-authored-by: qwerty287 <ndev@web.de>
Co-authored-by: M M Arif <mmarif@noreply.codeberg.org>
Co-authored-by: 6543 <6543@obermui.de>
Reviewed-on: https://codeberg.org/gitnex/GitNex/pulls/1155
Reviewed-by: 6543 <6543@noreply.codeberg.org>
Co-authored-by: qwerty287 <qwerty287@noreply.codeberg.org>
Co-committed-by: qwerty287 <qwerty287@noreply.codeberg.org>
2022-07-29 14:08:44 +02:00

147 lines
5.5 KiB
Java

package org.mian.gitnex.adapters;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.core.widget.ImageViewCompat;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import org.gitnex.tea4j.v2.models.Label;
import org.mian.gitnex.R;
import org.mian.gitnex.activities.CreateLabelActivity;
import org.mian.gitnex.activities.OrganizationDetailActivity;
import org.mian.gitnex.activities.RepoDetailActivity;
import org.mian.gitnex.helpers.AlertDialogs;
import org.mian.gitnex.helpers.ColorInverter;
import org.mian.gitnex.helpers.contexts.RepositoryContext;
import java.util.List;
/**
* @author M M Arif
*/
public class LabelsAdapter extends RecyclerView.Adapter<LabelsAdapter.LabelsViewHolder> {
private final List<Label> labelsList;
private final String type;
private final String orgName;
class LabelsViewHolder extends RecyclerView.ViewHolder {
private Label labels;
private final CardView labelView;
private final ImageView labelIcon;
private final TextView labelName;
private LabelsViewHolder(View itemView) {
super(itemView);
labelView = itemView.findViewById(R.id.labelView);
labelIcon = itemView.findViewById(R.id.labelIcon);
labelName = itemView.findViewById(R.id.labelName);
ImageView labelsOptionsMenu = itemView.findViewById(R.id.labelsOptionsMenu);
if((type.equals("repo") && !((RepoDetailActivity) itemView.getContext()).repository.getPermissions().isPush()) ||
(type.equals("org") && (((OrganizationDetailActivity) itemView.getContext()).permissions == null || !((OrganizationDetailActivity) itemView.getContext()).permissions.isIsOwner()))) {
labelsOptionsMenu.setVisibility(View.GONE);
}
labelsOptionsMenu.setOnClickListener(v -> {
final Context context = v.getContext();
@SuppressLint("InflateParams")
View view = LayoutInflater.from(context).inflate(R.layout.bottom_sheet_labels_in_list, null);
TextView labelMenuEdit = view.findViewById(R.id.labelMenuEdit);
TextView labelMenuDelete = view.findViewById(R.id.labelMenuDelete);
TextView bottomSheetHeader = view.findViewById(R.id.bottomSheetHeader);
bottomSheetHeader.setText(labels.getName());
BottomSheetDialog dialog = new BottomSheetDialog(context);
dialog.setContentView(view);
dialog.show();
labelMenuEdit.setOnClickListener(editLabel -> {
Intent intent = new Intent(context, CreateLabelActivity.class);
intent.putExtra("labelId", String.valueOf(labels.getId()));
intent.putExtra("labelTitle", labels.getName());
intent.putExtra("labelColor", labels.getColor());
intent.putExtra("labelAction", "edit");
intent.putExtra("type", type);
intent.putExtra("orgName", orgName);
if(type.equals("repo")) {
intent.putExtra(RepositoryContext.INTENT_EXTRA, ((RepoDetailActivity) itemView.getContext()).repository);
}
context.startActivity(intent);
dialog.dismiss();
});
labelMenuDelete.setOnClickListener(deleteLabel -> {
RepositoryContext repo;
if(type.equals("repo")) {
repo = ((RepoDetailActivity) itemView.getContext()).repository;
} else {
repo = null;
}
AlertDialogs.labelDeleteDialog(context, labels.getName(), String.valueOf(labels.getId()),
type, orgName, repo);
dialog.dismiss();
});
});
}
}
public LabelsAdapter(Context ctx, List<Label> labelsMain, String type, String orgName) {
this.labelsList = labelsMain;
this.type = type;
this.orgName = orgName;
}
@NonNull
@Override
public LabelsAdapter.LabelsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_labels, parent, false);
return new LabelsAdapter.LabelsViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull LabelsAdapter.LabelsViewHolder holder, int position) {
Label currentItem = labelsList.get(position);
holder.labels = currentItem;
String labelColor = currentItem.getColor();
String labelName = currentItem.getName();
int color = Color.parseColor("#" + labelColor);
int contrastColor = new ColorInverter().getContrastColor(color);
ImageViewCompat.setImageTintList(holder.labelIcon, ColorStateList.valueOf(contrastColor));
holder.labelName.setTextColor(contrastColor);
holder.labelName.setText(labelName);
holder.labelView.setCardBackgroundColor(color);
}
@Override
public int getItemCount() {
return labelsList.size();
}
}