From 3a4624b6bbf9a83c5308b081b6f6aaba5dfa1300 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20S=C3=A1nchez?= Date: Thu, 12 Dec 2024 09:50:24 -0300 Subject: [PATCH] ticker first approach --- .../com/example/dcav2gui/TickerTracker.java | 6 +- .../dcav2gui/ui/home/HomeFragment.java | 102 +++++++++++++++--- 2 files changed, 91 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/com/example/dcav2gui/TickerTracker.java b/app/src/main/java/com/example/dcav2gui/TickerTracker.java index a9d4aa9..013978a 100644 --- a/app/src/main/java/com/example/dcav2gui/TickerTracker.java +++ b/app/src/main/java/com/example/dcav2gui/TickerTracker.java @@ -1,6 +1,6 @@ package com.example.dcav2gui; -import com.google.gson.Gson; +//import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonObject; import com.google.gson.JsonParser; @@ -14,7 +14,7 @@ import okhttp3.Response; public class TickerTracker { private static final String BINANCE_API_BASE_URL = "https://api.binance.com/api/v3/ticker/24hr"; private static final OkHttpClient httpClient = new OkHttpClient(); - private static final Gson gson = new Gson(); + //private static final Gson gson = new Gson(); /** * Retrieves price change percentages for a specific trading pair from Binance. @@ -113,7 +113,7 @@ public class TickerTracker { this.priceChangePercent30d = priceChangePercent30d; } - // Getters (Just in case) + // Getters public String getSymbol() { return symbol; } public double getCurrentPrice() { return currentPrice; } public double getPriceChangePercent24h() { return priceChangePercent24h; } 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 2bd4873..8808b3e 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,8 @@ package com.example.dcav2gui.ui.home; +import android.annotation.SuppressLint; import android.os.Bundle; +import android.os.Handler; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -9,36 +11,58 @@ import android.widget.TextView; import androidx.annotation.NonNull; import androidx.fragment.app.Fragment; +import androidx.lifecycle.ViewModelProvider; import com.example.dcav2gui.R; +import com.example.dcav2gui.TickerTracker; import com.example.dcav2gui.databinding.FragmentHomeBinding; public class HomeFragment extends Fragment { + private HomeViewModel homeViewModel; private FragmentHomeBinding binding; + private Handler handler; + private Runnable runnable; + + private TextView pricePair1; + private TextView pricePair124hPercentage ; + private TextView pricePair17dPercentage; + private TextView pricePair130dPercentage; + private TextView pricePair2; + private TextView pricePair224hPercentage; + private TextView pricePair27dPercentage; + private TextView pricePair230dPercentage; + private TextView pricePair3; + private TextView pricePair324hPercentage; + private TextView pricePair37dPercentage; + private TextView pricePair330dPercentage; + public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - binding = FragmentHomeBinding.inflate(inflater, container, false); - View root = binding.getRoot(); + //binding = FragmentHomeBinding.inflate(inflater, container, false); + //View root = binding.getRoot(); + + homeViewModel = new ViewModelProvider(this).get(HomeViewModel.class); + View root = inflater.inflate(R.layout.fragment_home, container, false); // Bind text views // Tickers - TextView pricePair1 = root.findViewById(R.id.pricesCardPrice1); - TextView pricePair124hPercentage = root.findViewById(R.id.pricesCard24hText1); - TextView pricePair17dPercentage = root.findViewById(R.id.pricesCard7dText1); - TextView pricePair130dPercentage = root.findViewById(R.id.pricesCard30dText1); + pricePair1 = root.findViewById(R.id.pricesCardPrice1); + pricePair124hPercentage = root.findViewById(R.id.pricesCard24hText1); + pricePair17dPercentage = root.findViewById(R.id.pricesCard7dText1); + pricePair130dPercentage = root.findViewById(R.id.pricesCard30dText1); - TextView pricePair2 = root.findViewById(R.id.pricesCardPrice2); - TextView pricePair224hPercentage = root.findViewById(R.id.pricesCard24hText2); - TextView pricePair27dPercentage = root.findViewById(R.id.pricesCard7dText2); - TextView pricePair230dPercentage = root.findViewById(R.id.pricesCard30dText2); + pricePair2 = root.findViewById(R.id.pricesCardPrice2); + pricePair224hPercentage = root.findViewById(R.id.pricesCard24hText2); + pricePair27dPercentage = root.findViewById(R.id.pricesCard7dText2); + pricePair230dPercentage = root.findViewById(R.id.pricesCard30dText2); - TextView pricePair3 = root.findViewById(R.id.pricesCardPrice3); - TextView pricePair324hPercentage = root.findViewById(R.id.pricesCard24hText3); - TextView pricePair37dPercentage = root.findViewById(R.id.pricesCard7dText3); - TextView pricePair330dPercentage = root.findViewById(R.id.pricesCard30dText3); + pricePair3 = root.findViewById(R.id.pricesCardPrice3); + pricePair324hPercentage = root.findViewById(R.id.pricesCard24hText3); + pricePair37dPercentage = root.findViewById(R.id.pricesCard7dText3); + pricePair330dPercentage = root.findViewById(R.id.pricesCard30dText3); // Profits today and this month TextView profitsToday = root.findViewById(R.id.profitsTodayValue); @@ -112,6 +136,8 @@ public class HomeFragment extends Fragment { pricePair37dPercentage.setText(R.string.percentage_example); pricePair330dPercentage.setText(R.string.percentage_example); + + // Profits today and this month profitsToday.setText(R.string.profits_today_example); profitsThisMonth.setText(R.string.profits_this_month_example); @@ -167,12 +193,60 @@ public class HomeFragment extends Fragment { log3Content.setText(R.string.log_example); log4Content.setText(R.string.log_example); + // Let's fetch prices + fetchAndDisplayPriceData(); + + // Setup task + handler = new Handler(); + runnable = new Runnable() { + @Override + public void run() { + fetchAndDisplayPriceData(); + handler.postDelayed(this, 5000); + } + }; + handler.post(runnable); + return root; } + @SuppressLint("DefaultLocale") + private void fetchAndDisplayPriceData() { + try { + String base_1 = "@string/base_price_ticker_1"; + String quote_1 = "@string/quote_price_ticker_1"; + String base_2 = "@string/base_price_ticker_2"; + String quote_2 = "@string/quote_price_ticker_2"; + String base_3 = "@string/base_price_ticker_3"; + String quote_3 = "@string/quote_price_ticker_3"; + + TickerTracker.PriceChangeData priceData = TickerTracker.getPriceChanges( base_1 + quote_1); + 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())); + + TickerTracker.PriceChangeData priceData2 = TickerTracker.getPriceChanges(base_2 + quote_2); + 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())); + + TickerTracker.PriceChangeData priceData3 = TickerTracker.getPriceChanges(base_3 + quote_3); + 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) { + e.printStackTrace(); + } + } + @Override public void onDestroyView() { super.onDestroyView(); + handler.removeCallbacks(runnable); binding = null; } } \ No newline at end of file