114 lines
3.8 KiB
Java
114 lines
3.8 KiB
Java
package com.utopiaindustries.qualitycontrol.viewmodels;
|
|
|
|
import android.util.Log;
|
|
|
|
import androidx.lifecycle.LiveData;
|
|
import androidx.lifecycle.MutableLiveData;
|
|
import androidx.lifecycle.ViewModel;
|
|
|
|
import com.utopiaindustries.qualitycontrol.apiservice.ApiService;
|
|
import com.utopiaindustries.qualitycontrol.apiservice.ApiServiceFactory;
|
|
import com.utopiaindustries.qualitycontrol.models.QualityControlResponse;
|
|
import com.utopiaindustries.qualitycontrol.models.QualitySaveResponse;
|
|
|
|
import java.util.concurrent.ExecutorService;
|
|
import java.util.concurrent.Executors;
|
|
|
|
import retrofit2.Call;
|
|
import retrofit2.Callback;
|
|
import retrofit2.Response;
|
|
|
|
public class HomeViewModel extends ViewModel {
|
|
|
|
private MutableLiveData<QualitySaveResponse> userLiveData;
|
|
private MutableLiveData<String> errorLiveData;
|
|
private MutableLiveData<Boolean> isLoading;
|
|
private ApiService apiService;
|
|
private final ExecutorService executorService;
|
|
|
|
public HomeViewModel() {
|
|
apiService = ApiServiceFactory.getApiService();
|
|
userLiveData = new MutableLiveData<>();
|
|
errorLiveData = new MutableLiveData<>();
|
|
isLoading = new MutableLiveData<>();
|
|
this.executorService = Executors.newFixedThreadPool(4);
|
|
}
|
|
|
|
public LiveData<QualitySaveResponse> getUserLiveData() {
|
|
return userLiveData;
|
|
}
|
|
|
|
public LiveData<Boolean> getLoadingState() {
|
|
|
|
return isLoading;
|
|
}
|
|
|
|
public LiveData<String> getErrorMessage() {
|
|
return errorLiveData;
|
|
}
|
|
|
|
/*public void saveQualityControlData(QualityControl qualityControl) {
|
|
isLoading.setValue(true);
|
|
apiService.saveQualityControlReport(qualityControl).enqueue(new Callback<QualitySaveResponse>() {
|
|
@Override
|
|
public void onResponse(Call<QualitySaveResponse> call, Response<QualitySaveResponse> response) {
|
|
//Log.e("onResponse: ",""+response);
|
|
isLoading.setValue(false);
|
|
if (response.isSuccessful() && response.body() != null) {
|
|
//Log.e("onResponse-success: ",""+response);
|
|
userLiveData.setValue(response.body());
|
|
} else {
|
|
errorLiveData.setValue(response.message());
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onFailure(Call<QualitySaveResponse> call, Throwable t) {
|
|
//Log.e("onResponse-fail: ",""+t.getMessage());
|
|
isLoading.setValue(false);
|
|
errorLiveData.setValue(t.getMessage());
|
|
}
|
|
});
|
|
}*/
|
|
|
|
public void saveQualityControlData(QualityControl qualityControl) {
|
|
isLoading.setValue(true); // Notify UI that the task is starting
|
|
|
|
// Execute the task in the background
|
|
executorService.execute(() -> {
|
|
try {
|
|
// Synchronous network call
|
|
Response<QualitySaveResponse> response = apiService.saveQualityControlReport(qualityControl).execute();
|
|
|
|
// Switch to the main thread to update LiveData
|
|
if (response.isSuccessful() && response.body() != null) {
|
|
userLiveData.postValue(response.body());
|
|
} else {
|
|
errorLiveData.postValue(response.message());
|
|
}
|
|
} catch (Exception e) {
|
|
// Handle errors
|
|
errorLiveData.postValue(e.getMessage());
|
|
} finally {
|
|
// Hide the loading spinner
|
|
isLoading.postValue(false);
|
|
}
|
|
});
|
|
}
|
|
|
|
public LiveData<QualitySaveResponse> getUser() {
|
|
return userLiveData;
|
|
}
|
|
|
|
public LiveData<String> getError() {
|
|
return errorLiveData;
|
|
}
|
|
|
|
@Override
|
|
protected void onCleared() {
|
|
super.onCleared();
|
|
// Shut down the executor service to prevent memory leaks
|
|
executorService.shutdown();
|
|
}
|
|
}
|