design
This commit is contained in:
@@ -3,31 +3,158 @@ package com.example.lifegrid.menu;
|
||||
import android.os.Bundle;
|
||||
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.example.lifegrid.R;
|
||||
import com.example.lifegrid.models.Transacao;
|
||||
import com.google.firebase.auth.FirebaseAuth;
|
||||
import com.google.firebase.auth.FirebaseUser;
|
||||
import com.google.firebase.database.DataSnapshot;
|
||||
import com.google.firebase.database.DatabaseError;
|
||||
import com.google.firebase.database.DatabaseReference;
|
||||
import com.google.firebase.database.FirebaseDatabase;
|
||||
import com.google.firebase.database.ValueEventListener;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
public class HomeFragment extends Fragment {
|
||||
|
||||
private TextView tvValor;
|
||||
private TextView tvTransacoes;
|
||||
private TextView tvValor2;
|
||||
private TextView tvTransacoes2;
|
||||
private TextView tvValor3;
|
||||
private TextView tvTransacoes3;
|
||||
private TextView tvValor4;
|
||||
private TextView tvTransacoes4;
|
||||
private TextView tvTitulo;
|
||||
private TextView tvTitulo2;
|
||||
|
||||
public HomeFragment() {
|
||||
// Required empty public constructor
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
}
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
// Inflate the layout for this fragment
|
||||
View root = inflater.inflate(R.layout.fragment_home, container, false);
|
||||
|
||||
tvValor = root.findViewById(R.id.tvValor);
|
||||
tvTransacoes = root.findViewById(R.id.tvTransacoes);
|
||||
tvValor2 = root.findViewById(R.id.tvValor2);
|
||||
tvTransacoes2 = root.findViewById(R.id.tvTransacoes2);
|
||||
tvValor3 = root.findViewById(R.id.tvValor3);
|
||||
tvTransacoes3 = root.findViewById(R.id.tvTransacoes3);
|
||||
tvValor4 = root.findViewById(R.id.tvValor4);
|
||||
tvTransacoes4 = root.findViewById(R.id.tvTransacoes4);
|
||||
tvTitulo = root.findViewById(R.id.tvTitulo);
|
||||
tvTitulo2 = root.findViewById(R.id.tvTitulo2);
|
||||
|
||||
// Se precisarmos que o fragmento carregue dados logo ao iniciar,
|
||||
// a atividade vai chamar este método quando o fragmento estiver pronto.
|
||||
|
||||
root.findViewById(R.id.receitasCardView).setOnClickListener(v -> {
|
||||
Fragment transacoesFragment = new TransacoesFragment();
|
||||
requireActivity().getSupportFragmentManager().beginTransaction()
|
||||
.replace(R.id.fragmentContainerView, transacoesFragment)
|
||||
.commit();
|
||||
});
|
||||
|
||||
root.findViewById(R.id.despesasCardView).setOnClickListener(v -> {
|
||||
Fragment transacoesFragment = new TransacoesFragment();
|
||||
requireActivity().getSupportFragmentManager().beginTransaction()
|
||||
.replace(R.id.fragmentContainerView, transacoesFragment)
|
||||
.commit();
|
||||
});
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
if (getActivity() instanceof com.example.lifegrid.TelaInicialActivity) {
|
||||
((com.example.lifegrid.TelaInicialActivity) getActivity()).atualizarDadosHome();
|
||||
}
|
||||
}
|
||||
|
||||
public void carregarDados(int mesSelecionado, String anoSelecionado, String mesNome) {
|
||||
if (tvTitulo == null) return;
|
||||
|
||||
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
|
||||
if (user == null) return;
|
||||
|
||||
tvTitulo.setText("Receitas (" + mesNome + " " + anoSelecionado + ")");
|
||||
tvTitulo2.setText("Despesas (" + mesNome + " " + anoSelecionado + ")");
|
||||
|
||||
DatabaseReference transacoesRef = FirebaseDatabase.getInstance().getReference()
|
||||
.child("users").child(user.getUid()).child("transacoes");
|
||||
|
||||
transacoesRef.addValueEventListener(new ValueEventListener() {
|
||||
@Override
|
||||
public void onDataChange(@NonNull DataSnapshot snapshot) {
|
||||
if (!isAdded()) return;
|
||||
|
||||
double totalReceitas = 0;
|
||||
double totalDespesas = 0;
|
||||
int countReceitas = 0;
|
||||
int countDespesas = 0;
|
||||
|
||||
for (DataSnapshot ds : snapshot.getChildren()) {
|
||||
Transacao transacao = ds.getValue(Transacao.class);
|
||||
if (transacao != null && transacao.getData() != null) {
|
||||
String[] parts = transacao.getData().split("/");
|
||||
if (parts.length == 3) {
|
||||
try {
|
||||
int mesTransacao = Integer.parseInt(parts[1]);
|
||||
String anoTransacao = parts[2];
|
||||
|
||||
if (mesTransacao == mesSelecionado && anoTransacao.equals(anoSelecionado)) {
|
||||
double valor = Double.parseDouble(transacao.getValor().replace(",", "."));
|
||||
if ("Receita".equals(transacao.getTipo())) {
|
||||
totalReceitas += valor;
|
||||
countReceitas++;
|
||||
} else if ("Despesa".equals(transacao.getTipo())) {
|
||||
totalDespesas += valor;
|
||||
countDespesas++;
|
||||
}
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tvValor.setText(String.format(Locale.getDefault(), "%.2f€", totalReceitas));
|
||||
tvTransacoes.setText(countReceitas + " transações");
|
||||
|
||||
tvValor2.setText(String.format(Locale.getDefault(), "%.2f€", totalDespesas));
|
||||
tvTransacoes2.setText(countDespesas + " transações");
|
||||
|
||||
double saldo = totalReceitas - totalDespesas;
|
||||
tvValor3.setText(String.format(Locale.getDefault(), "%.2f€", saldo));
|
||||
if (saldo >= 0) {
|
||||
tvTransacoes3.setText("Poupança positiva");
|
||||
tvTransacoes3.setTextColor(android.graphics.Color.parseColor("#8E8E8E"));
|
||||
} else {
|
||||
tvTransacoes3.setText("Poupança negativa");
|
||||
tvTransacoes3.setTextColor(android.graphics.Color.parseColor("#FF0000"));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCancelled(@NonNull DatabaseError error) {
|
||||
if (isAdded()) {
|
||||
Toast.makeText(getContext(), "Erro ao carregar dados", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user