Skip to content

Commit

Permalink
Merge pull request #248 from hieuwu/feature/improve-usecase-code
Browse files Browse the repository at this point in the history
Simplify single method with invoke operator
  • Loading branch information
hieuwu authored Jul 29, 2024
2 parents dba2bdd + fd9dd14 commit 5b9c64a
Show file tree
Hide file tree
Showing 43 changed files with 74 additions and 74 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.hieuwu.groceriesstore.domain.usecases

interface SuspendUseCase<Input, Output> {
suspend fun execute(input: Input): Output
suspend operator fun invoke(input: Input): Output
}

interface UseCase<Input, Output> {
fun execute(input: Input): Output
operator fun invoke(input: Input): Output
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import javax.inject.Inject
class AddMealToPlanUseCaseImpl @Inject constructor(
private val mealPlanRepository: MealPlanRepository
) : AddMealToPlanUseCase {
override suspend fun execute(input: AddMealToPlanUseCase.Input): AddMealToPlanUseCase.Output {
override suspend fun invoke(input: AddMealToPlanUseCase.Input): AddMealToPlanUseCase.Output {
mealPlanRepository.addMealToPlan(
weekDay = input.weekDay,
name = input.name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class AddToCartUseCaseImpl @Inject constructor(
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
) :
AddToCartUseCase {
override suspend fun execute(input: AddToCartUseCase.Input): AddToCartUseCase.Output {
override suspend fun invoke(input: AddToCartUseCase.Input): AddToCartUseCase.Output {
withContext(ioDispatcher) {
orderRepository.addLineItem(input.lineItem)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class CreateNewOrderUseCaseImpl @Inject constructor(
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
) :
CreateNewOrderUseCase {
override suspend fun execute(input: CreateNewOrderUseCase.Input): CreateNewOrderUseCase.Output {
override suspend fun invoke(input: CreateNewOrderUseCase.Input): CreateNewOrderUseCase.Output {
return withContext(ioDispatcher) {
val result = orderRepository.createOrUpdate(input.order)
CreateNewOrderUseCase.Output(result)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import javax.inject.Inject
class GetCategoriesListUseCaseImpl @Inject constructor(
private val categoryRepository: CategoryRepository
) : GetCategoriesListUseCase {
override suspend fun execute(input: GetCategoriesListUseCase.Input): GetCategoriesListUseCase.Output {
override suspend fun invoke(input: GetCategoriesListUseCase.Input): GetCategoriesListUseCase.Output {
val result = categoryRepository.getFromLocal()
return GetCategoriesListUseCase.Output(result)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class GetCurrentCartUseCaseImpl @Inject constructor(
private val orderRepository: OrderRepository,
) : GetCurrentCartUseCase {

override suspend fun execute(input: GetCurrentCartUseCase.Input): GetCurrentCartUseCase.Output {
override suspend fun invoke(input: GetCurrentCartUseCase.Input): GetCurrentCartUseCase.Output {
val res = orderRepository.getOneOrderByStatus(OrderStatus.IN_CART)
return GetCurrentCartUseCase.Output(res)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class GetOrderListUseCaseImpl @Inject constructor(
private val orderRepository: OrderRepository,
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
) : GetOrderListUseCase {
override suspend fun execute(input: GetOrderListUseCase.Input): GetOrderListUseCase.Output {
override suspend fun invoke(input: GetOrderListUseCase.Input): GetOrderListUseCase.Output {
return withContext(ioDispatcher) {
val result = orderRepository.getOrders()
GetOrderListUseCase.Output.Success(data = result)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import javax.inject.Inject

class GetProductsByCategoryUseCaseImpl @Inject constructor(private val productRepository: ProductRepository) :
GetProductsByCategoryUseCase {
override fun execute(input: GetProductsByCategoryUseCase.Input): GetProductsByCategoryUseCase.Output {
override fun invoke(input: GetProductsByCategoryUseCase.Input): GetProductsByCategoryUseCase.Output {
val result = productRepository.getAllProductsByCategory(input.categoryId)
return GetProductsByCategoryUseCase.Output(result)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import javax.inject.Inject

class GetProductsListUseCaseImpl @Inject constructor(private val productRepository: ProductRepository) :
GetProductsListUseCase {
override fun execute(input: GetProductsListUseCase.Input): GetProductsListUseCase.Output {
override fun invoke(input: GetProductsListUseCase.Input): GetProductsListUseCase.Output {
val result = productRepository.products
return GetProductsListUseCase.Output(result)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class GetProfileUseCaseImpl @Inject constructor(
private val userRepository: UserRepository,
) :
GetProfileUseCase {
override suspend fun execute(input: GetProfileUseCase.Input): GetProfileUseCase.Output {
override suspend fun invoke(input: GetProfileUseCase.Input): GetProfileUseCase.Output {
val user = userRepository.getCurrentUser()
return GetProfileUseCase.Output(user)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class RefreshAppDataUseCaseImpl @Inject constructor(
private val recipeRepository: RecipeRepository,
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
) : RefreshAppDataUseCase {
override suspend fun execute(input: Unit) {
override suspend fun invoke(input: Unit) {
withContext(ioDispatcher) {
categoryRepository.refreshDatabase()
productRepository.refreshDatabase()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class RemoveMealFromPlanUseCaseImpl @Inject constructor(
private val mealPlanRepository: MealPlanRepository,
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
) : RemoveMealFromPlanUseCase {
override suspend fun execute(input: RemoveMealFromPlanUseCase.Input): RemoveMealFromPlanUseCase.Output {
override suspend fun invoke(input: RemoveMealFromPlanUseCase.Input): RemoveMealFromPlanUseCase.Output {
return withContext(ioDispatcher) {
try {
mealPlanRepository.removeMealFromPlan(input.id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class RetrieveMealByTypeUseCaseImpl @Inject constructor(
private val mealPlanRepository: MealPlanRepository,
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
) : RetrieveMealByTypeUseCase {
override suspend fun execute(input: RetrieveMealByTypeUseCase.Input): RetrieveMealByTypeUseCase.Output {
override suspend fun invoke(input: RetrieveMealByTypeUseCase.Input): RetrieveMealByTypeUseCase.Output {
return withContext(ioDispatcher) {
try {
val result = mealPlanRepository.retrieveMealByType(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class SearchProductUseCaseImpl @Inject constructor(
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
) :
SearchProductUseCase {
override suspend fun execute(input: SearchProductUseCase.Input): SearchProductUseCase.Output {
override suspend fun invoke(input: SearchProductUseCase.Input): SearchProductUseCase.Output {
return withContext(ioDispatcher) {
val result = productRepository.searchProductsListByName(input.name)
SearchProductUseCase.Output(result)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class SignInUseCaseImpl @Inject constructor(
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
) :
SignInUseCase {
override suspend fun execute(input: SignInUseCase.Input): SignInUseCase.Output {
override suspend fun invoke(input: SignInUseCase.Input): SignInUseCase.Output {
return withContext(ioDispatcher) {
try {
val res = userRepository.authenticate(input.email, input.password)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class SignOutUseCaseImpl @Inject constructor(
@IoDispatcher private val ioDispatcher: CoroutineDispatcher
) :
SignOutUseCase {
override suspend fun execute(input: SignOutUseCase.Input): SignOutUseCase.Output {
override suspend fun invoke(input: SignOutUseCase.Input): SignOutUseCase.Output {
return withContext(ioDispatcher) {
userRepository.clearUser()
SignOutUseCase.Output()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class SubmitOrderUseCaseImpl @Inject constructor(
private val orderRepository: OrderRepository,
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
) : SubmitOrderUseCase {
override suspend fun execute(input: SubmitOrderUseCase.Input): SubmitOrderUseCase.Output {
override suspend fun invoke(input: SubmitOrderUseCase.Input): SubmitOrderUseCase.Output {
return withContext(ioDispatcher) {
val result = orderRepository.sendOrderToServer(input.order)
SubmitOrderUseCase.Output(result)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class UpdateProfileUseCaseImpl @Inject constructor(
private val userRepository: UserRepository,
) :
UpdateProfileUseCase {
override suspend fun execute(input: UpdateProfileUseCase.Input): UpdateProfileUseCase.Output {
override suspend fun invoke(input: UpdateProfileUseCase.Input): UpdateProfileUseCase.Output {
userRepository.updateUserProfile(
input.userId, input.name, input.email, input.phone, input.address
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class UserSettingsUseCaseImpl @Inject constructor(
) :
UserSettingsUseCase {

override suspend fun execute(input: UserSettingsUseCase.Input): UserSettingsUseCase.Output {
override suspend fun invoke(input: UserSettingsUseCase.Input): UserSettingsUseCase.Output {
withContext(ioDispatcher) {
userRepository.updateUserSettings(
input.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ class AccountViewModel @Inject constructor(

private fun getCurrentUser() {
viewModelScope.launch {
getProfileUseCase.execute(GetProfileUseCase.Input()).result.collect {
getProfileUseCase(GetProfileUseCase.Input()).result.collect {
_user.value = it
}
}
}

fun signOut() {
viewModelScope.launch {
signOutUseCase.execute(SignOutUseCase.Input())
signOutUseCase(SignOutUseCase.Input())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class SignInViewModel @Inject constructor(

fun signIn() {
viewModelScope.launch {
when (signInUseCase.execute(
when (signInUseCase(
SignInUseCase.Input(
email = _email.value,
password = _password.value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ class CheckOutViewModel @Inject constructor(
private fun getCurrentCard(): Flow<OrderModel?>? {
var res: Flow<OrderModel?>? = null
viewModelScope.launch {
res = getCurrentCartUseCase.execute(GetCurrentCartUseCase.Input()).result
res = getCurrentCartUseCase(GetCurrentCartUseCase.Input()).result
}
return res
}

private fun getCurrentUser(): Flow<UserModel?>? {
var res: Flow<UserModel?>? = null
viewModelScope.launch {
res = getProfileUseCase.execute(GetProfileUseCase.Input()).result
res = getProfileUseCase(GetProfileUseCase.Input()).result
}
return res
}
Expand All @@ -75,7 +75,7 @@ class CheckOutViewModel @Inject constructor(
setOrderAddress()
viewModelScope.launch {
_isOrderSentSuccessful.value =
submitOrderUseCase.execute(SubmitOrderUseCase.Input(order.value!!)).result
submitOrderUseCase(SubmitOrderUseCase.Input(order.value!!)).result
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class ExploreViewModel @Inject constructor(
if (name.isNotBlank()) {
viewModelScope.launch {
val res =
searchProductUseCase.execute(SearchProductUseCase.Input(name = name.trim()))
searchProductUseCase(SearchProductUseCase.Input(name = name.trim()))
res.result.collect {
_productList.value = it
}
Expand All @@ -78,15 +78,15 @@ class ExploreViewModel @Inject constructor(
private fun getCurrentCard(): Flow<OrderModel?>? {
var res: Flow<OrderModel?>? = null
viewModelScope.launch {
res = getCurrentCartUseCase.execute(GetCurrentCartUseCase.Input()).result
res = getCurrentCartUseCase(GetCurrentCartUseCase.Input()).result
}
return res
}

private fun getCategoriesList(): Flow<List<CategoryModel>>? {
var res: Flow<List<CategoryModel>>? = null
viewModelScope.launch {
res = getCategoriesListUseCase.execute(GetCategoriesListUseCase.Input()).result
res = getCategoriesListUseCase(GetCategoriesListUseCase.Input()).result
}
return res
}
Expand All @@ -102,20 +102,20 @@ class ExploreViewModel @Inject constructor(
quantity = 1,
subtotal = product.price!!
)
addToCartUseCase.execute(AddToCartUseCase.Input(lineItem = lineItem))
addToCartUseCase(AddToCartUseCase.Input(lineItem = lineItem))
}
} else {
val id = UUID.randomUUID().toString()
val newOrder = Order(id, OrderStatus.IN_CART.value, "")
viewModelScope.launch {
createNewOrderUseCase.execute(CreateNewOrderUseCase.Input(order = newOrder))
createNewOrderUseCase(CreateNewOrderUseCase.Input(order = newOrder))
val lineItem = LineItem(
productId = product.id,
orderId = id,
quantity = 1,
subtotal = product.price!!
)
addToCartUseCase.execute(AddToCartUseCase.Input(lineItem = lineItem))
addToCartUseCase(AddToCartUseCase.Input(lineItem = lineItem))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class OverviewViewModel @Inject constructor(
weekDayValue: WeekDayValue,
mealType: MealType,
) {
val result = retrieveMealByTypeUseCase.execute(
val result = retrieveMealByTypeUseCase(
RetrieveMealByTypeUseCase.Input(
dayValue = weekDayValue,
mealType = mealType
Expand Down Expand Up @@ -126,7 +126,7 @@ class OverviewViewModel @Inject constructor(

fun onRemoveMeal(id: String) {
viewModelScope.launch {
removeMealFromPlanUseCase.execute(
removeMealFromPlanUseCase(
RemoveMealFromPlanUseCase.Input(
id = id
)
Expand All @@ -149,7 +149,7 @@ class OverviewViewModel @Inject constructor(

private fun onAddBreakfast(name: String, ingredients: List<String>, mealImageUri: ByteArray) {
viewModelScope.launch {
addMealToPlanUseCase.execute(
addMealToPlanUseCase(
AddMealToPlanUseCase.Input(
name = name,
weekDay = _days.value[selectedDayIndex].name,
Expand All @@ -167,7 +167,7 @@ class OverviewViewModel @Inject constructor(

private fun onAddLunch(name: String, ingredients: List<String>, mealImageUri: ByteArray) {
viewModelScope.launch {
addMealToPlanUseCase.execute(
addMealToPlanUseCase(
AddMealToPlanUseCase.Input(
name = name,
weekDay = _days.value[selectedDayIndex].name,
Expand All @@ -185,7 +185,7 @@ class OverviewViewModel @Inject constructor(

private fun onAddDinner(name: String, ingredients: List<String>, mealImageUri: ByteArray) {
viewModelScope.launch {
addMealToPlanUseCase.execute(
addMealToPlanUseCase(
AddMealToPlanUseCase.Input(
name = name,
weekDay = _days.value[selectedDayIndex].name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class NotificationSettingsViewModel @Inject constructor(

private fun getCurrentUser() {
viewModelScope.launch {
getProfileUseCase.execute(GetProfileUseCase.Input()).result.collect {
getProfileUseCase(GetProfileUseCase.Input()).result.collect {
_user.value = it
}
}
Expand All @@ -65,7 +65,7 @@ class NotificationSettingsViewModel @Inject constructor(
fun updateNotificationSettings() {
Timber.d("promo: $isPromotionNotiEnabled, ord: $isOrderCreatedNotiEnabled, db: $isDatabaseRefreshedNotiEnabled")
viewModelScope.launch {
userSettingsUseCase.execute(
userSettingsUseCase(
UserSettingsUseCase.Input(
user.value?.id!!,
isOrderCreatedNotiEnabled.value!!,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class OnboardingViewModel @Inject constructor(
} else {
try {
viewModelScope.launch {
refreshAppDataUseCase.execute(Unit)
refreshAppDataUseCase(Unit)
with(sharedPreferences.edit()) {
putBoolean("PRODUCT_SYNC_SUCCESS", true)
apply()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class OrderListViewModel @Inject constructor(

private fun getOrderList() {
viewModelScope.launch {
when (val result = getOrderListUseCase.execute(GetOrderListUseCase.Input())) {
when (val result = getOrderListUseCase(GetOrderListUseCase.Input())) {
is GetOrderListUseCase.Output.Success -> {
_orderList.emit(result.data)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,24 @@ class ProductListViewModel @Inject constructor(
private fun getCurrentCart(): Flow<OrderModel?>? {
var res: Flow<OrderModel?>? = null
viewModelScope.launch {
res = getCurrentCartUseCase.execute(GetCurrentCartUseCase.Input()).result
res = getCurrentCartUseCase(GetCurrentCartUseCase.Input()).result
}
return res
}

private fun getProductLists(): Flow<List<ProductModel>> {
return getProductsListUseCase.execute(GetProductsListUseCase.Input()).result
return getProductsListUseCase(GetProductsListUseCase.Input()).result
}

private fun getProductLists(categoryId: String): Flow<List<ProductModel>> {
return getProductsByCategoryUseCase.execute(GetProductsByCategoryUseCase.Input(categoryId)).result
return getProductsByCategoryUseCase(GetProductsByCategoryUseCase.Input(categoryId)).result
}

fun addToCart(product: ProductModel) {
viewModelScope.launch {
if (currentCart.value != null) {
// Add to cart
addToCartUseCase.execute(
addToCartUseCase(
AddToCartUseCase.Input(
LineItem(
productId = product.id,
Expand All @@ -94,8 +94,8 @@ class ProductListViewModel @Inject constructor(
// TODO: Move order creation to repository layer, at this point only pass domain object
val id = UUID.randomUUID().toString()
val newOrder = Order(id, OrderStatus.IN_CART.value, "")
createNewOrderUseCase.execute(CreateNewOrderUseCase.Input(newOrder))
addToCartUseCase.execute(
createNewOrderUseCase(CreateNewOrderUseCase.Input(newOrder))
addToCartUseCase(
AddToCartUseCase.Input(
LineItem(
productId = product.id,
Expand Down
Loading

0 comments on commit 5b9c64a

Please sign in to comment.