ticker first approach
This commit is contained in:
parent
948430aaad
commit
3a4624b6bb
|
|
@ -1,6 +1,6 @@
|
||||||
package com.example.dcav2gui;
|
package com.example.dcav2gui;
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
//import com.google.gson.Gson;
|
||||||
import com.google.gson.JsonArray;
|
import com.google.gson.JsonArray;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import com.google.gson.JsonParser;
|
import com.google.gson.JsonParser;
|
||||||
|
|
@ -14,7 +14,7 @@ import okhttp3.Response;
|
||||||
public class TickerTracker {
|
public class TickerTracker {
|
||||||
private static final String BINANCE_API_BASE_URL = "https://api.binance.com/api/v3/ticker/24hr";
|
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 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.
|
* Retrieves price change percentages for a specific trading pair from Binance.
|
||||||
|
|
@ -113,7 +113,7 @@ public class TickerTracker {
|
||||||
this.priceChangePercent30d = priceChangePercent30d;
|
this.priceChangePercent30d = priceChangePercent30d;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Getters (Just in case)
|
// Getters
|
||||||
public String getSymbol() { return symbol; }
|
public String getSymbol() { return symbol; }
|
||||||
public double getCurrentPrice() { return currentPrice; }
|
public double getCurrentPrice() { return currentPrice; }
|
||||||
public double getPriceChangePercent24h() { return priceChangePercent24h; }
|
public double getPriceChangePercent24h() { return priceChangePercent24h; }
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
package com.example.dcav2gui.ui.home;
|
package com.example.dcav2gui.ui.home;
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.os.Handler;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
|
|
@ -9,36 +11,58 @@ import android.widget.TextView;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.fragment.app.Fragment;
|
import androidx.fragment.app.Fragment;
|
||||||
|
import androidx.lifecycle.ViewModelProvider;
|
||||||
|
|
||||||
import com.example.dcav2gui.R;
|
import com.example.dcav2gui.R;
|
||||||
|
import com.example.dcav2gui.TickerTracker;
|
||||||
import com.example.dcav2gui.databinding.FragmentHomeBinding;
|
import com.example.dcav2gui.databinding.FragmentHomeBinding;
|
||||||
|
|
||||||
public class HomeFragment extends Fragment {
|
public class HomeFragment extends Fragment {
|
||||||
|
|
||||||
|
private HomeViewModel homeViewModel;
|
||||||
private FragmentHomeBinding binding;
|
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,
|
public View onCreateView(@NonNull LayoutInflater inflater,
|
||||||
ViewGroup container, Bundle savedInstanceState) {
|
ViewGroup container, Bundle savedInstanceState) {
|
||||||
|
|
||||||
binding = FragmentHomeBinding.inflate(inflater, container, false);
|
//binding = FragmentHomeBinding.inflate(inflater, container, false);
|
||||||
View root = binding.getRoot();
|
//View root = binding.getRoot();
|
||||||
|
|
||||||
|
homeViewModel = new ViewModelProvider(this).get(HomeViewModel.class);
|
||||||
|
View root = inflater.inflate(R.layout.fragment_home, container, false);
|
||||||
|
|
||||||
// Bind text views
|
// Bind text views
|
||||||
// Tickers
|
// Tickers
|
||||||
TextView pricePair1 = root.findViewById(R.id.pricesCardPrice1);
|
pricePair1 = root.findViewById(R.id.pricesCardPrice1);
|
||||||
TextView pricePair124hPercentage = root.findViewById(R.id.pricesCard24hText1);
|
pricePair124hPercentage = root.findViewById(R.id.pricesCard24hText1);
|
||||||
TextView pricePair17dPercentage = root.findViewById(R.id.pricesCard7dText1);
|
pricePair17dPercentage = root.findViewById(R.id.pricesCard7dText1);
|
||||||
TextView pricePair130dPercentage = root.findViewById(R.id.pricesCard30dText1);
|
pricePair130dPercentage = root.findViewById(R.id.pricesCard30dText1);
|
||||||
|
|
||||||
TextView pricePair2 = root.findViewById(R.id.pricesCardPrice2);
|
pricePair2 = root.findViewById(R.id.pricesCardPrice2);
|
||||||
TextView pricePair224hPercentage = root.findViewById(R.id.pricesCard24hText2);
|
pricePair224hPercentage = root.findViewById(R.id.pricesCard24hText2);
|
||||||
TextView pricePair27dPercentage = root.findViewById(R.id.pricesCard7dText2);
|
pricePair27dPercentage = root.findViewById(R.id.pricesCard7dText2);
|
||||||
TextView pricePair230dPercentage = root.findViewById(R.id.pricesCard30dText2);
|
pricePair230dPercentage = root.findViewById(R.id.pricesCard30dText2);
|
||||||
|
|
||||||
TextView pricePair3 = root.findViewById(R.id.pricesCardPrice3);
|
pricePair3 = root.findViewById(R.id.pricesCardPrice3);
|
||||||
TextView pricePair324hPercentage = root.findViewById(R.id.pricesCard24hText3);
|
pricePair324hPercentage = root.findViewById(R.id.pricesCard24hText3);
|
||||||
TextView pricePair37dPercentage = root.findViewById(R.id.pricesCard7dText3);
|
pricePair37dPercentage = root.findViewById(R.id.pricesCard7dText3);
|
||||||
TextView pricePair330dPercentage = root.findViewById(R.id.pricesCard30dText3);
|
pricePair330dPercentage = root.findViewById(R.id.pricesCard30dText3);
|
||||||
|
|
||||||
// Profits today and this month
|
// Profits today and this month
|
||||||
TextView profitsToday = root.findViewById(R.id.profitsTodayValue);
|
TextView profitsToday = root.findViewById(R.id.profitsTodayValue);
|
||||||
|
|
@ -112,6 +136,8 @@ public class HomeFragment extends Fragment {
|
||||||
pricePair37dPercentage.setText(R.string.percentage_example);
|
pricePair37dPercentage.setText(R.string.percentage_example);
|
||||||
pricePair330dPercentage.setText(R.string.percentage_example);
|
pricePair330dPercentage.setText(R.string.percentage_example);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Profits today and this month
|
// Profits today and this month
|
||||||
profitsToday.setText(R.string.profits_today_example);
|
profitsToday.setText(R.string.profits_today_example);
|
||||||
profitsThisMonth.setText(R.string.profits_this_month_example);
|
profitsThisMonth.setText(R.string.profits_this_month_example);
|
||||||
|
|
@ -167,12 +193,60 @@ public class HomeFragment extends Fragment {
|
||||||
log3Content.setText(R.string.log_example);
|
log3Content.setText(R.string.log_example);
|
||||||
log4Content.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;
|
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
|
@Override
|
||||||
public void onDestroyView() {
|
public void onDestroyView() {
|
||||||
super.onDestroyView();
|
super.onDestroyView();
|
||||||
|
handler.removeCallbacks(runnable);
|
||||||
binding = null;
|
binding = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue