mostly working ticker prices

This commit is contained in:
Nicolás Sánchez 2024-12-12 12:22:18 -03:00
parent 04d179425f
commit daebb67a56
1 changed files with 49 additions and 21 deletions

View File

@ -1,8 +1,7 @@
package com.example.dcav2gui.ui.home;
import static com.example.dcav2gui.MainActivity.globalSettings;
import android.annotation.SuppressLint;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
@ -13,14 +12,15 @@ import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import com.example.dcav2gui.MainActivity;
import com.example.dcav2gui.R;
import com.example.dcav2gui.TickerTracker;
import com.example.dcav2gui.ui.settings.SettingsData;
import java.util.Locale;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@ -214,7 +214,10 @@ public class HomeFragment extends Fragment {
@Override
public void run() {
fetchAndDisplayPriceData();
long delay = (long) MainActivity.getGlobalSettings().timeBetweenQueries * 1000;
long delay = 1000;
if (MainActivity.getGlobalSettings() != null) {
delay = (long) MainActivity.getGlobalSettings().timeBetweenQueries * 1000;
}
handler.postDelayed(this, delay);
}
};
@ -222,32 +225,57 @@ public class HomeFragment extends Fragment {
return root;
}
@SuppressLint("DefaultLocale")
// Adds a plus sign if the percentage is positive
private String formatPercentage(double value) {
if (value>=0) {
return "+"+String.format(Locale.ROOT, "%.2f%%",value);
} else {
return String.format(Locale.ROOT, "%.2f%%",value);
}
}
private void setPercentageColor(TextView textView, double percentage) {
String formattedPercentage = formatPercentage(percentage);
textView.setText(formattedPercentage);
if (percentage > 0) {
textView.setTextColor(Color.GREEN);
} else {
textView.setTextColor(Color.RED);
}
}
private void fetchAndDisplayPriceData() {
executorService.execute(() -> {
try {
// Fetch price data in background
TickerTracker.PriceChangeData priceData = TickerTracker.getPriceChanges("BTCUSDT");
TickerTracker.PriceChangeData priceData2 = TickerTracker.getPriceChanges("ETHUSDT");
TickerTracker.PriceChangeData priceData3 = TickerTracker.getPriceChanges("USDTARS");
String ticker1 = "BTCUSDT";
String ticker2 = "ETHUSDT";
String ticker3 = "USDTARS";
TickerTracker.PriceChangeData priceData = TickerTracker.getPriceChanges(ticker1);
TickerTracker.PriceChangeData priceData2 = TickerTracker.getPriceChanges(ticker2);
TickerTracker.PriceChangeData priceData3 = TickerTracker.getPriceChanges(ticker3);
// Update UI on the main thread
requireActivity().runOnUiThread(() -> {
try {
pricePair1.setText(String.format("%.2f", priceData.getCurrentPrice()));
pricePair124hPercentage.setText(String.format("%.2f%%", priceData.getPriceChangePercent24h()));
pricePair17dPercentage.setText(String.format("%.2f%%", priceData.getPriceChangePercent7d()));
pricePair130dPercentage.setText(String.format("%.2f%%", priceData.getPriceChangePercent30d()));
pricePair1.setText(String.format(Locale.ROOT,"%.2f", priceData.getCurrentPrice()));
setPercentageColor(pricePair124hPercentage,priceData.getPriceChangePercent24h());
setPercentageColor(pricePair17dPercentage,priceData.getPriceChangePercent7d());
setPercentageColor(pricePair130dPercentage,priceData.getPriceChangePercent30d());
pricePair2.setText(String.format("%.2f", priceData2.getCurrentPrice()));
pricePair224hPercentage.setText(String.format("%.2f%%", priceData2.getPriceChangePercent24h()));
pricePair27dPercentage.setText(String.format("%.2f%%", priceData2.getPriceChangePercent7d()));
pricePair230dPercentage.setText(String.format("%.2f%%", priceData2.getPriceChangePercent30d()));
pricePair2.setText(String.format(Locale.ROOT,"%.2f", priceData2.getCurrentPrice()));
setPercentageColor(pricePair224hPercentage,priceData2.getPriceChangePercent24h());
setPercentageColor(pricePair27dPercentage,priceData2.getPriceChangePercent7d());
setPercentageColor(pricePair230dPercentage,priceData2.getPriceChangePercent30d());
pricePair3.setText(String.format(Locale.ROOT,"%.2f", priceData3.getCurrentPrice()));
setPercentageColor(pricePair324hPercentage,priceData3.getPriceChangePercent24h());
setPercentageColor(pricePair37dPercentage,priceData3.getPriceChangePercent7d());
setPercentageColor(pricePair330dPercentage,priceData3.getPriceChangePercent30d());
pricePair3.setText(String.format("%.2f", priceData3.getCurrentPrice()));
pricePair324hPercentage.setText(String.format("%.2f%%", priceData3.getPriceChangePercent24h()));
pricePair37dPercentage.setText(String.format("%.2f%%", priceData3.getPriceChangePercent7d()));
pricePair330dPercentage.setText(String.format("%.2f%%", priceData3.getPriceChangePercent30d()));
} catch (Exception e) {
Toast.makeText(getContext(), "Error updating UI", Toast.LENGTH_SHORT).show();
}