-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9ce3db3
commit 61d3ae9
Showing
28 changed files
with
1,069 additions
and
228 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
45 changes: 45 additions & 0 deletions
45
app/src/main/java/dev/brahmkshatriya/echo/data/extensions/PagedFlow.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,45 @@ | ||
package dev.brahmkshatriya.echo.data.extensions | ||
|
||
import androidx.paging.Pager | ||
import androidx.paging.PagingConfig | ||
import androidx.paging.PagingSource | ||
import androidx.paging.PagingState | ||
import java.io.IOException | ||
|
||
abstract class PagedFlow<T : Any>( | ||
val pageSize: Int = 10 | ||
) { | ||
abstract fun loadItems(page: Int, pageSize: Int): List<T> | ||
|
||
fun getFlow() = Pager( | ||
config = PagingConfig(pageSize = pageSize, enablePlaceholders = false), | ||
pagingSourceFactory = { | ||
object : PagingSource<Int, T>() { | ||
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) | ||
} | ||
} | ||
|
||
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, T> { | ||
val page = params.key ?: 0 | ||
val pageSize = params.loadSize | ||
return try { | ||
val items = loadItems(page, pageSize) | ||
val nextKey = if (items.isEmpty()) null | ||
else if (page == 0) 1 | ||
else page + 1 | ||
|
||
LoadResult.Page( | ||
data = items, | ||
prevKey = if (page == 0) null else page - 1, | ||
nextKey = nextKey | ||
) | ||
} catch (exception: IOException) { | ||
return LoadResult.Error(exception) | ||
} | ||
} | ||
} | ||
}).flow | ||
} |
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
Oops, something went wrong.