EarnerViewModel
This commit is contained in:
parent
d9cad6f07f
commit
adcbdad32d
|
|
@ -1,4 +0,0 @@
|
|||
package com.example.dcav2gui.ui.earners;
|
||||
|
||||
public class EarnViewModel {
|
||||
}
|
||||
|
|
@ -0,0 +1,63 @@
|
|||
package com.example.dcav2gui.ui.earners;
|
||||
|
||||
import android.app.Application;
|
||||
import android.os.Handler;
|
||||
import android.os.HandlerThread;
|
||||
import android.os.Looper;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.lifecycle.AndroidViewModel;
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
|
||||
import com.example.dcav2gui.EarnerInterface;
|
||||
import com.example.dcav2gui.MainActivity;
|
||||
import com.example.dcav2gui.ui.settings.SettingsData;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class EarnerViewModel extends AndroidViewModel {
|
||||
|
||||
private final MutableLiveData<EarnerInterface.EarnerGlobalData> earnerGlobalData;
|
||||
SettingsData settingsData = MainActivity.getGlobalSettings();
|
||||
|
||||
public EarnerViewModel(@NonNull Application application) {
|
||||
super(application);
|
||||
earnerGlobalData = new MutableLiveData<>();
|
||||
fetchEarnerData(); // Initial data fetch
|
||||
}
|
||||
|
||||
public LiveData<EarnerInterface.EarnerGlobalData> getWorkerData() {
|
||||
|
||||
if (earnerGlobalData.getValue() == null) {
|
||||
fetchEarnerData();
|
||||
}
|
||||
return earnerGlobalData;
|
||||
}
|
||||
|
||||
private void fetchEarnerData() {
|
||||
HandlerThread handlerThread = new HandlerThread("FetchWorkerDataThread");
|
||||
handlerThread.start();
|
||||
Handler handler = new Handler(handlerThread.getLooper());
|
||||
handler.post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
// Fetch and update the data
|
||||
EarnerInterface.EarnerGlobalData newData = EarnerInterface.getEarnerGlobalData(true);
|
||||
earnerGlobalData.postValue(newData);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// Schedule the next execution
|
||||
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
fetchEarnerData();
|
||||
}
|
||||
}, (long) settingsData.timeBetweenQueries * 1000);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue