WIP: Update morefiles #152
@@ -27,11 +27,7 @@ public class AdoptionRepository {
|
||||
*/
|
||||
public LiveData<Resource<PageResponse<AdoptionDTO>>> getAllAdoptions(int page, int size) {
|
||||
MutableLiveData<Resource<PageResponse<AdoptionDTO>>> data = new MutableLiveData<>();
|
||||
data.setValue(Resource.loading(null));
|
||||
|
||||
adoptionApi.getAllAdoptions(page, size).enqueue(RetrofitUtils.createSilentCallback(TAG,
|
||||
result -> data.setValue(Resource.success(result))));
|
||||
|
||||
RetrofitUtils.enqueue(adoptionApi.getAllAdoptions(page, size), data, TAG);
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -40,11 +36,7 @@ public class AdoptionRepository {
|
||||
*/
|
||||
public LiveData<Resource<AdoptionDTO>> getAdoptionById(Long id) {
|
||||
MutableLiveData<Resource<AdoptionDTO>> data = new MutableLiveData<>();
|
||||
data.setValue(Resource.loading(null));
|
||||
|
||||
adoptionApi.getAdoptionById(id).enqueue(RetrofitUtils.createSilentCallback(TAG,
|
||||
result -> data.setValue(Resource.success(result))));
|
||||
|
||||
RetrofitUtils.enqueue(adoptionApi.getAdoptionById(id), data, TAG);
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -53,11 +45,7 @@ public class AdoptionRepository {
|
||||
*/
|
||||
public LiveData<Resource<AdoptionDTO>> createAdoption(AdoptionDTO adoption) {
|
||||
MutableLiveData<Resource<AdoptionDTO>> data = new MutableLiveData<>();
|
||||
data.setValue(Resource.loading(null));
|
||||
|
||||
adoptionApi.createAdoption(adoption).enqueue(RetrofitUtils.createSilentCallback(TAG,
|
||||
result -> data.setValue(Resource.success(result))));
|
||||
|
||||
RetrofitUtils.enqueue(adoptionApi.createAdoption(adoption), data, TAG);
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -66,11 +54,7 @@ public class AdoptionRepository {
|
||||
*/
|
||||
public LiveData<Resource<AdoptionDTO>> updateAdoption(Long id, AdoptionDTO adoption) {
|
||||
MutableLiveData<Resource<AdoptionDTO>> data = new MutableLiveData<>();
|
||||
data.setValue(Resource.loading(null));
|
||||
|
||||
adoptionApi.updateAdoption(id, adoption).enqueue(RetrofitUtils.createSilentCallback(TAG,
|
||||
result -> data.setValue(Resource.success(result))));
|
||||
|
||||
RetrofitUtils.enqueue(adoptionApi.updateAdoption(id, adoption), data, TAG);
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -79,11 +63,7 @@ public class AdoptionRepository {
|
||||
*/
|
||||
public LiveData<Resource<Void>> deleteAdoption(Long id) {
|
||||
MutableLiveData<Resource<Void>> data = new MutableLiveData<>();
|
||||
data.setValue(Resource.loading(null));
|
||||
|
||||
adoptionApi.deleteAdoption(id).enqueue(RetrofitUtils.createSilentCallback(TAG,
|
||||
result -> data.setValue(Resource.success(null))));
|
||||
|
||||
RetrofitUtils.enqueue(adoptionApi.deleteAdoption(id), data, TAG);
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,11 +27,7 @@ public class AppointmentRepository {
|
||||
*/
|
||||
public LiveData<Resource<PageResponse<AppointmentDTO>>> getAllAppointments(int page, int size) {
|
||||
MutableLiveData<Resource<PageResponse<AppointmentDTO>>> data = new MutableLiveData<>();
|
||||
data.setValue(Resource.loading(null));
|
||||
|
||||
appointmentApi.getAllAppointments(page, size).enqueue(RetrofitUtils.createSilentCallback(TAG,
|
||||
result -> data.setValue(Resource.success(result))));
|
||||
|
||||
RetrofitUtils.enqueue(appointmentApi.getAllAppointments(page, size), data, TAG);
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -40,11 +36,7 @@ public class AppointmentRepository {
|
||||
*/
|
||||
public LiveData<Resource<AppointmentDTO>> getAppointmentById(Long id) {
|
||||
MutableLiveData<Resource<AppointmentDTO>> data = new MutableLiveData<>();
|
||||
data.setValue(Resource.loading(null));
|
||||
|
||||
appointmentApi.getAppointmentById(id).enqueue(RetrofitUtils.createSilentCallback(TAG,
|
||||
result -> data.setValue(Resource.success(result))));
|
||||
|
||||
RetrofitUtils.enqueue(appointmentApi.getAppointmentById(id), data, TAG);
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -53,11 +45,7 @@ public class AppointmentRepository {
|
||||
*/
|
||||
public LiveData<Resource<AppointmentDTO>> createAppointment(AppointmentDTO appointment) {
|
||||
MutableLiveData<Resource<AppointmentDTO>> data = new MutableLiveData<>();
|
||||
data.setValue(Resource.loading(null));
|
||||
|
||||
appointmentApi.createAppointment(appointment).enqueue(RetrofitUtils.createSilentCallback(TAG,
|
||||
result -> data.setValue(Resource.success(result))));
|
||||
|
||||
RetrofitUtils.enqueue(appointmentApi.createAppointment(appointment), data, TAG);
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -66,11 +54,7 @@ public class AppointmentRepository {
|
||||
*/
|
||||
public LiveData<Resource<AppointmentDTO>> updateAppointment(Long id, AppointmentDTO appointment) {
|
||||
MutableLiveData<Resource<AppointmentDTO>> data = new MutableLiveData<>();
|
||||
data.setValue(Resource.loading(null));
|
||||
|
||||
appointmentApi.updateAppointment(id, appointment).enqueue(RetrofitUtils.createSilentCallback(TAG,
|
||||
result -> data.setValue(Resource.success(result))));
|
||||
|
||||
RetrofitUtils.enqueue(appointmentApi.updateAppointment(id, appointment), data, TAG);
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -79,11 +63,7 @@ public class AppointmentRepository {
|
||||
*/
|
||||
public LiveData<Resource<Void>> deleteAppointment(Long id) {
|
||||
MutableLiveData<Resource<Void>> data = new MutableLiveData<>();
|
||||
data.setValue(Resource.loading(null));
|
||||
|
||||
appointmentApi.deleteAppointment(id).enqueue(RetrofitUtils.createSilentCallback(TAG,
|
||||
result -> data.setValue(Resource.success(null))));
|
||||
|
||||
RetrofitUtils.enqueue(appointmentApi.deleteAppointment(id), data, TAG);
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.example.petstoremobile.repositories;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
|
||||
@@ -9,6 +10,7 @@ import com.example.petstoremobile.api.auth.AuthApi;
|
||||
import com.example.petstoremobile.api.auth.TokenManager;
|
||||
import com.example.petstoremobile.dtos.AuthDTO;
|
||||
import com.example.petstoremobile.dtos.UserDTO;
|
||||
import com.example.petstoremobile.utils.ErrorUtils;
|
||||
import com.example.petstoremobile.utils.Resource;
|
||||
import com.example.petstoremobile.utils.RetrofitUtils;
|
||||
|
||||
@@ -18,6 +20,9 @@ import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
|
||||
import okhttp3.MultipartBody;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
import retrofit2.Response;
|
||||
|
||||
@Singleton
|
||||
public class AuthRepository {
|
||||
@@ -38,14 +43,28 @@ public class AuthRepository {
|
||||
MutableLiveData<Resource<AuthDTO.LoginResponse>> data = new MutableLiveData<>();
|
||||
data.setValue(Resource.loading(null));
|
||||
|
||||
authApi.login(loginRequest).enqueue(RetrofitUtils.createSilentCallback(TAG, result -> {
|
||||
if (result != null && result.getToken() != null) {
|
||||
tokenManager.saveLoginData(result.getToken(), result.getUsername(), result.getRole());
|
||||
data.setValue(Resource.success(result));
|
||||
} else {
|
||||
data.setValue(Resource.error("Login failed", null));
|
||||
authApi.login(loginRequest).enqueue(new Callback<AuthDTO.LoginResponse>() {
|
||||
@Override
|
||||
public void onResponse(@NonNull Call<AuthDTO.LoginResponse> call, @NonNull Response<AuthDTO.LoginResponse> response) {
|
||||
if (response.isSuccessful()) {
|
||||
AuthDTO.LoginResponse result = response.body();
|
||||
if (result != null && result.getToken() != null) {
|
||||
tokenManager.saveLoginData(result.getToken(), result.getUsername(), result.getRole());
|
||||
data.setValue(Resource.success(result));
|
||||
} else {
|
||||
data.setValue(Resource.error("Login failed: Invalid response", null));
|
||||
}
|
||||
} else {
|
||||
String errorMsg = ErrorUtils.getErrorMessage(response, "Login failed");
|
||||
data.setValue(Resource.error(errorMsg, null));
|
||||
}
|
||||
}
|
||||
}));
|
||||
|
||||
@Override
|
||||
public void onFailure(@NonNull Call<AuthDTO.LoginResponse> call, @NonNull Throwable t) {
|
||||
data.setValue(Resource.error("Network error: " + t.getMessage(), null));
|
||||
}
|
||||
});
|
||||
|
||||
return data;
|
||||
}
|
||||
@@ -55,11 +74,7 @@ public class AuthRepository {
|
||||
*/
|
||||
public LiveData<Resource<UserDTO>> getMe() {
|
||||
MutableLiveData<Resource<UserDTO>> data = new MutableLiveData<>();
|
||||
data.setValue(Resource.loading(null));
|
||||
|
||||
authApi.getMe().enqueue(RetrofitUtils.createSilentCallback(TAG,
|
||||
result -> data.setValue(Resource.success(result))));
|
||||
|
||||
RetrofitUtils.enqueue(authApi.getMe(), data, TAG);
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -68,11 +83,7 @@ public class AuthRepository {
|
||||
*/
|
||||
public LiveData<Resource<UserDTO>> updateMe(Map<String, String> updates) {
|
||||
MutableLiveData<Resource<UserDTO>> data = new MutableLiveData<>();
|
||||
data.setValue(Resource.loading(null));
|
||||
|
||||
authApi.updateMe(updates).enqueue(RetrofitUtils.createSilentCallback(TAG,
|
||||
result -> data.setValue(Resource.success(result))));
|
||||
|
||||
RetrofitUtils.enqueue(authApi.updateMe(updates), data, TAG);
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -81,11 +92,7 @@ public class AuthRepository {
|
||||
*/
|
||||
public LiveData<Resource<UserDTO>> uploadAvatar(MultipartBody.Part avatar) {
|
||||
MutableLiveData<Resource<UserDTO>> data = new MutableLiveData<>();
|
||||
data.setValue(Resource.loading(null));
|
||||
|
||||
authApi.uploadAvatar(avatar).enqueue(RetrofitUtils.createSilentCallback(TAG,
|
||||
result -> data.setValue(Resource.success(result))));
|
||||
|
||||
RetrofitUtils.enqueue(authApi.uploadAvatar(avatar), data, TAG);
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -94,11 +101,7 @@ public class AuthRepository {
|
||||
*/
|
||||
public LiveData<Resource<Void>> deleteAvatar() {
|
||||
MutableLiveData<Resource<Void>> data = new MutableLiveData<>();
|
||||
data.setValue(Resource.loading(null));
|
||||
|
||||
authApi.deleteAvatar().enqueue(RetrofitUtils.createSilentCallback(TAG,
|
||||
result -> data.setValue(Resource.success(null))));
|
||||
|
||||
RetrofitUtils.enqueue(authApi.deleteAvatar(), data, TAG);
|
||||
return data;
|
||||
}
|
||||
|
||||
|
||||
@@ -27,11 +27,7 @@ public class CategoryRepository {
|
||||
*/
|
||||
public LiveData<Resource<PageResponse<CategoryDTO>>> getAllCategories(int page, int size) {
|
||||
MutableLiveData<Resource<PageResponse<CategoryDTO>>> data = new MutableLiveData<>();
|
||||
data.setValue(Resource.loading(null));
|
||||
|
||||
categoryApi.getAllCategories(page, size).enqueue(RetrofitUtils.createSilentCallback(TAG,
|
||||
result -> data.setValue(Resource.success(result))));
|
||||
|
||||
RetrofitUtils.enqueue(categoryApi.getAllCategories(page, size), data, TAG);
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,11 +27,7 @@ public class CustomerRepository {
|
||||
*/
|
||||
public LiveData<Resource<PageResponse<CustomerDTO>>> getAllCustomers(int page, int size) {
|
||||
MutableLiveData<Resource<PageResponse<CustomerDTO>>> data = new MutableLiveData<>();
|
||||
data.setValue(Resource.loading(null));
|
||||
|
||||
customerApi.getAllCustomers(page, size).enqueue(RetrofitUtils.createSilentCallback(TAG,
|
||||
result -> data.setValue(Resource.success(result))));
|
||||
|
||||
RetrofitUtils.enqueue(customerApi.getAllCustomers(page, size), data, TAG);
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -40,11 +36,7 @@ public class CustomerRepository {
|
||||
*/
|
||||
public LiveData<Resource<CustomerDTO>> getCustomerById(Long id) {
|
||||
MutableLiveData<Resource<CustomerDTO>> data = new MutableLiveData<>();
|
||||
data.setValue(Resource.loading(null));
|
||||
|
||||
customerApi.getCustomerById(id).enqueue(RetrofitUtils.createSilentCallback(TAG,
|
||||
result -> data.setValue(Resource.success(result))));
|
||||
|
||||
RetrofitUtils.enqueue(customerApi.getCustomerById(id), data, TAG);
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,11 +31,7 @@ public class InventoryRepository {
|
||||
*/
|
||||
public LiveData<Resource<PageResponse<InventoryDTO>>> getAllInventory(String query, int page, int size, String sort) {
|
||||
MutableLiveData<Resource<PageResponse<InventoryDTO>>> data = new MutableLiveData<>();
|
||||
data.setValue(Resource.loading(null));
|
||||
|
||||
inventoryApi.getAllInventory(query, page, size, sort).enqueue(RetrofitUtils.createSilentCallback(TAG,
|
||||
result -> data.setValue(Resource.success(result))));
|
||||
|
||||
RetrofitUtils.enqueue(inventoryApi.getAllInventory(query, page, size, sort), data, TAG);
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -44,11 +40,7 @@ public class InventoryRepository {
|
||||
*/
|
||||
public LiveData<Resource<InventoryDTO>> getInventoryById(Long id) {
|
||||
MutableLiveData<Resource<InventoryDTO>> data = new MutableLiveData<>();
|
||||
data.setValue(Resource.loading(null));
|
||||
|
||||
inventoryApi.getInventoryById(id).enqueue(RetrofitUtils.createSilentCallback(TAG,
|
||||
result -> data.setValue(Resource.success(result))));
|
||||
|
||||
RetrofitUtils.enqueue(inventoryApi.getInventoryById(id), data, TAG);
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -57,21 +49,13 @@ public class InventoryRepository {
|
||||
*/
|
||||
public LiveData<Resource<InventoryDTO>> createInventory(InventoryRequest request) {
|
||||
MutableLiveData<Resource<InventoryDTO>> data = new MutableLiveData<>();
|
||||
data.setValue(Resource.loading(null));
|
||||
|
||||
inventoryApi.createInventory(request).enqueue(RetrofitUtils.createSilentCallback(TAG,
|
||||
result -> data.setValue(Resource.success(result))));
|
||||
|
||||
RetrofitUtils.enqueue(inventoryApi.createInventory(request), data, TAG);
|
||||
return data;
|
||||
}
|
||||
|
||||
public LiveData<Resource<InventoryDTO>> updateInventory(Long id, InventoryRequest request) {
|
||||
MutableLiveData<Resource<InventoryDTO>> data = new MutableLiveData<>();
|
||||
data.setValue(Resource.loading(null));
|
||||
|
||||
inventoryApi.updateInventory(id, request).enqueue(RetrofitUtils.createSilentCallback(TAG,
|
||||
result -> data.setValue(Resource.success(result))));
|
||||
|
||||
RetrofitUtils.enqueue(inventoryApi.updateInventory(id, request), data, TAG);
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -80,21 +64,13 @@ public class InventoryRepository {
|
||||
*/
|
||||
public LiveData<Resource<Void>> deleteInventory(Long id) {
|
||||
MutableLiveData<Resource<Void>> data = new MutableLiveData<>();
|
||||
data.setValue(Resource.loading(null));
|
||||
|
||||
inventoryApi.deleteInventory(id).enqueue(RetrofitUtils.createSilentCallback(TAG,
|
||||
result -> data.setValue(Resource.success(null))));
|
||||
|
||||
RetrofitUtils.enqueue(inventoryApi.deleteInventory(id), data, TAG);
|
||||
return data;
|
||||
}
|
||||
|
||||
public LiveData<Resource<Void>> bulkDeleteInventory(BulkDeleteRequest request) {
|
||||
MutableLiveData<Resource<Void>> data = new MutableLiveData<>();
|
||||
data.setValue(Resource.loading(null));
|
||||
|
||||
inventoryApi.bulkDeleteInventory(request).enqueue(RetrofitUtils.createSilentCallback(TAG,
|
||||
result -> data.setValue(Resource.success(null))));
|
||||
|
||||
RetrofitUtils.enqueue(inventoryApi.bulkDeleteInventory(request), data, TAG);
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,11 +29,7 @@ public class PetRepository {
|
||||
*/
|
||||
public LiveData<Resource<PageResponse<PetDTO>>> getAllPets(int page, int size) {
|
||||
MutableLiveData<Resource<PageResponse<PetDTO>>> data = new MutableLiveData<>();
|
||||
data.setValue(Resource.loading(null));
|
||||
|
||||
petApi.getAllPets(page, size).enqueue(RetrofitUtils.createSilentCallback(TAG,
|
||||
result -> data.setValue(Resource.success(result))));
|
||||
|
||||
RetrofitUtils.enqueue(petApi.getAllPets(page, size), data, TAG);
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -42,11 +38,7 @@ public class PetRepository {
|
||||
*/
|
||||
public LiveData<Resource<PetDTO>> getPetById(Long id) {
|
||||
MutableLiveData<Resource<PetDTO>> data = new MutableLiveData<>();
|
||||
data.setValue(Resource.loading(null));
|
||||
|
||||
petApi.getPetById(id).enqueue(RetrofitUtils.createSilentCallback(TAG,
|
||||
result -> data.setValue(Resource.success(result))));
|
||||
|
||||
RetrofitUtils.enqueue(petApi.getPetById(id), data, TAG);
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -55,11 +47,7 @@ public class PetRepository {
|
||||
*/
|
||||
public LiveData<Resource<PetDTO>> createPet(PetDTO pet) {
|
||||
MutableLiveData<Resource<PetDTO>> data = new MutableLiveData<>();
|
||||
data.setValue(Resource.loading(null));
|
||||
|
||||
petApi.createPet(pet).enqueue(RetrofitUtils.createSilentCallback(TAG,
|
||||
result -> data.setValue(Resource.success(result))));
|
||||
|
||||
RetrofitUtils.enqueue(petApi.createPet(pet), data, TAG);
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -68,11 +56,7 @@ public class PetRepository {
|
||||
*/
|
||||
public LiveData<Resource<PetDTO>> updatePet(Long id, PetDTO pet) {
|
||||
MutableLiveData<Resource<PetDTO>> data = new MutableLiveData<>();
|
||||
data.setValue(Resource.loading(null));
|
||||
|
||||
petApi.updatePet(id, pet).enqueue(RetrofitUtils.createSilentCallback(TAG,
|
||||
result -> data.setValue(Resource.success(result))));
|
||||
|
||||
RetrofitUtils.enqueue(petApi.updatePet(id, pet), data, TAG);
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -81,11 +65,7 @@ public class PetRepository {
|
||||
*/
|
||||
public LiveData<Resource<Void>> deletePet(Long id) {
|
||||
MutableLiveData<Resource<Void>> data = new MutableLiveData<>();
|
||||
data.setValue(Resource.loading(null));
|
||||
|
||||
petApi.deletePet(id).enqueue(RetrofitUtils.createSilentCallback(TAG,
|
||||
result -> data.setValue(Resource.success(null))));
|
||||
|
||||
RetrofitUtils.enqueue(petApi.deletePet(id), data, TAG);
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -94,11 +74,7 @@ public class PetRepository {
|
||||
*/
|
||||
public LiveData<Resource<Void>> uploadPetImage(Long id, MultipartBody.Part image) {
|
||||
MutableLiveData<Resource<Void>> data = new MutableLiveData<>();
|
||||
data.setValue(Resource.loading(null));
|
||||
|
||||
petApi.uploadPetImage(id, image).enqueue(RetrofitUtils.createSilentCallback(TAG,
|
||||
result -> data.setValue(Resource.success(null))));
|
||||
|
||||
RetrofitUtils.enqueue(petApi.uploadPetImage(id, image), data, TAG);
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -107,11 +83,7 @@ public class PetRepository {
|
||||
*/
|
||||
public LiveData<Resource<Void>> deletePetImage(Long id) {
|
||||
MutableLiveData<Resource<Void>> data = new MutableLiveData<>();
|
||||
data.setValue(Resource.loading(null));
|
||||
|
||||
petApi.deletePetImage(id).enqueue(RetrofitUtils.createSilentCallback(TAG,
|
||||
result -> data.setValue(Resource.success(null))));
|
||||
|
||||
RetrofitUtils.enqueue(petApi.deletePetImage(id), data, TAG);
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,11 +29,7 @@ public class ProductRepository {
|
||||
*/
|
||||
public LiveData<Resource<PageResponse<ProductDTO>>> getAllProducts(String query, int page, int size) {
|
||||
MutableLiveData<Resource<PageResponse<ProductDTO>>> data = new MutableLiveData<>();
|
||||
data.setValue(Resource.loading(null));
|
||||
|
||||
productApi.getAllProducts(query, page, size).enqueue(RetrofitUtils.createSilentCallback(TAG,
|
||||
result -> data.setValue(Resource.success(result))));
|
||||
|
||||
RetrofitUtils.enqueue(productApi.getAllProducts(query, page, size), data, TAG);
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -42,11 +38,7 @@ public class ProductRepository {
|
||||
*/
|
||||
public LiveData<Resource<ProductDTO>> getProductById(Long id) {
|
||||
MutableLiveData<Resource<ProductDTO>> data = new MutableLiveData<>();
|
||||
data.setValue(Resource.loading(null));
|
||||
|
||||
productApi.getProductById(id).enqueue(RetrofitUtils.createSilentCallback(TAG,
|
||||
result -> data.setValue(Resource.success(result))));
|
||||
|
||||
RetrofitUtils.enqueue(productApi.getProductById(id), data, TAG);
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -55,11 +47,7 @@ public class ProductRepository {
|
||||
*/
|
||||
public LiveData<Resource<ProductDTO>> createProduct(ProductDTO product) {
|
||||
MutableLiveData<Resource<ProductDTO>> data = new MutableLiveData<>();
|
||||
data.setValue(Resource.loading(null));
|
||||
|
||||
productApi.createProduct(product).enqueue(RetrofitUtils.createSilentCallback(TAG,
|
||||
result -> data.setValue(Resource.success(result))));
|
||||
|
||||
RetrofitUtils.enqueue(productApi.createProduct(product), data, TAG);
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -68,11 +56,7 @@ public class ProductRepository {
|
||||
*/
|
||||
public LiveData<Resource<ProductDTO>> updateProduct(Long id, ProductDTO product) {
|
||||
MutableLiveData<Resource<ProductDTO>> data = new MutableLiveData<>();
|
||||
data.setValue(Resource.loading(null));
|
||||
|
||||
productApi.updateProduct(id, product).enqueue(RetrofitUtils.createSilentCallback(TAG,
|
||||
result -> data.setValue(Resource.success(result))));
|
||||
|
||||
RetrofitUtils.enqueue(productApi.updateProduct(id, product), data, TAG);
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -81,11 +65,7 @@ public class ProductRepository {
|
||||
*/
|
||||
public LiveData<Resource<Void>> deleteProduct(Long id) {
|
||||
MutableLiveData<Resource<Void>> data = new MutableLiveData<>();
|
||||
data.setValue(Resource.loading(null));
|
||||
|
||||
productApi.deleteProduct(id).enqueue(RetrofitUtils.createSilentCallback(TAG,
|
||||
result -> data.setValue(Resource.success(null))));
|
||||
|
||||
RetrofitUtils.enqueue(productApi.deleteProduct(id), data, TAG);
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -94,11 +74,7 @@ public class ProductRepository {
|
||||
*/
|
||||
public LiveData<Resource<Void>> uploadProductImage(Long id, MultipartBody.Part image) {
|
||||
MutableLiveData<Resource<Void>> data = new MutableLiveData<>();
|
||||
data.setValue(Resource.loading(null));
|
||||
|
||||
productApi.uploadProductImage(id, image).enqueue(RetrofitUtils.createSilentCallback(TAG,
|
||||
result -> data.setValue(Resource.success(null))));
|
||||
|
||||
RetrofitUtils.enqueue(productApi.uploadProductImage(id, image), data, TAG);
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -107,11 +83,7 @@ public class ProductRepository {
|
||||
*/
|
||||
public LiveData<Resource<Void>> deleteProductImage(Long id) {
|
||||
MutableLiveData<Resource<Void>> data = new MutableLiveData<>();
|
||||
data.setValue(Resource.loading(null));
|
||||
|
||||
productApi.deleteProductImage(id).enqueue(RetrofitUtils.createSilentCallback(TAG,
|
||||
result -> data.setValue(Resource.success(null))));
|
||||
|
||||
RetrofitUtils.enqueue(productApi.deleteProductImage(id), data, TAG);
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,11 +27,7 @@ public class ProductSupplierRepository {
|
||||
*/
|
||||
public LiveData<Resource<PageResponse<ProductSupplierDTO>>> getAllProductSuppliers(int page, int size) {
|
||||
MutableLiveData<Resource<PageResponse<ProductSupplierDTO>>> data = new MutableLiveData<>();
|
||||
data.setValue(Resource.loading(null));
|
||||
|
||||
api.getAllProductSuppliers(page, size).enqueue(RetrofitUtils.createSilentCallback(TAG,
|
||||
result -> data.setValue(Resource.success(result))));
|
||||
|
||||
RetrofitUtils.enqueue(api.getAllProductSuppliers(page, size), data, TAG);
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -40,11 +36,7 @@ public class ProductSupplierRepository {
|
||||
*/
|
||||
public LiveData<Resource<ProductSupplierDTO>> createProductSupplier(ProductSupplierDTO dto) {
|
||||
MutableLiveData<Resource<ProductSupplierDTO>> data = new MutableLiveData<>();
|
||||
data.setValue(Resource.loading(null));
|
||||
|
||||
api.createProductSupplier(dto).enqueue(RetrofitUtils.createSilentCallback(TAG,
|
||||
result -> data.setValue(Resource.success(result))));
|
||||
|
||||
RetrofitUtils.enqueue(api.createProductSupplier(dto), data, TAG);
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -53,11 +45,7 @@ public class ProductSupplierRepository {
|
||||
*/
|
||||
public LiveData<Resource<ProductSupplierDTO>> updateProductSupplier(Long productId, Long supplierId, ProductSupplierDTO dto) {
|
||||
MutableLiveData<Resource<ProductSupplierDTO>> data = new MutableLiveData<>();
|
||||
data.setValue(Resource.loading(null));
|
||||
|
||||
api.updateProductSupplier(productId, supplierId, dto).enqueue(RetrofitUtils.createSilentCallback(TAG,
|
||||
result -> data.setValue(Resource.success(result))));
|
||||
|
||||
RetrofitUtils.enqueue(api.updateProductSupplier(productId, supplierId, dto), data, TAG);
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -66,11 +54,7 @@ public class ProductSupplierRepository {
|
||||
*/
|
||||
public LiveData<Resource<Void>> deleteProductSupplier(Long productId, Long supplierId) {
|
||||
MutableLiveData<Resource<Void>> data = new MutableLiveData<>();
|
||||
data.setValue(Resource.loading(null));
|
||||
|
||||
api.deleteProductSupplier(productId, supplierId).enqueue(RetrofitUtils.createSilentCallback(TAG,
|
||||
result -> data.setValue(Resource.success(null))));
|
||||
|
||||
RetrofitUtils.enqueue(api.deleteProductSupplier(productId, supplierId), data, TAG);
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,10 +12,6 @@ import com.example.petstoremobile.utils.RetrofitUtils;
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
import retrofit2.Response;
|
||||
|
||||
@Singleton
|
||||
public class PurchaseOrderRepository {
|
||||
private static final String TAG = "PurchaseOrderRepo";
|
||||
@@ -31,12 +27,7 @@ public class PurchaseOrderRepository {
|
||||
*/
|
||||
public LiveData<Resource<PageResponse<PurchaseOrderDTO>>> getAllPurchaseOrders(int page, int size) {
|
||||
MutableLiveData<Resource<PageResponse<PurchaseOrderDTO>>> data = new MutableLiveData<>();
|
||||
data.setValue(Resource.loading(null));
|
||||
|
||||
api.getAllPurchaseOrders(page, size).enqueue(RetrofitUtils.createSilentCallback(TAG, result -> {
|
||||
data.setValue(Resource.success(result));
|
||||
}));
|
||||
|
||||
RetrofitUtils.enqueue(api.getAllPurchaseOrders(page, size), data, TAG);
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -45,12 +36,7 @@ public class PurchaseOrderRepository {
|
||||
*/
|
||||
public LiveData<Resource<PurchaseOrderDTO>> getPurchaseOrderById(Long id) {
|
||||
MutableLiveData<Resource<PurchaseOrderDTO>> data = new MutableLiveData<>();
|
||||
data.setValue(Resource.loading(null));
|
||||
|
||||
api.getPurchaseOrderById(id).enqueue(RetrofitUtils.createSilentCallback(TAG, result -> {
|
||||
data.setValue(Resource.success(result));
|
||||
}));
|
||||
|
||||
RetrofitUtils.enqueue(api.getPurchaseOrderById(id), data, TAG);
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,11 +27,7 @@ public class ServiceRepository {
|
||||
*/
|
||||
public LiveData<Resource<PageResponse<ServiceDTO>>> getAllServices(int page, int size) {
|
||||
MutableLiveData<Resource<PageResponse<ServiceDTO>>> data = new MutableLiveData<>();
|
||||
data.setValue(Resource.loading(null));
|
||||
|
||||
serviceApi.getAllServices(page, size).enqueue(RetrofitUtils.createSilentCallback(TAG,
|
||||
result -> data.setValue(Resource.success(result))));
|
||||
|
||||
RetrofitUtils.enqueue(serviceApi.getAllServices(page, size), data, TAG);
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -40,11 +36,7 @@ public class ServiceRepository {
|
||||
*/
|
||||
public LiveData<Resource<ServiceDTO>> getServiceById(Long id) {
|
||||
MutableLiveData<Resource<ServiceDTO>> data = new MutableLiveData<>();
|
||||
data.setValue(Resource.loading(null));
|
||||
|
||||
serviceApi.getServiceById(id).enqueue(RetrofitUtils.createSilentCallback(TAG,
|
||||
result -> data.setValue(Resource.success(result))));
|
||||
|
||||
RetrofitUtils.enqueue(serviceApi.getServiceById(id), data, TAG);
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -53,11 +45,7 @@ public class ServiceRepository {
|
||||
*/
|
||||
public LiveData<Resource<ServiceDTO>> createService(ServiceDTO service) {
|
||||
MutableLiveData<Resource<ServiceDTO>> data = new MutableLiveData<>();
|
||||
data.setValue(Resource.loading(null));
|
||||
|
||||
serviceApi.createService(service).enqueue(RetrofitUtils.createSilentCallback(TAG,
|
||||
result -> data.setValue(Resource.success(result))));
|
||||
|
||||
RetrofitUtils.enqueue(serviceApi.createService(service), data, TAG);
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -66,11 +54,7 @@ public class ServiceRepository {
|
||||
*/
|
||||
public LiveData<Resource<ServiceDTO>> updateService(Long id, ServiceDTO service) {
|
||||
MutableLiveData<Resource<ServiceDTO>> data = new MutableLiveData<>();
|
||||
data.setValue(Resource.loading(null));
|
||||
|
||||
serviceApi.updateService(id, service).enqueue(RetrofitUtils.createSilentCallback(TAG,
|
||||
result -> data.setValue(Resource.success(result))));
|
||||
|
||||
RetrofitUtils.enqueue(serviceApi.updateService(id, service), data, TAG);
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -79,11 +63,7 @@ public class ServiceRepository {
|
||||
*/
|
||||
public LiveData<Resource<Void>> deleteService(Long id) {
|
||||
MutableLiveData<Resource<Void>> data = new MutableLiveData<>();
|
||||
data.setValue(Resource.loading(null));
|
||||
|
||||
serviceApi.deleteService(id).enqueue(RetrofitUtils.createSilentCallback(TAG,
|
||||
result -> data.setValue(Resource.success(null))));
|
||||
|
||||
RetrofitUtils.enqueue(serviceApi.deleteService(id), data, TAG);
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,11 +27,7 @@ public class StoreRepository {
|
||||
*/
|
||||
public LiveData<Resource<PageResponse<StoreDTO>>> getAllStores(int page, int size) {
|
||||
MutableLiveData<Resource<PageResponse<StoreDTO>>> data = new MutableLiveData<>();
|
||||
data.setValue(Resource.loading(null));
|
||||
|
||||
storeApi.getAllStores(page, size).enqueue(RetrofitUtils.createSilentCallback(TAG,
|
||||
result -> data.setValue(Resource.success(result))));
|
||||
|
||||
RetrofitUtils.enqueue(storeApi.getAllStores(page, size), data, TAG);
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,11 +27,7 @@ public class SupplierRepository {
|
||||
*/
|
||||
public LiveData<Resource<PageResponse<SupplierDTO>>> getAllSuppliers(int page, int size) {
|
||||
MutableLiveData<Resource<PageResponse<SupplierDTO>>> data = new MutableLiveData<>();
|
||||
data.setValue(Resource.loading(null));
|
||||
|
||||
supplierApi.getAllSuppliers(page, size).enqueue(RetrofitUtils.createSilentCallback(TAG,
|
||||
result -> data.setValue(Resource.success(result))));
|
||||
|
||||
RetrofitUtils.enqueue(supplierApi.getAllSuppliers(page, size), data, TAG);
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -40,11 +36,7 @@ public class SupplierRepository {
|
||||
*/
|
||||
public LiveData<Resource<SupplierDTO>> getSupplierById(Long id) {
|
||||
MutableLiveData<Resource<SupplierDTO>> data = new MutableLiveData<>();
|
||||
data.setValue(Resource.loading(null));
|
||||
|
||||
supplierApi.getSupplierById(id).enqueue(RetrofitUtils.createSilentCallback(TAG,
|
||||
result -> data.setValue(Resource.success(result))));
|
||||
|
||||
RetrofitUtils.enqueue(supplierApi.getSupplierById(id), data, TAG);
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -53,11 +45,7 @@ public class SupplierRepository {
|
||||
*/
|
||||
public LiveData<Resource<SupplierDTO>> createSupplier(SupplierDTO supplier) {
|
||||
MutableLiveData<Resource<SupplierDTO>> data = new MutableLiveData<>();
|
||||
data.setValue(Resource.loading(null));
|
||||
|
||||
supplierApi.createSupplier(supplier).enqueue(RetrofitUtils.createSilentCallback(TAG,
|
||||
result -> data.setValue(Resource.success(result))));
|
||||
|
||||
RetrofitUtils.enqueue(supplierApi.createSupplier(supplier), data, TAG);
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -65,15 +53,9 @@ public class SupplierRepository {
|
||||
* Sends a request to the API to update an existing supplier record by ID.
|
||||
*/
|
||||
public LiveData<Resource<SupplierDTO>> updateSupplier(Long id, SupplierDTO supplier) {
|
||||
MutableLiveData<Resource<Resource<SupplierDTO>>> data = new MutableLiveData<>();
|
||||
// Note: The original return type was LiveData<Resource<SupplierDTO>>, fixing here
|
||||
MutableLiveData<Resource<SupplierDTO>> resultData = new MutableLiveData<>();
|
||||
resultData.setValue(Resource.loading(null));
|
||||
|
||||
supplierApi.updateSupplier(id, supplier).enqueue(RetrofitUtils.createSilentCallback(TAG,
|
||||
result -> resultData.setValue(Resource.success(result))));
|
||||
|
||||
return resultData;
|
||||
MutableLiveData<Resource<SupplierDTO>> data = new MutableLiveData<>();
|
||||
RetrofitUtils.enqueue(supplierApi.updateSupplier(id, supplier), data, TAG);
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -81,11 +63,7 @@ public class SupplierRepository {
|
||||
*/
|
||||
public LiveData<Resource<Void>> deleteSupplier(Long id) {
|
||||
MutableLiveData<Resource<Void>> data = new MutableLiveData<>();
|
||||
data.setValue(Resource.loading(null));
|
||||
|
||||
supplierApi.deleteSupplier(id).enqueue(RetrofitUtils.createSilentCallback(TAG,
|
||||
result -> data.setValue(Resource.success(null))));
|
||||
|
||||
RetrofitUtils.enqueue(supplierApi.deleteSupplier(id), data, TAG);
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,18 +15,24 @@ public class ErrorUtils {
|
||||
* Shows an error message to toast based on the response.
|
||||
*/
|
||||
public static void showErrorMessage(Context context, Response<?> response, String defaultMessage) {
|
||||
Toast.makeText(context, getErrorMessage(response, defaultMessage), Toast.LENGTH_LONG).show();
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts the error message from the response body.
|
||||
*/
|
||||
public static String getErrorMessage(Response<?> response, String defaultMessage) {
|
||||
try {
|
||||
if (response.errorBody() != null) {
|
||||
if (response != null && response.errorBody() != null) {
|
||||
String errorJson = response.errorBody().string();
|
||||
ErrorResponse errorResponse = new Gson().fromJson(errorJson, ErrorResponse.class);
|
||||
if (errorResponse != null && errorResponse.getMessage() != null) {
|
||||
Toast.makeText(context, errorResponse.getMessage(), Toast.LENGTH_LONG).show();
|
||||
return;
|
||||
return errorResponse.getMessage();
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.e("ErrorUtils", "Error parsing error body", e);
|
||||
}
|
||||
Toast.makeText(context, defaultMessage, Toast.LENGTH_SHORT).show();
|
||||
return defaultMessage;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
@@ -23,9 +24,36 @@ public class RetrofitUtils {
|
||||
void onSuccess(T result);
|
||||
}
|
||||
|
||||
/**
|
||||
* call and updates the provided MutableLiveData with Resource states.
|
||||
*/
|
||||
public static <T> void enqueue(@NonNull Call<T> call, @NonNull MutableLiveData<Resource<T>> data, String tag) {
|
||||
data.setValue(Resource.loading(null));
|
||||
call.enqueue(new Callback<T>() {
|
||||
@Override
|
||||
public void onResponse(@NonNull Call<T> call, @NonNull Response<T> response) {
|
||||
if (response.isSuccessful()) {
|
||||
data.setValue(Resource.success(response.body()));
|
||||
} else {
|
||||
String errorMsg = ErrorUtils.getErrorMessage(response, "API Error: " + response.code());
|
||||
Log.e(tag, errorMsg);
|
||||
data.setValue(Resource.error(errorMsg, null));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(@NonNull Call<T> call, @NonNull Throwable t) {
|
||||
String errorMsg = "Network Error: " + t.getMessage();
|
||||
Log.e(tag, errorMsg);
|
||||
data.setValue(Resource.error(errorMsg, null));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a callback for Retrofit calls that handles errors and logging.
|
||||
*/
|
||||
@Deprecated
|
||||
public static <T> Callback<T> createCallback(Context context, String tag, String successMsg, SuccessCallback<T> successCallback) {
|
||||
return new Callback<T>() {
|
||||
@Override
|
||||
@@ -54,6 +82,7 @@ public class RetrofitUtils {
|
||||
/**
|
||||
* Creates a callback that doesn't show toasts
|
||||
*/
|
||||
@Deprecated
|
||||
public static <T> Callback<T> createSilentCallback(String tag, SuccessCallback<T> successCallback) {
|
||||
return new Callback<T>() {
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user