exchange section populated

This commit is contained in:
Nicolás Sánchez 2024-12-17 19:08:13 -03:00
parent 3b289c0273
commit 85ca15269c
13 changed files with 228 additions and 145 deletions

View File

@ -644,6 +644,14 @@ public class InstanceInterface {
workerDataList.add(workerData); workerDataList.add(workerData);
} }
} }
// Sort the list by uptime
workerDataList.sort(new Comparator<WorkerData>() {
@Override
public int compare(WorkerData w1, WorkerData w2) {
return Double.compare(w1.getUptime(), w2.getUptime());
}
});
return workerDataList; return workerDataList;
} }

View File

@ -8,13 +8,9 @@ import android.view.ViewGroup;
import androidx.annotation.NonNull; 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.R;
import com.example.dcav2gui.databinding.FragmentBinanceBinding; import com.example.dcav2gui.databinding.FragmentBinanceBinding;
import com.example.dcav2gui.ui.exchanges.adapters.WorkerCardAdapter; import com.example.dcav2gui.ui.exchanges.adapters.WorkerCardAdapter;
import com.example.dcav2gui.ui.exchanges.WorkerData;
import java.io.IOException;
import java.util.List;
public class BinanceFragment extends Fragment { public class BinanceFragment extends Fragment {

View File

@ -7,17 +7,17 @@ import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData; import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.MutableLiveData;
import com.example.dcav2gui.InstanceInterface; import com.example.dcav2gui.InstanceInterface;
import com.google.gson.JsonObject; import com.example.dcav2gui.MainActivity;
import com.example.dcav2gui.ui.settings.SettingsData;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import java.util.List;
public class BinanceViewModel extends AndroidViewModel { public class BinanceViewModel extends AndroidViewModel {
private MutableLiveData<List<WorkerData>> workerDataList; private final MutableLiveData<List<WorkerData>> workerDataList;
SettingsData settingsData = MainActivity.getGlobalSettings();
public BinanceViewModel(@NonNull Application application) { public BinanceViewModel(@NonNull Application application) {
super(application); super(application);
@ -30,21 +30,14 @@ public class BinanceViewModel extends AndroidViewModel {
if (workerDataList.getValue() == null) { if (workerDataList.getValue() == null) {
fetchWorkerData(); fetchWorkerData();
} }
//System.err.println(workerDataList.getValue().get(0).toString());
return workerDataList; return workerDataList;
} }
private void fetchWorkerData() { private void fetchWorkerData() {
// Simulate fetching data from a source
//List<WorkerData> data = new ArrayList<>();
// Add WorkerData objects to the list
//workerDataList.setValue(data);
// Schedule a periodic update
new Thread(() -> { new Thread(() -> {
while (true) { while (true) {
try { try {
Thread.sleep(5000); Thread.sleep((long) settingsData.timeBetweenQueries*1000);
// Fetch and update the data // Fetch and update the data
List<WorkerData> newData = InstanceInterface.fetchWorkersDataForCards("binance"); List<WorkerData> newData = InstanceInterface.fetchWorkersDataForCards("binance");
//System.err.println(newData.toString()); //System.err.println(newData.toString());

View File

@ -4,34 +4,35 @@ import android.os.Bundle;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
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 androidx.lifecycle.ViewModelProvider;
import com.example.dcav2gui.databinding.FragmentGateioBinding; import com.example.dcav2gui.databinding.FragmentGateioBinding;
import com.example.dcav2gui.ui.exchanges.adapters.WorkerCardAdapter;
public class GateioFragment extends Fragment { public class GateioFragment extends Fragment {
private FragmentGateioBinding binding; private FragmentGateioBinding binding;
private GateioViewModel GateioViewModel;
private WorkerCardAdapter workerCardAdapter;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) { ViewGroup container, Bundle savedInstanceState) {
GateioViewModel GateioViewModel =
new ViewModelProvider(this).get(GateioViewModel.class);
binding = FragmentGateioBinding.inflate(inflater, container, false); binding = FragmentGateioBinding.inflate(inflater, container, false);
View root = binding.getRoot(); View root = binding.getRoot();
final TextView textView = binding.textGateio; GateioViewModel = new ViewModelProvider(this).get(GateioViewModel.class);
GateioViewModel.getText().observe(getViewLifecycleOwner(), textView::setText);
workerCardAdapter = new WorkerCardAdapter(binding.gateioCardsContainer);
GateioViewModel.getWorkerData().observe(getViewLifecycleOwner(), workerDataList -> {
if (workerDataList != null) {
workerCardAdapter.updateData(workerDataList);
}
});
return root; return root;
} }
@Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
}
} }

View File

@ -1,19 +1,53 @@
package com.example.dcav2gui.ui.exchanges; package com.example.dcav2gui.ui.exchanges;
import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData; import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
public class GateioViewModel extends ViewModel { import com.example.dcav2gui.InstanceInterface;
import com.example.dcav2gui.MainActivity;
import com.example.dcav2gui.ui.settings.SettingsData;
private final MutableLiveData<String> mText; import java.io.IOException;
import java.util.List;
public GateioViewModel() { public class GateioViewModel extends AndroidViewModel {
mText = new MutableLiveData<>();
mText.setValue("This is Gate.io fragment"); private final MutableLiveData<List<WorkerData>> workerDataList;
SettingsData settingsData = MainActivity.getGlobalSettings();
public GateioViewModel(@NonNull Application application) {
super(application);
workerDataList = new MutableLiveData<>();
fetchWorkerData(); // Initial data fetch
} }
public LiveData<String> getText() { public LiveData<List<WorkerData>> getWorkerData() {
return mText; //Iterate through workerDataList
if (workerDataList.getValue() == null) {
fetchWorkerData();
}
return workerDataList;
} }
private void fetchWorkerData() {
new Thread(() -> {
while (true) {
try {
Thread.sleep((long) settingsData.timeBetweenQueries*1000);
// Fetch and update the data
List<WorkerData> newData = InstanceInterface.fetchWorkersDataForCards("gateio");
//System.err.println(newData.toString());
workerDataList.postValue(newData);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}).start();
}
} }

View File

@ -4,34 +4,35 @@ import android.os.Bundle;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
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 androidx.lifecycle.ViewModelProvider;
import com.example.dcav2gui.databinding.FragmentKucoinBinding; import com.example.dcav2gui.databinding.FragmentKucoinBinding;
import com.example.dcav2gui.ui.exchanges.adapters.WorkerCardAdapter;
public class KucoinFragment extends Fragment { public class KucoinFragment extends Fragment {
private FragmentKucoinBinding binding; private FragmentKucoinBinding binding;
private KucoinViewModel KucoinViewModel;
private WorkerCardAdapter workerCardAdapter;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) { ViewGroup container, Bundle savedInstanceState) {
KucoinViewModel KucoinViewModel =
new ViewModelProvider(this).get(KucoinViewModel.class);
binding = FragmentKucoinBinding.inflate(inflater, container, false); binding = FragmentKucoinBinding.inflate(inflater, container, false);
View root = binding.getRoot(); View root = binding.getRoot();
final TextView textView = binding.textKucoin; KucoinViewModel = new ViewModelProvider(this).get(KucoinViewModel.class);
KucoinViewModel.getText().observe(getViewLifecycleOwner(), textView::setText);
workerCardAdapter = new WorkerCardAdapter(binding.kucoinCardsContainer);
KucoinViewModel.getWorkerData().observe(getViewLifecycleOwner(), workerDataList -> {
if (workerDataList != null) {
workerCardAdapter.updateData(workerDataList);
}
});
return root; return root;
} }
@Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
}
} }

View File

@ -1,19 +1,53 @@
package com.example.dcav2gui.ui.exchanges; package com.example.dcav2gui.ui.exchanges;
import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData; import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
public class KucoinViewModel extends ViewModel { import com.example.dcav2gui.InstanceInterface;
import com.example.dcav2gui.MainActivity;
import com.example.dcav2gui.ui.settings.SettingsData;
private final MutableLiveData<String> mText; import java.io.IOException;
import java.util.List;
public KucoinViewModel() { public class KucoinViewModel extends AndroidViewModel {
mText = new MutableLiveData<>();
mText.setValue("This is KuCoin fragment"); private final MutableLiveData<List<WorkerData>> workerDataList;
SettingsData settingsData = MainActivity.getGlobalSettings();
public KucoinViewModel(@NonNull Application application) {
super(application);
workerDataList = new MutableLiveData<>();
fetchWorkerData(); // Initial data fetch
} }
public LiveData<String> getText() { public LiveData<List<WorkerData>> getWorkerData() {
return mText; //Iterate through workerDataList
if (workerDataList.getValue() == null) {
fetchWorkerData();
}
return workerDataList;
} }
private void fetchWorkerData() {
new Thread(() -> {
while (true) {
try {
Thread.sleep((long) settingsData.timeBetweenQueries*1000);
// Fetch and update the data
List<WorkerData> newData = InstanceInterface.fetchWorkersDataForCards("kucoin");
//System.err.println(newData.toString());
workerDataList.postValue(newData);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}).start();
}
} }

View File

@ -4,34 +4,35 @@ import android.os.Bundle;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
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 androidx.lifecycle.ViewModelProvider;
import com.example.dcav2gui.databinding.FragmentOkxBinding; import com.example.dcav2gui.databinding.FragmentOkxBinding;
import com.example.dcav2gui.ui.exchanges.adapters.WorkerCardAdapter;
public class OkxFragment extends Fragment { public class OkxFragment extends Fragment {
private FragmentOkxBinding binding; private FragmentOkxBinding binding;
private OkxViewModel OkxViewModel;
private WorkerCardAdapter workerCardAdapter;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) { ViewGroup container, Bundle savedInstanceState) {
OkxViewModel OkxViewModel =
new ViewModelProvider(this).get(OkxViewModel.class);
binding = FragmentOkxBinding.inflate(inflater, container, false); binding = FragmentOkxBinding.inflate(inflater, container, false);
View root = binding.getRoot(); View root = binding.getRoot();
final TextView textView = binding.textOkx; OkxViewModel = new ViewModelProvider(this).get(OkxViewModel.class);
OkxViewModel.getText().observe(getViewLifecycleOwner(), textView::setText);
workerCardAdapter = new WorkerCardAdapter(binding.okexCardsContainer);
OkxViewModel.getWorkerData().observe(getViewLifecycleOwner(), workerDataList -> {
if (workerDataList != null) {
workerCardAdapter.updateData(workerDataList);
}
});
return root; return root;
} }
@Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
}
} }

View File

@ -1,19 +1,53 @@
package com.example.dcav2gui.ui.exchanges; package com.example.dcav2gui.ui.exchanges;
import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData; import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
public class OkxViewModel extends ViewModel { import com.example.dcav2gui.InstanceInterface;
import com.example.dcav2gui.MainActivity;
import com.example.dcav2gui.ui.settings.SettingsData;
private final MutableLiveData<String> mText; import java.io.IOException;
import java.util.List;
public OkxViewModel() { public class OkxViewModel extends AndroidViewModel {
mText = new MutableLiveData<>();
mText.setValue("This is OKX fragment"); private final MutableLiveData<List<WorkerData>> workerDataList;
SettingsData settingsData = MainActivity.getGlobalSettings();
public OkxViewModel(@NonNull Application application) {
super(application);
workerDataList = new MutableLiveData<>();
fetchWorkerData(); // Initial data fetch
} }
public LiveData<String> getText() { public LiveData<List<WorkerData>> getWorkerData() {
return mText; //Iterate through workerDataList
if (workerDataList.getValue() == null) {
fetchWorkerData();
}
return workerDataList;
} }
private void fetchWorkerData() {
new Thread(() -> {
while (true) {
try {
Thread.sleep((long) settingsData.timeBetweenQueries*1000);
// Fetch and update the data
List<WorkerData> newData = InstanceInterface.fetchWorkersDataForCards("okex");
//System.err.println(newData.toString());
workerDataList.postValue(newData);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}).start();
}
} }

View File

@ -1,13 +1,15 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/binance_cards_container"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="match_parent">
android:orientation="vertical" <LinearLayout
android:visibility="visible" android:id="@+id/binance_cards_container"
android:scrollbars="vertical"> android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="visible">
<!-- Each card will be added dynamically here --> <!-- Each card will be added dynamically here -->
</LinearLayout> </LinearLayout>
</ScrollView>

View File

@ -1,22 +1,15 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent">
tools:context=".ui.exchanges.GateioFragment"> <LinearLayout
android:id="@+id/gateio_cards_container"
<TextView
android:id="@+id/text_gateio"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="8dp" android:orientation="vertical"
android:layout_marginTop="8dp" android:visibility="visible">
android:layout_marginEnd="8dp"
android:textAlignment="center" <!-- Each card will be added dynamically here -->
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent" </LinearLayout>
app:layout_constraintEnd_toEndOf="parent" </ScrollView>
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -1,22 +1,15 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent">
tools:context=".ui.exchanges.KucoinFragment"> <LinearLayout
android:id="@+id/kucoin_cards_container"
<TextView
android:id="@+id/text_kucoin"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="8dp" android:orientation="vertical"
android:layout_marginTop="8dp" android:visibility="visible">
android:layout_marginEnd="8dp"
android:textAlignment="center" <!-- Each card will be added dynamically here -->
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent" </LinearLayout>
app:layout_constraintEnd_toEndOf="parent" </ScrollView>
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -1,22 +1,15 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent">
tools:context=".ui.exchanges.OkxFragment"> <LinearLayout
android:id="@+id/okex_cards_container"
<TextView
android:id="@+id/text_okx"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="8dp" android:orientation="vertical"
android:layout_marginTop="8dp" android:visibility="visible">
android:layout_marginEnd="8dp"
android:textAlignment="center" <!-- Each card will be added dynamically here -->
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent" </LinearLayout>
app:layout_constraintEnd_toEndOf="parent" </ScrollView>
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>