This repository has been archived by the owner on Mar 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
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
Showing
6 changed files
with
186 additions
and
121 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
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
64 changes: 64 additions & 0 deletions
64
...s/ui-calendar/src/main/java/com/ankitsuda/rebound/ui/calendar/CalendarPagingDataSource.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,64 @@ | ||
/* | ||
* Copyright (c) 2022 Ankit Suda. | ||
* | ||
* Licensed under the GNU General Public License v3 | ||
* | ||
* This is free software: you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or (at your option) any later version. | ||
* | ||
* This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; | ||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
* See the GNU General Public License for more details. | ||
*/ | ||
|
||
package com.ankitsuda.rebound.ui.calendar | ||
|
||
import androidx.paging.PagingSource | ||
import androidx.paging.PagingState | ||
import com.ankitsuda.rebound.ui.calendar.models.CalendarMonth | ||
import com.ankitsuda.rebound.ui.calendar.models.InDateStyle | ||
import com.ankitsuda.rebound.ui.calendar.models.MonthConfig | ||
import com.ankitsuda.rebound.ui.calendar.models.OutDateStyle | ||
import kotlinx.coroutines.Job | ||
import java.time.DayOfWeek | ||
import java.time.Month | ||
import java.time.YearMonth | ||
|
||
class CalendarPagingDataSource( | ||
private val startYear: Int, | ||
private val firstDayOfWeek: DayOfWeek, | ||
) : | ||
PagingSource<Int, CalendarMonth>() { | ||
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, CalendarMonth> { | ||
val year = params.key ?: startYear | ||
return try { | ||
val monthConfig = MonthConfig( | ||
outDateStyle = OutDateStyle.NONE, | ||
inDateStyle = InDateStyle.ALL_MONTHS, | ||
startMonth = YearMonth.of(year, Month.JANUARY), | ||
endMonth = YearMonth.of(year, Month.DECEMBER), | ||
hasBoundaries = true, | ||
maxRowCount = Int.MAX_VALUE, | ||
firstDayOfWeek = firstDayOfWeek, | ||
job = Job() | ||
) | ||
|
||
|
||
LoadResult.Page( | ||
data = monthConfig.months, | ||
prevKey = year - 1, | ||
nextKey = year + 1 | ||
) | ||
} catch (e: Exception) { | ||
LoadResult.Error(e) | ||
} | ||
} | ||
|
||
override fun getRefreshKey(state: PagingState<Int, CalendarMonth>): Int? { | ||
return state.anchorPosition?.let { anchorPosition -> | ||
state.closestPageToPosition(anchorPosition)?.prevKey?.plus(1) | ||
?: state.closestPageToPosition(anchorPosition)?.nextKey?.minus(1) | ||
} | ||
} | ||
} |
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.