title: Create Date List with conditions in Android
body: <p>I am using <strong>ViewPager 2</strong> and <strong>Paging 3</strong> 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:-</p>
  <p><strong>1.</strong> List will create dates until End Date.</p>
  <p><strong>2.</strong> Otherwise, list will create unlimited dates.</p>
  <p><strong>1st example</strong></p>
  <p>My list's sizes will always be  smaller than or equal to <strong>5</strong> and I am passing <strong>initial date</strong> as the current date with no end range.</p>
  <pre><code>....... unlimited pages on negative side
  
  page -1 -&gt; [23/09/2021, 24/09/2021, 25/09/2021, 26/09/2021, 27/09/2021]
  
  page 0  -&gt; [28/09/2021, 29/09/2021, 30/09/2021, 01/10/2021, 02/10/2021]
  
  page 1  -&gt; [03/10/2021, 04/10/2021, 05/10/2021, 06/10/2021, 07/10/2021]
  
   ....... till unlimited **pages**
  </code></pre>
  <p><strong>2nd Example</strong></p>
  <p>I pass <strong>initial date</strong> as <strong>26/09/2021</strong> and <strong>end date</strong> as <strong>28/09/2021</strong></p>
  <pre><code>....... unlimited pages on negative side
      
      page -1 -&gt; [21/09/2021, 22/09/2021, 23/09/2021, 24/09/2021, 25/09/2021]
      
      page 0  -&gt; [26/09/2021, 27/09/2021, 28/09/2021]
  
  ..... end if we pass end date
  </code></pre>
  <p><strong>ViewPagerDataSource.kt</strong></p>
  <pre><code>class ViewPagerDataSource(private val pageSize: Int = 5,
          private val initialDate: Date,
          private val endLimitDate: Date?) {
  
      fun listOfDates(pageNumber: Int): List&lt;Date&gt; {
  
          val dates = mutableListOf&lt;Date&gt;()
          val startDateForPage = getStartDate(pageNumber)
          val tempCalendar = Calendar.getInstance()
  
          tempCalendar.time = startDateForPage
          val lastDateForPage = getLastDate(startDateForPage)
  
          while (tempCalendar.time &lt; 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
          }
      }
  }
  </code></pre>
  <p>This logic does not output the proper order. As my initial date is <strong>28/09/21</strong></p>
  <pre><code>....... unlimited pages on negative side
      
      page 0 -&gt; [23/09/2021, 24/09/2021, 25/09/2021, 26/09/2021, 27/09/2021]
      
      page 1  -&gt; [28/09/2021, 29/09/2021, 30/09/2021, 01/10/2021, 02/10/2021]
         
  ....... till unlimited **pages**
  </code></pre>
  <p>My expected result is the <strong>1st Example</strong> above.</p>
  <p>Does any one know what I am doing wrong in my program logic?</p>
  <p>After @broot anwser I am adding my whole code</p>
  <p>I am trying to use alpha of paging 3 dependency</p>
  <pre><code>implementation &quot;androidx.paging:paging-runtime-ktx:3.0.0-alpha09&quot;
  </code></pre>
  <p><strong>ViewPagerPagingSource</strong></p>
  <pre><code>class ViewPagerPagingSource(
      private val dataSource: ViewPagerDataSource
  ) : PagingSource&lt;Int, Date&gt;() {
  
      override suspend fun load(params: LoadParams&lt;Int&gt;): LoadResult&lt;Int, Date&gt; {
          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)
          }
      }
  }
  </code></pre>
  <p><strong>ViewPagerViewModel</strong></p>
  <pre><code>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
      }
  }
  </code></pre>
  <p>If I am using above libary version it's working fine with index 0. But I go to stable version of paging libary 3</p>
  <pre><code>implementation &quot;androidx.paging:paging-runtime-ktx:3.0.1&quot;
  </code></pre>
  <p>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?</p>
  <p><strong>ViewPagerPagingSource.kt</strong></p>
  <pre><code>val position = params.key ?: 1
  </code></pre>