package com.utopiaindustries.hseobservationsapp.viewmodels; import android.util.Log; import androidx.annotation.NonNull; import androidx.lifecycle.LiveData; import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.ViewModel; import com.utopiaindustries.hseobservationsapp.apiservice.ApiService; import com.utopiaindustries.hseobservationsapp.apiservice.ApiServiceFactory; import com.utopiaindustries.hseobservationsapp.models.HseData.DailyWageResponse; import com.utopiaindustries.hseobservationsapp.models.HseData.EmployeeInfoResponse; import com.utopiaindustries.hseobservationsapp.models.HseData.HseResponse; import com.utopiaindustries.hseobservationsapp.models.HseData.HseSaveResponse; import com.utopiaindustries.hseobservationsapp.utils.HSERequestModel; import com.utopiaindustries.hseobservationsapp.utils.StorageManager.HseReportRequest; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; import retrofit2.http.PUT; public class LoginViewModel extends ViewModel { private MutableLiveData employeeLiveData; private MutableLiveData dailyWageLiveData; private MutableLiveData userLiveData; private MutableLiveData userSaveLiveData; private MutableLiveData userLoginLiveData; private MutableLiveData errorLiveData; private MutableLiveData isLoading; private ApiService apiService; public LoginViewModel() { apiService = ApiServiceFactory.getApiService(); userLiveData = new MutableLiveData<>(); employeeLiveData = new MutableLiveData<>(); dailyWageLiveData = new MutableLiveData<>(); userSaveLiveData = new MutableLiveData<>(); userLoginLiveData = new MutableLiveData<>(); errorLiveData = new MutableLiveData<>(); isLoading = new MutableLiveData<>(); } public LiveData getEmployeeLiveData() { return employeeLiveData; } public LiveData getDailyWageLiveData() { return dailyWageLiveData; } public LiveData getUserLiveData() { return userLiveData; } public LiveData getUserSaveLiveData() { return userSaveLiveData; } public LiveData getLoadingState() { return isLoading; } public LiveData getErrorMessage() { return errorLiveData; } public void isUserAuthenticated(String username, String password, String[] roles) { isLoading.setValue(true); apiService.isUserAuthenticated(username, password, roles).enqueue(new Callback() { @Override public void onResponse(@NonNull Call call, @NonNull Response response) { isLoading.setValue(false); Log.e("onResponse-1: ", "Successful: "+response); if (response.isSuccessful() && response.body() != null) { Log.e("onResponse-2: ", "Successful: "+response); userLoginLiveData.setValue(response.body()); } else { userLoginLiveData.setValue(false); } } @Override public void onFailure(@NonNull Call call, @NonNull Throwable t) { //Log.e("onResponse-2: ", "failed"+t.getMessage()); isLoading.setValue(false); errorLiveData.setValue(t.getMessage()); } }); } public void getEmployeeData(String empId) { isLoading.setValue(true); apiService.getEmployeeData(empId).enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { isLoading.setValue(false); if (response.isSuccessful() && response.body() != null) { //Log.e("onResponse: ", "Successful"); employeeLiveData.setValue(response.body()); } else { errorLiveData.setValue(response.message()); } } @Override public void onFailure(Call call, Throwable t) { //Log.e("onResponse: ", "Fail"); isLoading.setValue(false); errorLiveData.setValue(t.getMessage()); } }); } public void getDailyWageWorkerData(String visitorId) { isLoading.setValue(true); apiService.getDailyWageData(visitorId).enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { isLoading.setValue(false); if (response.isSuccessful() && response.body() != null) { //Log.e("onResponse: ", "Successful"); dailyWageLiveData.setValue(response.body()); } else { errorLiveData.setValue(response.message()); } } @Override public void onFailure(Call call, Throwable t) { //Log.e("onResponse: ", "Fail"); isLoading.setValue(false); errorLiveData.setValue(t.getMessage()); } }); } public void getHSEData() { isLoading.setValue(true); apiService.getHseData().enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { isLoading.setValue(false); if (response.isSuccessful() && response.body() != null) { //Log.e("onResponse: ", "Successful"); userLiveData.setValue(response.body()); } else { errorLiveData.setValue(response.message()); } } @Override public void onFailure(Call call, Throwable t) { //Log.e("onResponse: ", "Fail"); isLoading.setValue(false); errorLiveData.setValue(t.getMessage()); } }); } public void saveHSEData(HseReportRequest hseRequestModel) { isLoading.setValue(true); apiService.saveHseReport(hseRequestModel).enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { isLoading.setValue(false); if (response.isSuccessful() && response.body() != null) { //Log.e("onResponse: ", "Successful"); userSaveLiveData.setValue(response.body()); } else { errorLiveData.setValue(response.message()); } } @Override public void onFailure(Call call, Throwable t) { //Log.e("onResponse: ", "Fail"); isLoading.setValue(false); errorLiveData.setValue(t.getMessage()); } }); } public LiveData getLoginUser() { return userLoginLiveData; } public LiveData getUser() { return userLiveData; } public LiveData getError() { return errorLiveData; } }