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

[FEAT/#315] 딱 맞는 인턴 공고 페이징 구현 #317

Merged
merged 13 commits into from
Jan 4, 2025
Merged
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import com.terning.build_logic.convention.configureCoroutineKotlin
import com.terning.build_logic.convention.configureKotlin

plugins {
Expand Down
3 changes: 3 additions & 0 deletions data/home/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ android {
dependencies {
// domain
implementation(projects.domain.home)

//paging
implementation(libs.paging.runtime)
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ interface HomeDataSource {

suspend fun getRecommendIntern(
sortBy: String,
page: Int,
): BaseResponse<HomeRecommendInternResponseDto>

suspend fun getFilteringInfo(): BaseResponse<HomeFilteringInfoResponseDto>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ class HomeDataSourceImpl @Inject constructor(

override suspend fun getRecommendIntern(
sortBy: String,
page: Int,
): BaseResponse<HomeRecommendInternResponseDto> =
homeService.getRecommendIntern(
sortBy = sortBy,
page = page
)

override suspend fun getFilteringInfo(): BaseResponse<HomeFilteringInfoResponseDto> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import kotlinx.serialization.Serializable
data class HomeRecommendInternResponseDto(
@SerialName("totalCount")
val totalCount: Int,
@SerialName("hasNext")
val hasNextPage: Boolean,
@SerialName("result")
val result: List<Result>
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
package com.terning.data.home.mapper

import com.terning.data.home.dto.response.HomeRecommendInternResponseDto
import com.terning.domain.home.entity.HomeRecommendIntern
import com.terning.domain.home.entity.HomeRecommendedIntern

fun HomeRecommendInternResponseDto.toHomeRecommendInternList(): HomeRecommendIntern =
HomeRecommendIntern(
totalCount = this.totalCount,
homeRecommendInternDetail = this.result.map {
it.toHomeRecommendInternDetail()
}
)

fun HomeRecommendInternResponseDto.Result.toHomeRecommendInternDetail(): HomeRecommendIntern.HomeRecommendInternDetail =
HomeRecommendIntern.HomeRecommendInternDetail(
fun HomeRecommendInternResponseDto.Result.toHomeRecommendedIntern(totalCount: Int): HomeRecommendedIntern =
HomeRecommendedIntern(
totalCount = totalCount,
internshipAnnouncementId = this.internshipAnnouncementId,
title = this.title,
companyImage = this.companyImage,
dDay = this.dDay,
deadline = deadline,
title = this.title,
workingPeriod = this.workingPeriod,
startYearMonth = this.startYearMonth,
companyImage = this.companyImage,
isScrapped = this.isScrapped,
color = this.color,
deadline = this.deadline,
startYearMonth = this.startYearMonth,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.terning.data.home.pagingsource

import androidx.paging.PagingSource
import androidx.paging.PagingState
import com.terning.data.home.datasource.HomeDataSource
import com.terning.data.home.dto.response.HomeRecommendInternResponseDto

class HomePagingSource(
private val sortBy: String,
private val dataSource: HomeDataSource
) : PagingSource<Int, Pair<Int, HomeRecommendInternResponseDto.Result>>() {


override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Pair<Int, HomeRecommendInternResponseDto.Result>> {
return try {
val nextParamKey = params.key ?: 0

val response = dataSource.getRecommendIntern(sortBy = sortBy, page = nextParamKey)
val totalCount = response.result.totalCount
val hasNextPage = response.result.hasNextPage

LoadResult.Page(
data = response.result.result.map {
Pair(totalCount, it)
},
prevKey = null, // 다음 페이지 로딩만 가능하도록 설정
nextKey = if (hasNextPage) nextParamKey + 1 else null
)
} catch (e: Exception) {
LoadResult.Error(e)
}
}

override fun getRefreshKey(state: PagingState<Int, Pair<Int, HomeRecommendInternResponseDto.Result>>): Int? {
return state.anchorPosition?.let { anchorPosition ->
val anchorPage = state.closestPageToPosition(anchorPosition)
anchorPage?.prevKey?.plus(1) ?: anchorPage?.nextKey?.minus(1)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,33 +1,45 @@
package com.terning.data.home.repositoryimpl

import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import androidx.paging.map
import com.terning.data.home.datasource.HomeDataSource
import com.terning.data.home.dto.request.toChangeFilterRequestDto
import com.terning.data.home.mapper.toHomeFilteringInfo
import com.terning.data.home.mapper.toHomeRecommendInternList
import com.terning.data.home.mapper.toHomeRecommendedIntern
import com.terning.data.home.mapper.toHomeUpcomingInternList
import com.terning.data.home.pagingsource.HomePagingSource
import com.terning.domain.home.entity.ChangeFilteringRequestModel
import com.terning.domain.home.entity.HomeFilteringInfo
import com.terning.domain.home.entity.HomeRecommendIntern
import com.terning.domain.home.entity.HomeRecommendedIntern
import com.terning.domain.home.entity.HomeUpcomingIntern
import com.terning.domain.home.repository.HomeRepository
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import javax.inject.Inject

class HomeRepositoryImpl @Inject constructor(
private val homeDataSource: HomeDataSource,
) : HomeRepository {

override suspend fun getHomeUpcomingInternList(): Result<HomeUpcomingIntern> =
runCatching {
homeDataSource.getUpcomingIntern().result.toHomeUpcomingInternList()
}

override suspend fun getRecommendIntern(
sortBy: String,
): Result<HomeRecommendIntern> =
runCatching {
homeDataSource.getRecommendIntern(
override fun getRecommendIntern(sortBy: String): Flow<PagingData<HomeRecommendedIntern>> {
return Pager(
PagingConfig(pageSize = 10)
) {
HomePagingSource(
sortBy = sortBy,
).result.toHomeRecommendInternList()
dataSource = homeDataSource
)
}.flow.map { pagedData ->
pagedData.map { it.second.toHomeRecommendedIntern(it.first) }
}
}

override suspend fun getFilteringInfo(): Result<HomeFilteringInfo> =
runCatching {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface HomeService {
@GET("api/v1/home")
suspend fun getRecommendIntern(
@Query("sortBy") sortBy: String,
@Query("page") page: Int,
): BaseResponse<HomeRecommendInternResponseDto>

@GET("api/v1/filters")
Expand Down
6 changes: 6 additions & 0 deletions domain/home/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
plugins {
alias(libs.plugins.terning.kotlin)
}

dependencies {
implementation(libs.paging.common)
implementation(libs.coroutines.core)
implementation(libs.coroutines.test)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.terning.domain.home.entity

data class HomeRecommendedIntern(
val totalCount: Int,
val internshipAnnouncementId: Long,
val companyImage: String,
val dDay: String,
val title: String,
val workingPeriod: String,
val isScrapped: Boolean,
val color: String?,
val deadline: String,
val startYearMonth: String,
)
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package com.terning.domain.home.repository

import androidx.paging.PagingData
import com.terning.domain.home.entity.ChangeFilteringRequestModel
import com.terning.domain.home.entity.HomeFilteringInfo
import com.terning.domain.home.entity.HomeRecommendIntern
import com.terning.domain.home.entity.HomeRecommendedIntern
import com.terning.domain.home.entity.HomeUpcomingIntern
import kotlinx.coroutines.flow.Flow

interface HomeRepository {
suspend fun getHomeUpcomingInternList(): Result<HomeUpcomingIntern>

suspend fun getRecommendIntern(
sortBy: String,
): Result<HomeRecommendIntern>
fun getRecommendIntern(
sortBy: String
): Flow<PagingData<HomeRecommendedIntern>>

suspend fun getFilteringInfo(): Result<HomeFilteringInfo>

Expand Down
4 changes: 4 additions & 0 deletions feature/home/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ dependencies {

// feature
implementation(projects.feature.dialog)

// paging
implementation(libs.paging.runtime)
implementation(libs.paging.compose)
}
Loading
Loading