Skip to content

Commit

Permalink
Fix month scrolling logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
kizitonwose committed Aug 16, 2020
1 parent 0e5e59e commit 6b472f3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,38 +23,39 @@ internal class CalendarLayoutManager(private val calView: CalendarView, @Recycle
get() = calView.context

fun scrollToMonth(month: YearMonth) {
scrollToPositionWithOffset(adapter.getAdapterPosition(month), 0)
val position = adapter.getAdapterPosition(month)
if (position == NO_INDEX) return
scrollToPositionWithOffset(position, 0)
calView.post { adapter.notifyMonthScrollListenerIfNeeded() }
}

fun smoothScrollToMonth(month: YearMonth) {
val position = adapter.getAdapterPosition(month)
if (position != -1) {
startSmoothScroll(CalendarSmoothScroller(position, null))
}
if (position == NO_INDEX) return
startSmoothScroll(CalendarSmoothScroller(position, null))
}

fun smoothScrollToDay(day: CalendarDay) {
val position = adapter.getAdapterPosition(day)
if (position != -1) {
// Can't target a specific day in a paged calendar.
val isPaged = calView.scrollMode == ScrollMode.PAGED
startSmoothScroll(CalendarSmoothScroller(position, if (isPaged) null else day))
}
val monthPosition = adapter.getAdapterPosition(day)
if (monthPosition == NO_INDEX) return
// Can't target a specific day in a paged calendar.
val isPaged = calView.scrollMode == ScrollMode.PAGED
startSmoothScroll(CalendarSmoothScroller(monthPosition, if (isPaged) null else day))
}

fun scrollToDay(day: CalendarDay) {
val monthPosition = adapter.getAdapterPosition(day)
if (monthPosition == NO_INDEX) return
scrollToPositionWithOffset(monthPosition, 0)
calView.post { adapter.notifyMonthScrollListenerIfNeeded() }
// Can't target a specific day in a paged calendar.
if (calView.scrollMode == ScrollMode.PAGED) return
calView.post {
if (monthPosition != NO_INDEX) {
val viewHolder =
calView.findViewHolderForAdapterPosition(monthPosition) as? MonthViewHolder ?: return@post
if (calView.scrollMode == ScrollMode.PAGED) {
calView.post { adapter.notifyMonthScrollListenerIfNeeded() }
} else {
calView.post {
val viewHolder = calView.findViewHolderForAdapterPosition(monthPosition) as? MonthViewHolder ?: return@post
val offset = calculateDayViewOffsetInParent(day, viewHolder.itemView)
scrollToPositionWithOffset(monthPosition, -offset)
calView.post { adapter.notifyMonthScrollListenerIfNeeded() }
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,20 +208,38 @@ class CalenderViewTests {

val calendarView = findFragment<Example1Fragment>().findViewById<CalendarView>(R.id.exOneCalendar)

val targetMonth = currentMonth.plusMonths(2)

var targetCalMonth: CalendarMonth? = null
calendarView.monthScrollListener = { month ->
targetCalMonth = month
}

val twoMonthsAhead = currentMonth.plusMonths(2)
homeScreenRule.runOnUiThread {
calendarView.smoothScrollToMonth(targetMonth)
calendarView.smoothScrollToMonth(twoMonthsAhead)
}
sleep(3000) // Enough time for smooth scrolling animation.
assertEquals(targetCalMonth?.yearMonth, twoMonthsAhead)

sleep(5000) // Enough time for smooth scrolling animation.
val fourMonthsAhead = currentMonth.plusMonths(4)
homeScreenRule.runOnUiThread {
calendarView.scrollToMonth(fourMonthsAhead)
}
sleep(3000)
assertEquals(targetCalMonth?.yearMonth, fourMonthsAhead)

val sixMonthsAhead = currentMonth.plusMonths(6)
homeScreenRule.runOnUiThread {
calendarView.smoothScrollToDate(sixMonthsAhead.atDay(1))
}
sleep(3000)
assertEquals(targetCalMonth?.yearMonth, sixMonthsAhead)

assertEquals(targetCalMonth?.yearMonth, targetMonth)
val eightMonthsAhead = currentMonth.plusMonths(8)
homeScreenRule.runOnUiThread {
calendarView.scrollToDate(eightMonthsAhead.atDay(1))
}
sleep(3000)
assertEquals(targetCalMonth?.yearMonth, eightMonthsAhead)
}

@Test
Expand Down

0 comments on commit 6b472f3

Please sign in to comment.