Skip to content

Commit

Permalink
♻️ change the return type of feed repository
Browse files Browse the repository at this point in the history
  • Loading branch information
kmkim2689 committed Jul 25, 2024
1 parent 34dd931 commit 6083b21
Show file tree
Hide file tree
Showing 13 changed files with 90 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<Feed>>,
) : PagingSource<Int, Feed>() {
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Feed> {
private val fetchFeeds: suspend (pageNumber: Int, size: Int) -> Result<List<Recipe>>,
) : PagingSource<Int, Recipe>() {
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Recipe> {
val pageNumber = params.key ?: initialPageNumber
return runCatching {
val feeds = fetchFeeds(pageNumber, params.loadSize)
Expand All @@ -20,11 +20,11 @@ class FeedPagingSource(
nextKey = nextKey,
)
}.onFailure { throwable ->
LoadResult.Error<Int, Feed>(throwable)
LoadResult.Error<Int, Recipe>(throwable)
}.getOrThrow()
}

override fun getRefreshKey(state: PagingState<Int, Feed>): Int? {
override fun getRefreshKey(state: PagingState<Int, Recipe>): Int? {
return state.anchorPosition?.let { anchorPosition ->
state.closestPageToPosition(anchorPosition)?.prevKey
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ data class FeedItemResponse(
val description: String,
val difficulty: Int,
val ingredient: List<IngredientResponse>,
val likeCount: Int,
val likeCount: Long,
val recipeId: Long,
val thumbnail: String,
val title: String,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -16,10 +16,10 @@ class DefaultFeedRepository(
override suspend fun fetchRecipes(
pageNumber: Int,
pageSize: Int,
): Result<List<Feed>> {
): Result<List<Recipe>> {
return runCatching {
val response = feedRemoteDataSource.fetchRecipes(pageNumber, pageSize)
body(response, RESPONSE_CODE_SUCCESS).map(FeedItemResponse::toFeed)
body(response, RESPONSE_CODE_SUCCESS).map(FeedItemResponse::toRecipe)
}
}

Expand All @@ -34,11 +34,11 @@ class DefaultFeedRepository(
pageNumber: Int,
pageSize: Int,
category: String,
): Result<List<Feed>> {
): Result<List<Recipe>> {
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)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
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<List<Feed>>
): Result<List<Recipe>>

suspend fun fetchRecipeSteps(recipeId: Long): Result<List<RecipeStep>>

suspend fun fetchRecipesByCategory(
pageNumber: Int,
pageSize: Int,
category: String,
): Result<List<Feed>>
): Result<List<Recipe>>
}
Original file line number Diff line number Diff line change
@@ -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,
)

Expand All @@ -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,
)

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
val cookingTime: String,
val thumbnail: String,
val user: User,
val favoriteCount: Long,
val ingredients: List<String>,
val timeRequired: Int,
val difficulty: String,
val ingredients: List<Ingredient>,
val difficulty: Int,
val introduction: String,
val commentCount: Long,
) : Parcelable
Original file line number Diff line number Diff line change
Expand Up @@ -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<Feed, FeedRecyclerViewAdapter.ViewHolder>(diffCallback) {
PagingDataAdapter<Recipe, FeedRecyclerViewAdapter.ViewHolder>(diffCallback) {
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int,
Expand All @@ -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<Feed>() {
object : DiffUtil.ItemCallback<Recipe>() {
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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<PagingData<Feed>> =
val feedData: LiveData<PagingData<Recipe>> =
Pager(
config = PagingConfig(pageSize = PAGE_SIZE, enablePlaceholders = false),
pagingSourceFactory = { FeedPagingSource(fetchFeeds = feedRepository::fetchRecipes) },
Expand All @@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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)
}
16 changes: 8 additions & 8 deletions android/app/src/main/res/layout/item_feed.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<data>

<variable
name="feed"
type="net.pengcook.android.presentation.core.model.Feed" />
name="recipe"
type="net.pengcook.android.presentation.core.model.Recipe" />
</data>

<com.google.android.material.card.MaterialCardView
Expand All @@ -25,7 +25,7 @@
android:layout_width="0dp"
android:layout_height="200dp"
android:scaleType="centerCrop"
app:imageUrl="@{feed.recipeImageUrl}"
app:imageUrl="@{recipe.thumbnail}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
Expand All @@ -39,7 +39,7 @@
android:layout_height="40dp"
android:layout_marginEnd="8dp"
app:shapeAppearanceOverlay="@style/roundImageView"
app:imageUrl="@{feed.profileImageUrl}"
app:imageUrl="@{recipe.user.profile}"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:src="@drawable/ic_launcher_background"
Expand All @@ -54,7 +54,7 @@
android:id="@+id/username"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@{feed.username}"
android:text="@{recipe.user.username}"
style="@style/Body.Medium.B2"
android:layout_marginHorizontal="8dp"
tools:text="Username"
Expand Down Expand Up @@ -82,7 +82,7 @@
android:id="@+id/recipe_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@{feed.recipeTitle}"
android:text="@{recipe.title}"
style="@style/Headline.H4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/recipe_image"
Expand All @@ -107,7 +107,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="@{String.valueOf(feed.likeCount)}"
android:text="@{String.valueOf(recipe.favoriteCount)}"
tools:text="1200"
app:layout_constraintStart_toEndOf="@id/like_icon"
app:layout_constraintTop_toTopOf="@id/like_icon"
Expand All @@ -129,7 +129,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="@{String.valueOf(feed.commentCount)}"
android:text="@{String.valueOf(recipe.commentCount)}"
tools:text="800"
app:layout_constraintStart_toEndOf="@id/comment_icon"
app:layout_constraintTop_toTopOf="@id/comment_icon"
Expand Down

0 comments on commit 6083b21

Please sign in to comment.