From 6083b219bedec67ad93cf097e6c055c2e0d36691 Mon Sep 17 00:00:00 2001 From: Gimun Kim <101035437+kmkim2689@users.noreply.github.com> Date: Thu, 25 Jul 2024 20:56:14 +0900 Subject: [PATCH] :recycle: change the return type of feed repository --- .../data/datasource/FeedPagingSource.kt | 12 +++--- .../data/model/feed/item/FeedItemResponse.kt | 2 +- .../data/repository/DummyFeedsRepository.kt | 22 ---------- .../repository/feed/DefaultFeedRepository.kt | 12 +++--- .../data/repository/feed/FeedRepository.kt | 6 +-- .../android/data/util/mapper/FeedMapper.kt | 43 +++++++++++++++---- .../android/presentation/core/model/Feed.kt | 11 ----- .../presentation/core/model/Ingredient.kt | 11 +++++ .../android/presentation/core/model/Recipe.kt | 10 +++-- .../home/FeedRecyclerViewAdapter.kt | 20 ++++----- .../presentation/home/HomeViewModel.kt | 6 +-- .../home/listener/FeedItemEventListener.kt | 4 +- android/app/src/main/res/layout/item_feed.xml | 16 +++---- 13 files changed, 90 insertions(+), 85 deletions(-) delete mode 100644 android/app/src/main/java/net/pengcook/android/data/repository/DummyFeedsRepository.kt delete mode 100644 android/app/src/main/java/net/pengcook/android/presentation/core/model/Feed.kt create mode 100644 android/app/src/main/java/net/pengcook/android/presentation/core/model/Ingredient.kt diff --git a/android/app/src/main/java/net/pengcook/android/data/datasource/FeedPagingSource.kt b/android/app/src/main/java/net/pengcook/android/data/datasource/FeedPagingSource.kt index 11efd376..6498c7e1 100644 --- a/android/app/src/main/java/net/pengcook/android/data/datasource/FeedPagingSource.kt +++ b/android/app/src/main/java/net/pengcook/android/data/datasource/FeedPagingSource.kt @@ -2,13 +2,13 @@ package net.pengcook.android.data.datasource import androidx.paging.PagingSource import androidx.paging.PagingState -import net.pengcook.android.presentation.core.model.Feed +import net.pengcook.android.presentation.core.model.Recipe class FeedPagingSource( private val initialPageNumber: Int = 0, - private val fetchFeeds: suspend (pageNumber: Int, size: Int) -> Result>, -) : PagingSource() { - override suspend fun load(params: LoadParams): LoadResult { + private val fetchFeeds: suspend (pageNumber: Int, size: Int) -> Result>, +) : PagingSource() { + override suspend fun load(params: LoadParams): LoadResult { val pageNumber = params.key ?: initialPageNumber return runCatching { val feeds = fetchFeeds(pageNumber, params.loadSize) @@ -20,11 +20,11 @@ class FeedPagingSource( nextKey = nextKey, ) }.onFailure { throwable -> - LoadResult.Error(throwable) + LoadResult.Error(throwable) }.getOrThrow() } - override fun getRefreshKey(state: PagingState): Int? { + override fun getRefreshKey(state: PagingState): Int? { return state.anchorPosition?.let { anchorPosition -> state.closestPageToPosition(anchorPosition)?.prevKey } diff --git a/android/app/src/main/java/net/pengcook/android/data/model/feed/item/FeedItemResponse.kt b/android/app/src/main/java/net/pengcook/android/data/model/feed/item/FeedItemResponse.kt index 911e11ff..2979111d 100644 --- a/android/app/src/main/java/net/pengcook/android/data/model/feed/item/FeedItemResponse.kt +++ b/android/app/src/main/java/net/pengcook/android/data/model/feed/item/FeedItemResponse.kt @@ -7,7 +7,7 @@ data class FeedItemResponse( val description: String, val difficulty: Int, val ingredient: List, - val likeCount: Int, + val likeCount: Long, val recipeId: Long, val thumbnail: String, val title: String, diff --git a/android/app/src/main/java/net/pengcook/android/data/repository/DummyFeedsRepository.kt b/android/app/src/main/java/net/pengcook/android/data/repository/DummyFeedsRepository.kt deleted file mode 100644 index d4f5b2eb..00000000 --- a/android/app/src/main/java/net/pengcook/android/data/repository/DummyFeedsRepository.kt +++ /dev/null @@ -1,22 +0,0 @@ -package net.pengcook.android.data.repository - -import net.pengcook.android.presentation.core.model.Feed - -class DummyFeedsRepository { - suspend fun fetchFeeds( - pageNumber: Int, - size: Int, - ): List { - return (0 until size).map { - Feed( - id = it.toLong(), - username = "username$it", - profileImageUrl = "https://cdn.crowdpic.net/detail-thumb/thumb_d_23832961B1BC3712116E240165A4FAF6.jpg", - recipeImageUrl = "https://cdn.crowdpic.net/detail-thumb/thumb_d_23832961B1BC3712116E240165A4FAF6.jpg", - recipeTitle = "Recipe Title $it", - likeCount = it, - commentCount = it, - ) - } - } -} diff --git a/android/app/src/main/java/net/pengcook/android/data/repository/feed/DefaultFeedRepository.kt b/android/app/src/main/java/net/pengcook/android/data/repository/feed/DefaultFeedRepository.kt index 30539fc4..bf1f70f2 100644 --- a/android/app/src/main/java/net/pengcook/android/data/repository/feed/DefaultFeedRepository.kt +++ b/android/app/src/main/java/net/pengcook/android/data/repository/feed/DefaultFeedRepository.kt @@ -3,10 +3,10 @@ package net.pengcook.android.data.repository.feed import net.pengcook.android.data.datasource.feed.FeedRemoteDataSource import net.pengcook.android.data.model.feed.item.FeedItemResponse import net.pengcook.android.data.model.feed.step.RecipeStepResponse -import net.pengcook.android.data.util.mapper.toFeed +import net.pengcook.android.data.util.mapper.toRecipe import net.pengcook.android.data.util.mapper.toRecipeStep import net.pengcook.android.data.util.network.NetworkResponseHandler -import net.pengcook.android.presentation.core.model.Feed +import net.pengcook.android.presentation.core.model.Recipe import net.pengcook.android.presentation.detail.RecipeStep import retrofit2.Response @@ -16,10 +16,10 @@ class DefaultFeedRepository( override suspend fun fetchRecipes( pageNumber: Int, pageSize: Int, - ): Result> { + ): Result> { return runCatching { val response = feedRemoteDataSource.fetchRecipes(pageNumber, pageSize) - body(response, RESPONSE_CODE_SUCCESS).map(FeedItemResponse::toFeed) + body(response, RESPONSE_CODE_SUCCESS).map(FeedItemResponse::toRecipe) } } @@ -34,11 +34,11 @@ class DefaultFeedRepository( pageNumber: Int, pageSize: Int, category: String, - ): Result> { + ): Result> { return runCatching { val response = feedRemoteDataSource.fetchRecipesByCategory(pageNumber, pageSize, category) - body(response, RESPONSE_CODE_SUCCESS).map(FeedItemResponse::toFeed) + body(response, RESPONSE_CODE_SUCCESS).map(FeedItemResponse::toRecipe) } } diff --git a/android/app/src/main/java/net/pengcook/android/data/repository/feed/FeedRepository.kt b/android/app/src/main/java/net/pengcook/android/data/repository/feed/FeedRepository.kt index 55b87e9a..14a207c9 100644 --- a/android/app/src/main/java/net/pengcook/android/data/repository/feed/FeedRepository.kt +++ b/android/app/src/main/java/net/pengcook/android/data/repository/feed/FeedRepository.kt @@ -1,13 +1,13 @@ package net.pengcook.android.data.repository.feed -import net.pengcook.android.presentation.core.model.Feed +import net.pengcook.android.presentation.core.model.Recipe import net.pengcook.android.presentation.detail.RecipeStep interface FeedRepository { suspend fun fetchRecipes( pageNumber: Int, pageSize: Int, - ): Result> + ): Result> suspend fun fetchRecipeSteps(recipeId: Long): Result> @@ -15,5 +15,5 @@ interface FeedRepository { pageNumber: Int, pageSize: Int, category: String, - ): Result> + ): Result> } diff --git a/android/app/src/main/java/net/pengcook/android/data/util/mapper/FeedMapper.kt b/android/app/src/main/java/net/pengcook/android/data/util/mapper/FeedMapper.kt index 0263d0c1..45757e31 100644 --- a/android/app/src/main/java/net/pengcook/android/data/util/mapper/FeedMapper.kt +++ b/android/app/src/main/java/net/pengcook/android/data/util/mapper/FeedMapper.kt @@ -1,18 +1,27 @@ package net.pengcook.android.data.util.mapper +import net.pengcook.android.data.model.feed.item.AuthorResponse +import net.pengcook.android.data.model.feed.item.CategoryResponse import net.pengcook.android.data.model.feed.item.FeedItemResponse +import net.pengcook.android.data.model.feed.item.IngredientResponse import net.pengcook.android.data.model.feed.step.RecipeStepResponse -import net.pengcook.android.presentation.core.model.Feed +import net.pengcook.android.presentation.core.model.Ingredient +import net.pengcook.android.presentation.core.model.Recipe +import net.pengcook.android.presentation.core.model.User import net.pengcook.android.presentation.detail.RecipeStep -fun FeedItemResponse.toFeed(): Feed = - Feed( - id = recipeId, - username = author.authorName, - profileImageUrl = author.authorImage, - recipeImageUrl = thumbnail, - recipeTitle = title, - likeCount = likeCount, +fun FeedItemResponse.toRecipe(): Recipe = + Recipe( + title = title, + recipeId = recipeId, + category = category.map(CategoryResponse::toCategoryText), + cookingTime = cookingTime, + thumbnail = thumbnail, + user = author.toUser(), + favoriteCount = likeCount, + ingredients = ingredient.map(IngredientResponse::toIngredient), + difficulty = difficulty, + introduction = description, commentCount = 0, ) @@ -24,3 +33,19 @@ fun RecipeStepResponse.toRecipeStep(): RecipeStep = image = image, sequence = sequence, ) + +private fun CategoryResponse.toCategoryText(): String = categoryName + +private fun AuthorResponse.toUser(): User = + User( + id = authorId, + username = authorName, + profile = authorImage, + ) + +private fun IngredientResponse.toIngredient(): Ingredient = + Ingredient( + ingredientId = ingredientId, + ingredientName = ingredientName, + requirement = requirement, + ) diff --git a/android/app/src/main/java/net/pengcook/android/presentation/core/model/Feed.kt b/android/app/src/main/java/net/pengcook/android/presentation/core/model/Feed.kt deleted file mode 100644 index 6d43cc70..00000000 --- a/android/app/src/main/java/net/pengcook/android/presentation/core/model/Feed.kt +++ /dev/null @@ -1,11 +0,0 @@ -package net.pengcook.android.presentation.core.model - -data class Feed( - val id: Long, - val username: String, - val profileImageUrl: String, - val recipeImageUrl: String, - val recipeTitle: String, - val likeCount: Int, - val commentCount: Int, -) diff --git a/android/app/src/main/java/net/pengcook/android/presentation/core/model/Ingredient.kt b/android/app/src/main/java/net/pengcook/android/presentation/core/model/Ingredient.kt new file mode 100644 index 00000000..f466f16b --- /dev/null +++ b/android/app/src/main/java/net/pengcook/android/presentation/core/model/Ingredient.kt @@ -0,0 +1,11 @@ +package net.pengcook.android.presentation.core.model + +import android.os.Parcelable +import kotlinx.parcelize.Parcelize + +@Parcelize +data class Ingredient( + val ingredientId: Long, + val ingredientName: String, + val requirement: String, +) : Parcelable diff --git a/android/app/src/main/java/net/pengcook/android/presentation/core/model/Recipe.kt b/android/app/src/main/java/net/pengcook/android/presentation/core/model/Recipe.kt index 29da4035..65d2ea73 100644 --- a/android/app/src/main/java/net/pengcook/android/presentation/core/model/Recipe.kt +++ b/android/app/src/main/java/net/pengcook/android/presentation/core/model/Recipe.kt @@ -5,13 +5,15 @@ import kotlinx.parcelize.Parcelize @Parcelize data class Recipe( + val recipeId: Long = 1L, val title: String, - val category: String, + val category: List, + val cookingTime: String, val thumbnail: String, val user: User, val favoriteCount: Long, - val ingredients: List, - val timeRequired: Int, - val difficulty: String, + val ingredients: List, + val difficulty: Int, val introduction: String, + val commentCount: Long, ) : Parcelable diff --git a/android/app/src/main/java/net/pengcook/android/presentation/home/FeedRecyclerViewAdapter.kt b/android/app/src/main/java/net/pengcook/android/presentation/home/FeedRecyclerViewAdapter.kt index a05a16be..cc49fa94 100644 --- a/android/app/src/main/java/net/pengcook/android/presentation/home/FeedRecyclerViewAdapter.kt +++ b/android/app/src/main/java/net/pengcook/android/presentation/home/FeedRecyclerViewAdapter.kt @@ -6,11 +6,11 @@ import androidx.paging.PagingDataAdapter import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.RecyclerView import net.pengcook.android.databinding.ItemFeedBinding -import net.pengcook.android.presentation.core.model.Feed +import net.pengcook.android.presentation.core.model.Recipe import net.pengcook.android.presentation.home.listener.FeedItemEventListener class FeedRecyclerViewAdapter(private val eventListener: FeedItemEventListener) : - PagingDataAdapter(diffCallback) { + PagingDataAdapter(diffCallback) { override fun onCreateViewHolder( parent: ViewGroup, viewType: Int, @@ -31,25 +31,25 @@ class FeedRecyclerViewAdapter(private val eventListener: FeedItemEventListener) } class ViewHolder(private val binding: ItemFeedBinding) : RecyclerView.ViewHolder(binding.root) { - fun bind(item: Feed) { - binding.feed = item + fun bind(item: Recipe) { + binding.recipe = item binding.executePendingBindings() } } companion object { val diffCallback = - object : DiffUtil.ItemCallback() { + object : DiffUtil.ItemCallback() { override fun areItemsTheSame( - oldItem: Feed, - newItem: Feed, + oldItem: Recipe, + newItem: Recipe, ): Boolean { - return oldItem.id == newItem.id + return oldItem.recipeId == newItem.recipeId } override fun areContentsTheSame( - oldItem: Feed, - newItem: Feed, + oldItem: Recipe, + newItem: Recipe, ): Boolean { return oldItem == newItem } diff --git a/android/app/src/main/java/net/pengcook/android/presentation/home/HomeViewModel.kt b/android/app/src/main/java/net/pengcook/android/presentation/home/HomeViewModel.kt index 9746077a..a67f574a 100644 --- a/android/app/src/main/java/net/pengcook/android/presentation/home/HomeViewModel.kt +++ b/android/app/src/main/java/net/pengcook/android/presentation/home/HomeViewModel.kt @@ -10,13 +10,13 @@ import androidx.paging.cachedIn import androidx.paging.liveData import net.pengcook.android.data.datasource.FeedPagingSource import net.pengcook.android.data.repository.feed.FeedRepository -import net.pengcook.android.presentation.core.model.Feed +import net.pengcook.android.presentation.core.model.Recipe import net.pengcook.android.presentation.home.listener.FeedItemEventListener class HomeViewModel( private val feedRepository: FeedRepository, ) : ViewModel(), FeedItemEventListener { - val feedData: LiveData> = + val feedData: LiveData> = Pager( config = PagingConfig(pageSize = PAGE_SIZE, enablePlaceholders = false), pagingSourceFactory = { FeedPagingSource(fetchFeeds = feedRepository::fetchRecipes) }, @@ -28,7 +28,7 @@ class HomeViewModel( private const val PAGE_SIZE = 10 } - override fun onNavigateToDetail(feedInfo: Feed) { + override fun onNavigateToDetail(recipe: Recipe) { // Navigate to detail page } } diff --git a/android/app/src/main/java/net/pengcook/android/presentation/home/listener/FeedItemEventListener.kt b/android/app/src/main/java/net/pengcook/android/presentation/home/listener/FeedItemEventListener.kt index 58e6d504..0d20ac34 100644 --- a/android/app/src/main/java/net/pengcook/android/presentation/home/listener/FeedItemEventListener.kt +++ b/android/app/src/main/java/net/pengcook/android/presentation/home/listener/FeedItemEventListener.kt @@ -1,7 +1,7 @@ package net.pengcook.android.presentation.home.listener -import net.pengcook.android.presentation.core.model.Feed +import net.pengcook.android.presentation.core.model.Recipe interface FeedItemEventListener { - fun onNavigateToDetail(feedInfo: Feed) + fun onNavigateToDetail(recipe: Recipe) } diff --git a/android/app/src/main/res/layout/item_feed.xml b/android/app/src/main/res/layout/item_feed.xml index 3388c8f7..234ebf02 100644 --- a/android/app/src/main/res/layout/item_feed.xml +++ b/android/app/src/main/res/layout/item_feed.xml @@ -6,8 +6,8 @@ + name="recipe" + type="net.pengcook.android.presentation.core.model.Recipe" />