-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #67 from snuhcs-course/feat/android-emoji-pagination
feat: emoji pagination
- Loading branch information
Showing
5 changed files
with
82 additions
and
45 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
android/app/src/main/java/com/goliath/emojihub/data_sources/EmojiPagingSource.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,34 @@ | ||
package com.goliath.emojihub.data_sources | ||
|
||
import androidx.paging.PagingSource | ||
import androidx.paging.PagingState | ||
import com.goliath.emojihub.data_sources.api.EmojiApi | ||
import com.goliath.emojihub.models.EmojiDto | ||
import javax.inject.Inject | ||
|
||
class EmojiPagingSource @Inject constructor( | ||
private val api: EmojiApi | ||
): PagingSource<Int, EmojiDto>() { | ||
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, EmojiDto> { | ||
val cursor = params.key ?: 1 | ||
val count = params.loadSize | ||
return try { | ||
val response = api.fetchEmojiList(1, cursor, count).body() | ||
val data = response ?: listOf() | ||
LoadResult.Page( | ||
data = data, | ||
prevKey = if (cursor == 1) null else cursor - 1, | ||
nextKey = if (data.isEmpty()) null else cursor + 1 | ||
) | ||
} catch (exception: Exception) { | ||
LoadResult.Error(exception) | ||
} | ||
} | ||
|
||
override fun getRefreshKey(state: PagingState<Int, EmojiDto>): Int? { | ||
return state.anchorPosition?.let { anchorPosition -> | ||
state.closestPageToPosition(anchorPosition)?.prevKey?.plus(1) | ||
?: state.closestPageToPosition(anchorPosition)?.nextKey?.minus(1) | ||
} | ||
} | ||
} |
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
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