1

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
Kotlin Learner
  • 3,995
  • 6
  • 47
  • 127

1 Answers1

1

For me it seems your code actually works properly:

val format = SimpleDateFormat("dd/MM/yyyy")
val paging = PagingSource(5, format.parse("28/09/21"), null)

println("page0: " + paging.listOfDates(0).map(format::format))
println("page1: " + paging.listOfDates(1).map(format::format))

Output:

page0: [28/09/0021, 29/09/0021, 30/09/0021, 01/10/0021, 02/10/0021]
page1: [03/10/0021, 04/10/0021, 05/10/0021, 06/10/0021, 07/10/0021]

My guess is you actually misuse it and mistake is in another place. For example you receive page as 1-indexed and you assumed it is 0-indexed. Then you use it directly with your code which is 0-indexed. If this is the case, then you just need to subtract 1 when passing the page number to listOfDates().

broot
  • 21,588
  • 3
  • 30
  • 35