mostly working ticker prices
This commit is contained in:
parent
04d179425f
commit
daebb67a56
|
|
@ -1,8 +1,7 @@
|
||||||
package com.example.dcav2gui.ui.home;
|
package com.example.dcav2gui.ui.home;
|
||||||
|
|
||||||
import static com.example.dcav2gui.MainActivity.globalSettings;
|
import android.content.res.ColorStateList;
|
||||||
|
import android.graphics.Color;
|
||||||
import android.annotation.SuppressLint;
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
|
|
@ -13,14 +12,15 @@ import android.widget.TextView;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
|
import androidx.core.content.ContextCompat;
|
||||||
import androidx.fragment.app.Fragment;
|
import androidx.fragment.app.Fragment;
|
||||||
import androidx.lifecycle.ViewModelProvider;
|
import androidx.lifecycle.ViewModelProvider;
|
||||||
|
|
||||||
import com.example.dcav2gui.MainActivity;
|
import com.example.dcav2gui.MainActivity;
|
||||||
import com.example.dcav2gui.R;
|
import com.example.dcav2gui.R;
|
||||||
import com.example.dcav2gui.TickerTracker;
|
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.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
|
|
@ -214,7 +214,10 @@ public class HomeFragment extends Fragment {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
fetchAndDisplayPriceData();
|
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);
|
handler.postDelayed(this, delay);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
@ -222,32 +225,57 @@ public class HomeFragment extends Fragment {
|
||||||
|
|
||||||
return root;
|
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() {
|
private void fetchAndDisplayPriceData() {
|
||||||
executorService.execute(() -> {
|
executorService.execute(() -> {
|
||||||
try {
|
try {
|
||||||
// Fetch price data in background
|
// Fetch price data in background
|
||||||
TickerTracker.PriceChangeData priceData = TickerTracker.getPriceChanges("BTCUSDT");
|
String ticker1 = "BTCUSDT";
|
||||||
TickerTracker.PriceChangeData priceData2 = TickerTracker.getPriceChanges("ETHUSDT");
|
String ticker2 = "ETHUSDT";
|
||||||
TickerTracker.PriceChangeData priceData3 = TickerTracker.getPriceChanges("USDTARS");
|
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
|
// Update UI on the main thread
|
||||||
requireActivity().runOnUiThread(() -> {
|
requireActivity().runOnUiThread(() -> {
|
||||||
try {
|
try {
|
||||||
pricePair1.setText(String.format("%.2f", priceData.getCurrentPrice()));
|
pricePair1.setText(String.format(Locale.ROOT,"%.2f", priceData.getCurrentPrice()));
|
||||||
pricePair124hPercentage.setText(String.format("%.2f%%", priceData.getPriceChangePercent24h()));
|
setPercentageColor(pricePair124hPercentage,priceData.getPriceChangePercent24h());
|
||||||
pricePair17dPercentage.setText(String.format("%.2f%%", priceData.getPriceChangePercent7d()));
|
setPercentageColor(pricePair17dPercentage,priceData.getPriceChangePercent7d());
|
||||||
pricePair130dPercentage.setText(String.format("%.2f%%", priceData.getPriceChangePercent30d()));
|
setPercentageColor(pricePair130dPercentage,priceData.getPriceChangePercent30d());
|
||||||
|
|
||||||
pricePair2.setText(String.format("%.2f", priceData2.getCurrentPrice()));
|
pricePair2.setText(String.format(Locale.ROOT,"%.2f", priceData2.getCurrentPrice()));
|
||||||
pricePair224hPercentage.setText(String.format("%.2f%%", priceData2.getPriceChangePercent24h()));
|
setPercentageColor(pricePair224hPercentage,priceData2.getPriceChangePercent24h());
|
||||||
pricePair27dPercentage.setText(String.format("%.2f%%", priceData2.getPriceChangePercent7d()));
|
setPercentageColor(pricePair27dPercentage,priceData2.getPriceChangePercent7d());
|
||||||
pricePair230dPercentage.setText(String.format("%.2f%%", priceData2.getPriceChangePercent30d()));
|
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) {
|
} catch (Exception e) {
|
||||||
Toast.makeText(getContext(), "Error updating UI", Toast.LENGTH_SHORT).show();
|
Toast.makeText(getContext(), "Error updating UI", Toast.LENGTH_SHORT).show();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue