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

냉장고 칸별 재료 목록보기 추가 #57

Merged
merged 4 commits into from
Oct 11, 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
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ import com.sundaegukbap.banchango.ContainerIngredient

interface IngredientRepository {
suspend fun getIngredientContainers(): Result<List<ContainerIngredient>>
suspend fun addIngredientContainer(containerName: String): Result<Unit>
}
1 change: 1 addition & 0 deletions Android/core/data/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ dependencies {
implementation(libs.okhttp3.okhttp)
implementation(libs.retrofit2.kotlinx.serialization.converter)
implementation(libs.kotlinx.serialization.json)
implementation(libs.retrofit.adapters.result)
}

fun getSecretKey(propertyKey: String): String {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
package com.sundaegukbap.banchango.core.data.api

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

internal interface IngredientApi {
@GET("api/ingredients/main/list/{userid}")
suspend fun getIngredients(
@Path("userid") userId: Long,
): Response<ContainerIngredientDtos>
}
): Result<ContainerIngredientDtos>

@POST("/api/container/{userId}")
suspend fun addIngredientContainer(
@Path("userId") userId: Long,
@Body addIngredientContainerRequest: AddIngredientContainerRequest,
): Result<Unit>
}

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 AddIngredientContainerRequest(
val containerName: String,
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.sundaegukbap.banchango.core.data.di

import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory
import com.skydoves.retrofit.adapters.result.ResultCallAdapterFactory
import com.sundaegukbap.banchango.core.data.BuildConfig
import com.sundaegukbap.banchango.core.data.api.IngredientApi
import com.sundaegukbap.banchango.core.data.api.RecipeApi
Expand Down Expand Up @@ -44,6 +45,7 @@ internal object ApiModule {
): Retrofit = Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(converterFactory)
.addCallAdapterFactory(ResultCallAdapterFactory.create())
.build()

@Provides
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ 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
Expand All @@ -17,9 +15,9 @@ import javax.inject.Singleton
internal interface RepositoryModule {
@Singleton
@Binds
fun bindsRecipeRepository(recipeRepository: FakeRecipeRepository): RecipeRepository
fun bindsRecipeRepository(recipeRepository: DefaultRecipeRepository): RecipeRepository

@Singleton
@Binds
fun bindsIngredientRepository(ingredientRepository: FakeIngredientRepository): IngredientRepository
fun bindsIngredientRepository(ingredientRepository: DefaultIngredientRepository): IngredientRepository
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,22 @@
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.api.model.AddIngredientContainerRequest
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")
}
return ingredientApi.getIngredients(1).mapCatching { dto ->
dto.containerIngredientDtos.map { it.toData() }
}
}

override suspend fun addIngredientContainer(containerName: String): Result<Unit> {
return ingredientApi.addIngredientContainer(1, AddIngredientContainerRequest(containerName))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,7 @@ internal class FakeIngredientRepository @Inject constructor() : IngredientReposi
)
}

override suspend fun addIngredientContainer(containerName: String): Result<Unit> {
return Result.success(Unit)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import com.sundaegukbap.banchango.core.designsystem.theme.BanchangoTheme

@OptIn(ExperimentalGlideComposeApi::class)
@Composable
fun NetworkImage(modifier: Modifier, url: String) {
fun NetworkImage(url: String, modifier: Modifier = Modifier) {
GlideImage(
model = url,
contentScale = ContentScale.Crop,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,32 @@ package com.sundaegukbap.banchango
class ContainerIngredients(
containerIngredients: List<ContainerIngredient>
) {
private val _value: MutableList<ContainerIngredient> = containerIngredients.toMutableList()
val value: List<ContainerIngredient> get() = _value.toList()
private val _value: MutableMap<Container, IngredientContainer> = containerIngredients
.groupBy { it.container }
.mapValues { (container, containerIngredients) ->
IngredientContainer(
container = container,
kindIngredientContainers = containerIngredients.toKindIngredientContainers()
)
}.toMutableMap()

val value: Map<Container, IngredientContainer> get() = _value.toMap()

fun getKindIngredientContainerDetail(
container: Container,
kind: IngredientKind
): KindIngredientContainer {
return _value[container]?.kindIngredientContainers?.find { it.kind == kind }!!
}


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

private fun List<ContainerIngredient>.toKindIngredientContainers(): List<KindIngredientContainer> {
Expand Down
5 changes: 5 additions & 0 deletions Android/feature/home/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ dependencies {
implementation(libs.navigation.compose)
implementation(libs.hilt.navigation.compose)

implementation(libs.orbit.core)
implementation(libs.orbit.viewmodel)
implementation(libs.orbit.compose)
testImplementation(libs.orbit.test)

// status bar
implementation(libs.accompanist.systemuicontroller)

Expand Down
Loading
Loading