mostly working ticker prices
This commit is contained in:
parent
1f9f8cee81
commit
04d179425f
|
|
@ -41,7 +41,8 @@ dependencies {
|
|||
implementation libs.navigation.fragment
|
||||
implementation libs.navigation.ui
|
||||
implementation libs.gson
|
||||
implementation libs.okhttp
|
||||
//implementation libs.okhttp
|
||||
implementation libs.okhttp.v492
|
||||
implementation libs.firebase.crashlytics.buildtools
|
||||
testImplementation libs.junit
|
||||
androidTestImplementation libs.ext.junit
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ public class MainActivity extends AppCompatActivity {
|
|||
|
||||
private AppBarConfiguration mAppBarConfiguration;
|
||||
private ActivityMainBinding binding;
|
||||
private static SettingsData globalSettings;
|
||||
public static SettingsData globalSettings;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
package com.example.dcav2gui.ui.home;
|
||||
|
||||
import static com.example.dcav2gui.MainActivity.globalSettings;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
|
|
@ -14,9 +16,13 @@ import androidx.annotation.NonNull;
|
|||
import androidx.fragment.app.Fragment;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
|
||||
import com.example.dcav2gui.MainActivity;
|
||||
import com.example.dcav2gui.R;
|
||||
import com.example.dcav2gui.TickerTracker;
|
||||
import com.example.dcav2gui.databinding.FragmentHomeBinding;
|
||||
import com.example.dcav2gui.ui.settings.SettingsData;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
|
|
@ -24,8 +30,10 @@ public class HomeFragment extends Fragment {
|
|||
|
||||
private HomeViewModel homeViewModel;
|
||||
//private FragmentHomeBinding binding;
|
||||
private Handler handler;
|
||||
private Runnable runnable;
|
||||
private final ExecutorService executorService = Executors.newSingleThreadExecutor();
|
||||
|
||||
private Handler handler = new Handler();
|
||||
private Runnable updateRunnable;
|
||||
|
||||
private TextView pricePair1;
|
||||
private TextView pricePair124hPercentage ;
|
||||
|
|
@ -47,6 +55,7 @@ public class HomeFragment extends Fragment {
|
|||
//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);
|
||||
|
||||
|
|
@ -201,48 +210,62 @@ public class HomeFragment extends Fragment {
|
|||
|
||||
// Setup task
|
||||
handler = new Handler();
|
||||
runnable = new Runnable() {
|
||||
updateRunnable = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
fetchAndDisplayPriceData();
|
||||
handler.postDelayed(this, 5000);
|
||||
long delay = (long) MainActivity.getGlobalSettings().timeBetweenQueries * 1000;
|
||||
handler.postDelayed(this, delay);
|
||||
}
|
||||
};
|
||||
handler.post(runnable);
|
||||
handler.post(updateRunnable);
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
@SuppressLint("DefaultLocale")
|
||||
private void fetchAndDisplayPriceData() {
|
||||
executorService.execute(() -> {
|
||||
try {
|
||||
// Fetch price data in background
|
||||
TickerTracker.PriceChangeData priceData = TickerTracker.getPriceChanges("BTCUSDT");
|
||||
TickerTracker.PriceChangeData priceData2 = TickerTracker.getPriceChanges("ETHUSDT");
|
||||
TickerTracker.PriceChangeData priceData3 = TickerTracker.getPriceChanges("USDTARS");
|
||||
|
||||
// Update UI on the main thread
|
||||
requireActivity().runOnUiThread(() -> {
|
||||
try {
|
||||
TickerTracker.PriceChangeData priceData = TickerTracker.getPriceChanges( "BTCUSDT");
|
||||
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("ETHUSDT");
|
||||
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("USDTARS");
|
||||
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();
|
||||
Toast.makeText(getContext(), "Error updating UI", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
});
|
||||
} catch (IOException e) {
|
||||
requireActivity().runOnUiThread(() ->
|
||||
Toast.makeText(getContext(), "Failed to fetch price data. Check your connection.", Toast.LENGTH_SHORT).show()
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onDestroyView() {
|
||||
super.onDestroyView();
|
||||
handler.removeCallbacks(runnable);
|
||||
handler.removeCallbacks(updateRunnable);
|
||||
//executorService.shutdownNow();
|
||||
//binding = null;
|
||||
}
|
||||
}
|
||||
|
|
@ -13,6 +13,7 @@ navigationUi = "2.8.4"
|
|||
gson = "2.11.0"
|
||||
firebaseCrashlyticsBuildtools = "3.0.2"
|
||||
okhttp = "3.14.6"
|
||||
okhttpVersion = "4.9.2"
|
||||
|
||||
[libraries]
|
||||
junit = { group = "junit", name = "junit", version.ref = "junit" }
|
||||
|
|
@ -28,6 +29,7 @@ navigation-ui = { group = "androidx.navigation", name = "navigation-ui", version
|
|||
gson = { group = "com.google.code.gson", name = "gson", version.ref = "gson" }
|
||||
firebase-crashlytics-buildtools = { group = "com.google.firebase", name = "firebase-crashlytics-buildtools", version.ref = "firebaseCrashlyticsBuildtools" }
|
||||
okhttp = { module = "com.squareup.okhttp3:okhttp", version.ref = "okhttp" }
|
||||
okhttp-v492 = { module = "com.squareup.okhttp3:okhttp", version.ref = "okhttpVersion" }
|
||||
|
||||
[plugins]
|
||||
android-application = { id = "com.android.application", version.ref = "agp" }
|
||||
|
|
|
|||
Loading…
Reference in New Issue