tudo a andar rapido vamossss
This commit is contained in:
@@ -22,10 +22,22 @@ import java.util.Calendar;
|
|||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
import com.example.lifegrid.R;
|
import com.example.lifegrid.R;
|
||||||
|
import com.example.lifegrid.adapters.AtivosAdapter;
|
||||||
import com.example.lifegrid.models.Ativos;
|
import com.example.lifegrid.models.Ativos;
|
||||||
import com.google.firebase.auth.FirebaseAuth;
|
import com.google.firebase.auth.FirebaseAuth;
|
||||||
|
import com.google.firebase.database.DataSnapshot;
|
||||||
|
import com.google.firebase.database.DatabaseError;
|
||||||
import com.google.firebase.database.DatabaseReference;
|
import com.google.firebase.database.DatabaseReference;
|
||||||
import com.google.firebase.database.FirebaseDatabase;
|
import com.google.firebase.database.FirebaseDatabase;
|
||||||
|
import com.google.firebase.database.ValueEventListener;
|
||||||
|
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||||
|
import androidx.recyclerview.widget.RecyclerView;
|
||||||
|
|
||||||
|
import android.widget.TextView;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* AtivosFragment diz respeito à janela do ecossistema dedicada à gestão
|
* AtivosFragment diz respeito à janela do ecossistema dedicada à gestão
|
||||||
@@ -33,6 +45,13 @@ import com.google.firebase.database.FirebaseDatabase;
|
|||||||
*/
|
*/
|
||||||
public class AtivosFragment extends Fragment {
|
public class AtivosFragment extends Fragment {
|
||||||
|
|
||||||
|
private RecyclerView ativosRecyclerView;
|
||||||
|
private AtivosAdapter ativosAdapter;
|
||||||
|
private List<Ativos> ativosList;
|
||||||
|
private List<String> keysList;
|
||||||
|
private DatabaseReference databaseReference;
|
||||||
|
private TextView textViewNenhumInvestimento;
|
||||||
|
|
||||||
public AtivosFragment() {
|
public AtivosFragment() {
|
||||||
// Construtor público vazio obrigatório
|
// Construtor público vazio obrigatório
|
||||||
}
|
}
|
||||||
@@ -45,15 +64,95 @@ public class AtivosFragment extends Fragment {
|
|||||||
@Override
|
@Override
|
||||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||||
Bundle savedInstanceState) {
|
Bundle savedInstanceState) {
|
||||||
// Inflaciona o layout para este fragmento
|
|
||||||
View root = inflater.inflate(R.layout.fragment_ativos, container, false);
|
View root = inflater.inflate(R.layout.fragment_ativos, container, false);
|
||||||
|
|
||||||
Button novaTransacaoButton = root.findViewById(R.id.novaTransacaoButton);
|
Button novaTransacaoButton = root.findViewById(R.id.novaTransacaoButton);
|
||||||
novaTransacaoButton.setOnClickListener(v -> showNovoAtivoDialog());
|
novaTransacaoButton.setOnClickListener(v -> showNovoAtivoDialog());
|
||||||
|
|
||||||
|
ativosRecyclerView = root.findViewById(R.id.ativosRecyclerView);
|
||||||
|
textViewNenhumInvestimento = root.findViewById(R.id.textView13);
|
||||||
|
|
||||||
|
ativosRecyclerView.setLayoutManager(new LinearLayoutManager(requireContext()));
|
||||||
|
ativosList = new ArrayList<>();
|
||||||
|
keysList = new ArrayList<>();
|
||||||
|
|
||||||
|
databaseReference = FirebaseDatabase.getInstance().getReference();
|
||||||
|
|
||||||
|
ativosAdapter = new AtivosAdapter(requireContext(), ativosList, keysList, new AtivosAdapter.OnItemClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onDeleteClick(int position, String key) {
|
||||||
|
String userId = FirebaseAuth.getInstance().getCurrentUser().getUid();
|
||||||
|
databaseReference.child("users").child(userId).child("ativos").child(key).removeValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onRefreshClick(int position, String key, Ativos ativo) {
|
||||||
|
// A implementar no futuro
|
||||||
|
}
|
||||||
|
});
|
||||||
|
ativosRecyclerView.setAdapter(ativosAdapter);
|
||||||
|
|
||||||
|
loadAtivos();
|
||||||
|
|
||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void loadAtivos() {
|
||||||
|
String userId = FirebaseAuth.getInstance().getCurrentUser().getUid();
|
||||||
|
databaseReference.child("users").child(userId).child("ativos").addValueEventListener(new ValueEventListener() {
|
||||||
|
@Override
|
||||||
|
public void onDataChange(@NonNull DataSnapshot snapshot) {
|
||||||
|
ativosList.clear();
|
||||||
|
keysList.clear();
|
||||||
|
double totalInvestido = 0;
|
||||||
|
double valorTotalPortfolio = 0;
|
||||||
|
|
||||||
|
for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
|
||||||
|
Ativos ativo = dataSnapshot.getValue(Ativos.class);
|
||||||
|
if (ativo != null) {
|
||||||
|
ativosList.add(ativo);
|
||||||
|
keysList.add(dataSnapshot.getKey());
|
||||||
|
|
||||||
|
double qtd = 0;
|
||||||
|
try {
|
||||||
|
qtd = Double.parseDouble(ativo.getQuantidade().replace(",", "."));
|
||||||
|
} catch (Exception e) {}
|
||||||
|
|
||||||
|
totalInvestido += qtd * ativo.getPrecoCompra();
|
||||||
|
valorTotalPortfolio += qtd * ativo.getPrecoAtual();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ativosAdapter.notifyDataSetChanged();
|
||||||
|
|
||||||
|
if (ativosList.isEmpty()) {
|
||||||
|
textViewNenhumInvestimento.setVisibility(View.VISIBLE);
|
||||||
|
ativosRecyclerView.setVisibility(View.GONE);
|
||||||
|
} else {
|
||||||
|
textViewNenhumInvestimento.setVisibility(View.GONE);
|
||||||
|
ativosRecyclerView.setVisibility(View.VISIBLE);
|
||||||
|
}
|
||||||
|
|
||||||
|
View view = getView();
|
||||||
|
if (view != null) {
|
||||||
|
TextView tvValorTotal = view.findViewById(R.id.textView17);
|
||||||
|
TextView tvTotalInvestido = view.findViewById(R.id.textView18);
|
||||||
|
TextView tvRoiTotal = view.findViewById(R.id.textView19);
|
||||||
|
|
||||||
|
tvValorTotal.setText(String.format(Locale.getDefault(), "%.2f€", valorTotalPortfolio));
|
||||||
|
tvTotalInvestido.setText(String.format(Locale.getDefault(), "%.2f€", totalInvestido));
|
||||||
|
|
||||||
|
double roi = valorTotalPortfolio - totalInvestido;
|
||||||
|
tvRoiTotal.setText(String.format(Locale.getDefault(), "%.2f€", roi));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCancelled(@NonNull DatabaseError error) {
|
||||||
|
Toast.makeText(requireContext(), "Erro ao carregar ativos.", Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
private void showNovoAtivoDialog() {
|
private void showNovoAtivoDialog() {
|
||||||
AlertDialog.Builder builder = new AlertDialog.Builder(requireContext());
|
AlertDialog.Builder builder = new AlertDialog.Builder(requireContext());
|
||||||
View dialogView = getLayoutInflater().inflate(R.layout.dialog_novo_ativo, null);
|
View dialogView = getLayoutInflater().inflate(R.layout.dialog_novo_ativo, null);
|
||||||
@@ -87,20 +186,18 @@ public class AtivosFragment extends Fragment {
|
|||||||
Spinner spinnerTipoAtivo = dialogView.findViewById(R.id.spinnerTipoAtivo);
|
Spinner spinnerTipoAtivo = dialogView.findViewById(R.id.spinnerTipoAtivo);
|
||||||
EditText etQuantidade = dialogView.findViewById(R.id.etQuantidade);
|
EditText etQuantidade = dialogView.findViewById(R.id.etQuantidade);
|
||||||
EditText etPrecoCompra = dialogView.findViewById(R.id.etPrecoCompra);
|
EditText etPrecoCompra = dialogView.findViewById(R.id.etPrecoCompra);
|
||||||
EditText etPrecoAtual = dialogView.findViewById(R.id.etPrecoAtual);
|
|
||||||
|
|
||||||
btnAdicionarAtivoDialog.setOnClickListener(v -> {
|
btnAdicionarAtivoDialog.setOnClickListener(v -> {
|
||||||
String nome = etNomeAtivo.getText().toString().trim();
|
String nome = etNomeAtivo.getText().toString().trim();
|
||||||
String tipo = spinnerTipoAtivo.getSelectedItem().toString();
|
String tipo = spinnerTipoAtivo.getSelectedItem().toString();
|
||||||
String quantidade = etQuantidade.getText().toString().trim();
|
String quantidade = etQuantidade.getText().toString().trim();
|
||||||
String precoCompra = etPrecoCompra.getText().toString().trim();
|
String precoCompra = etPrecoCompra.getText().toString().trim();
|
||||||
String precoAtual = etPrecoAtual.getText().toString().trim();
|
|
||||||
String dataCompra = etDataCompra.getText().toString().trim();
|
String dataCompra = etDataCompra.getText().toString().trim();
|
||||||
|
|
||||||
Ativos ativos = new Ativos(nome, quantidade, Double.parseDouble(precoCompra), Double.parseDouble(precoAtual), dataCompra, tipo);
|
Ativos ativos = new Ativos(nome, quantidade, Double.parseDouble(precoCompra), Double.parseDouble(precoCompra), dataCompra, tipo);
|
||||||
|
|
||||||
|
|
||||||
if (nome.isEmpty() || quantidade.isEmpty() || precoCompra.isEmpty() || precoAtual.isEmpty() || dataCompra.isEmpty() || spinnerTipoAtivo.getSelectedItemPosition() == 0) {
|
if (nome.isEmpty() || quantidade.isEmpty() || precoCompra.isEmpty() || dataCompra.isEmpty() || spinnerTipoAtivo.getSelectedItemPosition() == 0) {
|
||||||
Toast.makeText(requireContext(), "Por favor, preencha os campos obrigatórios.", Toast.LENGTH_SHORT).show();
|
Toast.makeText(requireContext(), "Por favor, preencha os campos obrigatórios.", Toast.LENGTH_SHORT).show();
|
||||||
} else {
|
} else {
|
||||||
// Aqui seria a lógica para guardar na Firebase
|
// Aqui seria a lógica para guardar na Firebase
|
||||||
|
|||||||
@@ -107,24 +107,6 @@
|
|||||||
android:paddingHorizontal="16dp"
|
android:paddingHorizontal="16dp"
|
||||||
android:textColor="@color/preto" />
|
android:textColor="@color/preto" />
|
||||||
|
|
||||||
<TextView
|
|
||||||
android:layout_width="wrap_content"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:layout_marginBottom="8dp"
|
|
||||||
android:text="Preço Atual (€)"
|
|
||||||
android:textColor="@color/preto"
|
|
||||||
android:textStyle="bold" />
|
|
||||||
|
|
||||||
<EditText
|
|
||||||
android:id="@+id/etPrecoAtual"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="50dp"
|
|
||||||
android:layout_marginBottom="16dp"
|
|
||||||
android:background="@drawable/rounded_input_bg"
|
|
||||||
android:hint="0.00"
|
|
||||||
android:inputType="numberDecimal"
|
|
||||||
android:paddingHorizontal="16dp"
|
|
||||||
android:textColor="@color/preto" />
|
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
|
|||||||
@@ -56,6 +56,7 @@
|
|||||||
app:layout_constraintTop_toBottomOf="@+id/textView12" />
|
app:layout_constraintTop_toBottomOf="@+id/textView12" />
|
||||||
|
|
||||||
<androidx.recyclerview.widget.RecyclerView
|
<androidx.recyclerview.widget.RecyclerView
|
||||||
|
android:id="@+id/ativosRecyclerView"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="211dp"
|
android:layout_height="211dp"
|
||||||
android:layout_marginStart="16dp"
|
android:layout_marginStart="16dp"
|
||||||
|
|||||||
Reference in New Issue
Block a user