fetchWorkerData, but using handlers

This commit is contained in:
Nicolás Sánchez 2024-12-18 11:50:46 -03:00
parent d47567e226
commit 529e0bdd47
6 changed files with 78 additions and 42 deletions

View File

@ -4,10 +4,10 @@
<selectionStates>
<SelectionState runConfigName="app">
<option name="selectionMode" value="DROPDOWN" />
<DropdownSelection timestamp="2024-12-18T13:29:16.439093075Z">
<DropdownSelection timestamp="2024-12-18T13:48:59.828906476Z">
<Target type="DEFAULT_BOOT">
<handle>
<DeviceId pluginId="LocalEmulator" identifier="path=/home/nicolas/.android/avd/Pixel_6_Pro_API_34.avd" />
<DeviceId pluginId="PhysicalDevice" identifier="serial=ZY22FN7MHQ" />
</handle>
</Target>
</DropdownSelection>

View File

@ -651,16 +651,6 @@ public class InstanceInterface {
}
public static List<WorkerData> fetchWorkersDataForCards(String exchange) throws IOException {
// Implement this method to fetch actual data
// call List<WorkerStatsData> getAllWorkersStats from InstanceInterface and
// translate all the WorkerStatsData objects to WorkerData objects!
// Example implementation:
List<InstanceInterface.WorkerStatsData> workerStatsDataList = InstanceInterface.getAllWorkersStats(exchange, true);
return translateToWorkerData(workerStatsDataList);
}
public static class DealData {
private final double timestamp;
private final String pair;

View File

@ -1,6 +1,8 @@
package com.example.dcav2gui.ui.exchanges;
import android.app.Application;
import android.util.Log;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
@ -34,19 +36,27 @@ public class BinanceViewModel extends AndroidViewModel {
}
private void fetchWorkerData() {
new Thread(() -> {
while (true) {
HandlerThread handlerThread = new HandlerThread("FetchWorkerDataThread");
handlerThread.start();
Handler handler = new Handler(handlerThread.getLooper());
handler.post(new Runnable() {
@Override
public void run() {
try {
Thread.sleep((long) settingsData.timeBetweenQueries*1000);
// Fetch and update the data
List<WorkerData> newData = InstanceInterface.fetchWorkersDataForCards("binance");
//System.err.println(newData.toString());
List<WorkerData> newData = InstanceInterface.translateToWorkerData(InstanceInterface.getAllWorkersStats("binance", true));
workerDataList.postValue(newData);
} catch (InterruptedException | IOException e) {
} catch (IOException e) {
e.printStackTrace();
}
// Schedule the next execution
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run() {
fetchWorkerData();
}
}).start();
}, (long) settingsData.timeBetweenQueries * 1000);
}
});
}
}

View File

@ -1,5 +1,8 @@
package com.example.dcav2gui.ui.exchanges;
import android.app.Application;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
@ -33,19 +36,28 @@ public class GateioViewModel extends AndroidViewModel {
}
private void fetchWorkerData() {
new Thread(() -> {
while (true) {
HandlerThread handlerThread = new HandlerThread("FetchWorkerDataThread");
handlerThread.start();
Handler handler = new Handler(handlerThread.getLooper());
handler.post(new Runnable() {
@Override
public void run() {
try {
Thread.sleep((long) settingsData.timeBetweenQueries*1000);
// Fetch and update the data
List<WorkerData> newData = InstanceInterface.fetchWorkersDataForCards("gateio");
//System.err.println(newData.toString());
List<WorkerData> newData = InstanceInterface.translateToWorkerData(InstanceInterface.getAllWorkersStats("gateio", true));
workerDataList.postValue(newData);
} catch (InterruptedException | IOException e) {
} catch (IOException e) {
e.printStackTrace();
}
// Schedule the next execution
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run() {
fetchWorkerData();
}
}).start();
}, (long) settingsData.timeBetweenQueries * 1000);
}
});
}
}

View File

@ -1,5 +1,8 @@
package com.example.dcav2gui.ui.exchanges;
import android.app.Application;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
@ -33,19 +36,28 @@ public class KucoinViewModel extends AndroidViewModel {
}
private void fetchWorkerData() {
new Thread(() -> {
while (true) {
HandlerThread handlerThread = new HandlerThread("FetchWorkerDataThread");
handlerThread.start();
Handler handler = new Handler(handlerThread.getLooper());
handler.post(new Runnable() {
@Override
public void run() {
try {
Thread.sleep((long) settingsData.timeBetweenQueries*1000);
// Fetch and update the data
List<WorkerData> newData = InstanceInterface.fetchWorkersDataForCards("kucoin");
//System.err.println(newData.toString());
List<WorkerData> newData = InstanceInterface.translateToWorkerData(InstanceInterface.getAllWorkersStats("kucoin", true));
workerDataList.postValue(newData);
} catch (InterruptedException | IOException e) {
} catch (IOException e) {
e.printStackTrace();
}
// Schedule the next execution
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run() {
fetchWorkerData();
}
}).start();
}, (long) settingsData.timeBetweenQueries * 1000);
}
});
}
}

View File

@ -1,5 +1,8 @@
package com.example.dcav2gui.ui.exchanges;
import android.app.Application;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
@ -33,19 +36,28 @@ public class OkxViewModel extends AndroidViewModel {
}
private void fetchWorkerData() {
new Thread(() -> {
while (true) {
HandlerThread handlerThread = new HandlerThread("FetchWorkerDataThread");
handlerThread.start();
Handler handler = new Handler(handlerThread.getLooper());
handler.post(new Runnable() {
@Override
public void run() {
try {
Thread.sleep((long) settingsData.timeBetweenQueries*1000);
// Fetch and update the data
List<WorkerData> newData = InstanceInterface.fetchWorkersDataForCards("okex");
//System.err.println(newData.toString());
List<WorkerData> newData = InstanceInterface.translateToWorkerData(InstanceInterface.getAllWorkersStats("okex", true));
workerDataList.postValue(newData);
} catch (InterruptedException | IOException e) {
} catch (IOException e) {
e.printStackTrace();
}
// Schedule the next execution
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run() {
fetchWorkerData();
}
}).start();
}, (long) settingsData.timeBetweenQueries * 1000);
}
});
}
}