Untitled

 avatar
unknown
plain_text
a year ago
5.7 kB
2
Indexable
package com.dinesys.analytics.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.caverock.androidsvg.SVG;
import com.caverock.androidsvg.SVGImageView;
import com.caverock.androidsvg.SVGParseException;
import com.dinesys.analytics.R; // Asegúrate de importar el paquete correcto para R
import com.dinesys.analytics.model.Reporte;
import com.dinesys.analytics.model.Seccion;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class HomeAdapter extends ArrayAdapter<Reporte> {
    private List<Reporte> list;
    private Context context;
    private int layout;

    public HomeAdapter(@NonNull Context context, int resource, @NonNull List<Reporte> objects) {
        super(context, resource, objects);
        this.list = objects;
        this.context = context;
        this.layout = resource;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(layout, null);
        }
        final Reporte row = list.get(position);
        List<Seccion> sections = row.getSecciones();

        // CARD:
        TextView cardTitle = view.findViewById(R.id.txtTitle);
        SVGImageView cardIcon = view.findViewById(R.id.imgIcon);

        // Sección 1:
        SVGImageView sec1Icon = view.findViewById(R.id.imgS2Icon);
        TextView sec1Value = view.findViewById(R.id.txtS2Value);
        TextView sec1Description = view.findViewById(R.id.txtS2Description);

        // Sección 2:
        SVGImageView sec2Icon = view.findViewById(R.id.imgS1Icon);
        TextView sec2Value = view.findViewById(R.id.txtS1Value);
        SVGImageView sec2Arrow = view.findViewById(R.id.imgS2Arrow);
        TextView sec2Description = view.findViewById(R.id.txtS1Description);

        // Asignaciones
        // Card
        cardTitle.setText(row.getTitulo());

        // Cargar y mostrar el icono SVG en el SVGImageView
        String iconName = row.getIcono();
        if (iconName != null && !iconName.isEmpty()) {
            // Reemplazar guiones "-" por guiones bajos "_"
            iconName = iconName.replace("-", "_");
            try {
                // Cargar el archivo SVG desde res/raw
                int resourceId = context.getResources().getIdentifier(iconName, "raw", context.getPackageName());
                if (resourceId != 0) {
                    InputStream inputStream = context.getResources().openRawResource(resourceId);
                    SVG svg = SVG.getFromInputStream(inputStream);
                    cardIcon.setSVG(svg);
                }
            } catch (SVGParseException e) {
                e.printStackTrace();
            }
        }

        // Sección 1
        for (Seccion seccion : sections) {
            if ("venta".equalsIgnoreCase(seccion.getSeccion())) {
                sec1Value.setText(seccion.getValor());
                sec1Description.setText(seccion.getDescripcion());
                // Cargar y mostrar el icono SVG de la sección 1
                String sec1IconName = seccion.getIcono();
                if (sec1IconName != null && !sec1IconName.isEmpty()) {
                    // Reemplazar guiones "-" por guiones bajos "_"
                    sec1IconName = sec1IconName.replace("-", "_");
                    int sec1ResourceId = context.getResources().getIdentifier(sec1IconName, "raw", context.getPackageName());
                    if (sec1ResourceId != 0) {
                        try {
                            InputStream sec1InputStream = context.getResources().openRawResource(sec1ResourceId);
                            SVG sec1SVG = SVG.getFromInputStream(sec1InputStream);
                            sec1Icon.setSVG(sec1SVG);
                        } catch (SVGParseException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }

        // Sección 2
        for (Seccion seccion : sections) {
            if ("inventario".equalsIgnoreCase(seccion.getSeccion())) {
                sec2Value.setText(seccion.getValor());
                sec2Description.setText(seccion.getDescripcion());
                // Cargar y mostrar el icono SVG de la sección 2
                String sec2IconName = seccion.getIcono();
                if (sec2IconName != null && !sec2IconName.isEmpty()) {
                    // Reemplazar guiones "-" por guiones bajos "_"
                    sec2IconName = sec2IconName.replace("-", "_");
                    int sec2ResourceId = context.getResources().getIdentifier(sec2IconName, "raw", context.getPackageName());
                    if (sec2ResourceId != 0) {
                        try {
                            InputStream sec2InputStream = context.getResources().openRawResource(sec2ResourceId);
                            SVG sec2SVG = SVG.getFromInputStream(sec2InputStream);
                            sec2Icon.setSVG(sec2SVG);
                        } catch (SVGParseException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }

        return view;
    }
}