no recyclerview

This commit is contained in:
Nicolás Sánchez 2024-12-17 16:46:16 -03:00
parent 1326ba08e7
commit ddda285f41
4 changed files with 68 additions and 100 deletions

View File

@ -1,3 +1,4 @@
// BinanceFragment.java
package com.example.dcav2gui.ui.exchanges; package com.example.dcav2gui.ui.exchanges;
import android.os.Bundle; import android.os.Bundle;
@ -7,51 +8,34 @@ import android.view.ViewGroup;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider; import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.dcav2gui.R; import com.example.dcav2gui.R;
import com.example.dcav2gui.databinding.FragmentBinanceBinding; import com.example.dcav2gui.databinding.FragmentBinanceBinding;
import com.example.dcav2gui.ui.exchanges.adapters.WorkerCardAdapter; import com.example.dcav2gui.ui.exchanges.adapters.WorkerCardAdapter;
import com.example.dcav2gui.ui.exchanges.WorkerData;
import java.util.ArrayList; import java.io.IOException;
import java.util.List; import java.util.List;
public class BinanceFragment extends Fragment { public class BinanceFragment extends Fragment {
private FragmentBinanceBinding binding; private FragmentBinanceBinding binding;
private WorkerCardAdapter adapter; private BinanceViewModel binanceViewModel;
private WorkerCardAdapter workerCardAdapter;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) { ViewGroup container, Bundle savedInstanceState) {
BinanceViewModel binanceViewModel =
new ViewModelProvider(this).get(BinanceViewModel.class);
binding = FragmentBinanceBinding.inflate(inflater, container, false); binding = FragmentBinanceBinding.inflate(inflater, container, false);
View root = binding.getRoot(); View root = binding.getRoot();
RecyclerView recyclerView = binding.recyclerView; binanceViewModel = new ViewModelProvider(this).get(BinanceViewModel.class);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
adapter = new WorkerCardAdapter(new ArrayList<>()); workerCardAdapter = new WorkerCardAdapter(binding.binanceCardsContainer);
//adapter = new WorkerCardAdapter(binanceViewModel.getWorkerData().getValue() != null ? binanceViewModel.getWorkerData().getValue() : new ArrayList<>()); binanceViewModel.getWorkerData().observe(getViewLifecycleOwner(), workerDataList -> {
recyclerView.setAdapter(adapter); workerCardAdapter.updateData(workerDataList);
//binanceViewModel.getWorkerData().observe(getViewLifecycleOwner(), adapter::updateData);
binanceViewModel.getWorkerData().observe(getViewLifecycleOwner(), newData -> {
if (adapter == null) {
adapter = new WorkerCardAdapter(newData);
recyclerView.setAdapter(adapter);
} else {
adapter.updateData(newData);
}
}); });
return root; return root;
} }
@Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
}
} }

View File

@ -18,82 +18,81 @@ import java.util.Locale;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public class WorkerCardAdapter extends RecyclerView.Adapter<WorkerCardAdapter.WorkerViewHolder> { public class WorkerCardAdapter{
private List<WorkerData> workerDataList; private LinearLayout container;
public WorkerCardAdapter(List<WorkerData> workerDataList) { public WorkerCardAdapter(LinearLayout container) {
this.workerDataList = workerDataList; this.container = container;
} }
@NonNull public void updateData(List<WorkerData> workerDataList) {
@Override container.removeAllViews(); // Clear existing views
public WorkerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { for (WorkerData worker : workerDataList) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.worker_card, parent, false); addCard(worker);
return new WorkerViewHolder(view); }
} }
@Override private void addCard(WorkerData worker) {
public void onBindViewHolder(@NonNull WorkerViewHolder holder, int position) {
//TODO: progress bar color //TODO: progress bar color
View view = LayoutInflater.from(container.getContext()).inflate(R.layout.worker_card, container, false);
TextView pair = view.findViewById(R.id.workerCardPair);
TextView safetyOrders = view.findViewById(R.id.workerCardSafetyOrders);
TextView uptime = view.findViewById(R.id.workerCardUptime);
TextView nextSoPrice = view.findViewById(R.id.workerCardNextSoPrice);
TextView price = view.findViewById(R.id.workerCardCurrentPrice);
TextView takeProfitPrice = view.findViewById(R.id.workerCardTakeProfitPrice);
TextView percentage = view.findViewById(R.id.workerCardPercentageToDeal);
ProgressBar progressBar = view.findViewById(R.id.workerCardProgressBar);
TextView workerStatusString = view.findViewById(R.id.workerCardStatusString);
ImageView workerCardIcon = view.findViewById(R.id.workerCardIcon);
LinearLayout cardLayout = view.findViewById(R.id.workerCard);
WorkerData worker = workerDataList.get(position); if (pair == null || safetyOrders == null || uptime == null || nextSoPrice == null ||
holder.pair.setText(worker.getPair()); price == null || takeProfitPrice == null || percentage == null || progressBar == null ||
workerStatusString == null || workerCardIcon == null || cardLayout == null) {
System.out.println("One or more views are null");
return;
}
pair.setText(worker.getPair());
if (worker.isShort()) { if (worker.isShort()) {
holder.pair.setTextColor(Color.parseColor("#FFA500")); pair.setTextColor(Color.parseColor("#FFA500"));
} else { } else {
//Cyan //Cyan
holder.pair.setTextColor(Color.parseColor("#008B8B")); pair.setTextColor(Color.parseColor("#008B8B"));
} }
holder.price.setText(String.format(Locale.ROOT, "%.8f", worker.getPrice())); price.setText(String.format(Locale.ROOT, "%.8f", worker.getPrice()));
holder.nextSoPrice.setText(String.format(Locale.ROOT, "%.8f", worker.getNextSoPrice())); nextSoPrice.setText(String.format(Locale.ROOT, "%.8f", worker.getNextSoPrice()));
holder.takeProfitPrice.setText(String.format(Locale.ROOT, "%.8f", worker.getTakeProfitPrice())); takeProfitPrice.setText(String.format(Locale.ROOT, "%.8f", worker.getTakeProfitPrice()));
String percentage = String.format(Locale.ROOT, "%.2f",abs(worker.getTakeProfitPrice()- worker.getPrice())/worker.getPrice()*100)+"%"; String percentageToDisplay = String.format(Locale.ROOT, "%.2f",abs(worker.getTakeProfitPrice()- worker.getPrice())/worker.getPrice()*100)+"%";
holder.percentage.setText(percentage); percentage.setText(percentageToDisplay);
String safetyOrders = worker.getAmountOfSafetyOrders()-1 + " / " + worker.getMaxSafetyOrders(); String safetyOrdersToDisplay = worker.getAmountOfSafetyOrders()-1 + " / " + worker.getMaxSafetyOrders();
holder.safetyOrders.setText(safetyOrders); safetyOrders.setText(safetyOrdersToDisplay);
holder.uptime.setText(formatSeconds(worker.getUptime())); uptime.setText(formatSeconds(worker.getUptime()));
double progressBarPercentage = (worker.getPrice()-worker.getNextSoPrice())/(worker.getTakeProfitPrice()-worker.getNextSoPrice()); double progressBarPercentage = (worker.getPrice()-worker.getNextSoPrice())/(worker.getTakeProfitPrice()-worker.getNextSoPrice());
holder.progressBar.setProgress((int) (progressBarPercentage * 100)); progressBar.setProgress((int) (progressBarPercentage * 100));
double breakEven = worker.getTotalAmountOfQuote()/worker.getTotalAmountOfBase(); double breakEven = worker.getTotalAmountOfQuote()/worker.getTotalAmountOfBase();
if (worker.isBoosted()) { if (worker.isBoosted()) {
holder.workerCardIcon.setVisibility(View.VISIBLE); workerCardIcon.setVisibility(View.VISIBLE);
} else { } else {
holder.workerCardIcon.setVisibility(View.INVISIBLE); workerCardIcon.setVisibility(View.INVISIBLE);
} }
if (worker.getStopWhenProfit()) { if (worker.getStopWhenProfit()) {
holder.workerStatusString.setText("LAST DEAL"); workerStatusString.setText("LAST DEAL");
} else if (worker.isPaused()) { } else if (worker.isPaused()) {
holder.cardLayout.setBackgroundColor(Color.parseColor("#C5C281")); cardLayout.setBackgroundColor(Color.parseColor("#C5C281"));
} else if (worker.isAuto()) { } else if (worker.isAuto()) {
holder.workerStatusString.setText("AUTO"); workerStatusString.setText("AUTO");
} else { } else {
holder.workerStatusString.setText(""); workerStatusString.setText("");
} }
System.err.println(worker.getPair());
System.err.println(workerDataList.size());
System.err.println(workerDataList.get(0).getPair());
System.err.println(workerDataList.get(20).getPair());
// int color;
// if (worker.getNextSoPrice()< worker.getTakeProfitPrice()) {
// if (worker.getPrice()>=breakEven){
// color = Color.GREEN;
// } else {
// color = Color.RED;
// }
// } else {
// if (worker.getPrice()<breakEven){
// color = Color.GREEN;
// } else {
// color = Color.RED;
// }
// }
} }
public static String formatSeconds(double seconds) { public static String formatSeconds(double seconds) {
@ -106,15 +105,6 @@ public class WorkerCardAdapter extends RecyclerView.Adapter<WorkerCardAdapter.Wo
} }
@Override
public int getItemCount() {
return workerDataList.size();
}
public void updateData(List<WorkerData> newData) {
workerDataList = newData;
notifyDataSetChanged();
}
public static class WorkerViewHolder extends RecyclerView.ViewHolder { public static class WorkerViewHolder extends RecyclerView.ViewHolder {
TextView pair; TextView pair;

View File

@ -1,19 +1,13 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/binance_cards_container"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="wrap_content"
tools:context=".ui.exchanges.BinanceFragment"> android:orientation="vertical"
android:background="#FF0000"
android:visibility="visible">
<androidx.recyclerview.widget.RecyclerView <!-- Each card will be added dynamically here -->
android:id="@+id/recyclerView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout> </LinearLayout>

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent">
@ -115,4 +115,4 @@
android:progressDrawable="@drawable/worker_progress_bar"/> android:progressDrawable="@drawable/worker_progress_bar"/>
</LinearLayout> </LinearLayout>
</LinearLayout> </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout> </LinearLayout>