-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* build: pagination라이브러리 추가 * feat: 홈 화면 무한 스크롤 기능 구현
- Loading branch information
1 parent
b0b5ccc
commit ddc2d6a
Showing
7 changed files
with
58 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
android/app/src/main/java/com/zzang/chongdae/domain/paging/OfferingPagingSource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.zzang.chongdae.domain.paging | ||
|
||
import androidx.paging.PagingSource | ||
import androidx.paging.PagingState | ||
import com.zzang.chongdae.domain.model.Offering | ||
|
||
class OfferingPagingSource( | ||
private val fetchOfferings: suspend (lastOfferingId: Long, pageSize: Int) -> List<Offering>, | ||
) : PagingSource<Long, Offering>() { | ||
override suspend fun load(params: LoadParams<Long>): LoadResult<Long, Offering> { | ||
val lastOfferingId = params.key ?: INIT_LAST_OFFERING_ID | ||
return runCatching { | ||
val offerings = fetchOfferings(lastOfferingId, params.loadSize) | ||
LoadResult.Page( | ||
data = offerings, | ||
prevKey = if (lastOfferingId == INIT_LAST_OFFERING_ID) null else lastOfferingId - params.loadSize, | ||
nextKey = if (offerings.isEmpty()) null else lastOfferingId + params.loadSize, | ||
) | ||
}.onFailure { throwable -> | ||
LoadResult.Error<Long, Offering>(throwable) | ||
}.getOrThrow() | ||
} | ||
|
||
override fun getRefreshKey(state: PagingState<Long, Offering>): Long? { | ||
return state.anchorPosition?.let { anchorPosition -> | ||
state.closestPageToPosition(anchorPosition)?.prevKey | ||
} | ||
} | ||
|
||
companion object { | ||
private const val INIT_LAST_OFFERING_ID = 0L | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 14 additions & 12 deletions
26
android/app/src/main/java/com/zzang/chongdae/presentation/view/home/OfferingViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,28 @@ | ||
package com.zzang.chongdae.presentation.view.home | ||
|
||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import androidx.paging.Pager | ||
import androidx.paging.PagingConfig | ||
import androidx.paging.PagingData | ||
import androidx.paging.cachedIn | ||
import androidx.paging.liveData | ||
import com.zzang.chongdae.domain.model.Offering | ||
import com.zzang.chongdae.domain.paging.OfferingPagingSource | ||
import com.zzang.chongdae.domain.repository.OfferingsRepository | ||
import kotlinx.coroutines.launch | ||
|
||
class OfferingViewModel( | ||
private val offeringsRepository: OfferingsRepository, | ||
) : ViewModel() { | ||
private val _offerings: MutableLiveData<List<Offering>> = MutableLiveData() | ||
val offerings: LiveData<List<Offering>> get() = _offerings | ||
val offerings: LiveData<PagingData<Offering>> = | ||
Pager( | ||
config = PagingConfig(pageSize = PAGE_SIZE, enablePlaceholders = false), | ||
pagingSourceFactory = { OfferingPagingSource(fetchOfferings = offeringsRepository::fetchOfferings) }, | ||
).liveData | ||
.cachedIn(viewModelScope) | ||
|
||
// 무한 스크롤 적용 시 수정 예정 | ||
fun updateArticles() { | ||
viewModelScope.launch { | ||
offeringsRepository.fetchOfferings(0L, 100).onSuccess { | ||
_offerings.value = it | ||
}.onFailure { | ||
} | ||
} | ||
companion object { | ||
private const val PAGE_SIZE = 10 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters