Edited RetrofitUtils to also call enqueue to reduce code in repository

This commit is contained in:
Alex
2026-04-05 21:57:53 -06:00
parent b14e318df2
commit 1137688d60
15 changed files with 121 additions and 291 deletions

View File

@@ -27,11 +27,7 @@ public class AdoptionRepository {
*/ */
public LiveData<Resource<PageResponse<AdoptionDTO>>> getAllAdoptions(int page, int size) { public LiveData<Resource<PageResponse<AdoptionDTO>>> getAllAdoptions(int page, int size) {
MutableLiveData<Resource<PageResponse<AdoptionDTO>>> data = new MutableLiveData<>(); MutableLiveData<Resource<PageResponse<AdoptionDTO>>> data = new MutableLiveData<>();
data.setValue(Resource.loading(null)); RetrofitUtils.enqueue(adoptionApi.getAllAdoptions(page, size), data, TAG);
adoptionApi.getAllAdoptions(page, size).enqueue(RetrofitUtils.createSilentCallback(TAG,
result -> data.setValue(Resource.success(result))));
return data; return data;
} }
@@ -40,11 +36,7 @@ public class AdoptionRepository {
*/ */
public LiveData<Resource<AdoptionDTO>> getAdoptionById(Long id) { public LiveData<Resource<AdoptionDTO>> getAdoptionById(Long id) {
MutableLiveData<Resource<AdoptionDTO>> data = new MutableLiveData<>(); MutableLiveData<Resource<AdoptionDTO>> data = new MutableLiveData<>();
data.setValue(Resource.loading(null)); RetrofitUtils.enqueue(adoptionApi.getAdoptionById(id), data, TAG);
adoptionApi.getAdoptionById(id).enqueue(RetrofitUtils.createSilentCallback(TAG,
result -> data.setValue(Resource.success(result))));
return data; return data;
} }
@@ -53,11 +45,7 @@ public class AdoptionRepository {
*/ */
public LiveData<Resource<AdoptionDTO>> createAdoption(AdoptionDTO adoption) { public LiveData<Resource<AdoptionDTO>> createAdoption(AdoptionDTO adoption) {
MutableLiveData<Resource<AdoptionDTO>> data = new MutableLiveData<>(); MutableLiveData<Resource<AdoptionDTO>> data = new MutableLiveData<>();
data.setValue(Resource.loading(null)); RetrofitUtils.enqueue(adoptionApi.createAdoption(adoption), data, TAG);
adoptionApi.createAdoption(adoption).enqueue(RetrofitUtils.createSilentCallback(TAG,
result -> data.setValue(Resource.success(result))));
return data; return data;
} }
@@ -66,11 +54,7 @@ public class AdoptionRepository {
*/ */
public LiveData<Resource<AdoptionDTO>> updateAdoption(Long id, AdoptionDTO adoption) { public LiveData<Resource<AdoptionDTO>> updateAdoption(Long id, AdoptionDTO adoption) {
MutableLiveData<Resource<AdoptionDTO>> data = new MutableLiveData<>(); MutableLiveData<Resource<AdoptionDTO>> data = new MutableLiveData<>();
data.setValue(Resource.loading(null)); RetrofitUtils.enqueue(adoptionApi.updateAdoption(id, adoption), data, TAG);
adoptionApi.updateAdoption(id, adoption).enqueue(RetrofitUtils.createSilentCallback(TAG,
result -> data.setValue(Resource.success(result))));
return data; return data;
} }
@@ -79,11 +63,7 @@ public class AdoptionRepository {
*/ */
public LiveData<Resource<Void>> deleteAdoption(Long id) { public LiveData<Resource<Void>> deleteAdoption(Long id) {
MutableLiveData<Resource<Void>> data = new MutableLiveData<>(); MutableLiveData<Resource<Void>> data = new MutableLiveData<>();
data.setValue(Resource.loading(null)); RetrofitUtils.enqueue(adoptionApi.deleteAdoption(id), data, TAG);
adoptionApi.deleteAdoption(id).enqueue(RetrofitUtils.createSilentCallback(TAG,
result -> data.setValue(Resource.success(null))));
return data; return data;
} }
} }

View File

@@ -27,11 +27,7 @@ public class AppointmentRepository {
*/ */
public LiveData<Resource<PageResponse<AppointmentDTO>>> getAllAppointments(int page, int size) { public LiveData<Resource<PageResponse<AppointmentDTO>>> getAllAppointments(int page, int size) {
MutableLiveData<Resource<PageResponse<AppointmentDTO>>> data = new MutableLiveData<>(); MutableLiveData<Resource<PageResponse<AppointmentDTO>>> data = new MutableLiveData<>();
data.setValue(Resource.loading(null)); RetrofitUtils.enqueue(appointmentApi.getAllAppointments(page, size), data, TAG);
appointmentApi.getAllAppointments(page, size).enqueue(RetrofitUtils.createSilentCallback(TAG,
result -> data.setValue(Resource.success(result))));
return data; return data;
} }
@@ -40,11 +36,7 @@ public class AppointmentRepository {
*/ */
public LiveData<Resource<AppointmentDTO>> getAppointmentById(Long id) { public LiveData<Resource<AppointmentDTO>> getAppointmentById(Long id) {
MutableLiveData<Resource<AppointmentDTO>> data = new MutableLiveData<>(); MutableLiveData<Resource<AppointmentDTO>> data = new MutableLiveData<>();
data.setValue(Resource.loading(null)); RetrofitUtils.enqueue(appointmentApi.getAppointmentById(id), data, TAG);
appointmentApi.getAppointmentById(id).enqueue(RetrofitUtils.createSilentCallback(TAG,
result -> data.setValue(Resource.success(result))));
return data; return data;
} }
@@ -53,11 +45,7 @@ public class AppointmentRepository {
*/ */
public LiveData<Resource<AppointmentDTO>> createAppointment(AppointmentDTO appointment) { public LiveData<Resource<AppointmentDTO>> createAppointment(AppointmentDTO appointment) {
MutableLiveData<Resource<AppointmentDTO>> data = new MutableLiveData<>(); MutableLiveData<Resource<AppointmentDTO>> data = new MutableLiveData<>();
data.setValue(Resource.loading(null)); RetrofitUtils.enqueue(appointmentApi.createAppointment(appointment), data, TAG);
appointmentApi.createAppointment(appointment).enqueue(RetrofitUtils.createSilentCallback(TAG,
result -> data.setValue(Resource.success(result))));
return data; return data;
} }
@@ -66,11 +54,7 @@ public class AppointmentRepository {
*/ */
public LiveData<Resource<AppointmentDTO>> updateAppointment(Long id, AppointmentDTO appointment) { public LiveData<Resource<AppointmentDTO>> updateAppointment(Long id, AppointmentDTO appointment) {
MutableLiveData<Resource<AppointmentDTO>> data = new MutableLiveData<>(); MutableLiveData<Resource<AppointmentDTO>> data = new MutableLiveData<>();
data.setValue(Resource.loading(null)); RetrofitUtils.enqueue(appointmentApi.updateAppointment(id, appointment), data, TAG);
appointmentApi.updateAppointment(id, appointment).enqueue(RetrofitUtils.createSilentCallback(TAG,
result -> data.setValue(Resource.success(result))));
return data; return data;
} }
@@ -79,11 +63,7 @@ public class AppointmentRepository {
*/ */
public LiveData<Resource<Void>> deleteAppointment(Long id) { public LiveData<Resource<Void>> deleteAppointment(Long id) {
MutableLiveData<Resource<Void>> data = new MutableLiveData<>(); MutableLiveData<Resource<Void>> data = new MutableLiveData<>();
data.setValue(Resource.loading(null)); RetrofitUtils.enqueue(appointmentApi.deleteAppointment(id), data, TAG);
appointmentApi.deleteAppointment(id).enqueue(RetrofitUtils.createSilentCallback(TAG,
result -> data.setValue(Resource.success(null))));
return data; return data;
} }
} }

View File

@@ -2,6 +2,7 @@ package com.example.petstoremobile.repositories;
import android.util.Log; import android.util.Log;
import androidx.annotation.NonNull;
import androidx.lifecycle.LiveData; import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData; 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.api.auth.TokenManager;
import com.example.petstoremobile.dtos.AuthDTO; import com.example.petstoremobile.dtos.AuthDTO;
import com.example.petstoremobile.dtos.UserDTO; import com.example.petstoremobile.dtos.UserDTO;
import com.example.petstoremobile.utils.ErrorUtils;
import com.example.petstoremobile.utils.Resource; import com.example.petstoremobile.utils.Resource;
import com.example.petstoremobile.utils.RetrofitUtils; import com.example.petstoremobile.utils.RetrofitUtils;
@@ -18,6 +20,9 @@ import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
import okhttp3.MultipartBody; import okhttp3.MultipartBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
@Singleton @Singleton
public class AuthRepository { public class AuthRepository {
@@ -38,14 +43,28 @@ public class AuthRepository {
MutableLiveData<Resource<AuthDTO.LoginResponse>> data = new MutableLiveData<>(); MutableLiveData<Resource<AuthDTO.LoginResponse>> data = new MutableLiveData<>();
data.setValue(Resource.loading(null)); data.setValue(Resource.loading(null));
authApi.login(loginRequest).enqueue(RetrofitUtils.createSilentCallback(TAG, result -> { authApi.login(loginRequest).enqueue(new Callback<AuthDTO.LoginResponse>() {
if (result != null && result.getToken() != null) { @Override
tokenManager.saveLoginData(result.getToken(), result.getUsername(), result.getRole()); public void onResponse(@NonNull Call<AuthDTO.LoginResponse> call, @NonNull Response<AuthDTO.LoginResponse> response) {
data.setValue(Resource.success(result)); if (response.isSuccessful()) {
} else { AuthDTO.LoginResponse result = response.body();
data.setValue(Resource.error("Login failed", null)); 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; return data;
} }
@@ -55,11 +74,7 @@ public class AuthRepository {
*/ */
public LiveData<Resource<UserDTO>> getMe() { public LiveData<Resource<UserDTO>> getMe() {
MutableLiveData<Resource<UserDTO>> data = new MutableLiveData<>(); MutableLiveData<Resource<UserDTO>> data = new MutableLiveData<>();
data.setValue(Resource.loading(null)); RetrofitUtils.enqueue(authApi.getMe(), data, TAG);
authApi.getMe().enqueue(RetrofitUtils.createSilentCallback(TAG,
result -> data.setValue(Resource.success(result))));
return data; return data;
} }
@@ -68,11 +83,7 @@ public class AuthRepository {
*/ */
public LiveData<Resource<UserDTO>> updateMe(Map<String, String> updates) { public LiveData<Resource<UserDTO>> updateMe(Map<String, String> updates) {
MutableLiveData<Resource<UserDTO>> data = new MutableLiveData<>(); MutableLiveData<Resource<UserDTO>> data = new MutableLiveData<>();
data.setValue(Resource.loading(null)); RetrofitUtils.enqueue(authApi.updateMe(updates), data, TAG);
authApi.updateMe(updates).enqueue(RetrofitUtils.createSilentCallback(TAG,
result -> data.setValue(Resource.success(result))));
return data; return data;
} }
@@ -81,11 +92,7 @@ public class AuthRepository {
*/ */
public LiveData<Resource<UserDTO>> uploadAvatar(MultipartBody.Part avatar) { public LiveData<Resource<UserDTO>> uploadAvatar(MultipartBody.Part avatar) {
MutableLiveData<Resource<UserDTO>> data = new MutableLiveData<>(); MutableLiveData<Resource<UserDTO>> data = new MutableLiveData<>();
data.setValue(Resource.loading(null)); RetrofitUtils.enqueue(authApi.uploadAvatar(avatar), data, TAG);
authApi.uploadAvatar(avatar).enqueue(RetrofitUtils.createSilentCallback(TAG,
result -> data.setValue(Resource.success(result))));
return data; return data;
} }
@@ -94,11 +101,7 @@ public class AuthRepository {
*/ */
public LiveData<Resource<Void>> deleteAvatar() { public LiveData<Resource<Void>> deleteAvatar() {
MutableLiveData<Resource<Void>> data = new MutableLiveData<>(); MutableLiveData<Resource<Void>> data = new MutableLiveData<>();
data.setValue(Resource.loading(null)); RetrofitUtils.enqueue(authApi.deleteAvatar(), data, TAG);
authApi.deleteAvatar().enqueue(RetrofitUtils.createSilentCallback(TAG,
result -> data.setValue(Resource.success(null))));
return data; return data;
} }

View File

@@ -27,11 +27,7 @@ public class CategoryRepository {
*/ */
public LiveData<Resource<PageResponse<CategoryDTO>>> getAllCategories(int page, int size) { public LiveData<Resource<PageResponse<CategoryDTO>>> getAllCategories(int page, int size) {
MutableLiveData<Resource<PageResponse<CategoryDTO>>> data = new MutableLiveData<>(); MutableLiveData<Resource<PageResponse<CategoryDTO>>> data = new MutableLiveData<>();
data.setValue(Resource.loading(null)); RetrofitUtils.enqueue(categoryApi.getAllCategories(page, size), data, TAG);
categoryApi.getAllCategories(page, size).enqueue(RetrofitUtils.createSilentCallback(TAG,
result -> data.setValue(Resource.success(result))));
return data; return data;
} }
} }

View File

@@ -27,11 +27,7 @@ public class CustomerRepository {
*/ */
public LiveData<Resource<PageResponse<CustomerDTO>>> getAllCustomers(int page, int size) { public LiveData<Resource<PageResponse<CustomerDTO>>> getAllCustomers(int page, int size) {
MutableLiveData<Resource<PageResponse<CustomerDTO>>> data = new MutableLiveData<>(); MutableLiveData<Resource<PageResponse<CustomerDTO>>> data = new MutableLiveData<>();
data.setValue(Resource.loading(null)); RetrofitUtils.enqueue(customerApi.getAllCustomers(page, size), data, TAG);
customerApi.getAllCustomers(page, size).enqueue(RetrofitUtils.createSilentCallback(TAG,
result -> data.setValue(Resource.success(result))));
return data; return data;
} }
@@ -40,11 +36,7 @@ public class CustomerRepository {
*/ */
public LiveData<Resource<CustomerDTO>> getCustomerById(Long id) { public LiveData<Resource<CustomerDTO>> getCustomerById(Long id) {
MutableLiveData<Resource<CustomerDTO>> data = new MutableLiveData<>(); MutableLiveData<Resource<CustomerDTO>> data = new MutableLiveData<>();
data.setValue(Resource.loading(null)); RetrofitUtils.enqueue(customerApi.getCustomerById(id), data, TAG);
customerApi.getCustomerById(id).enqueue(RetrofitUtils.createSilentCallback(TAG,
result -> data.setValue(Resource.success(result))));
return data; return data;
} }
} }

View File

@@ -31,11 +31,7 @@ public class InventoryRepository {
*/ */
public LiveData<Resource<PageResponse<InventoryDTO>>> getAllInventory(String query, int page, int size, String sort) { public LiveData<Resource<PageResponse<InventoryDTO>>> getAllInventory(String query, int page, int size, String sort) {
MutableLiveData<Resource<PageResponse<InventoryDTO>>> data = new MutableLiveData<>(); MutableLiveData<Resource<PageResponse<InventoryDTO>>> data = new MutableLiveData<>();
data.setValue(Resource.loading(null)); RetrofitUtils.enqueue(inventoryApi.getAllInventory(query, page, size, sort), data, TAG);
inventoryApi.getAllInventory(query, page, size, sort).enqueue(RetrofitUtils.createSilentCallback(TAG,
result -> data.setValue(Resource.success(result))));
return data; return data;
} }
@@ -44,11 +40,7 @@ public class InventoryRepository {
*/ */
public LiveData<Resource<InventoryDTO>> getInventoryById(Long id) { public LiveData<Resource<InventoryDTO>> getInventoryById(Long id) {
MutableLiveData<Resource<InventoryDTO>> data = new MutableLiveData<>(); MutableLiveData<Resource<InventoryDTO>> data = new MutableLiveData<>();
data.setValue(Resource.loading(null)); RetrofitUtils.enqueue(inventoryApi.getInventoryById(id), data, TAG);
inventoryApi.getInventoryById(id).enqueue(RetrofitUtils.createSilentCallback(TAG,
result -> data.setValue(Resource.success(result))));
return data; return data;
} }
@@ -57,21 +49,13 @@ public class InventoryRepository {
*/ */
public LiveData<Resource<InventoryDTO>> createInventory(InventoryRequest request) { public LiveData<Resource<InventoryDTO>> createInventory(InventoryRequest request) {
MutableLiveData<Resource<InventoryDTO>> data = new MutableLiveData<>(); MutableLiveData<Resource<InventoryDTO>> data = new MutableLiveData<>();
data.setValue(Resource.loading(null)); RetrofitUtils.enqueue(inventoryApi.createInventory(request), data, TAG);
inventoryApi.createInventory(request).enqueue(RetrofitUtils.createSilentCallback(TAG,
result -> data.setValue(Resource.success(result))));
return data; return data;
} }
public LiveData<Resource<InventoryDTO>> updateInventory(Long id, InventoryRequest request) { public LiveData<Resource<InventoryDTO>> updateInventory(Long id, InventoryRequest request) {
MutableLiveData<Resource<InventoryDTO>> data = new MutableLiveData<>(); MutableLiveData<Resource<InventoryDTO>> data = new MutableLiveData<>();
data.setValue(Resource.loading(null)); RetrofitUtils.enqueue(inventoryApi.updateInventory(id, request), data, TAG);
inventoryApi.updateInventory(id, request).enqueue(RetrofitUtils.createSilentCallback(TAG,
result -> data.setValue(Resource.success(result))));
return data; return data;
} }
@@ -80,21 +64,13 @@ public class InventoryRepository {
*/ */
public LiveData<Resource<Void>> deleteInventory(Long id) { public LiveData<Resource<Void>> deleteInventory(Long id) {
MutableLiveData<Resource<Void>> data = new MutableLiveData<>(); MutableLiveData<Resource<Void>> data = new MutableLiveData<>();
data.setValue(Resource.loading(null)); RetrofitUtils.enqueue(inventoryApi.deleteInventory(id), data, TAG);
inventoryApi.deleteInventory(id).enqueue(RetrofitUtils.createSilentCallback(TAG,
result -> data.setValue(Resource.success(null))));
return data; return data;
} }
public LiveData<Resource<Void>> bulkDeleteInventory(BulkDeleteRequest request) { public LiveData<Resource<Void>> bulkDeleteInventory(BulkDeleteRequest request) {
MutableLiveData<Resource<Void>> data = new MutableLiveData<>(); MutableLiveData<Resource<Void>> data = new MutableLiveData<>();
data.setValue(Resource.loading(null)); RetrofitUtils.enqueue(inventoryApi.bulkDeleteInventory(request), data, TAG);
inventoryApi.bulkDeleteInventory(request).enqueue(RetrofitUtils.createSilentCallback(TAG,
result -> data.setValue(Resource.success(null))));
return data; return data;
} }
} }

View File

@@ -29,11 +29,7 @@ public class PetRepository {
*/ */
public LiveData<Resource<PageResponse<PetDTO>>> getAllPets(int page, int size) { public LiveData<Resource<PageResponse<PetDTO>>> getAllPets(int page, int size) {
MutableLiveData<Resource<PageResponse<PetDTO>>> data = new MutableLiveData<>(); MutableLiveData<Resource<PageResponse<PetDTO>>> data = new MutableLiveData<>();
data.setValue(Resource.loading(null)); RetrofitUtils.enqueue(petApi.getAllPets(page, size), data, TAG);
petApi.getAllPets(page, size).enqueue(RetrofitUtils.createSilentCallback(TAG,
result -> data.setValue(Resource.success(result))));
return data; return data;
} }
@@ -42,11 +38,7 @@ public class PetRepository {
*/ */
public LiveData<Resource<PetDTO>> getPetById(Long id) { public LiveData<Resource<PetDTO>> getPetById(Long id) {
MutableLiveData<Resource<PetDTO>> data = new MutableLiveData<>(); MutableLiveData<Resource<PetDTO>> data = new MutableLiveData<>();
data.setValue(Resource.loading(null)); RetrofitUtils.enqueue(petApi.getPetById(id), data, TAG);
petApi.getPetById(id).enqueue(RetrofitUtils.createSilentCallback(TAG,
result -> data.setValue(Resource.success(result))));
return data; return data;
} }
@@ -55,11 +47,7 @@ public class PetRepository {
*/ */
public LiveData<Resource<PetDTO>> createPet(PetDTO pet) { public LiveData<Resource<PetDTO>> createPet(PetDTO pet) {
MutableLiveData<Resource<PetDTO>> data = new MutableLiveData<>(); MutableLiveData<Resource<PetDTO>> data = new MutableLiveData<>();
data.setValue(Resource.loading(null)); RetrofitUtils.enqueue(petApi.createPet(pet), data, TAG);
petApi.createPet(pet).enqueue(RetrofitUtils.createSilentCallback(TAG,
result -> data.setValue(Resource.success(result))));
return data; return data;
} }
@@ -68,11 +56,7 @@ public class PetRepository {
*/ */
public LiveData<Resource<PetDTO>> updatePet(Long id, PetDTO pet) { public LiveData<Resource<PetDTO>> updatePet(Long id, PetDTO pet) {
MutableLiveData<Resource<PetDTO>> data = new MutableLiveData<>(); MutableLiveData<Resource<PetDTO>> data = new MutableLiveData<>();
data.setValue(Resource.loading(null)); RetrofitUtils.enqueue(petApi.updatePet(id, pet), data, TAG);
petApi.updatePet(id, pet).enqueue(RetrofitUtils.createSilentCallback(TAG,
result -> data.setValue(Resource.success(result))));
return data; return data;
} }
@@ -81,11 +65,7 @@ public class PetRepository {
*/ */
public LiveData<Resource<Void>> deletePet(Long id) { public LiveData<Resource<Void>> deletePet(Long id) {
MutableLiveData<Resource<Void>> data = new MutableLiveData<>(); MutableLiveData<Resource<Void>> data = new MutableLiveData<>();
data.setValue(Resource.loading(null)); RetrofitUtils.enqueue(petApi.deletePet(id), data, TAG);
petApi.deletePet(id).enqueue(RetrofitUtils.createSilentCallback(TAG,
result -> data.setValue(Resource.success(null))));
return data; return data;
} }
@@ -94,11 +74,7 @@ public class PetRepository {
*/ */
public LiveData<Resource<Void>> uploadPetImage(Long id, MultipartBody.Part image) { public LiveData<Resource<Void>> uploadPetImage(Long id, MultipartBody.Part image) {
MutableLiveData<Resource<Void>> data = new MutableLiveData<>(); MutableLiveData<Resource<Void>> data = new MutableLiveData<>();
data.setValue(Resource.loading(null)); RetrofitUtils.enqueue(petApi.uploadPetImage(id, image), data, TAG);
petApi.uploadPetImage(id, image).enqueue(RetrofitUtils.createSilentCallback(TAG,
result -> data.setValue(Resource.success(null))));
return data; return data;
} }
@@ -107,11 +83,7 @@ public class PetRepository {
*/ */
public LiveData<Resource<Void>> deletePetImage(Long id) { public LiveData<Resource<Void>> deletePetImage(Long id) {
MutableLiveData<Resource<Void>> data = new MutableLiveData<>(); MutableLiveData<Resource<Void>> data = new MutableLiveData<>();
data.setValue(Resource.loading(null)); RetrofitUtils.enqueue(petApi.deletePetImage(id), data, TAG);
petApi.deletePetImage(id).enqueue(RetrofitUtils.createSilentCallback(TAG,
result -> data.setValue(Resource.success(null))));
return data; return data;
} }
} }

View File

@@ -29,11 +29,7 @@ public class ProductRepository {
*/ */
public LiveData<Resource<PageResponse<ProductDTO>>> getAllProducts(String query, int page, int size) { public LiveData<Resource<PageResponse<ProductDTO>>> getAllProducts(String query, int page, int size) {
MutableLiveData<Resource<PageResponse<ProductDTO>>> data = new MutableLiveData<>(); MutableLiveData<Resource<PageResponse<ProductDTO>>> data = new MutableLiveData<>();
data.setValue(Resource.loading(null)); RetrofitUtils.enqueue(productApi.getAllProducts(query, page, size), data, TAG);
productApi.getAllProducts(query, page, size).enqueue(RetrofitUtils.createSilentCallback(TAG,
result -> data.setValue(Resource.success(result))));
return data; return data;
} }
@@ -42,11 +38,7 @@ public class ProductRepository {
*/ */
public LiveData<Resource<ProductDTO>> getProductById(Long id) { public LiveData<Resource<ProductDTO>> getProductById(Long id) {
MutableLiveData<Resource<ProductDTO>> data = new MutableLiveData<>(); MutableLiveData<Resource<ProductDTO>> data = new MutableLiveData<>();
data.setValue(Resource.loading(null)); RetrofitUtils.enqueue(productApi.getProductById(id), data, TAG);
productApi.getProductById(id).enqueue(RetrofitUtils.createSilentCallback(TAG,
result -> data.setValue(Resource.success(result))));
return data; return data;
} }
@@ -55,11 +47,7 @@ public class ProductRepository {
*/ */
public LiveData<Resource<ProductDTO>> createProduct(ProductDTO product) { public LiveData<Resource<ProductDTO>> createProduct(ProductDTO product) {
MutableLiveData<Resource<ProductDTO>> data = new MutableLiveData<>(); MutableLiveData<Resource<ProductDTO>> data = new MutableLiveData<>();
data.setValue(Resource.loading(null)); RetrofitUtils.enqueue(productApi.createProduct(product), data, TAG);
productApi.createProduct(product).enqueue(RetrofitUtils.createSilentCallback(TAG,
result -> data.setValue(Resource.success(result))));
return data; return data;
} }
@@ -68,11 +56,7 @@ public class ProductRepository {
*/ */
public LiveData<Resource<ProductDTO>> updateProduct(Long id, ProductDTO product) { public LiveData<Resource<ProductDTO>> updateProduct(Long id, ProductDTO product) {
MutableLiveData<Resource<ProductDTO>> data = new MutableLiveData<>(); MutableLiveData<Resource<ProductDTO>> data = new MutableLiveData<>();
data.setValue(Resource.loading(null)); RetrofitUtils.enqueue(productApi.updateProduct(id, product), data, TAG);
productApi.updateProduct(id, product).enqueue(RetrofitUtils.createSilentCallback(TAG,
result -> data.setValue(Resource.success(result))));
return data; return data;
} }
@@ -81,11 +65,7 @@ public class ProductRepository {
*/ */
public LiveData<Resource<Void>> deleteProduct(Long id) { public LiveData<Resource<Void>> deleteProduct(Long id) {
MutableLiveData<Resource<Void>> data = new MutableLiveData<>(); MutableLiveData<Resource<Void>> data = new MutableLiveData<>();
data.setValue(Resource.loading(null)); RetrofitUtils.enqueue(productApi.deleteProduct(id), data, TAG);
productApi.deleteProduct(id).enqueue(RetrofitUtils.createSilentCallback(TAG,
result -> data.setValue(Resource.success(null))));
return data; return data;
} }
@@ -94,11 +74,7 @@ public class ProductRepository {
*/ */
public LiveData<Resource<Void>> uploadProductImage(Long id, MultipartBody.Part image) { public LiveData<Resource<Void>> uploadProductImage(Long id, MultipartBody.Part image) {
MutableLiveData<Resource<Void>> data = new MutableLiveData<>(); MutableLiveData<Resource<Void>> data = new MutableLiveData<>();
data.setValue(Resource.loading(null)); RetrofitUtils.enqueue(productApi.uploadProductImage(id, image), data, TAG);
productApi.uploadProductImage(id, image).enqueue(RetrofitUtils.createSilentCallback(TAG,
result -> data.setValue(Resource.success(null))));
return data; return data;
} }
@@ -107,11 +83,7 @@ public class ProductRepository {
*/ */
public LiveData<Resource<Void>> deleteProductImage(Long id) { public LiveData<Resource<Void>> deleteProductImage(Long id) {
MutableLiveData<Resource<Void>> data = new MutableLiveData<>(); MutableLiveData<Resource<Void>> data = new MutableLiveData<>();
data.setValue(Resource.loading(null)); RetrofitUtils.enqueue(productApi.deleteProductImage(id), data, TAG);
productApi.deleteProductImage(id).enqueue(RetrofitUtils.createSilentCallback(TAG,
result -> data.setValue(Resource.success(null))));
return data; return data;
} }
} }

View File

@@ -27,11 +27,7 @@ public class ProductSupplierRepository {
*/ */
public LiveData<Resource<PageResponse<ProductSupplierDTO>>> getAllProductSuppliers(int page, int size) { public LiveData<Resource<PageResponse<ProductSupplierDTO>>> getAllProductSuppliers(int page, int size) {
MutableLiveData<Resource<PageResponse<ProductSupplierDTO>>> data = new MutableLiveData<>(); MutableLiveData<Resource<PageResponse<ProductSupplierDTO>>> data = new MutableLiveData<>();
data.setValue(Resource.loading(null)); RetrofitUtils.enqueue(api.getAllProductSuppliers(page, size), data, TAG);
api.getAllProductSuppliers(page, size).enqueue(RetrofitUtils.createSilentCallback(TAG,
result -> data.setValue(Resource.success(result))));
return data; return data;
} }
@@ -40,11 +36,7 @@ public class ProductSupplierRepository {
*/ */
public LiveData<Resource<ProductSupplierDTO>> createProductSupplier(ProductSupplierDTO dto) { public LiveData<Resource<ProductSupplierDTO>> createProductSupplier(ProductSupplierDTO dto) {
MutableLiveData<Resource<ProductSupplierDTO>> data = new MutableLiveData<>(); MutableLiveData<Resource<ProductSupplierDTO>> data = new MutableLiveData<>();
data.setValue(Resource.loading(null)); RetrofitUtils.enqueue(api.createProductSupplier(dto), data, TAG);
api.createProductSupplier(dto).enqueue(RetrofitUtils.createSilentCallback(TAG,
result -> data.setValue(Resource.success(result))));
return data; return data;
} }
@@ -53,11 +45,7 @@ public class ProductSupplierRepository {
*/ */
public LiveData<Resource<ProductSupplierDTO>> updateProductSupplier(Long productId, Long supplierId, ProductSupplierDTO dto) { public LiveData<Resource<ProductSupplierDTO>> updateProductSupplier(Long productId, Long supplierId, ProductSupplierDTO dto) {
MutableLiveData<Resource<ProductSupplierDTO>> data = new MutableLiveData<>(); MutableLiveData<Resource<ProductSupplierDTO>> data = new MutableLiveData<>();
data.setValue(Resource.loading(null)); RetrofitUtils.enqueue(api.updateProductSupplier(productId, supplierId, dto), data, TAG);
api.updateProductSupplier(productId, supplierId, dto).enqueue(RetrofitUtils.createSilentCallback(TAG,
result -> data.setValue(Resource.success(result))));
return data; return data;
} }
@@ -66,11 +54,7 @@ public class ProductSupplierRepository {
*/ */
public LiveData<Resource<Void>> deleteProductSupplier(Long productId, Long supplierId) { public LiveData<Resource<Void>> deleteProductSupplier(Long productId, Long supplierId) {
MutableLiveData<Resource<Void>> data = new MutableLiveData<>(); MutableLiveData<Resource<Void>> data = new MutableLiveData<>();
data.setValue(Resource.loading(null)); RetrofitUtils.enqueue(api.deleteProductSupplier(productId, supplierId), data, TAG);
api.deleteProductSupplier(productId, supplierId).enqueue(RetrofitUtils.createSilentCallback(TAG,
result -> data.setValue(Resource.success(null))));
return data; return data;
} }
} }

View File

@@ -12,10 +12,6 @@ import com.example.petstoremobile.utils.RetrofitUtils;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
@Singleton @Singleton
public class PurchaseOrderRepository { public class PurchaseOrderRepository {
private static final String TAG = "PurchaseOrderRepo"; private static final String TAG = "PurchaseOrderRepo";
@@ -31,12 +27,7 @@ public class PurchaseOrderRepository {
*/ */
public LiveData<Resource<PageResponse<PurchaseOrderDTO>>> getAllPurchaseOrders(int page, int size) { public LiveData<Resource<PageResponse<PurchaseOrderDTO>>> getAllPurchaseOrders(int page, int size) {
MutableLiveData<Resource<PageResponse<PurchaseOrderDTO>>> data = new MutableLiveData<>(); MutableLiveData<Resource<PageResponse<PurchaseOrderDTO>>> data = new MutableLiveData<>();
data.setValue(Resource.loading(null)); RetrofitUtils.enqueue(api.getAllPurchaseOrders(page, size), data, TAG);
api.getAllPurchaseOrders(page, size).enqueue(RetrofitUtils.createSilentCallback(TAG, result -> {
data.setValue(Resource.success(result));
}));
return data; return data;
} }
@@ -45,12 +36,7 @@ public class PurchaseOrderRepository {
*/ */
public LiveData<Resource<PurchaseOrderDTO>> getPurchaseOrderById(Long id) { public LiveData<Resource<PurchaseOrderDTO>> getPurchaseOrderById(Long id) {
MutableLiveData<Resource<PurchaseOrderDTO>> data = new MutableLiveData<>(); MutableLiveData<Resource<PurchaseOrderDTO>> data = new MutableLiveData<>();
data.setValue(Resource.loading(null)); RetrofitUtils.enqueue(api.getPurchaseOrderById(id), data, TAG);
api.getPurchaseOrderById(id).enqueue(RetrofitUtils.createSilentCallback(TAG, result -> {
data.setValue(Resource.success(result));
}));
return data; return data;
} }
} }

View File

@@ -27,11 +27,7 @@ public class ServiceRepository {
*/ */
public LiveData<Resource<PageResponse<ServiceDTO>>> getAllServices(int page, int size) { public LiveData<Resource<PageResponse<ServiceDTO>>> getAllServices(int page, int size) {
MutableLiveData<Resource<PageResponse<ServiceDTO>>> data = new MutableLiveData<>(); MutableLiveData<Resource<PageResponse<ServiceDTO>>> data = new MutableLiveData<>();
data.setValue(Resource.loading(null)); RetrofitUtils.enqueue(serviceApi.getAllServices(page, size), data, TAG);
serviceApi.getAllServices(page, size).enqueue(RetrofitUtils.createSilentCallback(TAG,
result -> data.setValue(Resource.success(result))));
return data; return data;
} }
@@ -40,11 +36,7 @@ public class ServiceRepository {
*/ */
public LiveData<Resource<ServiceDTO>> getServiceById(Long id) { public LiveData<Resource<ServiceDTO>> getServiceById(Long id) {
MutableLiveData<Resource<ServiceDTO>> data = new MutableLiveData<>(); MutableLiveData<Resource<ServiceDTO>> data = new MutableLiveData<>();
data.setValue(Resource.loading(null)); RetrofitUtils.enqueue(serviceApi.getServiceById(id), data, TAG);
serviceApi.getServiceById(id).enqueue(RetrofitUtils.createSilentCallback(TAG,
result -> data.setValue(Resource.success(result))));
return data; return data;
} }
@@ -53,11 +45,7 @@ public class ServiceRepository {
*/ */
public LiveData<Resource<ServiceDTO>> createService(ServiceDTO service) { public LiveData<Resource<ServiceDTO>> createService(ServiceDTO service) {
MutableLiveData<Resource<ServiceDTO>> data = new MutableLiveData<>(); MutableLiveData<Resource<ServiceDTO>> data = new MutableLiveData<>();
data.setValue(Resource.loading(null)); RetrofitUtils.enqueue(serviceApi.createService(service), data, TAG);
serviceApi.createService(service).enqueue(RetrofitUtils.createSilentCallback(TAG,
result -> data.setValue(Resource.success(result))));
return data; return data;
} }
@@ -66,11 +54,7 @@ public class ServiceRepository {
*/ */
public LiveData<Resource<ServiceDTO>> updateService(Long id, ServiceDTO service) { public LiveData<Resource<ServiceDTO>> updateService(Long id, ServiceDTO service) {
MutableLiveData<Resource<ServiceDTO>> data = new MutableLiveData<>(); MutableLiveData<Resource<ServiceDTO>> data = new MutableLiveData<>();
data.setValue(Resource.loading(null)); RetrofitUtils.enqueue(serviceApi.updateService(id, service), data, TAG);
serviceApi.updateService(id, service).enqueue(RetrofitUtils.createSilentCallback(TAG,
result -> data.setValue(Resource.success(result))));
return data; return data;
} }
@@ -79,11 +63,7 @@ public class ServiceRepository {
*/ */
public LiveData<Resource<Void>> deleteService(Long id) { public LiveData<Resource<Void>> deleteService(Long id) {
MutableLiveData<Resource<Void>> data = new MutableLiveData<>(); MutableLiveData<Resource<Void>> data = new MutableLiveData<>();
data.setValue(Resource.loading(null)); RetrofitUtils.enqueue(serviceApi.deleteService(id), data, TAG);
serviceApi.deleteService(id).enqueue(RetrofitUtils.createSilentCallback(TAG,
result -> data.setValue(Resource.success(null))));
return data; return data;
} }
} }

View File

@@ -27,11 +27,7 @@ public class StoreRepository {
*/ */
public LiveData<Resource<PageResponse<StoreDTO>>> getAllStores(int page, int size) { public LiveData<Resource<PageResponse<StoreDTO>>> getAllStores(int page, int size) {
MutableLiveData<Resource<PageResponse<StoreDTO>>> data = new MutableLiveData<>(); MutableLiveData<Resource<PageResponse<StoreDTO>>> data = new MutableLiveData<>();
data.setValue(Resource.loading(null)); RetrofitUtils.enqueue(storeApi.getAllStores(page, size), data, TAG);
storeApi.getAllStores(page, size).enqueue(RetrofitUtils.createSilentCallback(TAG,
result -> data.setValue(Resource.success(result))));
return data; return data;
} }
} }

View File

@@ -27,11 +27,7 @@ public class SupplierRepository {
*/ */
public LiveData<Resource<PageResponse<SupplierDTO>>> getAllSuppliers(int page, int size) { public LiveData<Resource<PageResponse<SupplierDTO>>> getAllSuppliers(int page, int size) {
MutableLiveData<Resource<PageResponse<SupplierDTO>>> data = new MutableLiveData<>(); MutableLiveData<Resource<PageResponse<SupplierDTO>>> data = new MutableLiveData<>();
data.setValue(Resource.loading(null)); RetrofitUtils.enqueue(supplierApi.getAllSuppliers(page, size), data, TAG);
supplierApi.getAllSuppliers(page, size).enqueue(RetrofitUtils.createSilentCallback(TAG,
result -> data.setValue(Resource.success(result))));
return data; return data;
} }
@@ -40,11 +36,7 @@ public class SupplierRepository {
*/ */
public LiveData<Resource<SupplierDTO>> getSupplierById(Long id) { public LiveData<Resource<SupplierDTO>> getSupplierById(Long id) {
MutableLiveData<Resource<SupplierDTO>> data = new MutableLiveData<>(); MutableLiveData<Resource<SupplierDTO>> data = new MutableLiveData<>();
data.setValue(Resource.loading(null)); RetrofitUtils.enqueue(supplierApi.getSupplierById(id), data, TAG);
supplierApi.getSupplierById(id).enqueue(RetrofitUtils.createSilentCallback(TAG,
result -> data.setValue(Resource.success(result))));
return data; return data;
} }
@@ -53,11 +45,7 @@ public class SupplierRepository {
*/ */
public LiveData<Resource<SupplierDTO>> createSupplier(SupplierDTO supplier) { public LiveData<Resource<SupplierDTO>> createSupplier(SupplierDTO supplier) {
MutableLiveData<Resource<SupplierDTO>> data = new MutableLiveData<>(); MutableLiveData<Resource<SupplierDTO>> data = new MutableLiveData<>();
data.setValue(Resource.loading(null)); RetrofitUtils.enqueue(supplierApi.createSupplier(supplier), data, TAG);
supplierApi.createSupplier(supplier).enqueue(RetrofitUtils.createSilentCallback(TAG,
result -> data.setValue(Resource.success(result))));
return data; return data;
} }
@@ -65,15 +53,9 @@ public class SupplierRepository {
* Sends a request to the API to update an existing supplier record by ID. * Sends a request to the API to update an existing supplier record by ID.
*/ */
public LiveData<Resource<SupplierDTO>> updateSupplier(Long id, SupplierDTO supplier) { public LiveData<Resource<SupplierDTO>> updateSupplier(Long id, SupplierDTO supplier) {
MutableLiveData<Resource<Resource<SupplierDTO>>> data = new MutableLiveData<>(); MutableLiveData<Resource<SupplierDTO>> data = new MutableLiveData<>();
// Note: The original return type was LiveData<Resource<SupplierDTO>>, fixing here RetrofitUtils.enqueue(supplierApi.updateSupplier(id, supplier), data, TAG);
MutableLiveData<Resource<SupplierDTO>> resultData = new MutableLiveData<>(); return data;
resultData.setValue(Resource.loading(null));
supplierApi.updateSupplier(id, supplier).enqueue(RetrofitUtils.createSilentCallback(TAG,
result -> resultData.setValue(Resource.success(result))));
return resultData;
} }
/** /**
@@ -81,11 +63,7 @@ public class SupplierRepository {
*/ */
public LiveData<Resource<Void>> deleteSupplier(Long id) { public LiveData<Resource<Void>> deleteSupplier(Long id) {
MutableLiveData<Resource<Void>> data = new MutableLiveData<>(); MutableLiveData<Resource<Void>> data = new MutableLiveData<>();
data.setValue(Resource.loading(null)); RetrofitUtils.enqueue(supplierApi.deleteSupplier(id), data, TAG);
supplierApi.deleteSupplier(id).enqueue(RetrofitUtils.createSilentCallback(TAG,
result -> data.setValue(Resource.success(null))));
return data; return data;
} }
} }

View File

@@ -15,18 +15,24 @@ public class ErrorUtils {
* Shows an error message to toast based on the response. * Shows an error message to toast based on the response.
*/ */
public static void showErrorMessage(Context context, Response<?> response, String defaultMessage) { 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 { try {
if (response.errorBody() != null) { if (response != null && response.errorBody() != null) {
String errorJson = response.errorBody().string(); String errorJson = response.errorBody().string();
ErrorResponse errorResponse = new Gson().fromJson(errorJson, ErrorResponse.class); ErrorResponse errorResponse = new Gson().fromJson(errorJson, ErrorResponse.class);
if (errorResponse != null && errorResponse.getMessage() != null) { if (errorResponse != null && errorResponse.getMessage() != null) {
Toast.makeText(context, errorResponse.getMessage(), Toast.LENGTH_LONG).show(); return errorResponse.getMessage();
return;
} }
} }
} catch (Exception e) { } catch (Exception e) {
Log.e("ErrorUtils", "Error parsing error body", e); Log.e("ErrorUtils", "Error parsing error body", e);
} }
Toast.makeText(context, defaultMessage, Toast.LENGTH_SHORT).show(); return defaultMessage;
} }
} }

View File

@@ -5,6 +5,7 @@ import android.util.Log;
import android.widget.Toast; import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.Callback; import retrofit2.Callback;
@@ -23,9 +24,36 @@ public class RetrofitUtils {
void onSuccess(T result); 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. * 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) { public static <T> Callback<T> createCallback(Context context, String tag, String successMsg, SuccessCallback<T> successCallback) {
return new Callback<T>() { return new Callback<T>() {
@Override @Override
@@ -54,6 +82,7 @@ public class RetrofitUtils {
/** /**
* Creates a callback that doesn't show toasts * Creates a callback that doesn't show toasts
*/ */
@Deprecated
public static <T> Callback<T> createSilentCallback(String tag, SuccessCallback<T> successCallback) { public static <T> Callback<T> createSilentCallback(String tag, SuccessCallback<T> successCallback) {
return new Callback<T>() { return new Callback<T>() {
@Override @Override