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

✨ connect home layout to server via API #96

Merged
merged 12 commits into from
Jul 25, 2024
Merged
15 changes: 12 additions & 3 deletions android/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ plugins {
}

android {
val properties = Properties()
properties.load(FileInputStream(rootProject.file("local.properties")))

namespace = "net.pengcook.android"
compileSdk = 34

Expand All @@ -22,9 +25,6 @@ android {

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"

val properties = Properties()
properties.load(FileInputStream(rootProject.file("local.properties")))

buildConfigField(
"String",
"GOOGLE_WEB_CLIENT_ID",
Expand All @@ -39,6 +39,15 @@ android {
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro",
)
buildConfigField(
"String",
"BASE_URL",
properties.getProperty("base_url_release")
)
}

debug {
buildConfigField("String", "BASE_URL", properties.getProperty("base_url_dev"))
}
}
compileOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@ import net.pengcook.android.presentation.core.model.Feed

class FeedPagingSource(
private val initialPageNumber: Int = 0,
private val fetchFeeds: suspend (pageNumber: Int, size: Int) -> List<Feed>,
private val fetchFeeds: suspend (pageNumber: Int, size: Int) -> Result<List<Feed>>,
) : PagingSource<Int, Feed>() {
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Feed> {
val pageNumber = params.key ?: initialPageNumber
return runCatching {
val feeds = fetchFeeds(pageNumber, params.loadSize)
val pageData = feeds.getOrNull() ?: emptyList()
val nextKey = if (pageData.size < params.loadSize) null else pageNumber + 1
LoadResult.Page(
data = feeds,
data = pageData,
prevKey = if (pageNumber == initialPageNumber) null else pageNumber - 1,
nextKey = pageNumber + 1,
nextKey = nextKey,
)
}.onFailure { throwable ->
LoadResult.Error<Int, Feed>(throwable)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package net.pengcook.android.data.datasource.feed

import net.pengcook.android.data.model.feed.item.FeedItemResponse
import net.pengcook.android.data.model.feed.step.RecipeStepResponse
import net.pengcook.android.data.remote.api.FeedService
import retrofit2.Response

class DefaultFeedRemoteDataSource(
private val feedService: FeedService,
) : FeedRemoteDataSource {
override suspend fun fetchRecipes(
pageNumber: Int,
pageSize: Int,
): Response<List<FeedItemResponse>> {
return feedService.fetchRecipes(pageNumber, pageSize)
}

override suspend fun fetchRecipeSteps(recipeId: Long): Response<List<RecipeStepResponse>> {
return feedService.fetchRecipeSteps(recipeId)
}

override suspend fun fetchRecipesByCategory(
pageNumber: Int,
pageSize: Int,
category: String,
): Response<List<FeedItemResponse>> {
return feedService.fetchRecipesByCategory(pageNumber, pageSize, category)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package net.pengcook.android.data.datasource.feed

import net.pengcook.android.data.model.feed.item.FeedItemResponse
import net.pengcook.android.data.model.feed.step.RecipeStepResponse
import retrofit2.Response

interface FeedRemoteDataSource {
suspend fun fetchRecipes(
pageNumber: Int,
pageSize: Int,
): Response<List<FeedItemResponse>>

suspend fun fetchRecipeSteps(recipeId: Long): Response<List<RecipeStepResponse>>

suspend fun fetchRecipesByCategory(
pageNumber: Int,
pageSize: Int,
category: String,
): Response<List<FeedItemResponse>>
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package net.pengcook.android.data.model.feed.item

import com.google.gson.annotations.SerializedName

data class AuthorResponse(
@SerializedName("authorId")
val authorId: Long,
@SerializedName("authorImage")
val authorImage: String,
@SerializedName("authorName")
val authorName: String,
)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SerializedName을 추가해야하나요? 어차피 변수명이랑 똑같은데

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

제거하겠습니다!

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package net.pengcook.android.data.model.feed.item

import com.google.gson.annotations.SerializedName

data class CategoryResponse(
@SerializedName("categoryId")
val categoryId: Long,
@SerializedName("categoryName")
val categoryName: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package net.pengcook.android.data.model.feed.item

import com.google.gson.annotations.SerializedName

data class FeedItemResponse(
@SerializedName("author")
val author: AuthorResponse,
@SerializedName("category")
val category: List<CategoryResponse>,
@SerializedName("cookingTime")
val cookingTime: String,
@SerializedName("description")
val description: String,
@SerializedName("difficulty")
val difficulty: Int,
@SerializedName("ingredient")
val ingredient: List<IngredientResponse>,
@SerializedName("likeCount")
val likeCount: Int,
@SerializedName("recipeId")
val recipeId: Long,
@SerializedName("thumbnail")
val thumbnail: String,
@SerializedName("title")
val title: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package net.pengcook.android.data.model.feed.item

import com.google.gson.annotations.SerializedName

data class IngredientResponse(
@SerializedName("ingredientId")
val ingredientId: Long,
@SerializedName("ingredientName")
val ingredientName: String,
@SerializedName("requirement")
val requirement: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package net.pengcook.android.data.model.feed.step

import com.google.gson.annotations.SerializedName

data class RecipeStepResponse(
@SerializedName("description")
val description: String,
@SerializedName("id")
val id: Long,
@SerializedName("image")
val image: Any,
@SerializedName("recipeId")
val recipeId: Int,
@SerializedName("sequence")
val sequence: Int,
)
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package net.pengcook.android.data.remote.api

import net.pengcook.android.data.model.feed.item.FeedItemResponse
import net.pengcook.android.data.model.feed.step.RecipeStepResponse
import retrofit2.Response
import retrofit2.http.GET
import retrofit2.http.Path
import retrofit2.http.Query

interface FeedService {
@GET("/api/recipes")
suspend fun fetchRecipes(
@Query("pageNumber") pageNumber: Int,
@Query("pageSize") pageSize: Int,
): Response<List<FeedItemResponse>>

@GET("/api/recipes/{recipeId}/steps")
suspend fun fetchRecipeSteps(
@Path("recipeId") recipeId: Long,
): Response<List<RecipeStepResponse>>

@GET("/api/categories")
suspend fun fetchRecipesByCategory(
@Query("pageNumber") pageNumber: Int,
@Query("pageSize") pageSize: Int,
@Query("category") category: String,
): Response<List<FeedItemResponse>>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
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.toRecipeStep
import net.pengcook.android.data.util.network.NetworkResponseHandler
import net.pengcook.android.presentation.core.model.Feed
import net.pengcook.android.presentation.detail.RecipeStep
import retrofit2.Response

class DefaultFeedRepository(
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

왜 이름이 Default가 붙어있나요?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

기본 구현체 이름에 prefix로 Default를 붙이기로 저희 팀 컨벤션으로 정했기에 붙였습니다!

private val feedRemoteDataSource: FeedRemoteDataSource,
) : FeedRepository, NetworkResponseHandler {
override suspend fun fetchRecipes(
pageNumber: Int,
pageSize: Int,
): Result<List<Feed>> {
return runCatching {
val response = feedRemoteDataSource.fetchRecipes(pageNumber, pageSize)
body(response, RESPONSE_CODE_SUCCESS).map(FeedItemResponse::toFeed)
}
}

override suspend fun fetchRecipeSteps(recipeId: Long): Result<List<RecipeStep>> {
return runCatching {
val response = feedRemoteDataSource.fetchRecipeSteps(recipeId)
body(response, RESPONSE_CODE_SUCCESS).map(RecipeStepResponse::toRecipeStep)
}
}

override suspend fun fetchRecipesByCategory(
pageNumber: Int,
pageSize: Int,
category: String,
): Result<List<Feed>> {
return runCatching {
val response =
feedRemoteDataSource.fetchRecipesByCategory(pageNumber, pageSize, category)
body(response, RESPONSE_CODE_SUCCESS).map(FeedItemResponse::toFeed)
}
}

override fun <T> body(
response: Response<T>,
validHttpCode: Int,
): T {
val code = response.code()
val body = response.body()
if (code != validHttpCode) throw RuntimeException(EXCEPTION_HTTP_CODE)
if (body == null) throw RuntimeException(EXCEPTION_NULL_BODY)
return body
}

companion object {
private const val EXCEPTION_HTTP_CODE = "Http code is not appropriate."
private const val EXCEPTION_NULL_BODY = "Response body is null."
private const val RESPONSE_CODE_SUCCESS = 200
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package net.pengcook.android.data.repository.feed

import net.pengcook.android.presentation.core.model.Feed
import net.pengcook.android.presentation.detail.RecipeStep

interface FeedRepository {
suspend fun fetchRecipes(
pageNumber: Int,
pageSize: Int,
): Result<List<Feed>>

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

suspend fun fetchRecipesByCategory(
pageNumber: Int,
pageSize: Int,
category: String,
): Result<List<Feed>>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package net.pengcook.android.data.util.mapper

import net.pengcook.android.data.model.feed.item.FeedItemResponse
import net.pengcook.android.data.model.feed.step.RecipeStepResponse
import net.pengcook.android.presentation.core.model.Feed
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,
commentCount = 0,
)

fun RecipeStepResponse.toRecipeStep(): RecipeStep =
RecipeStep(
stepId = id,
recipeId = recipeId,
description = description,
image = image,
sequence = sequence,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package net.pengcook.android.data.util.network

import retrofit2.Response

interface NetworkResponseHandler {
fun <T> body(
response: Response<T>,
validHttpCode: Int,
): T
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package net.pengcook.android.data.util.network

import net.pengcook.android.BuildConfig
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

object RetrofitClient {
private val retrofit =
Retrofit.Builder()
.baseUrl(BuildConfig.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()

fun <T> service(apiService: Class<T>): T {
return retrofit.create(apiService)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package net.pengcook.android.presentation.detail

data class RecipeStep(
val stepId: Long,
val recipeId: Int,
val description: String,
val image: Any,
val sequence: Int,
)
Loading
Loading