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.fragment
|
||||||
implementation libs.navigation.ui
|
implementation libs.navigation.ui
|
||||||
implementation libs.gson
|
implementation libs.gson
|
||||||
implementation libs.okhttp
|
//implementation libs.okhttp
|
||||||
|
implementation libs.okhttp.v492
|
||||||
implementation libs.firebase.crashlytics.buildtools
|
implementation libs.firebase.crashlytics.buildtools
|
||||||
testImplementation libs.junit
|
testImplementation libs.junit
|
||||||
androidTestImplementation libs.ext.junit
|
androidTestImplementation libs.ext.junit
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,7 @@ public class MainActivity extends AppCompatActivity {
|
||||||
|
|
||||||
private AppBarConfiguration mAppBarConfiguration;
|
private AppBarConfiguration mAppBarConfiguration;
|
||||||
private ActivityMainBinding binding;
|
private ActivityMainBinding binding;
|
||||||
private static SettingsData globalSettings;
|
public static SettingsData globalSettings;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
package com.example.dcav2gui.ui.home;
|
package com.example.dcav2gui.ui.home;
|
||||||
|
|
||||||
|
import static com.example.dcav2gui.MainActivity.globalSettings;
|
||||||
|
|
||||||
import android.annotation.SuppressLint;
|
import android.annotation.SuppressLint;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
|
|
@ -14,9 +16,13 @@ import androidx.annotation.NonNull;
|
||||||
import androidx.fragment.app.Fragment;
|
import androidx.fragment.app.Fragment;
|
||||||
import androidx.lifecycle.ViewModelProvider;
|
import androidx.lifecycle.ViewModelProvider;
|
||||||
|
|
||||||
|
import com.example.dcav2gui.MainActivity;
|
||||||
import com.example.dcav2gui.R;
|
import com.example.dcav2gui.R;
|
||||||
import com.example.dcav2gui.TickerTracker;
|
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;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
|
@ -24,8 +30,10 @@ public class HomeFragment extends Fragment {
|
||||||
|
|
||||||
private HomeViewModel homeViewModel;
|
private HomeViewModel homeViewModel;
|
||||||
//private FragmentHomeBinding binding;
|
//private FragmentHomeBinding binding;
|
||||||
private Handler handler;
|
private final ExecutorService executorService = Executors.newSingleThreadExecutor();
|
||||||
private Runnable runnable;
|
|
||||||
|
private Handler handler = new Handler();
|
||||||
|
private Runnable updateRunnable;
|
||||||
|
|
||||||
private TextView pricePair1;
|
private TextView pricePair1;
|
||||||
private TextView pricePair124hPercentage ;
|
private TextView pricePair124hPercentage ;
|
||||||
|
|
@ -47,6 +55,7 @@ public class HomeFragment extends Fragment {
|
||||||
//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);
|
homeViewModel = new ViewModelProvider(this).get(HomeViewModel.class);
|
||||||
View root = inflater.inflate(R.layout.fragment_home, container, false);
|
View root = inflater.inflate(R.layout.fragment_home, container, false);
|
||||||
|
|
||||||
|
|
@ -201,48 +210,62 @@ public class HomeFragment extends Fragment {
|
||||||
|
|
||||||
// Setup task
|
// Setup task
|
||||||
handler = new Handler();
|
handler = new Handler();
|
||||||
runnable = new Runnable() {
|
updateRunnable = new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
fetchAndDisplayPriceData();
|
fetchAndDisplayPriceData();
|
||||||
handler.postDelayed(this, 5000);
|
long delay = (long) MainActivity.getGlobalSettings().timeBetweenQueries * 1000;
|
||||||
|
handler.postDelayed(this, delay);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
handler.post(runnable);
|
handler.post(updateRunnable);
|
||||||
|
|
||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressLint("DefaultLocale")
|
@SuppressLint("DefaultLocale")
|
||||||
private void fetchAndDisplayPriceData() {
|
private void fetchAndDisplayPriceData() {
|
||||||
try {
|
executorService.execute(() -> {
|
||||||
TickerTracker.PriceChangeData priceData = TickerTracker.getPriceChanges( "BTCUSDT");
|
try {
|
||||||
pricePair1.setText(String.format("%.2f", priceData.getCurrentPrice()));
|
// Fetch price data in background
|
||||||
pricePair124hPercentage.setText(String.format("%.2f%%", priceData.getPriceChangePercent24h()));
|
TickerTracker.PriceChangeData priceData = TickerTracker.getPriceChanges("BTCUSDT");
|
||||||
pricePair17dPercentage.setText(String.format("%.2f%%", priceData.getPriceChangePercent7d()));
|
TickerTracker.PriceChangeData priceData2 = TickerTracker.getPriceChanges("ETHUSDT");
|
||||||
pricePair130dPercentage.setText(String.format("%.2f%%", priceData.getPriceChangePercent30d()));
|
TickerTracker.PriceChangeData priceData3 = TickerTracker.getPriceChanges("USDTARS");
|
||||||
|
|
||||||
TickerTracker.PriceChangeData priceData2 = TickerTracker.getPriceChanges("ETHUSDT");
|
// Update UI on the main thread
|
||||||
pricePair2.setText(String.format("%.2f", priceData2.getCurrentPrice()));
|
requireActivity().runOnUiThread(() -> {
|
||||||
pricePair224hPercentage.setText(String.format("%.2f%%", priceData2.getPriceChangePercent24h()));
|
try {
|
||||||
pricePair27dPercentage.setText(String.format("%.2f%%", priceData2.getPriceChangePercent7d()));
|
pricePair1.setText(String.format("%.2f", priceData.getCurrentPrice()));
|
||||||
pricePair230dPercentage.setText(String.format("%.2f%%", priceData2.getPriceChangePercent30d()));
|
pricePair124hPercentage.setText(String.format("%.2f%%", priceData.getPriceChangePercent24h()));
|
||||||
|
pricePair17dPercentage.setText(String.format("%.2f%%", priceData.getPriceChangePercent7d()));
|
||||||
|
pricePair130dPercentage.setText(String.format("%.2f%%", priceData.getPriceChangePercent30d()));
|
||||||
|
|
||||||
TickerTracker.PriceChangeData priceData3 = TickerTracker.getPriceChanges("USDTARS");
|
pricePair2.setText(String.format("%.2f", priceData2.getCurrentPrice()));
|
||||||
pricePair3.setText(String.format("%.2f", priceData3.getCurrentPrice()));
|
pricePair224hPercentage.setText(String.format("%.2f%%", priceData2.getPriceChangePercent24h()));
|
||||||
pricePair324hPercentage.setText(String.format("%.2f%%", priceData3.getPriceChangePercent24h()));
|
pricePair27dPercentage.setText(String.format("%.2f%%", priceData2.getPriceChangePercent7d()));
|
||||||
pricePair37dPercentage.setText(String.format("%.2f%%", priceData3.getPriceChangePercent7d()));
|
pricePair230dPercentage.setText(String.format("%.2f%%", priceData2.getPriceChangePercent30d()));
|
||||||
pricePair330dPercentage.setText(String.format("%.2f%%", priceData3.getPriceChangePercent30d()));
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
pricePair3.setText(String.format("%.2f", priceData3.getCurrentPrice()));
|
||||||
e.printStackTrace();
|
pricePair324hPercentage.setText(String.format("%.2f%%", priceData3.getPriceChangePercent24h()));
|
||||||
}
|
pricePair37dPercentage.setText(String.format("%.2f%%", priceData3.getPriceChangePercent7d()));
|
||||||
|
pricePair330dPercentage.setText(String.format("%.2f%%", priceData3.getPriceChangePercent30d()));
|
||||||
|
} catch (Exception e) {
|
||||||
|
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
|
@Override
|
||||||
public void onDestroyView() {
|
public void onDestroyView() {
|
||||||
super.onDestroyView();
|
super.onDestroyView();
|
||||||
handler.removeCallbacks(runnable);
|
handler.removeCallbacks(updateRunnable);
|
||||||
|
//executorService.shutdownNow();
|
||||||
//binding = null;
|
//binding = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -13,6 +13,7 @@ navigationUi = "2.8.4"
|
||||||
gson = "2.11.0"
|
gson = "2.11.0"
|
||||||
firebaseCrashlyticsBuildtools = "3.0.2"
|
firebaseCrashlyticsBuildtools = "3.0.2"
|
||||||
okhttp = "3.14.6"
|
okhttp = "3.14.6"
|
||||||
|
okhttpVersion = "4.9.2"
|
||||||
|
|
||||||
[libraries]
|
[libraries]
|
||||||
junit = { group = "junit", name = "junit", version.ref = "junit" }
|
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" }
|
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" }
|
firebase-crashlytics-buildtools = { group = "com.google.firebase", name = "firebase-crashlytics-buildtools", version.ref = "firebaseCrashlyticsBuildtools" }
|
||||||
okhttp = { module = "com.squareup.okhttp3:okhttp", version.ref = "okhttp" }
|
okhttp = { module = "com.squareup.okhttp3:okhttp", version.ref = "okhttp" }
|
||||||
|
okhttp-v492 = { module = "com.squareup.okhttp3:okhttp", version.ref = "okhttpVersion" }
|
||||||
|
|
||||||
[plugins]
|
[plugins]
|
||||||
android-application = { id = "com.android.application", version.ref = "agp" }
|
android-application = { id = "com.android.application", version.ref = "agp" }
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue