This repository has been archived by the owner on Aug 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NodeScreen: Implement paging support
Signed-off-by: Aayush Gupta <aayushgupta219@gmail.com>
- Loading branch information
1 parent
a2d2c46
commit 7452a18
Showing
6 changed files
with
96 additions
and
48 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
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
51 changes: 51 additions & 0 deletions
51
app/src/main/java/io/aayush/relabs/network/paging/GenericPagingSource.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,51 @@ | ||
package io.aayush.relabs.network.paging | ||
|
||
import androidx.paging.Pager | ||
import androidx.paging.PagingConfig | ||
import androidx.paging.PagingSource | ||
import androidx.paging.PagingState | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
|
||
class GenericPagingSource<T : Any>( | ||
private val totalPages: Int? = null, | ||
private val block: suspend (Int) -> List<T> | ||
) : PagingSource<Int, T>() { | ||
|
||
companion object { | ||
private const val DEFAULT_PAGE_SIZE = 20 | ||
|
||
fun <T : Any> createPager( | ||
totalPages: Int? = null, | ||
pageSize: Int = DEFAULT_PAGE_SIZE, | ||
enablePlaceholders: Boolean = false, | ||
block: suspend (Int) -> List<T> | ||
): Pager<Int, T> = Pager( | ||
config = PagingConfig(enablePlaceholders = enablePlaceholders, pageSize = pageSize), | ||
pagingSourceFactory = { GenericPagingSource(totalPages, block) } | ||
) | ||
} | ||
|
||
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, T> { | ||
val page = params.key ?: 1 | ||
return try { | ||
withContext(Dispatchers.IO) { | ||
val response = block(page) | ||
LoadResult.Page( | ||
data = response, | ||
prevKey = if (page == 1) null else page - 1, | ||
nextKey = if (totalPages != null && page == totalPages) null else page + 1 | ||
) | ||
} | ||
} catch (e: Exception) { | ||
LoadResult.Error(e) | ||
} | ||
} | ||
|
||
override fun getRefreshKey(state: PagingState<Int, T>): 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