I am using ViewPager 2 and Paging 3 Libraries with dates. I am trying to create a list of dates with specific sizes i.e. numbers of items in a list, and some important conditions are:-
1. List will create dates until End Date.
2. Otherwise, list will create unlimited dates.
1st example
My list's sizes will always be smaller than or equal to 5 and I am passing initial date as the current date with no end range.
....... unlimited pages on negative side
page -1 -> [23/09/2021, 24/09/2021, 25/09/2021, 26/09/2021, 27/09/2021]
page 0 -> [28/09/2021, 29/09/2021, 30/09/2021, 01/10/2021, 02/10/2021]
page 1 -> [03/10/2021, 04/10/2021, 05/10/2021, 06/10/2021, 07/10/2021]
....... till unlimited **pages**
2nd Example
I pass initial date as 26/09/2021 and end date as 28/09/2021
....... unlimited pages on negative side
page -1 -> [21/09/2021, 22/09/2021, 23/09/2021, 24/09/2021, 25/09/2021]
page 0 -> [26/09/2021, 27/09/2021, 28/09/2021]
..... end if we pass end date
ViewPagerDataSource.kt
class ViewPagerDataSource(private val pageSize: Int = 5,
private val initialDate: Date,
private val endLimitDate: Date?) {
fun listOfDates(pageNumber: Int): List<Date> {
val dates = mutableListOf<Date>()
val startDateForPage = getStartDate(pageNumber)
val tempCalendar = Calendar.getInstance()
tempCalendar.time = startDateForPage
val lastDateForPage = getLastDate(startDateForPage)
while (tempCalendar.time < lastDateForPage) {
if (endLimitDate == null || tempCalendar.time.before(endLimitDate) || tempCalendar.time == endLimitDate) {
dates.add(tempCalendar.time)
tempCalendar.add(Calendar.DATE, 1)
} else {
break
}
}
return dates
}
private fun getStartDate(pageNumber: Int): Date {
if (pageNumber == 0) {
return initialDate
} else {
Calendar.getInstance().let {
it.time = initialDate
it.add(Calendar.DATE, pageNumber * pageSize)
return it.time
}
}
}
private fun getLastDate(firstDateForPage: Date): Date {
Calendar.getInstance().let {
it.time = firstDateForPage
it.add(Calendar.DATE, pageSize)
return it.time
}
}
}
This logic does not output the proper order. As my initial date is 28/09/21
....... unlimited pages on negative side
page 0 -> [23/09/2021, 24/09/2021, 25/09/2021, 26/09/2021, 27/09/2021]
page 1 -> [28/09/2021, 29/09/2021, 30/09/2021, 01/10/2021, 02/10/2021]
....... till unlimited **pages**
My expected result is the 1st Example above.
Does any one know what I am doing wrong in my program logic?
After @broot anwser I am adding my whole code
I am trying to use alpha of paging 3 dependency
implementation "androidx.paging:paging-runtime-ktx:3.0.0-alpha09"
ViewPagerPagingSource
class ViewPagerPagingSource(
private val dataSource: ViewPagerDataSource
) : PagingSource<Int, Date>() {
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Date> {
val position = params.key ?: 0
return try {
val data = dataSource.listOfDates(position)
LoadResult.Page(
data = data,
prevKey = if (data.isEmpty()) null else position - 1,
nextKey = if (data.isEmpty()) null else position + 1,
itemsBefore = LoadResult.Page.COUNT_UNDEFINED,
itemsAfter = LoadResult.Page.COUNT_UNDEFINED
)
} catch (exception: IOException) {
LoadResult.Error(exception)
}
}
}
ViewPagerViewModel
class ViewPagerViewModel(app: Application) : AndroidViewModel(app) {
private val dataSource = ViewPagerDataSource(5, currentDate(), endWeek())
val dataList =
Pager(config = PagingConfig(
pageSize = 5,
enablePlaceholders = true
), pagingSourceFactory = {
ViewPagerPagingSource(dataSource)
}).flow
private fun currentDate(): Date {
val calendar = Calendar.getInstance()
return calendar.time
}
private fun endWeek(): Date {
val week: Calendar = Calendar.getInstance()
week.add(Calendar.WEEK_OF_YEAR, 1)
return week.time
}
}
If I am using above libary version it's working fine with index 0. But I go to stable version of paging libary 3
implementation "androidx.paging:paging-runtime-ktx:3.0.1"
It causing issue in my paging index. So i changed my position 0 with 1. It works fine as per @broot suggestion. But why this issue caused ,when change library to stable version. Which is the correct way of doing with index 0 or 1?
ViewPagerPagingSource.kt
val position = params.key ?: 1