-
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #246 from hieuwu/feature/overview-meal-planning
Meal planning
- Loading branch information
Showing
38 changed files
with
1,304 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
app/src/main/java/com/hieuwu/groceriesstore/data/network/dto/Meal.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.hieuwu.groceriesstore.data.network.dto | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class Meal( | ||
@SerialName("id") | ||
val id: String, | ||
|
||
@SerialName("name") | ||
val name: String, | ||
|
||
@SerialName("ingredients") | ||
val ingredients: List<String>, | ||
|
||
@SerialName("week_day") | ||
val weekDay: String, | ||
|
||
@SerialName("creator") | ||
val creatorId: String, | ||
|
||
@SerialName("meal_type") | ||
val mealType: String, | ||
) |
15 changes: 15 additions & 0 deletions
15
app/src/main/java/com/hieuwu/groceriesstore/data/repository/MealPlanRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.hieuwu.groceriesstore.data.repository | ||
|
||
import com.hieuwu.groceriesstore.domain.models.MealModel | ||
|
||
interface MealPlanRepository { | ||
suspend fun addMealToPlan( | ||
weekDay: String, | ||
name: String, | ||
ingredients: List<String>, | ||
mealType: String | ||
) | ||
|
||
suspend fun retrieveMealByType(type: String, weekDayValue: String): List<MealModel> | ||
suspend fun removeMealFromPlan(id: String) | ||
} |
66 changes: 66 additions & 0 deletions
66
app/src/main/java/com/hieuwu/groceriesstore/data/repository/impl/MealPlanRepositoryImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.hieuwu.groceriesstore.data.repository.impl | ||
|
||
import com.hieuwu.groceriesstore.data.network.dto.Meal | ||
import com.hieuwu.groceriesstore.data.network.dto.ProductDto | ||
import com.hieuwu.groceriesstore.data.repository.MealPlanRepository | ||
import com.hieuwu.groceriesstore.data.repository.UserRepository | ||
import com.hieuwu.groceriesstore.domain.models.MealModel | ||
import io.github.jan.supabase.postgrest.Postgrest | ||
import java.util.UUID | ||
import kotlinx.coroutines.flow.first | ||
import timber.log.Timber | ||
import javax.inject.Inject | ||
|
||
class MealPlanRepositoryImpl @Inject constructor( | ||
private val postgrest: Postgrest, | ||
private val userRepository: UserRepository | ||
) : MealPlanRepository { | ||
override suspend fun addMealToPlan( | ||
weekDay: String, | ||
name: String, | ||
ingredients: List<String>, | ||
mealType: String | ||
) { | ||
val userId = userRepository.getCurrentUser().first()?.id ?: "" | ||
postgrest["meal_plans"].insert<Meal>( | ||
Meal( | ||
id = UUID.randomUUID().toString(), | ||
name = name, | ||
ingredients = ingredients, | ||
creatorId = userId, | ||
weekDay = weekDay, | ||
mealType = mealType, | ||
) | ||
) | ||
} | ||
|
||
override suspend fun retrieveMealByType(type: String, weekDayValue: String): List<MealModel> { | ||
val userId = userRepository.getCurrentUser().first()?.id ?: "" | ||
val result = postgrest["meal_plans"].select { | ||
filter { | ||
eq("creator", userId) | ||
eq("week_day", weekDayValue) | ||
eq("meal_type", type) | ||
} | ||
}.decodeList<Meal>().map { | ||
Timber.d(it.toString()) | ||
MealModel( | ||
id = it.id, | ||
name = it.name, | ||
ingredients = it.ingredients, | ||
weekDay = it.weekDay, | ||
creatorId = it.creatorId, | ||
mealType = it.mealType | ||
) | ||
} | ||
return result | ||
} | ||
|
||
override suspend fun removeMealFromPlan(id: String) { | ||
postgrest["meal_plans"].delete { | ||
filter { | ||
eq("id", id) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
app/src/main/java/com/hieuwu/groceriesstore/domain/models/MealModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.hieuwu.groceriesstore.domain.models | ||
|
||
data class MealModel( | ||
val id: String, | ||
val name: String, | ||
val ingredients: List<String>, | ||
val weekDay: String, | ||
val creatorId: String, | ||
val mealType: String, | ||
) |
11 changes: 11 additions & 0 deletions
11
app/src/main/java/com/hieuwu/groceriesstore/domain/usecases/AddMealToPlanUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.hieuwu.groceriesstore.domain.usecases | ||
|
||
import com.hieuwu.groceriesstore.presentation.mealplanning.overview.state.MealType | ||
|
||
interface AddMealToPlanUseCase : | ||
SuspendUseCase<AddMealToPlanUseCase.Input, AddMealToPlanUseCase.Output> { | ||
class Input(val name: String, val weekDay: String, val ingredients: List<String>, | ||
val mealType: MealType | ||
) | ||
object Output | ||
} |
10 changes: 10 additions & 0 deletions
10
app/src/main/java/com/hieuwu/groceriesstore/domain/usecases/RemoveMealFromPlanUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.hieuwu.groceriesstore.domain.usecases | ||
|
||
interface RemoveMealFromPlanUseCase : | ||
SuspendUseCase<RemoveMealFromPlanUseCase.Input, RemoveMealFromPlanUseCase.Output> { | ||
class Input(val id: String) | ||
sealed class Output { | ||
object Success: Output() | ||
object Failure: Output() | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
app/src/main/java/com/hieuwu/groceriesstore/domain/usecases/RetrieveMealByTypeUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.hieuwu.groceriesstore.domain.usecases | ||
|
||
import com.hieuwu.groceriesstore.domain.models.MealModel | ||
import com.hieuwu.groceriesstore.presentation.mealplanning.overview.state.MealType | ||
import com.hieuwu.groceriesstore.presentation.mealplanning.overview.state.WeekDayValue | ||
|
||
interface RetrieveMealByTypeUseCase : | ||
SuspendUseCase<RetrieveMealByTypeUseCase.Input, RetrieveMealByTypeUseCase.Output> { | ||
class Input( | ||
val dayValue: WeekDayValue, | ||
val mealType: MealType, | ||
) | ||
|
||
sealed class Output { | ||
class Success(val data: List<MealModel>) : Output() | ||
data object Failure : Output() | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
app/src/main/java/com/hieuwu/groceriesstore/domain/usecases/impl/AddMealToPlanUseCaseImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.hieuwu.groceriesstore.domain.usecases.impl | ||
|
||
import com.hieuwu.groceriesstore.data.repository.MealPlanRepository | ||
import com.hieuwu.groceriesstore.domain.usecases.AddMealToPlanUseCase | ||
import javax.inject.Inject | ||
|
||
class AddMealToPlanUseCaseImpl @Inject constructor( | ||
private val mealPlanRepository: MealPlanRepository | ||
) : AddMealToPlanUseCase { | ||
override suspend fun execute(input: AddMealToPlanUseCase.Input): AddMealToPlanUseCase.Output { | ||
mealPlanRepository.addMealToPlan( | ||
weekDay = input.weekDay, | ||
name = input.name, | ||
ingredients = input.ingredients, | ||
mealType = input.mealType.value | ||
) | ||
return AddMealToPlanUseCase.Output | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...main/java/com/hieuwu/groceriesstore/domain/usecases/impl/RemoveMealFromPlanUseCaseImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.hieuwu.groceriesstore.domain.usecases.impl | ||
|
||
import com.hieuwu.groceriesstore.data.repository.MealPlanRepository | ||
import com.hieuwu.groceriesstore.di.IoDispatcher | ||
import com.hieuwu.groceriesstore.domain.usecases.RemoveMealFromPlanUseCase | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.withContext | ||
import javax.inject.Inject | ||
|
||
class RemoveMealFromPlanUseCaseImpl @Inject constructor( | ||
private val mealPlanRepository: MealPlanRepository, | ||
@IoDispatcher private val ioDispatcher: CoroutineDispatcher, | ||
) : RemoveMealFromPlanUseCase { | ||
override suspend fun execute(input: RemoveMealFromPlanUseCase.Input): RemoveMealFromPlanUseCase.Output { | ||
return withContext(ioDispatcher) { | ||
try { | ||
mealPlanRepository.removeMealFromPlan(input.id) | ||
RemoveMealFromPlanUseCase.Output.Success | ||
} catch (e: Exception) { | ||
RemoveMealFromPlanUseCase.Output.Failure | ||
} | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...main/java/com/hieuwu/groceriesstore/domain/usecases/impl/RetrieveMealByTypeUseCaseImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.hieuwu.groceriesstore.domain.usecases.impl | ||
|
||
import com.hieuwu.groceriesstore.data.repository.MealPlanRepository | ||
import com.hieuwu.groceriesstore.di.IoDispatcher | ||
import com.hieuwu.groceriesstore.domain.models.MealModel | ||
import com.hieuwu.groceriesstore.domain.usecases.RetrieveMealByTypeUseCase | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.flow.first | ||
import kotlinx.coroutines.flow.firstOrNull | ||
import kotlinx.coroutines.flow.flow | ||
import kotlinx.coroutines.flow.flowOn | ||
import kotlinx.coroutines.withContext | ||
import javax.inject.Inject | ||
|
||
class RetrieveMealByTypeUseCaseImpl @Inject constructor( | ||
private val mealPlanRepository: MealPlanRepository, | ||
@IoDispatcher private val ioDispatcher: CoroutineDispatcher, | ||
) : RetrieveMealByTypeUseCase { | ||
override suspend fun execute(input: RetrieveMealByTypeUseCase.Input): RetrieveMealByTypeUseCase.Output { | ||
return withContext(ioDispatcher) { | ||
try { | ||
val result = mealPlanRepository.retrieveMealByType( | ||
type = input.mealType.value, | ||
weekDayValue = input.dayValue.dayValue | ||
) | ||
RetrieveMealByTypeUseCase.Output.Success(data = result) | ||
} catch (e: Exception) { | ||
RetrieveMealByTypeUseCase.Output.Failure | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.