CreateGoGrabProTabBackendAdapter.java 7.25 KB
package etelligens.com.foodsafety.adapter;

import android.annotation.SuppressLint;
import android.content.Context;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import etelligens.com.foodsafety.R;
import etelligens.com.foodsafety.model.AddGoGrabLineModal;
import etelligens.com.foodsafety.model.CreateGoGrabTabModal;
import etelligens.com.foodsafety.model.GrabAndGoSubLabelsModel;

public class CreateGoGrabProTabBackendAdapter extends RecyclerView.Adapter<CreateGoGrabProTabBackendAdapter.GoGrabViewHolder> {
    Context context;
    private ArrayList<CreateGoGrabTabModal> goGrabTabModals;
    private ArrayList<GrabAndGoSubLabelsModel> grabAndGoSubLabelsModels;
    AddGoGrabLineBackendAdapter addGoGrabLineBackendAdapter;
    OnItemClick onItemClick;
    String nameItem;
    int idItem;
    //GoGrabLineAdapter goGrabLineAdapter;
    ArrayList<AddGoGrabLineModal> goGrabLineModals;

    public interface OnItemClick {
        void sendData(String id);

        void nameItem(CreateGoGrabTabModal createGoGrabTabModal, int count, int id, int pId, String name, String value, String per, int isLs,int position);
    }

    public CreateGoGrabProTabBackendAdapter(Context context, ArrayList<CreateGoGrabTabModal> list, OnItemClick onItemClick) {
        this.context = context;
        this.goGrabTabModals = list;
        this.onItemClick = onItemClick;
    }

    View view;

    @NonNull
    @Override
    public CreateGoGrabProTabBackendAdapter.GoGrabViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
        view = LayoutInflater.from(context).inflate(R.layout.item_layout_go_grab_pro_backend, parent, false);
        return new GoGrabViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final CreateGoGrabProTabBackendAdapter.GoGrabViewHolder holder, @SuppressLint("RecyclerView") int position) {
        final CreateGoGrabTabModal modal = goGrabTabModals.get(position);
        grabAndGoSubLabelsModels = (ArrayList<GrabAndGoSubLabelsModel>) modal.getSub_labels();
        String labelName = modal.getName();
        final int pId = modal.getId();
        Log.e("isHead===: ", modal.getIs_head() + "");

        if (modal.getIs_head() == 1) {
            holder.dailyValue_Layout.setVisibility(View.VISIBLE);
            holder.addlinetxt.setVisibility(View.GONE);

        } else if (modal.getIs_head() == 0) {
            holder.labelnameedittxt.setText(labelName);
            holder.addlinetxt.setVisibility(View.VISIBLE);
            holder.dailyValue_Layout.setVisibility(View.GONE);
        }
        if (labelName.equalsIgnoreCase(context.getString(R.string.calories))) {
            holder.addlinetxt.setVisibility(View.GONE);
        }else if (labelName.equalsIgnoreCase(context.getString(R.string.sodium))){
            holder.addlinetxt.setVisibility(View.GONE);
        }else {
            //do nothing...
        }

        if (labelName.equals(R.string.daily_value)) {
            holder.label_name_dailyvalue.setText("%" + labelName);
        } else if (modal.getIs_head() == 1) {
            holder.label_name_dailyvalue.setText("");
        }

        if (position == goGrabTabModals.size() - 1) {
            holder.addbuttontxt.setVisibility(View.VISIBLE);
        }

        holder.addbuttontxt.setOnClickListener(v -> onItemClick.sendData(String.valueOf(pId)));

        holder.addlineRecyclerview.setHasFixedSize(true);
        LinearLayoutManager layoutManager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
        holder.addlineRecyclerview.setLayoutManager(layoutManager);

        addGoGrabLineBackendAdapter = new AddGoGrabLineBackendAdapter(context, grabAndGoSubLabelsModels, new AddGoGrabLineBackendAdapter.PassData() {
            @Override
            public void sendData() {

            }

            @Override
            public void nameItem(GrabAndGoSubLabelsModel grabAndGoSubLabelsModel, int count) {

                int id = grabAndGoSubLabelsModel.getId();
                int pId = grabAndGoSubLabelsModel.getParent_id();
                String name = grabAndGoSubLabelsModel.getName();
                String value = grabAndGoSubLabelsModel.getValue();
                String per = grabAndGoSubLabelsModel.getPercent();
                int isLs = grabAndGoSubLabelsModel.getIs_list();

                onItemClick.nameItem(modal, count, id, pId, name, value, per, isLs,position);
            }
        });

        holder.addlineRecyclerview.setAdapter(addGoGrabLineBackendAdapter);

        addGoGrabLineBackendAdapter.notifyDataSetChanged();

        holder.addlinetxt.setOnClickListener(view -> {
            int id = grabAndGoSubLabelsModels.size() + 2;
            String name = "";
            addLine(id, name, pId, position);
        });
    }

    private void addLine(int id, String name, int pId, int pos) {

        System.out.println("checkkkk");
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("id", id);
            jsonObject.put("name", name);
            jsonObject.put("parent_id", pId);
            jsonObject.put("is_list", 0);
            Gson gson = new Gson();
            GrabAndGoSubLabelsModel addGoGrabLineModal = gson.fromJson(String.valueOf(jsonObject), GrabAndGoSubLabelsModel.class);
            goGrabTabModals.get(pos).getSub_labels().add(addGoGrabLineModal);
            notifyDataSetChanged();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getItemCount() {
        return goGrabTabModals.size();
    }

    public class GoGrabViewHolder extends RecyclerView.ViewHolder {
        RecyclerView addlineRecyclerview;
        TextView addlinetxt, addbuttontxt, label_name_dailyvalue;
        EditText labelnameedittxt, enteredittxt, enterlabelnametxt;
        Button savetxt;
        RelativeLayout rldialogboxlayout;
        LinearLayout dailyValue_Layout, ll_lable;

        public GoGrabViewHolder(View itemview) {
            super(itemview);
            addlineRecyclerview = itemview.findViewById(R.id.add_line_recyclerview);
            addlinetxt = itemview.findViewById(R.id.add_new_tatxt);
            addlinetxt.setText(R.string.add_line_txt);
            labelnameedittxt = itemview.findViewById(R.id.label_name_edittxt);
            addbuttontxt = itemview.findViewById(R.id.add_button_txt);
            //enteredittxt = itemview.findViewById(R.id.enter_parent_id_txt);
            savetxt = itemview.findViewById(R.id.savebutton);
            enterlabelnametxt = itemview.findViewById(R.id.enter_label_name_txt);
            rldialogboxlayout = itemview.findViewById(R.id.rl_dialog_boxlayout);
            label_name_dailyvalue = itemview.findViewById(R.id.label_name_dailyvalue);
            dailyValue_Layout = itemview.findViewById(R.id.dailyValue_Layout);
            ll_lable = itemview.findViewById(R.id.ll_lable);
        }
    }
}