diff --git a/app/src/main/java/com/example/dcav2gui/ui/home/HomeFragment.java b/app/src/main/java/com/example/dcav2gui/ui/home/HomeFragment.java index 459eeee..92d334c 100644 --- a/app/src/main/java/com/example/dcav2gui/ui/home/HomeFragment.java +++ b/app/src/main/java/com/example/dcav2gui/ui/home/HomeFragment.java @@ -1,6 +1,5 @@ package com.example.dcav2gui.ui.home; -import android.content.res.ColorStateList; import android.graphics.Color; import android.os.Bundle; import android.os.Handler; @@ -12,7 +11,6 @@ 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; @@ -50,6 +48,51 @@ public class HomeFragment extends Fragment { private TextView pricePair37dPercentage; private TextView pricePair330dPercentage; + private TextView profitsToday; + private TextView profitsThisMonth; + + private ImageView exchange1Status; + private ImageView exchange2Status; + private ImageView exchange3Status; + private ImageView exchange4Status; + + private TextView exchange1Name; + private TextView exchange2Name; + private TextView exchange3Name; + private TextView exchange4Name; + + private TextView exchange1Funds; + private TextView exchange2Funds; + private TextView exchange3Funds; + private TextView exchange4Funds; + + private TextView exchange1FundsNeeded; + private TextView exchange2FundsNeeded; + private TextView exchange3FundsNeeded; + private TextView exchange4FundsNeeded; + + private TextView exchange1FundsPercentage; + private TextView exchange2FundsPercentage; + private TextView exchange3FundsPercentage; + private TextView exchange4FundsPercentage; + + private TextView exchange1WorkersOnline; + private TextView exchange2WorkersOnline; + private TextView exchange3WorkersOnline; + private TextView exchange4WorkersOnline; + + private TextView exchange1WorkersLongShort; + private TextView exchange2WorkersLongShort; + private TextView exchange3WorkersLongShort; + private TextView exchange4WorkersLongShort; + + private TextView lastTrades; + + private TextView log1Content; + private TextView log2Content; + private TextView log3Content; + private TextView log4Content; + public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { @@ -79,58 +122,58 @@ public class HomeFragment extends Fragment { pricePair330dPercentage = root.findViewById(R.id.pricesCard30dText3); // Profits today and this month - TextView profitsToday = root.findViewById(R.id.profitsTodayValue); - TextView profitsThisMonth = root.findViewById(R.id.profitsThisMonthValue); + profitsToday = root.findViewById(R.id.profitsTodayValue); + profitsThisMonth = root.findViewById(R.id.profitsThisMonthValue); // Exchange status - ImageView exchange1Status = root.findViewById(R.id.exchangeStats1Icon); - ImageView exchange2Status = root.findViewById(R.id.exchangeStats2Icon); - ImageView exchange3Status = root.findViewById(R.id.exchangeStats3Icon); - ImageView exchange4Status = root.findViewById(R.id.exchangeStats4Icon); + exchange1Status = root.findViewById(R.id.exchangeStats1Icon); + exchange2Status = root.findViewById(R.id.exchangeStats2Icon); + exchange3Status = root.findViewById(R.id.exchangeStats3Icon); + exchange4Status = root.findViewById(R.id.exchangeStats4Icon); - TextView exchange1Name = root.findViewById(R.id.exchangeStats1Label); - TextView exchange2Name = root.findViewById(R.id.exchangeStats2Label); - TextView exchange3Name = root.findViewById(R.id.exchangeStats3Label); - TextView exchange4Name = root.findViewById(R.id.exchangeStats4Label); + exchange1Name = root.findViewById(R.id.exchangeStats1Label); + exchange2Name = root.findViewById(R.id.exchangeStats2Label); + exchange3Name = root.findViewById(R.id.exchangeStats3Label); + exchange4Name = root.findViewById(R.id.exchangeStats4Label); - TextView exchange1Funds = root.findViewById(R.id.exchangeStats1Funds); - TextView exchange2Funds = root.findViewById(R.id.exchangeStats2Funds); - TextView exchange3Funds = root.findViewById(R.id.exchangeStats3Funds); - TextView exchange4Funds = root.findViewById(R.id.exchangeStats4Funds); + exchange1Funds = root.findViewById(R.id.exchangeStats1Funds); + exchange2Funds = root.findViewById(R.id.exchangeStats2Funds); + exchange3Funds = root.findViewById(R.id.exchangeStats3Funds); + exchange4Funds = root.findViewById(R.id.exchangeStats4Funds); - TextView exchange1FundsNeeded = root.findViewById(R.id.exchangeStats1FundsNeeded); - TextView exchange2FundsNeeded = root.findViewById(R.id.exchangeStats2FundsNeeded); - TextView exchange3FundsNeeded = root.findViewById(R.id.exchangeStats3FundsNeeded); - TextView exchange4FundsNeeded = root.findViewById(R.id.exchangeStats4FundsNeeded); + exchange1FundsNeeded = root.findViewById(R.id.exchangeStats1FundsNeeded); + exchange2FundsNeeded = root.findViewById(R.id.exchangeStats2FundsNeeded); + exchange3FundsNeeded = root.findViewById(R.id.exchangeStats3FundsNeeded); + exchange4FundsNeeded = root.findViewById(R.id.exchangeStats4FundsNeeded); - TextView exchange1FundsPercentage = root.findViewById(R.id.exchangeStats1FundsPercentage); - TextView exchange2FundsPercentage = root.findViewById(R.id.exchangeStats2FundsPercentage); - TextView exchange3FundsPercentage = root.findViewById(R.id.exchangeStats3FundsPercentage); - TextView exchange4FundsPercentage = root.findViewById(R.id.exchangeStats4FundsPercentage); + exchange1FundsPercentage = root.findViewById(R.id.exchangeStats1FundsPercentage); + exchange2FundsPercentage = root.findViewById(R.id.exchangeStats2FundsPercentage); + exchange3FundsPercentage = root.findViewById(R.id.exchangeStats3FundsPercentage); + exchange4FundsPercentage = root.findViewById(R.id.exchangeStats4FundsPercentage); - TextView exchange1WorkersOnline = root.findViewById(R.id.exchangeStats1WorkersOnline); - TextView exchange2WorkersOnline = root.findViewById(R.id.exchangeStats2WorkersOnline); - TextView exchange3WorkersOnline = root.findViewById(R.id.exchangeStats3WorkersOnline); - TextView exchange4WorkersOnline = root.findViewById(R.id.exchangeStats4WorkersOnline); + exchange1WorkersOnline = root.findViewById(R.id.exchangeStats1WorkersOnline); + exchange2WorkersOnline = root.findViewById(R.id.exchangeStats2WorkersOnline); + exchange3WorkersOnline = root.findViewById(R.id.exchangeStats3WorkersOnline); + exchange4WorkersOnline = root.findViewById(R.id.exchangeStats4WorkersOnline); - TextView exchange1WorkersLongShort = root.findViewById(R.id.exchangeStats1WorkersLongShort); - TextView exchange2WorkersLongShort = root.findViewById(R.id.exchangeStats2WorkersLongShort); - TextView exchange3WorkersLongShort = root.findViewById(R.id.exchangeStats3WorkersLongShort); - TextView exchange4WorkersLongShort = root.findViewById(R.id.exchangeStats4WorkersLongShort); + exchange1WorkersLongShort = root.findViewById(R.id.exchangeStats1WorkersLongShort); + exchange2WorkersLongShort = root.findViewById(R.id.exchangeStats2WorkersLongShort); + exchange3WorkersLongShort = root.findViewById(R.id.exchangeStats3WorkersLongShort); + exchange4WorkersLongShort = root.findViewById(R.id.exchangeStats4WorkersLongShort); // Last deals - TextView lastTrades = root.findViewById(R.id.lastTrades); + lastTrades = root.findViewById(R.id.lastTrades); // Log entries - TextView log1Title = root.findViewById(R.id.log1CardTitle); - TextView log2Title = root.findViewById(R.id.log2CardTitle); - TextView log3Title = root.findViewById(R.id.log3CardTitle); - TextView log4Title = root.findViewById(R.id.log4CardTitle); +// TextView log1Title = root.findViewById(R.id.log1CardTitle); +// TextView log2Title = root.findViewById(R.id.log2CardTitle); +// TextView log3Title = root.findViewById(R.id.log3CardTitle); +// TextView log4Title = root.findViewById(R.id.log4CardTitle); - TextView log1Content = root.findViewById(R.id.log1CardContent); - TextView log2Content = root.findViewById(R.id.log2CardContent); - TextView log3Content = root.findViewById(R.id.log3CardContent); - TextView log4Content = root.findViewById(R.id.log4CardContent); + log1Content = root.findViewById(R.id.log1CardContent); + log2Content = root.findViewById(R.id.log2CardContent); + log3Content = root.findViewById(R.id.log3CardContent); + log4Content = root.findViewById(R.id.log4CardContent); // Profits today and this month @@ -138,7 +181,7 @@ public class HomeFragment extends Fragment { profitsThisMonth.setText(R.string.profits_this_month_example); - //Load cached values + // LOAD CACHED VALUES //Prices pricePair1.setText(R.string.default_price_ticker_1); pricePair2.setText(R.string.default_price_ticker_2); @@ -208,10 +251,10 @@ public class HomeFragment extends Fragment { lastTrades.setText(R.string.last_trades_example); // Log entries - log1Title.setText(R.string.exchange_1_log_title); - log2Title.setText(R.string.exchange_2_log_title); - log3Title.setText(R.string.exchange_3_log_title); - log4Title.setText(R.string.exchange_4_log_title); +// log1Title.setText(R.string.exchange_1_log_title); +// log2Title.setText(R.string.exchange_2_log_title); +// log3Title.setText(R.string.exchange_3_log_title); +// log4Title.setText(R.string.exchange_4_log_title); // Log entries log1Content.setText(R.string.log_example); @@ -227,7 +270,7 @@ public class HomeFragment extends Fragment { updateRunnable = new Runnable() { @Override public void run() { - fetchAndDisplayPriceData(); + fetchAndDisplayData(); long delay = 5000; if (MainActivity.getGlobalSettings() != null) { delay = (long) MainActivity.getGlobalSettings().timeBetweenQueries * 1000; @@ -260,18 +303,13 @@ public class HomeFragment extends Fragment { } } - private void fetchAndDisplayPriceData() { + private void fetchAndDisplayData() { executorService.execute(() -> { try { - // Fetch price data in background - String ticker1 = getString(R.string.ticker_1); - String ticker2 = getString(R.string.ticker_2); - String ticker3 = getString(R.string.ticker_3); - // Fetch price data in background using CompletableFuture CompletableFuture future1 = CompletableFuture.supplyAsync(() -> { try { - return TickerTracker.getPriceChanges(ticker1); + return TickerTracker.getPriceChanges(getString(R.string.ticker_1)); } catch (IOException e) { e.printStackTrace(); return null; @@ -280,7 +318,7 @@ public class HomeFragment extends Fragment { CompletableFuture future2 = CompletableFuture.supplyAsync(() -> { try { - return TickerTracker.getPriceChanges(ticker2); + return TickerTracker.getPriceChanges(getString(R.string.ticker_2)); } catch (IOException e) { e.printStackTrace(); return null; @@ -289,7 +327,7 @@ public class HomeFragment extends Fragment { CompletableFuture future3 = CompletableFuture.supplyAsync(() -> { try { - return TickerTracker.getPriceChanges(ticker3); + return TickerTracker.getPriceChanges(getString(R.string.ticker_3)); } catch (IOException e) { e.printStackTrace(); return null; @@ -300,42 +338,39 @@ public class HomeFragment extends Fragment { CompletableFuture allFutures = CompletableFuture.allOf(future1, future2, future3); + // Update UI + try { + TickerTracker.PriceChangeData priceData = future1.get(); + TickerTracker.PriceChangeData priceData2 = future2.get(); + TickerTracker.PriceChangeData priceData3 = future3.get();; - // Update UI on the main thread - requireActivity().runOnUiThread(() -> { - 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())); + pricePair124hPercentage.setText(formatPercentage(priceData.getPriceChangePercent24h())); + pricePair17dPercentage.setText(formatPercentage(priceData.getPriceChangePercent7d())); + pricePair130dPercentage.setText(formatPercentage(priceData.getPriceChangePercent30d())); + setPercentageColor(pricePair124hPercentage,priceData.getPriceChangePercent24h()); + setPercentageColor(pricePair17dPercentage,priceData.getPriceChangePercent7d()); + setPercentageColor(pricePair130dPercentage,priceData.getPriceChangePercent30d()); - pricePair1.setText(String.format(Locale.ROOT,"%.2f", priceData.getCurrentPrice())); - pricePair124hPercentage.setText(formatPercentage(priceData.getPriceChangePercent24h())); - pricePair17dPercentage.setText(formatPercentage(priceData.getPriceChangePercent7d())); - pricePair130dPercentage.setText(formatPercentage(priceData.getPriceChangePercent30d())); - setPercentageColor(pricePair124hPercentage,priceData.getPriceChangePercent24h()); - setPercentageColor(pricePair17dPercentage,priceData.getPriceChangePercent7d()); - setPercentageColor(pricePair130dPercentage,priceData.getPriceChangePercent30d()); + pricePair2.setText(String.format(Locale.ROOT,"%.2f", priceData2.getCurrentPrice())); + pricePair224hPercentage.setText(formatPercentage(priceData2.getPriceChangePercent24h())); + pricePair27dPercentage.setText(formatPercentage(priceData2.getPriceChangePercent7d())); + pricePair230dPercentage.setText(formatPercentage(priceData2.getPriceChangePercent30d())); + setPercentageColor(pricePair224hPercentage,priceData2.getPriceChangePercent24h()); + setPercentageColor(pricePair27dPercentage,priceData2.getPriceChangePercent7d()); + setPercentageColor(pricePair230dPercentage,priceData2.getPriceChangePercent30d()); - pricePair2.setText(String.format(Locale.ROOT,"%.2f", priceData2.getCurrentPrice())); - pricePair224hPercentage.setText(formatPercentage(priceData2.getPriceChangePercent24h())); - pricePair27dPercentage.setText(formatPercentage(priceData2.getPriceChangePercent7d())); - pricePair230dPercentage.setText(formatPercentage(priceData2.getPriceChangePercent30d())); - setPercentageColor(pricePair224hPercentage,priceData2.getPriceChangePercent24h()); - setPercentageColor(pricePair27dPercentage,priceData2.getPriceChangePercent7d()); - setPercentageColor(pricePair230dPercentage,priceData2.getPriceChangePercent30d()); + pricePair3.setText(String.format(Locale.ROOT,"%.2f", priceData3.getCurrentPrice())); + pricePair324hPercentage.setText(formatPercentage(priceData3.getPriceChangePercent24h())); + pricePair37dPercentage.setText(formatPercentage(priceData3.getPriceChangePercent7d())); + pricePair330dPercentage.setText(formatPercentage(priceData3.getPriceChangePercent30d())); + setPercentageColor(pricePair324hPercentage,priceData3.getPriceChangePercent24h()); + setPercentageColor(pricePair37dPercentage,priceData3.getPriceChangePercent7d()); + setPercentageColor(pricePair330dPercentage,priceData3.getPriceChangePercent30d()); - pricePair3.setText(String.format(Locale.ROOT,"%.2f", priceData3.getCurrentPrice())); - pricePair324hPercentage.setText(formatPercentage(priceData3.getPriceChangePercent24h())); - pricePair37dPercentage.setText(formatPercentage(priceData3.getPriceChangePercent7d())); - pricePair330dPercentage.setText(formatPercentage(priceData3.getPriceChangePercent30d())); - setPercentageColor(pricePair324hPercentage,priceData3.getPriceChangePercent24h()); - setPercentageColor(pricePair37dPercentage,priceData3.getPriceChangePercent7d()); - setPercentageColor(pricePair330dPercentage,priceData3.getPriceChangePercent30d()); - - } catch (Exception e) { - Toast.makeText(getContext(), "Error updating UI", Toast.LENGTH_SHORT).show(); - } - }); + } catch (Exception e) { + Toast.makeText(getContext(), "Error updating UI", Toast.LENGTH_SHORT).show(); + } } catch (Exception e) { requireActivity().runOnUiThread(() -> Toast.makeText(getContext(), "Failed to fetch price data. Check your connection.", Toast.LENGTH_SHORT).show()