From a82ebd54a76486a33a00365be7a8925e32ae24e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20S=C3=A1nchez?= Date: Sun, 15 Dec 2024 10:31:45 -0300 Subject: [PATCH] Home page cache --- .../com/example/dcav2gui/MainActivity.java | 9 + .../dcav2gui/ui/home/HomeFragment.java | 198 +++++++++--------- 2 files changed, 106 insertions(+), 101 deletions(-) diff --git a/app/src/main/java/com/example/dcav2gui/MainActivity.java b/app/src/main/java/com/example/dcav2gui/MainActivity.java index 291bad0..694ea32 100644 --- a/app/src/main/java/com/example/dcav2gui/MainActivity.java +++ b/app/src/main/java/com/example/dcav2gui/MainActivity.java @@ -32,6 +32,15 @@ public class MainActivity extends AppCompatActivity { public static SettingsData globalSettings; public HomeFragment.HomeCache homeViewCache; + + public HomeFragment.HomeCache getHomeViewCache() { + return homeViewCache; + } + + public void setHomeViewCache(HomeFragment.HomeCache newCache) { + this.homeViewCache = newCache; + } + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); 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 8433bd7..ac542e4 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 @@ -99,12 +99,19 @@ public class HomeFragment extends Fragment { private TextView log4Content; + + + + public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //binding = FragmentHomeBinding.inflate(inflater, container, false); //View root = binding.getRoot(); + // For the cache + MainActivity mainActivity = (MainActivity) requireActivity(); + HomeFragment.HomeCache homeViewCache = mainActivity.getHomeViewCache(); homeViewModel = new ViewModelProvider(this).get(HomeViewModel.class); View root = inflater.inflate(R.layout.fragment_home, container, false); @@ -164,12 +171,7 @@ public class HomeFragment extends Fragment { // Last deals 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); - + // Logs log1Content = root.findViewById(R.id.log1CardContent); log2Content = root.findViewById(R.id.log2CardContent); log3Content = root.findViewById(R.id.log3CardContent); @@ -177,95 +179,96 @@ public class HomeFragment extends Fragment { /* - * * LOAD CACHED VALUES - * */ - - //Prices - pricePair1.setText(R.string.default_price_ticker_1); - pricePair2.setText(R.string.default_price_ticker_2); - pricePair3.setText(R.string.default_price_ticker_3); + if (homeViewCache!=null) { + populateHome(homeViewCache.getFirstTickerChangeData(), + homeViewCache.getSecondTickerChangeData(), + homeViewCache.getThirdTickerChangeData(), + homeViewCache.getProfitsData(), + homeViewCache.getBinanceData(), + homeViewCache.getGateioData(), + homeViewCache.getKucoinData(), + homeViewCache.getOkexData(), + homeViewCache.getDealData(), + homeViewCache.getBinanceLogs(), + homeViewCache.getGateioLogs(), + homeViewCache.getKucoinLogs(), + homeViewCache.getOkexLogs()); + } else { + //Prices + pricePair1.setText(R.string.default_price_ticker_1); + pricePair2.setText(R.string.default_price_ticker_2); + pricePair3.setText(R.string.default_price_ticker_3); - pricePair124hPercentage.setText(R.string.percentage_example); - pricePair17dPercentage.setText(R.string.percentage_example); - pricePair130dPercentage.setText(R.string.percentage_example); + pricePair124hPercentage.setText(R.string.percentage_example); + pricePair17dPercentage.setText(R.string.percentage_example); + pricePair130dPercentage.setText(R.string.percentage_example); - pricePair224hPercentage.setText(R.string.percentage_example); - pricePair27dPercentage.setText(R.string.percentage_example); - pricePair230dPercentage.setText(R.string.percentage_example); + pricePair224hPercentage.setText(R.string.percentage_example); + pricePair27dPercentage.setText(R.string.percentage_example); + pricePair230dPercentage.setText(R.string.percentage_example); - pricePair324hPercentage.setText(R.string.percentage_example); - pricePair37dPercentage.setText(R.string.percentage_example); - pricePair330dPercentage.setText(R.string.percentage_example); + pricePair324hPercentage.setText(R.string.percentage_example); + pricePair37dPercentage.setText(R.string.percentage_example); + pricePair330dPercentage.setText(R.string.percentage_example); - pricePair124hPercentage.setTextColor(Color.GREEN); - pricePair17dPercentage.setTextColor(Color.GREEN); - pricePair130dPercentage.setTextColor(Color.GREEN); + pricePair124hPercentage.setTextColor(Color.GREEN); + pricePair17dPercentage.setTextColor(Color.GREEN); + pricePair130dPercentage.setTextColor(Color.GREEN); - pricePair224hPercentage.setTextColor(Color.GREEN); - pricePair27dPercentage.setTextColor(Color.GREEN); - pricePair230dPercentage.setTextColor(Color.GREEN); + pricePair224hPercentage.setTextColor(Color.GREEN); + pricePair27dPercentage.setTextColor(Color.GREEN); + pricePair230dPercentage.setTextColor(Color.GREEN); - pricePair324hPercentage.setTextColor(Color.GREEN); - pricePair37dPercentage.setTextColor(Color.GREEN); - pricePair330dPercentage.setTextColor(Color.GREEN); + pricePair324hPercentage.setTextColor(Color.GREEN); + pricePair37dPercentage.setTextColor(Color.GREEN); + pricePair330dPercentage.setTextColor(Color.GREEN); - // Profits today and this month - profitsToday.setText(R.string.profits_today_example); - profitsThisMonth.setText(R.string.profits_this_month_example); + // Profits today and this month + profitsToday.setText(R.string.profits_today_example); + profitsThisMonth.setText(R.string.profits_this_month_example); - //Exchange status - exchange1Status.setImageResource(R.drawable.ic_green_circle_48); - exchange2Status.setImageResource(R.drawable.ic_green_circle_48); - exchange3Status.setImageResource(R.drawable.ic_green_circle_48); - exchange4Status.setImageResource(R.drawable.ic_green_circle_48); + //Exchange status + exchange1Status.setImageResource(R.drawable.ic_green_circle_48); + exchange2Status.setImageResource(R.drawable.ic_green_circle_48); + exchange3Status.setImageResource(R.drawable.ic_green_circle_48); + exchange4Status.setImageResource(R.drawable.ic_green_circle_48); -// exchange1Name.setText(R.string.exchange_1_name); -// exchange2Name.setText(R.string.exchange_2_name); -// exchange3Name.setText(R.string.exchange_3_name); -// exchange4Name.setText(R.string.exchange_4_name); + exchange1Funds.setText(R.string.exchange_funds_example); + exchange2Funds.setText(R.string.exchange_funds_example); + exchange3Funds.setText(R.string.exchange_funds_example); + exchange4Funds.setText(R.string.exchange_funds_example); - exchange1Funds.setText(R.string.exchange_funds_example); - exchange2Funds.setText(R.string.exchange_funds_example); - exchange3Funds.setText(R.string.exchange_funds_example); - exchange4Funds.setText(R.string.exchange_funds_example); + exchange1FundsPercentage.setText(R.string.exchange_funds_percentage_example); + exchange2FundsPercentage.setText(R.string.exchange_funds_percentage_example); + exchange3FundsPercentage.setText(R.string.exchange_funds_percentage_example); + exchange4FundsPercentage.setText(R.string.exchange_funds_percentage_example); - exchange1FundsPercentage.setText(R.string.exchange_funds_percentage_example); - exchange2FundsPercentage.setText(R.string.exchange_funds_percentage_example); - exchange3FundsPercentage.setText(R.string.exchange_funds_percentage_example); - exchange4FundsPercentage.setText(R.string.exchange_funds_percentage_example); + exchange1WorkersOnline.setText(R.string.exchange_workers_online_example); + exchange2WorkersOnline.setText(R.string.exchange_workers_online_example); + exchange3WorkersOnline.setText(R.string.exchange_workers_online_example); + exchange4WorkersOnline.setText(R.string.exchange_workers_online_example); - exchange1WorkersOnline.setText(R.string.exchange_workers_online_example); - exchange2WorkersOnline.setText(R.string.exchange_workers_online_example); - exchange3WorkersOnline.setText(R.string.exchange_workers_online_example); - exchange4WorkersOnline.setText(R.string.exchange_workers_online_example); + exchange1WorkersLongShort.setText(R.string.exchange_workers_long_short_example); + exchange2WorkersLongShort.setText(R.string.exchange_workers_long_short_example); + exchange3WorkersLongShort.setText(R.string.exchange_workers_long_short_example); + exchange4WorkersLongShort.setText(R.string.exchange_workers_long_short_example); - exchange1WorkersLongShort.setText(R.string.exchange_workers_long_short_example); - exchange2WorkersLongShort.setText(R.string.exchange_workers_long_short_example); - exchange3WorkersLongShort.setText(R.string.exchange_workers_long_short_example); - exchange4WorkersLongShort.setText(R.string.exchange_workers_long_short_example); - - // Last trades - 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); - - // Log entries - log1Content.setText(R.string.log_example); - log2Content.setText(R.string.log_example); - log3Content.setText(R.string.log_example); - log4Content.setText(R.string.log_example); + // Last trades + lastTrades.setText(R.string.last_trades_example); + // Log entries + log1Content.setText(R.string.log_example); + log2Content.setText(R.string.log_example); + log3Content.setText(R.string.log_example); + log4Content.setText(R.string.log_example); + } // Setup task handler = new Handler(); @@ -306,6 +309,10 @@ public class HomeFragment extends Fragment { } private void fetchAndDisplayData() { + // For the cache + MainActivity mainActivity = (MainActivity) requireActivity(); + //HomeFragment.HomeCache homeViewCache = mainActivity.getHomeViewCache(); + executorService.execute(() -> { try { CompletableFuture future1 = CompletableFuture.supplyAsync(() -> { @@ -475,6 +482,9 @@ public class HomeFragment extends Fragment { } populateHome(priceData, priceData2, priceData3, profitsData, binanceStats, gateioStats, kucoinStats, okexStats, deals, logs1, logs2, logs3, logs4); + mainActivity.setHomeViewCache( + new HomeCache (priceData, priceData2, priceData3, profitsData, binanceStats, gateioStats, kucoinStats, okexStats, deals, logs1, logs2, logs3, logs4) + ); }); @@ -694,19 +704,19 @@ public class HomeFragment extends Fragment { public static class HomeCache { - private TickerTracker.PriceChangeData firstTickerChangeData; - private TickerTracker.PriceChangeData secondTickerChangeData; - private TickerTracker.PriceChangeData thirdTickerChangeData; - private InstanceInterface.ProfitStatsData profitsData; - private InstanceInterface.ExchangeStatsData binanceData; - private InstanceInterface.ExchangeStatsData gateioData; - private InstanceInterface.ExchangeStatsData kucoinData; - private InstanceInterface.ExchangeStatsData okexData; - private List dealData; - private String binanceLogs; - private String gateioLogs; - private String kucoinLogs; - private String okexLogs; + private final TickerTracker.PriceChangeData firstTickerChangeData; + private final TickerTracker.PriceChangeData secondTickerChangeData; + private final TickerTracker.PriceChangeData thirdTickerChangeData; + private final InstanceInterface.ProfitStatsData profitsData; + private final InstanceInterface.ExchangeStatsData binanceData; + private final InstanceInterface.ExchangeStatsData gateioData; + private final InstanceInterface.ExchangeStatsData kucoinData; + private final InstanceInterface.ExchangeStatsData okexData; + private final List dealData; + private final String binanceLogs; + private final String gateioLogs; + private final String kucoinLogs; + private final String okexLogs; public HomeCache(TickerTracker.PriceChangeData firstTickerChangeData, TickerTracker.PriceChangeData secondTickerChangeData, TickerTracker.PriceChangeData thirdTickerChangeData, InstanceInterface.ProfitStatsData profitsData, InstanceInterface.ExchangeStatsData binanceData, InstanceInterface.ExchangeStatsData gateioData, InstanceInterface.ExchangeStatsData kucoinData, InstanceInterface.ExchangeStatsData okexData, List dealData, String binanceLogs, String gateioLogs, String kucoinLogs, String okexLogs) { this.firstTickerChangeData = firstTickerChangeData; @@ -724,20 +734,6 @@ public class HomeFragment extends Fragment { this.okexLogs = okexLogs; } - public void setFirstTickerChangeData(TickerTracker.PriceChangeData firstTickerChangeData) { this.firstTickerChangeData = firstTickerChangeData; } - public void setSecondTickerChangeData(TickerTracker.PriceChangeData secondTickerChangeData) { this.secondTickerChangeData = secondTickerChangeData; } - public void setThirdTickerChangeData(TickerTracker.PriceChangeData thirdTickerChangeData) { this.thirdTickerChangeData = thirdTickerChangeData; } - public void setProfitsData(InstanceInterface.ProfitStatsData profitsData) { this.profitsData = profitsData; } - public void setBinanceData(InstanceInterface.ExchangeStatsData binanceData) { this.binanceData = binanceData; } - public void setGateioData(InstanceInterface.ExchangeStatsData gateioData) { this.gateioData = gateioData; } - public void setKucoinData(InstanceInterface.ExchangeStatsData kucoinData) { this.kucoinData = kucoinData; } - public void setOkexData(InstanceInterface.ExchangeStatsData okexData) { this.okexData = okexData; } - public void setDealData(List dealData) { this.dealData = dealData; } - public void setBinanceLogs(String binanceLogs) { this.binanceLogs = binanceLogs; } - public void setGateioLogs(String gateioLogs) { this.gateioLogs = gateioLogs; } - public void setKucoinLogs(String kucoinLogs) { this.kucoinLogs = kucoinLogs; } - public void setOkexLogs(String okexLogs) { this.okexLogs = okexLogs; } - public TickerTracker.PriceChangeData getFirstTickerChangeData() { return firstTickerChangeData; } public TickerTracker.PriceChangeData getSecondTickerChangeData() { return secondTickerChangeData; } public TickerTracker.PriceChangeData getThirdTickerChangeData() { return thirdTickerChangeData; }