Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Android] feat: 재료 목록 보기 개발(#41) #42

Merged
merged 8 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions Android/.idea/deploymentTargetSelector.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Android/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ dependencies {

// modules
implementation(project(":feature:main"))
implementation(project(":core:data"))

implementation(libs.androidx.core.ktx)
implementation(libs.androidx.lifecycle.runtime.ktx)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.sundaegukbap.banchango.core.data.repository.api

import com.sundaegukbap.banchango.ContainerIngredient

interface IngredientRepository {
suspend fun getIngredientContainers(): Result<List<ContainerIngredient>>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.sundaegukbap.banchango.core.data.api

import com.sundaegukbap.banchango.core.data.api.model.ContainerIngredientDtos
import retrofit2.Response
import retrofit2.http.GET
import retrofit2.http.Path

internal interface IngredientApi {
@GET("api/ingredients/main/list/{userid}")
suspend fun getIngredients(
@Path("userid") userId: Long,
): Response<ContainerIngredientDtos>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.sundaegukbap.banchango.core.data.api.model

import kotlinx.serialization.Serializable

@Serializable
data class ContainerDto(
val containerId: Long,
val containerName: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.sundaegukbap.banchango.core.data.api.model

import kotlinx.serialization.Serializable

@Serializable
data class ContainerIngredientDto(
val containerIngredientId: Long,
val containerDto: ContainerDto,
val ingredientDto: IngredientDto,
val createdAt: String,
val expirationDate: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.sundaegukbap.banchango.core.data.api.model

import kotlinx.serialization.Serializable

@Serializable
data class ContainerIngredientDtos(
val containerIngredientDtos: List<ContainerIngredientDto>
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.sundaegukbap.banchango.core.data.api.model

import kotlinx.serialization.Serializable

@Serializable
data class IngredientDto(
val id: Long,
val name: String,
val kind: String,
val image: String?,
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.sundaegukbap.banchango.core.data.di

import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory
import com.sundaegukbap.banchango.core.data.BuildConfig
import com.sundaegukbap.banchango.core.data.api.IngredientApi
import com.sundaegukbap.banchango.core.data.api.RecipeApi
import dagger.Module
import dagger.Provides
Expand Down Expand Up @@ -55,4 +56,10 @@ internal object ApiModule {
fun providesRecipeRecommendApi(
@DefaultRetrofitQualifier retrofit: Retrofit
): RecipeApi = retrofit.create(RecipeApi::class.java)

@Provides
@Singleton
fun providesIngredientApi(
@DefaultRetrofitQualifier retrofit: Retrofit
): IngredientApi = retrofit.create(IngredientApi::class.java)
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
package com.sundaegukbap.banchango.core.data.di

import com.sundaegukbap.banchango.core.data.repository.DefaultIngredientRepository
import com.sundaegukbap.banchango.core.data.repository.DefaultRecipeRepository
import com.sundaegukbap.banchango.core.data.repository.FakeIngredientRepository
import com.sundaegukbap.banchango.core.data.repository.FakeRecipeRepository
import com.sundaegukbap.banchango.core.data.repository.api.IngredientRepository
import com.sundaegukbap.banchango.core.data.repository.api.RecipeRepository
import dagger.Binds
import dagger.Module
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

@InstallIn(SingletonComponent::class)
@Module
internal abstract class RepositoryModule {
internal interface RepositoryModule {
@Singleton
@Binds
abstract fun bindsRecipeRepository(recipeRepository: DefaultRecipeRepository): RecipeRepository
fun bindsRecipeRepository(recipeRepository: FakeRecipeRepository): RecipeRepository

@Singleton
@Binds
fun bindsIngredientRepository(ingredientRepository: FakeIngredientRepository): IngredientRepository
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.sundaegukbap.banchango.core.data.mapper

import com.sundaegukbap.banchango.Container
import com.sundaegukbap.banchango.Ingredient
import com.sundaegukbap.banchango.ContainerIngredient
import com.sundaegukbap.banchango.IngredientKind
import com.sundaegukbap.banchango.core.data.api.model.ContainerDto
import com.sundaegukbap.banchango.core.data.api.model.ContainerIngredientDto
import com.sundaegukbap.banchango.core.data.api.model.IngredientDto
import java.time.LocalDateTime

internal fun ContainerIngredientDto.toData() = ContainerIngredient(
id = containerIngredientId,
container = containerDto.toData(),
ingredient = ingredientDto.toData(),
createdAt = LocalDateTime.parse(createdAt),
expirationDate = LocalDateTime.parse(expirationDate)
)

internal fun ContainerDto.toData() = Container(
id = containerId,
name = containerName
)

internal fun IngredientDto.toData() = Ingredient(
id = id,
name = name,
kind = when (kind) {
"육류" -> IngredientKind.MEAT
"해산물" -> IngredientKind.SEAFOOD
"채소" -> IngredientKind.VEGETABLE
"과일" -> IngredientKind.FRUIT
"기타" -> IngredientKind.ETC
else -> IngredientKind.ETC
},
image = image ?: ""
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.sundaegukbap.banchango.core.data.repository

import android.util.Log
import com.sundaegukbap.banchango.Container
import com.sundaegukbap.banchango.ContainerIngredient
import com.sundaegukbap.banchango.Ingredient
import com.sundaegukbap.banchango.IngredientKind
import com.sundaegukbap.banchango.core.data.api.IngredientApi
import com.sundaegukbap.banchango.core.data.mapper.toData
import com.sundaegukbap.banchango.core.data.repository.api.IngredientRepository
import java.time.LocalDateTime
import javax.inject.Inject

internal class DefaultIngredientRepository @Inject constructor(
private val ingredientApi: IngredientApi,
) : IngredientRepository {
override suspend fun getIngredientContainers(): Result<List<ContainerIngredient>> {
return runCatching {
val response = ingredientApi.getIngredients(1)
Log.d("asdf", "response: $response")
if (response.isSuccessful) {
if (response.body() == null) {
throw IllegalStateException("Response body is null")
}
response.body()!!.containerIngredientDtos.map { it.toData() }
} else {
throw IllegalStateException("Response is not successful")
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.sundaegukbap.banchango.core.data.repository

import com.sundaegukbap.banchango.Container
import com.sundaegukbap.banchango.ContainerIngredient
import com.sundaegukbap.banchango.Ingredient
import com.sundaegukbap.banchango.IngredientKind
import com.sundaegukbap.banchango.core.data.repository.api.IngredientRepository
import java.time.LocalDateTime
import javax.inject.Inject

internal class FakeIngredientRepository @Inject constructor() : IngredientRepository {
override suspend fun getIngredientContainers(): Result<List<ContainerIngredient>> {
return Result.success(
listOf(
ContainerIngredient(
1,
Container(1, "Container 1"),
Ingredient(1, "Ingredient 1", IngredientKind.SAUCE, "Ingredient 1 Image"),
LocalDateTime.now(),
LocalDateTime.now().plusDays(1)
),
ContainerIngredient(
2,
Container(2, "Container 2"),
Ingredient(2, "Ingredient 2", IngredientKind.VEGETABLE, "Ingredient 2 Image"),
LocalDateTime.now(),
LocalDateTime.now().plusDays(2)
),
ContainerIngredient(
3,
Container(2, "Container 2"),
Ingredient(3, "Ingredient 3", IngredientKind.MEAT, "Ingredient 3 Image"),
LocalDateTime.now(),
LocalDateTime.now().plusDays(3)
),
ContainerIngredient(
4,
Container(2, "Container 2"),
Ingredient(4, "Ingredient 4", IngredientKind.VEGETABLE, "Ingredient 4 Image"),
LocalDateTime.now(),
LocalDateTime.now().plusDays(4)
),
ContainerIngredient(
5,
Container(1, "Container 1"),
Ingredient(5, "Ingredient 5", IngredientKind.SAUCE, "Ingredient 5 Image"),
LocalDateTime.now(),
LocalDateTime.now().plusDays(5)
),
)
)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ val PurpleGrey40 = Color(0xFF625b71)
val Pink40 = Color(0xFF7D5260)

val Orange = Color(0xFFFF7A00)
val LightOrange = Color(0xFFFFC085)
val LightOrange = Color(0xFFFFA857)
val White = Color(0xFFFFFFFF)
val Black = Color(0xFF000000)
val lightGray = Color(0xFFF5F1EE)
val Gray = Color(0xFFF5F1EE)


// 투명도 50%
val SemiTransparentOrange = Color(0x80FF7A00)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.sundaegukbap.banchango

data class Container(
val id: Long,
val name: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.sundaegukbap.banchango

import java.time.LocalDateTime

data class ContainerIngredient(
val id: Long,
val container: Container,
val ingredient: Ingredient,
val createdAt: LocalDateTime,
val expirationDate: LocalDateTime
)

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.sundaegukbap.banchango

data class IngredientContainer(
val container: Container,
val kindIngredientContainers: List<KindIngredientContainer>
)

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.sundaegukbap.banchango

class ContainerIngredients(
containerIngredients: List<ContainerIngredient>
) {
private val _value: MutableList<ContainerIngredient> = containerIngredients.toMutableList()
val value: List<ContainerIngredient> get() = _value.toList()

fun getIngredientContainers(): List<IngredientContainer> {
return _value.groupBy { it.container }
.map { (container, containerIngredients) ->
IngredientContainer(
container = container,
kindIngredientContainers = containerIngredients.toKindIngredientContainers()
)
}
}

private fun List<ContainerIngredient>.toKindIngredientContainers(): List<KindIngredientContainer> {
return groupBy { it.ingredient.kind }
.map { (kind, ingredients) ->
KindIngredientContainer(
kind = kind,
ingredients = ingredients
)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.sundaegukbap.banchango

enum class IngredientKind {
MEAT,
VEGETABLE,
FRUIT,
SEAFOOD,
SAUCE,
ETC,
enum class IngredientKind(val label: String) {
MEAT("육류"),
VEGETABLE("채소"),
FRUIT("과일"),
SEAFOOD("해산물"),
SAUCE("소스"),
ETC("기타"),
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.sundaegukbap.banchango

data class KindIngredientContainer(
val kind: IngredientKind,
val ingredients: List<ContainerIngredient>
)
1 change: 1 addition & 0 deletions Android/feature/home/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ android {

dependencies {
implementation(project(":core:model"))
implementation(project(":core:data-api"))
implementation(project(":core:designsystem"))
implementation(project(":core:navigation"))
implementation(libs.androidx.core.ktx)
Expand Down
Loading
Loading