a acabar
This commit is contained in:
@@ -13,6 +13,7 @@ import android.widget.Toast;
|
||||
|
||||
import com.example.lifegrid.R;
|
||||
import com.example.lifegrid.models.Transacao;
|
||||
import com.example.lifegrid.models.Ativos;
|
||||
import com.google.firebase.auth.FirebaseAuth;
|
||||
import com.google.firebase.auth.FirebaseUser;
|
||||
import com.google.firebase.database.DataSnapshot;
|
||||
@@ -73,9 +74,74 @@ public class HomeFragment extends Fragment {
|
||||
.commit();
|
||||
});
|
||||
|
||||
root.findViewById(R.id.ativosCardView).setOnClickListener(v -> {
|
||||
Fragment ativosFragment = new AtivosFragment();
|
||||
requireActivity().getSupportFragmentManager().beginTransaction()
|
||||
.replace(R.id.fragmentContainerView, ativosFragment)
|
||||
.commit();
|
||||
});
|
||||
|
||||
carregarDadosAtivos();
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
private void carregarDadosAtivos() {
|
||||
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
|
||||
if (user == null) return;
|
||||
|
||||
DatabaseReference ativosRef = FirebaseDatabase.getInstance().getReference()
|
||||
.child("users").child(user.getUid()).child("ativos");
|
||||
|
||||
ativosRef.addValueEventListener(new ValueEventListener() {
|
||||
@Override
|
||||
public void onDataChange(@NonNull DataSnapshot snapshot) {
|
||||
if (!isAdded()) return;
|
||||
|
||||
double totalInvestido = 0;
|
||||
double valorTotalPortfolio = 0;
|
||||
|
||||
for (DataSnapshot ds : snapshot.getChildren()) {
|
||||
Ativos ativo = ds.getValue(Ativos.class);
|
||||
if (ativo != null) {
|
||||
double qtd = 0;
|
||||
try {
|
||||
qtd = Double.parseDouble(ativo.getQuantidade().replace(",", "."));
|
||||
} catch (Exception e) {}
|
||||
|
||||
totalInvestido += qtd * ativo.getPrecoCompra();
|
||||
valorTotalPortfolio += qtd * ativo.getPrecoAtual();
|
||||
}
|
||||
}
|
||||
|
||||
if (tvValor4 != null) {
|
||||
tvValor4.setText(String.format(Locale.getDefault(), "%.2f€", valorTotalPortfolio));
|
||||
}
|
||||
|
||||
if (tvTransacoes4 != null) {
|
||||
double roi = valorTotalPortfolio - totalInvestido;
|
||||
double roiPct = 0;
|
||||
if (totalInvestido > 0) {
|
||||
roiPct = (roi / totalInvestido) * 100;
|
||||
}
|
||||
|
||||
if (roi >= 0) {
|
||||
tvTransacoes4.setText(String.format(Locale.getDefault(), "+%.2f%% ROI", roiPct));
|
||||
tvTransacoes4.setTextColor(android.graphics.Color.parseColor("#22C55E"));
|
||||
} else {
|
||||
tvTransacoes4.setText(String.format(Locale.getDefault(), "%.2f%% ROI", roiPct));
|
||||
tvTransacoes4.setTextColor(android.graphics.Color.parseColor("#FF0000"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCancelled(@NonNull DatabaseError error) {
|
||||
// Ignore
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
|
||||
@@ -247,7 +247,7 @@
|
||||
android:layout_marginStart="7dp"
|
||||
android:layout_marginTop="12dp"
|
||||
android:fontFamily="sans-serif"
|
||||
android:text="Valor do Ativos"
|
||||
android:text="Valor de Ativos"
|
||||
android:textColor="#4A5568"
|
||||
android:textSize="16sp"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
|
||||
Reference in New Issue
Block a user