Skip to content

Instantly share code, notes, and snippets.

@KryptKode
Created August 23, 2019 11:55
Show Gist options
  • Save KryptKode/e8ffe5f5548497ca969e83f4c6d27844 to your computer and use it in GitHub Desktop.
Save KryptKode/e8ffe5f5548497ca969e83f4c6d27844 to your computer and use it in GitHub Desktop.
object PositionUtil {
fun computePositionalDataSourceEquivalent(position:Int, totalCount: Int): Int {
val pageSize = DataSource.PAGE_SIZE //DataSource.PAGE_SIZE is the requested page size
val initialLoadSize = DataSource.PAGE_SIZE * 3 //Default loadSize is 3 times the requested load size
var pageStart = position / pageSize * pageSize
// maximum start pos is that which will encompass end of list
val maximumLoadPage = (totalCount - initialLoadSize + pageSize - 1) / pageSize * pageSize
pageStart = Math.min(maximumLoadPage, pageStart)
// minimum start position is 0
pageStart = Math.max(0, pageStart)
return pageStart
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment