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;
import android.os.Bundle;
@ -7,51 +8,34 @@ import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.dcav2gui.R;
import com.example.dcav2gui.databinding.FragmentBinanceBinding;
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;
public class BinanceFragment extends Fragment {
private FragmentBinanceBinding binding;
private WorkerCardAdapter adapter;
private BinanceViewModel binanceViewModel;
private WorkerCardAdapter workerCardAdapter;
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
BinanceViewModel binanceViewModel =
new ViewModelProvider(this).get(BinanceViewModel.class);
binding = FragmentBinanceBinding.inflate(inflater, container, false);
View root = binding.getRoot();
RecyclerView recyclerView = binding.recyclerView;
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
binanceViewModel = new ViewModelProvider(this).get(BinanceViewModel.class);
adapter = new WorkerCardAdapter(new ArrayList<>());
workerCardAdapter = new WorkerCardAdapter(binding.binanceCardsContainer);
//adapter = new WorkerCardAdapter(binanceViewModel.getWorkerData().getValue() != null ? binanceViewModel.getWorkerData().getValue() : new ArrayList<>());
recyclerView.setAdapter(adapter);
//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);
}
binanceViewModel.getWorkerData().observe(getViewLifecycleOwner(), workerDataList -> {
workerCardAdapter.updateData(workerDataList);
});
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;
public class WorkerCardAdapter extends RecyclerView.Adapter<WorkerCardAdapter.WorkerViewHolder> {
public class WorkerCardAdapter{
private List<WorkerData> workerDataList;
private LinearLayout container;
public WorkerCardAdapter(List<WorkerData> workerDataList) {
this.workerDataList = workerDataList;
public WorkerCardAdapter(LinearLayout container) {
this.container = container;
}
@NonNull
@Override
public WorkerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.worker_card, parent, false);
return new WorkerViewHolder(view);
public void updateData(List<WorkerData> workerDataList) {
container.removeAllViews(); // Clear existing views
for (WorkerData worker : workerDataList) {
addCard(worker);
}
}
@Override
public void onBindViewHolder(@NonNull WorkerViewHolder holder, int position) {
private void addCard(WorkerData worker) {
//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);
holder.pair.setText(worker.getPair());
if (pair == null || safetyOrders == null || uptime == null || nextSoPrice == null ||
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()) {
holder.pair.setTextColor(Color.parseColor("#FFA500"));
pair.setTextColor(Color.parseColor("#FFA500"));
} else {
//Cyan
holder.pair.setTextColor(Color.parseColor("#008B8B"));
pair.setTextColor(Color.parseColor("#008B8B"));
}
holder.price.setText(String.format(Locale.ROOT, "%.8f", worker.getPrice()));
holder.nextSoPrice.setText(String.format(Locale.ROOT, "%.8f", worker.getNextSoPrice()));
holder.takeProfitPrice.setText(String.format(Locale.ROOT, "%.8f", worker.getTakeProfitPrice()));
price.setText(String.format(Locale.ROOT, "%.8f", worker.getPrice()));
nextSoPrice.setText(String.format(Locale.ROOT, "%.8f", worker.getNextSoPrice()));
takeProfitPrice.setText(String.format(Locale.ROOT, "%.8f", worker.getTakeProfitPrice()));
String percentage = String.format(Locale.ROOT, "%.2f",abs(worker.getTakeProfitPrice()- worker.getPrice())/worker.getPrice()*100)+"%";
holder.percentage.setText(percentage);
String percentageToDisplay = String.format(Locale.ROOT, "%.2f",abs(worker.getTakeProfitPrice()- worker.getPrice())/worker.getPrice()*100)+"%";
percentage.setText(percentageToDisplay);
String safetyOrders = worker.getAmountOfSafetyOrders()-1 + " / " + worker.getMaxSafetyOrders();
holder.safetyOrders.setText(safetyOrders);
String safetyOrdersToDisplay = worker.getAmountOfSafetyOrders()-1 + " / " + worker.getMaxSafetyOrders();
safetyOrders.setText(safetyOrdersToDisplay);
holder.uptime.setText(formatSeconds(worker.getUptime()));
uptime.setText(formatSeconds(worker.getUptime()));
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();
if (worker.isBoosted()) {
holder.workerCardIcon.setVisibility(View.VISIBLE);
workerCardIcon.setVisibility(View.VISIBLE);
} else {
holder.workerCardIcon.setVisibility(View.INVISIBLE);
workerCardIcon.setVisibility(View.INVISIBLE);
}
if (worker.getStopWhenProfit()) {
holder.workerStatusString.setText("LAST DEAL");
workerStatusString.setText("LAST DEAL");
} else if (worker.isPaused()) {
holder.cardLayout.setBackgroundColor(Color.parseColor("#C5C281"));
cardLayout.setBackgroundColor(Color.parseColor("#C5C281"));
} else if (worker.isAuto()) {
holder.workerStatusString.setText("AUTO");
workerStatusString.setText("AUTO");
} 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) {
@ -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 {
TextView pair;

View File

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

View File

@ -1,5 +1,5 @@
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
@ -115,4 +115,4 @@
android:progressDrawable="@drawable/worker_progress_bar"/>
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>