definições
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
package com.example.lifegrid.adapters;
|
||||
|
||||
import android.content.Context;
|
||||
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 android.widget.Toast;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.example.lifegrid.R;
|
||||
import com.example.lifegrid.models.Ativos;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
public class AtivosAdapter extends RecyclerView.Adapter<AtivosAdapter.AtivosViewHolder> {
|
||||
|
||||
private List<Ativos> ativosList;
|
||||
private List<String> keysList;
|
||||
private Context context;
|
||||
private OnItemClickListener listener;
|
||||
|
||||
public interface OnItemClickListener {
|
||||
void onDeleteClick(int position, String key);
|
||||
void onRefreshClick(int position, String key, Ativos ativo);
|
||||
}
|
||||
|
||||
public AtivosAdapter(Context context, List<Ativos> ativosList, List<String> keysList, OnItemClickListener listener) {
|
||||
this.context = context;
|
||||
this.ativosList = ativosList;
|
||||
this.keysList = keysList;
|
||||
this.listener = listener;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public AtivosViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
||||
View view = LayoutInflater.from(context).inflate(R.layout.item_ativo, parent, false);
|
||||
return new AtivosViewHolder(view);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(@NonNull AtivosViewHolder holder, int position) {
|
||||
Ativos ativo = ativosList.get(position);
|
||||
String key = keysList.get(position);
|
||||
|
||||
holder.tvNome.setText(ativo.getNome());
|
||||
holder.tvTipo.setText(ativo.getTipo());
|
||||
|
||||
String info = String.format(Locale.getDefault(), "%s unidades x %.2f€", ativo.getQuantidade(), ativo.getPrecoCompra());
|
||||
holder.tvInfo.setText(info);
|
||||
|
||||
double precoAtual = ativo.getPrecoAtual();
|
||||
double precoCompra = ativo.getPrecoCompra();
|
||||
double qtd = 0;
|
||||
try {
|
||||
qtd = Double.parseDouble(ativo.getQuantidade().replace(",", "."));
|
||||
} catch (NumberFormatException e) {
|
||||
qtd = 0;
|
||||
}
|
||||
|
||||
double total = qtd * precoAtual;
|
||||
|
||||
holder.tvPrecoAtual.setText(String.format(Locale.getDefault(), "%.2f€", total));
|
||||
|
||||
double percentagem = 0.0;
|
||||
if (precoCompra > 0) {
|
||||
percentagem = ((precoAtual - precoCompra) / precoCompra) * 100;
|
||||
}
|
||||
|
||||
if (percentagem >= 0) {
|
||||
holder.tvPercentagem.setText(String.format(Locale.getDefault(), "+%.2f%%", percentagem));
|
||||
holder.tvPercentagem.setTextColor(Color.parseColor("#16a34a")); // Green
|
||||
holder.ivTrend.setImageResource(R.drawable.ic_trend_up);
|
||||
holder.ivTrend.setColorFilter(Color.parseColor("#16a34a"));
|
||||
holder.ivTrend.setBackgroundResource(R.drawable.bg_circle_green);
|
||||
} else {
|
||||
holder.tvPercentagem.setText(String.format(Locale.getDefault(), "%.2f%%", percentagem));
|
||||
holder.tvPercentagem.setTextColor(Color.parseColor("#ef4444")); // Red
|
||||
holder.ivTrend.setImageResource(R.drawable.ic_arrow_down); // Fallback if no specific icon
|
||||
holder.ivTrend.setColorFilter(Color.parseColor("#ef4444"));
|
||||
holder.ivTrend.setBackgroundResource(R.drawable.bg_circle_red);
|
||||
}
|
||||
|
||||
holder.btnDelete.setOnClickListener(v -> {
|
||||
if (listener != null) listener.onDeleteClick(position, key);
|
||||
});
|
||||
|
||||
holder.btnRefresh.setOnClickListener(v -> {
|
||||
if (listener != null) listener.onRefreshClick(position, key, ativo);
|
||||
Toast.makeText(context, "Ativo atualizado (Exemplo)", Toast.LENGTH_SHORT).show();
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return ativosList.size();
|
||||
}
|
||||
|
||||
public static class AtivosViewHolder extends RecyclerView.ViewHolder {
|
||||
ImageView ivTrend, btnDelete, btnRefresh;
|
||||
TextView tvNome, tvTipo, tvInfo, tvPrecoAtual, tvPercentagem;
|
||||
|
||||
public AtivosViewHolder(@NonNull View itemView) {
|
||||
super(itemView);
|
||||
ivTrend = itemView.findViewById(R.id.ivTrend);
|
||||
tvNome = itemView.findViewById(R.id.tvNome);
|
||||
tvTipo = itemView.findViewById(R.id.tvTipo);
|
||||
tvInfo = itemView.findViewById(R.id.tvInfo);
|
||||
tvPrecoAtual = itemView.findViewById(R.id.tvPrecoAtual);
|
||||
tvPercentagem = itemView.findViewById(R.id.tvPercentagem);
|
||||
btnDelete = itemView.findViewById(R.id.btnDelete);
|
||||
btnRefresh = itemView.findViewById(R.id.btnRefresh);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user