Concurrent ticker fetching

This commit is contained in:
Nicolás Sánchez 2024-12-12 12:46:04 -03:00
parent daebb67a56
commit 8fbceb083c
1 changed files with 38 additions and 4 deletions

View File

@ -21,6 +21,7 @@ import com.example.dcav2gui.R;
import com.example.dcav2gui.TickerTracker; import com.example.dcav2gui.TickerTracker;
import java.util.Locale; import java.util.Locale;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
@ -254,13 +255,46 @@ public class HomeFragment extends Fragment {
String ticker2 = "ETHUSDT"; String ticker2 = "ETHUSDT";
String ticker3 = "USDTARS"; String ticker3 = "USDTARS";
TickerTracker.PriceChangeData priceData = TickerTracker.getPriceChanges(ticker1); // Fetch price data in background using CompletableFuture
TickerTracker.PriceChangeData priceData2 = TickerTracker.getPriceChanges(ticker2); CompletableFuture<TickerTracker.PriceChangeData> future1 = CompletableFuture.supplyAsync(() -> {
TickerTracker.PriceChangeData priceData3 = TickerTracker.getPriceChanges(ticker3); try {
return TickerTracker.getPriceChanges(ticker1);
} catch (IOException e) {
e.printStackTrace();
return null;
}
});
CompletableFuture<TickerTracker.PriceChangeData> future2 = CompletableFuture.supplyAsync(() -> {
try {
return TickerTracker.getPriceChanges(ticker2);
} catch (IOException e) {
e.printStackTrace();
return null;
}
});
CompletableFuture<TickerTracker.PriceChangeData> future3 = CompletableFuture.supplyAsync(() -> {
try {
return TickerTracker.getPriceChanges(ticker3);
} catch (IOException e) {
e.printStackTrace();
return null;
}
});
// Wait for all futures to complete
CompletableFuture<Void> allFutures = CompletableFuture.allOf(future1, future2, future3);
// Update UI on the main thread // Update UI on the main thread
requireActivity().runOnUiThread(() -> { requireActivity().runOnUiThread(() -> {
try { try {
TickerTracker.PriceChangeData priceData = future1.get();
TickerTracker.PriceChangeData priceData2 = future2.get();
TickerTracker.PriceChangeData priceData3 = future3.get();;
pricePair1.setText(String.format(Locale.ROOT,"%.2f", priceData.getCurrentPrice())); pricePair1.setText(String.format(Locale.ROOT,"%.2f", priceData.getCurrentPrice()));
setPercentageColor(pricePair124hPercentage,priceData.getPriceChangePercent24h()); setPercentageColor(pricePair124hPercentage,priceData.getPriceChangePercent24h());
setPercentageColor(pricePair17dPercentage,priceData.getPriceChangePercent7d()); setPercentageColor(pricePair17dPercentage,priceData.getPriceChangePercent7d());
@ -280,7 +314,7 @@ public class HomeFragment extends Fragment {
Toast.makeText(getContext(), "Error updating UI", Toast.LENGTH_SHORT).show(); Toast.makeText(getContext(), "Error updating UI", Toast.LENGTH_SHORT).show();
} }
}); });
} catch (IOException e) { } catch (Exception e) {
requireActivity().runOnUiThread(() -> requireActivity().runOnUiThread(() ->
Toast.makeText(getContext(), "Failed to fetch price data. Check your connection.", Toast.LENGTH_SHORT).show() Toast.makeText(getContext(), "Failed to fetch price data. Check your connection.", Toast.LENGTH_SHORT).show()
); );