202 lines
7.5 KiB
Java
202 lines
7.5 KiB
Java
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<EmployeeInfoResponse> employeeLiveData;
|
|
private MutableLiveData<DailyWageResponse> dailyWageLiveData;
|
|
private MutableLiveData<HseResponse> userLiveData;
|
|
private MutableLiveData<HseSaveResponse> userSaveLiveData;
|
|
private MutableLiveData<Boolean> userLoginLiveData;
|
|
private MutableLiveData<String> errorLiveData;
|
|
private MutableLiveData<Boolean> 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<EmployeeInfoResponse> getEmployeeLiveData()
|
|
{
|
|
return employeeLiveData;
|
|
}
|
|
|
|
public LiveData<DailyWageResponse> getDailyWageLiveData()
|
|
{
|
|
return dailyWageLiveData;
|
|
}
|
|
|
|
public LiveData<HseResponse> getUserLiveData() {
|
|
return userLiveData;
|
|
}
|
|
|
|
public LiveData<HseSaveResponse> getUserSaveLiveData() {
|
|
return userSaveLiveData;
|
|
}
|
|
|
|
public LiveData<Boolean> getLoadingState() {
|
|
|
|
return isLoading;
|
|
}
|
|
|
|
public LiveData<String> getErrorMessage() {
|
|
return errorLiveData;
|
|
}
|
|
|
|
public void isUserAuthenticated(String username, String password, String[] roles) {
|
|
isLoading.setValue(true);
|
|
apiService.isUserAuthenticated(username, password, roles).enqueue(new Callback<Boolean>() {
|
|
@Override
|
|
public void onResponse(@NonNull Call<Boolean> call, @NonNull Response<Boolean> 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<Boolean> 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<EmployeeInfoResponse>() {
|
|
@Override
|
|
public void onResponse(Call<EmployeeInfoResponse> call, Response<EmployeeInfoResponse> 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<EmployeeInfoResponse> 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<DailyWageResponse>() {
|
|
@Override
|
|
public void onResponse(Call<DailyWageResponse> call, Response<DailyWageResponse> 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<DailyWageResponse> 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<HseResponse>() {
|
|
@Override
|
|
public void onResponse(Call<HseResponse> call, Response<HseResponse> 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<HseResponse> 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<HseSaveResponse>() {
|
|
@Override
|
|
public void onResponse(Call<HseSaveResponse> call, Response<HseSaveResponse> 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<HseSaveResponse> call, Throwable t) {
|
|
//Log.e("onResponse: ", "Fail");
|
|
isLoading.setValue(false);
|
|
errorLiveData.setValue(t.getMessage());
|
|
}
|
|
});
|
|
}
|
|
|
|
public LiveData<Boolean> getLoginUser() {
|
|
return userLoginLiveData;
|
|
}
|
|
|
|
public LiveData<HseResponse> getUser() {
|
|
return userLiveData;
|
|
}
|
|
|
|
public LiveData<String> getError() {
|
|
return errorLiveData;
|
|
}
|
|
}
|