Skip to content

Commit

Permalink
fixup! Add an updateRange method.
Browse files Browse the repository at this point in the history
Run the diff algorithms on the "months" list to support
various maxRowCounts.
  • Loading branch information
msimonides committed Oct 26, 2019
1 parent 71f41bb commit 8a3cbc7
Showing 1 changed file with 15 additions and 21 deletions.
36 changes: 15 additions & 21 deletions library/src/main/java/com/kizitonwose/calendarview/CalendarView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import com.kizitonwose.calendarview.ui.*
import com.kizitonwose.calendarview.utils.NO_INDEX
import org.threeten.bp.DayOfWeek
import org.threeten.bp.LocalDate
import org.threeten.bp.Period
import org.threeten.bp.YearMonth


Expand Down Expand Up @@ -628,41 +627,36 @@ open class CalendarView : RecyclerView {
* month(s).
*/
fun updateRange(startMonth: YearMonth, endMonth: YearMonth) {
val oldStartMonth = this.startMonth!!
val oldEndMonth = this.endMonth!!
this.startMonth = startMonth
this.endMonth = endMonth

calendarAdapter.monthConfig =
MonthConfig(
outDateStyle, inDateStyle, maxRowCount,
startMonth, endMonth, firstDayOfWeek ?: return,
hasBoundaries
)
DiffUtil.calculateDiff(
MonthRangeDiffCallback(oldStartMonth, oldEndMonth, startMonth, endMonth), false)
val oldConfig = calendarAdapter.monthConfig
val newConfig = MonthConfig(
outDateStyle, inDateStyle, maxRowCount,
startMonth, endMonth, firstDayOfWeek ?: return,
hasBoundaries
)
calendarAdapter.monthConfig = newConfig

DiffUtil
.calculateDiff(MonthRangeDiffCallback(oldConfig.months, newConfig.months), false)
.dispatchUpdatesTo(calendarAdapter)
}

private class MonthRangeDiffCallback(
private val oldStartMonth: YearMonth,
private val oldEndMonth: YearMonth,
private val newStartMonth: YearMonth,
private val newEndMonth: YearMonth
private val oldItems: List<CalendarMonth>,
private val newItems: List<CalendarMonth>
) : DiffUtil.Callback() {

override fun getOldListSize() = monthCount(oldStartMonth, oldEndMonth)
override fun getOldListSize() = oldItems.size

override fun getNewListSize() = monthCount(newStartMonth, newEndMonth)
override fun getNewListSize() = newItems.size

override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int)
= oldStartMonth.plusMonths(oldItemPosition.toLong()) == newStartMonth.plusMonths(newItemPosition.toLong())
= oldItems[oldItemPosition] == newItems[newItemPosition]

override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int)
= areItemsTheSame(oldItemPosition, newItemPosition)

private fun monthCount(a: YearMonth, b: YearMonth) =
1 + Period.between(a.atDay(1), b.atDay(1)).months
}

companion object {
Expand Down

0 comments on commit 8a3cbc7

Please sign in to comment.