Skip to content

Commit

Permalink
♻️ 카테고리 입력 비즈니스 로직 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
kmkim2689 committed Oct 8, 2024
1 parent b47ea43 commit 94bed11
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ class DefaultMakingRecipeLocalDataSource
override suspend fun saveRecipeDescription(
recipeDescription: RecipeDescriptionEntity,
ingredients: List<IngredientEntity>,
categories: List<CategoryEntity>,
category: CategoryEntity,
): Long {
return database.withTransaction {
recipeDescriptionDao.insertCreatedRecipeDescription(recipeDescription)
ingredientDao.saveIngredients(ingredients)
categoryDao.saveCategories(categories)
categoryDao.saveSingleCategory(category)
recipeDescription.id
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface MakingRecipeLocalDataSource {
suspend fun saveRecipeDescription(
recipeDescription: RecipeDescriptionEntity,
ingredients: List<IngredientEntity>,
categories: List<CategoryEntity>,
category: CategoryEntity,
): Long

suspend fun fetchTotalRecipeData(): CreatedRecipe?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import net.pengcook.android.data.model.step.RecipeStepEntity

@Database(
entities = [RecipeDescriptionEntity::class, RecipeStepEntity::class, CategoryEntity::class, IngredientEntity::class],
version = 5,
version = 6,
)
abstract class RecipeDatabase : RoomDatabase() {
abstract fun recipeDescriptionDao(): RecipeDescriptionDao
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import net.pengcook.android.data.local.database.contract.RecipeDescriptionContra
],
)
data class CategoryEntity(
@PrimaryKey(autoGenerate = false)
@PrimaryKey(autoGenerate = true)
@ColumnInfo(CategoryContract.COLUMN_ID) val id: Long = 0,
@ColumnInfo(CategoryContract.COLUMN_RECIPE_DESCRIPTION_ID) val recipeId: Long,
@ColumnInfo(CategoryContract.COLUMN_CATEGORY_NAME) val categoryName: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import kotlinx.coroutines.launch
import net.pengcook.android.data.datasource.auth.SessionLocalDataSource
import net.pengcook.android.data.datasource.makingrecipe.MakingRecipeLocalDataSource
import net.pengcook.android.data.datasource.makingrecipe.MakingRecipeRemoteDataSource
import net.pengcook.android.data.util.mapper.toCategoryEntities
import net.pengcook.android.data.model.makingrecipe.entity.CategoryEntity
import net.pengcook.android.data.util.mapper.toIngredientEntities
import net.pengcook.android.data.util.mapper.toRecipeCreation
import net.pengcook.android.data.util.mapper.toRecipeCreationRequest
Expand Down Expand Up @@ -40,7 +40,9 @@ class DefaultMakingRecipeRepository

override suspend fun fetchTotalRecipeData(): Result<RecipeCreation?> =
runCatching {
makingRecipeLocalDataSource.fetchTotalRecipeData()?.toRecipeCreation()
val recipeCreation = makingRecipeLocalDataSource.fetchTotalRecipeData()?.toRecipeCreation()
println(recipeCreation)
recipeCreation
}

override suspend fun fetchRecipeDescription(): Result<RecipeDescription?> =
Expand All @@ -52,12 +54,13 @@ class DefaultMakingRecipeRepository
runCatching {
val id = recipeDescription.recipeDescriptionId
val recipeDescriptionEntity = recipeDescription.toRecipeDescriptionEntity(id)
val categoryEntities = recipeDescription.categories.toCategoryEntities(id)
// val categoryEntities = recipeDescription.categories.toCategoryEntities(id)
val categoryEntity = CategoryEntity(recipeId = id, categoryName = recipeDescription.categories.joinToString())
val ingredientEntities = recipeDescription.ingredients.toIngredientEntities(id)
makingRecipeLocalDataSource.saveRecipeDescription(
recipeDescription = recipeDescriptionEntity,
ingredients = ingredientEntities,
categories = categoryEntities,
category = categoryEntity,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fun CreatedRecipe.toRecipeCreation(): RecipeCreation =
cookingTime = recipeDescription.cookingTime,
difficulty = recipeDescription.difficulty,
ingredients = ingredients.map { it.toIngredient() },
categories = categories.map { it.categoryName },
categories = categories.first().categoryName.split(",").map(String::trim),
thumbnail = recipeDescription.thumbnail,
steps = steps.map { it.toRecipeStepMaking() },
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.GridLayoutManager
import dagger.hilt.android.AndroidEntryPoint
import net.pengcook.android.R
import net.pengcook.android.databinding.FragmentCategoryBinding
import net.pengcook.android.presentation.core.style.GridSpacingItemDecoration
import net.pengcook.android.presentation.core.util.AnalyticsLogging

@AndroidEntryPoint
Expand All @@ -37,11 +37,28 @@ class CategoryFragment : Fragment() {
super.onViewCreated(view, savedInstanceState)
AnalyticsLogging.init(requireContext()) // Firebase Analytics 초기화
AnalyticsLogging.viewLogEvent("Category")
binding.adapter = adapter
initializeAdapter()
setUpCategories()
observeEvents()
}

private fun initializeAdapter() {
binding.adapter = adapter
val layoutManager = GridLayoutManager(requireContext(), 3)
binding.rvCategory.layoutManager =
layoutManager.apply {
spanSizeLookup =
object : GridLayoutManager.SpanSizeLookup() {
override fun getSpanSize(position: Int): Int {
return when (position) {
0 -> 3
else -> 1
}
}
}
}
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
Expand All @@ -65,33 +82,31 @@ class CategoryFragment : Fragment() {
private fun setUpCategories() {
val categories = categories()
adapter.submitList(categories)
val spacingInPixels = resources.getDimensionPixelSize(R.dimen.item_spacing_category)
binding.rvCategory.addItemDecoration(GridSpacingItemDecoration(3, spacingInPixels))
}

private fun categories() =
listOf(
Category(
id = 100,
title = "흑백요리사",
title = getString(R.string.category_culinary_classwars),
imageUrl = "https://github.com/user-attachments/assets/81327a0a-715d-4c91-b570-43c3386d1866",
code = "흑백요리사",
),
Category(
1,
"한식",
getString(R.string.category_korean),
"https://www.90daykorean.com/wp-content/uploads/2015/04/bigstock-Bibimbap-On-A-Concrete-Backgro-449450285-1024x683.jpg",
"Korean",
),
Category(
2,
"일식",
getString(R.string.category_japanese),
"https://railrocker.com/playground/wp-content/uploads/2020/02/Onigiri-traditional-Japanese-Rice-Balls.jpg",
"Japanese",
),
Category(
3,
"중식",
getString(R.string.category_chinese),
"https://image.yes24.com/images/chyes24/0/a/3/7/0a379fd46d1e404ed63adc3beee52b33.jpg",
"Chinese",
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import androidx.activity.result.contract.ActivityResultContracts
import androidx.datastore.core.IOException
import androidx.fragment.app.Fragment
Expand Down Expand Up @@ -108,7 +107,7 @@ class RecipeMakingFragment : Fragment() {
initBinding()
initTimeFormatInput()
observeUiEvent()
setUpCategorySpinner()
// setUpCategorySpinner()
}

override fun onDestroyView() {
Expand Down Expand Up @@ -209,16 +208,16 @@ class RecipeMakingFragment : Fragment() {
}
}

private fun setUpCategorySpinner() {
val countryAdapter =
ArrayAdapter(
requireContext(),
android.R.layout.simple_spinner_item,
resources.getStringArray(R.array.signup_countries),
)
countryAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
binding.makingRecipeCategory.spFormContent.spDefault.adapter = countryAdapter
}
// private fun setUpCategorySpinner() {
// val countryAdapter =
// ArrayAdapter(
// requireContext(),
// android.R.layout.simple_spinner_item,
// resources.getStringArray(R.array.signup_countries),
// )
// countryAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
// binding.makingRecipeCategory.spFormContent.spDefault.adapter = countryAdapter
// }

private fun showSnackBar(message: String) {
Snackbar.make(binding.root, message, Snackbar.LENGTH_SHORT).show()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import kotlinx.coroutines.launch
import net.pengcook.android.data.repository.makingrecipe.MakingRecipeRepository
import net.pengcook.android.domain.model.recipemaking.RecipeDescription
import net.pengcook.android.presentation.core.listener.AppbarSingleActionEventListener
import net.pengcook.android.presentation.core.listener.SpinnerItemChangeListener
import net.pengcook.android.presentation.core.util.Event
import net.pengcook.android.presentation.making.listener.RecipeMakingEventListener
import java.io.File
Expand All @@ -24,16 +23,12 @@ class RecipeMakingViewModel
private val makingRecipeRepository: MakingRecipeRepository,
) : ViewModel(),
RecipeMakingEventListener,
SpinnerItemChangeListener,
AppbarSingleActionEventListener {
private val _uiEvent: MutableLiveData<Event<RecipeMakingEvent>> = MutableLiveData()
val uiEvent: LiveData<Event<RecipeMakingEvent>>
get() = _uiEvent

private val _categorySelectedValue: MutableLiveData<String> = MutableLiveData()
val categorySelectedValue: LiveData<String>
get() = _categorySelectedValue

val categoryContent = MutableLiveData<String>()
val titleContent = MutableLiveData<String>()
val ingredientContent = MutableLiveData<String>()
val difficultySelectedValue = MutableLiveData(0.0f)
Expand Down Expand Up @@ -106,7 +101,7 @@ class RecipeMakingViewModel

override fun onConfirm() {
viewModelScope.launch {
val category = categorySelectedValue.value?.trim()
val category = categoryContent.value?.trim()
val introduction = introductionContent.value?.trim()
val difficulty = difficultySelectedValue.value
val ingredients = ingredientContent.value?.trim()
Expand Down Expand Up @@ -146,10 +141,6 @@ class RecipeMakingViewModel
}
}

override fun onSelectionChange(item: String) {
_categorySelectedValue.value = item
}

override fun onNavigateBack() {
_uiEvent.value = Event(RecipeMakingEvent.MakingCancellation)
}
Expand Down Expand Up @@ -182,7 +173,7 @@ class RecipeMakingViewModel
thumbnailTitle = existingRecipe.thumbnail
_imageUploaded.value = true
_imageSelected.value = true
_categorySelectedValue.value = existingRecipe.categories.first()
categoryContent.value = existingRecipe.categories.joinToString()
_currentImage.value = Uri.parse(existingRecipe.imageUri)
}

Expand All @@ -197,7 +188,7 @@ class RecipeMakingViewModel
val recipeDescription =
RecipeDescription(
recipeDescriptionId = recipeId ?: return,
categories = listOf(category),
categories = category.split(SEPARATOR_INGREDIENTS).map { it.trim() },
cookingTime = timeRequired,
description = introduction,
difficulty = (difficulty * 2).toInt(),
Expand All @@ -212,6 +203,7 @@ class RecipeMakingViewModel
.onSuccess { recipeId ->
_uiEvent.value = Event(RecipeMakingEvent.NavigateToMakingStep(recipeId))
}.onFailure {
println(it.message)
_uiEvent.value = Event(RecipeMakingEvent.PostRecipeFailure)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ class SearchViewModel
): Boolean =
keyword.isEmpty() ||
recipe.title.contains(keyword, ignoreCase = true) ||
recipe.introduction.contains(keyword, ignoreCase = true)
recipe.introduction.contains(keyword, ignoreCase = true) ||
recipe.category.joinToString().contains(keyword, ignoreCase = true)

companion object {
private const val INITIAL_KEYWORD = ""
Expand Down

0 comments on commit 94bed11

Please sign in to comment.