Skip to content

Commit

Permalink
Merge pull request #194 from team-winey/feature/fix-wineyfeed-item-upโ€ฆ
Browse files Browse the repository at this point in the history
โ€ฆdate

[fix] ์œ„๋‹ˆํ”ผ๋“œ, ๋งˆ์ดํ”ผ๋“œ / ์ƒ์„ธํ”ผ๋“œ ๊ฐ”๋‹ค์˜ค๋ฉด ํ•ด๋‹น ์•„์ดํ…œ๋งŒ ๊ฐฑ์‹ ํ•˜๋„๋ก ๋ณ€๊ฒฝ
  • Loading branch information
leeeha authored Sep 13, 2023
2 parents b22ab89 + 08b1e23 commit 5509f17
Show file tree
Hide file tree
Showing 18 changed files with 343 additions and 226 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ class FeedRepositoryImpl @Inject constructor(
private val feedDataSource: FeedDataSource,
private val feedService: FeedService
) : FeedRepository {
override suspend fun getWineyFeedList(): Flow<PagingData<WineyFeed>> =
Pager(PagingConfig(WINEYFEED_PAGE_SIZE, prefetchDistance = LOAD_DISTANCE)) {
override fun getWineyFeedList(): Flow<PagingData<WineyFeed>> =
Pager(PagingConfig(pageSize = WINEYFEED_PAGE_SIZE, prefetchDistance = PRE_FETCH_DISTANCE)) {
WineyFeedPagingSource(feedService)
}.flow

override suspend fun getMyFeedList(): Flow<PagingData<WineyFeed>> =
Pager(PagingConfig(MYFEED_PAGE_SIZE, prefetchDistance = LOAD_DISTANCE)) {
override fun getMyFeedList(): Flow<PagingData<WineyFeed>> =
Pager(PagingConfig(pageSize = MYFEED_PAGE_SIZE, prefetchDistance = PRE_FETCH_DISTANCE)) {
MyFeedPagingSource(feedService)
}.flow

Expand Down Expand Up @@ -77,6 +77,8 @@ class FeedRepositoryImpl @Inject constructor(
companion object {
const val WINEYFEED_PAGE_SIZE = 20
const val MYFEED_PAGE_SIZE = 10
const val LOAD_DISTANCE = 2

// ํ˜„์žฌ ํŽ˜์ด์ง€ ๋งจ ๋ฐ‘์—์„œ 5๋ฒˆ์งธ ์•„์ดํ…œ์— ๋„๋‹ฌํ•˜๋ฉด, ๋‹ค์Œ ํŽ˜์ด์ง€ ๋กœ๋”ฉ
const val PRE_FETCH_DISTANCE = 5
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package org.go.sopt.winey.data.source.paging

import androidx.paging.PagingSource
import androidx.paging.PagingState
import kotlinx.coroutines.delay
import org.go.sopt.winey.data.service.FeedService
import org.go.sopt.winey.domain.entity.WineyFeed
import java.io.IOException

abstract class BasePagingSource(
protected val feedService: FeedService
) : PagingSource<Int, WineyFeed>() {
abstract suspend fun getFeedList(pageNumber: Int): List<WineyFeed>

override fun getRefreshKey(state: PagingState<Int, WineyFeed>): Int? {
return state.anchorPosition?.let { position ->
Expand All @@ -18,26 +18,22 @@ abstract class BasePagingSource(
}
}

abstract suspend fun getFeedList(position: Int): List<WineyFeed>

override suspend fun load(params: LoadParams<Int>): LoadResult<Int, WineyFeed> {
val position = params.key ?: STARTING_KEY
if (position != STARTING_KEY) delay(DELAY_MILLIS)

return try {
val feedData = getFeedList(position)
val pageNumber = params.key ?: FIRST_PAGE_NUM
val feeds = getFeedList(pageNumber)

LoadResult.Page(
data = feedData,
prevKey = if (position == STARTING_KEY) null else position - 1,
nextKey = if (feedData.isEmpty() || feedData.first().isEnd) null else position + 1
data = feeds,
prevKey = if (pageNumber == FIRST_PAGE_NUM) null else pageNumber - 1,
nextKey = if (feeds.isEmpty() || feeds.first().isEnd) null else pageNumber + 1
)
} catch (e: IOException) {
return LoadResult.Error(e)
}
}

companion object {
private const val STARTING_KEY = 1
private const val DELAY_MILLIS = 1_000L
private const val FIRST_PAGE_NUM = 1
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import javax.inject.Inject
class MyFeedPagingSource @Inject constructor(
feedService: FeedService
) : BasePagingSource(feedService) {
override suspend fun getFeedList(position: Int): List<WineyFeed> {
return feedService.getMyFeedList(position).toWineyFeed()
override suspend fun getFeedList(pageNumber: Int): List<WineyFeed> {
return feedService.getMyFeedList(pageNumber).toWineyFeed()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import javax.inject.Inject
class WineyFeedPagingSource @Inject constructor(
feedService: FeedService
) : BasePagingSource(feedService) {
override suspend fun getFeedList(position: Int): List<WineyFeed> {
return feedService.getWineyFeedList(position).toWineyFeed()
override suspend fun getFeedList(pageNumber: Int): List<WineyFeed> {
return feedService.getWineyFeedList(pageNumber).toWineyFeed()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ data class WineyFeed(
val writerLevel: Int,
val totalPageSize: Int,
val isEnd: Boolean,
val comments: Long,
val timeAgo: String
var comments: Long,
var timeAgo: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import org.go.sopt.winey.domain.entity.Like
import org.go.sopt.winey.domain.entity.WineyFeed

interface FeedRepository {
suspend fun getWineyFeedList(): Flow<PagingData<WineyFeed>>
fun getWineyFeedList(): Flow<PagingData<WineyFeed>>

suspend fun getMyFeedList(): Flow<PagingData<WineyFeed>>
fun getMyFeedList(): Flow<PagingData<WineyFeed>>

suspend fun postFeedLike(feedId: Int, requestPostLikeDto: RequestPostLikeDto): Result<Like>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.view.ViewGroup
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.RecyclerView
import org.go.sopt.winey.databinding.ItemWineyfeedPostBinding
import org.go.sopt.winey.domain.entity.DetailFeed
import org.go.sopt.winey.domain.entity.WineyFeed
import org.go.sopt.winey.util.view.ItemDiffCallback
import org.go.sopt.winey.util.view.setOnSingleClickListener
Expand Down Expand Up @@ -52,7 +53,7 @@ class WineyFeedAdapter(
holder.onBind(getItem(position))
}

fun updateItem(feedId: Int, isLiked: Boolean, likes: Int): WineyFeed? {
fun updateLikeNumber(feedId: Int, isLiked: Boolean, likes: Int): WineyFeed? {
snapshot().items.forEachIndexed { index, wineyFeed ->
if (wineyFeed.feedId == feedId) {
wineyFeed.isLiked = isLiked
Expand All @@ -64,6 +65,20 @@ class WineyFeedAdapter(
return null
}

fun updateItem(clickedItemId: Int, newFeed: DetailFeed) {
val currentList = snapshot().items
currentList.forEachIndexed { index, currentFeed ->
if (currentFeed.feedId == clickedItemId) {
currentFeed.isLiked = newFeed.isLiked
currentFeed.likes = newFeed.likes
currentFeed.comments = newFeed.comments
currentFeed.timeAgo = newFeed.timeAgo
notifyItemChanged(index)
return@forEachIndexed
}
}
}

fun deleteItem(feedId: Int): MutableList<WineyFeed> {
val currentList = snapshot().items
val newList = currentList.toMutableList()
Expand Down
Loading

0 comments on commit 5509f17

Please sign in to comment.