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

✨ 💄 add category "흑백요리사" #389

Merged
merged 7 commits into from
Oct 8, 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 @@ -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,53 +82,33 @@ 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(
1,
"Belgian",
"https://www.starttravel.co.uk/media/images/84310b25-c298-444e-8695-5ee4830e1e79.png",
"Belgian",
),
Category(
2,
"French",
"https://admin.expatica.com/fr/wp-content/uploads/sites/5/2023/11/french-food-1536x1024.jpg",
"French",
),
Category(
3,
"German",
"https://admin.expatica.com/de/wp-content/uploads/sites/6/2023/11/bratwurst-sauerkraut.jpg",
"German",
id = 100,
title = getString(R.string.category_culinary_classwars),
imageUrl = "https://github.com/user-attachments/assets/81327a0a-715d-4c91-b570-43c3386d1866",
code = "흑백요리사",
),
Category(
4,
"Indonesian",
"https://tasteofbali.ro/wp-content/uploads/2023/02/asian_food_Taste_of_Bali-1024x682.jpg",
"Indonesian",
),
Category(
5,
"Seafood",
"https://cdn11.bigcommerce.com/s-iz8lky8j1x/product_images/uploaded_images/istock-520490716.jpg",
"Seafood",
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(
6,
"Japanese",
2,
getString(R.string.category_japanese),
"https://railrocker.com/playground/wp-content/uploads/2020/02/Onigiri-traditional-Japanese-Rice-Balls.jpg",
"Japanese",
),
Category(
7,
"Korean",
"https://www.90daykorean.com/wp-content/uploads/2015/04/bigstock-Bibimbap-On-A-Concrete-Backgro-449450285-1024x683.jpg",
"Korean",
3,
getString(R.string.category_chinese),
"https://image.yes24.com/images/chyes24/0/a/3/7/0a379fd46d1e404ed63adc3beee52b33.jpg",
"Chinese",
),
)
}
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
2 changes: 0 additions & 2 deletions android/app/src/main/res/layout/fragment_category.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@
android:layout_width="match_parent"
android:layout_height="0dp"
android:adapter="@{adapter}"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:spanCount="3"
tools:listitem="@layout/item_category"
app:layout_constraintTop_toBottomOf="@id/abl_category"
app:layout_constraintBottom_toBottomOf="parent"
Expand Down
Loading
Loading