Skip to content

Commit

Permalink
Release 0.2.6 (#42)
Browse files Browse the repository at this point in the history
Release 0.2.6
  • Loading branch information
kizitonwose authored Aug 10, 2019
2 parents 8f8c3b4 + d9d1dce commit b130887
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
30 changes: 28 additions & 2 deletions library/src/main/java/com/kizitonwose/calendarview/CalendarView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import androidx.recyclerview.widget.PagerSnapHelper
import androidx.recyclerview.widget.RecyclerView
import com.kizitonwose.calendarview.model.*
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.YearMonth
Expand Down Expand Up @@ -402,16 +403,32 @@ open class CalendarView : RecyclerView {
post { calendarAdapter.notifyMonthScrollListenerIfNeeded() }
}

private fun updateAdapterMonthConfig() {
private fun updateAdapterMonthConfig(saveScroll: Boolean = false) {
if (adapter != null) {
val visiblePosition = calendarLayoutManager.findFirstVisibleItemPosition()
val visibleView = calendarLayoutManager.findViewByPosition(visiblePosition)

val viewOffset = (if (isVertical) visibleView?.top else visibleView?.left) ?: 0
val visibleMonth = calendarAdapter.monthConfig.months[visiblePosition]

calendarAdapter.monthConfig =
MonthConfig(
outDateStyle, inDateStyle, maxRowCount,
startMonth ?: return, endMonth ?: return, firstDayOfWeek ?: return,
hasBoundaries
)
calendarAdapter.notifyDataSetChanged()
post { calendarAdapter.notifyMonthScrollListenerIfNeeded() }

post {
if (saveScroll) { // Scroll to the previously visible month with offset.
val visibleMonthNewPos = calendarAdapter.monthConfig.months.indexOf(visibleMonth)
if (visibleMonthNewPos != NO_INDEX) {
calendarLayoutManager.scrollToPositionWithOffset(visibleMonthNewPos, viewOffset)
}
}
calendarAdapter.notifyMonthScrollListenerIfNeeded()
}

}
}

Expand Down Expand Up @@ -569,10 +586,19 @@ open class CalendarView : RecyclerView {
* @param firstDayOfWeek An instance of [DayOfWeek] enum to be the first day of week.
*/
fun setup(startMonth: YearMonth, endMonth: YearMonth, firstDayOfWeek: DayOfWeek) {
val oldStartMonth = this.startMonth
val oldEndMonth = this.endMonth
val oldFirstDayOfWeek = this.firstDayOfWeek

this.startMonth = startMonth
this.endMonth = endMonth
this.firstDayOfWeek = firstDayOfWeek

if (oldStartMonth != null && oldEndMonth != null && oldFirstDayOfWeek != null) {
updateAdapterMonthConfig(saveScroll = true)
return // Only update the months on the calendar, no need for a full setup.
}

clipToPadding = false
clipChildren = false //#ClipChildrenFix

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ internal class CalendarLayoutManager(private val calView: CalendarView, @Recycle
if (monthPosition != NO_INDEX) {
val viewHolder =
calView.findViewHolderForAdapterPosition(monthPosition) as? MonthViewHolder ?: return@post
val offset = calculateOffset(day, viewHolder.itemView)
val offset = calculateDayViewOffsetInParent(day, viewHolder.itemView)
scrollToPositionWithOffset(monthPosition, -offset)
calView.post { adapter.notifyMonthScrollListenerIfNeeded() }
}
}
}

private fun calculateOffset(day: CalendarDay, itemView: View): Int {
private fun calculateDayViewOffsetInParent(day: CalendarDay, itemView: View): Int {
val dayView = itemView.findViewById<View?>(day.date.hashCode()) ?: return 0
val rect = Rect()
dayView.getDrawingRect(rect)
Expand Down Expand Up @@ -85,7 +85,7 @@ internal class CalendarLayoutManager(private val calView: CalendarView, @Recycle
if (day == null) {
return dy
}
val offset = calculateOffset(day, view)
val offset = calculateDayViewOffsetInParent(day, view)
return dy - offset
}

Expand All @@ -94,7 +94,7 @@ internal class CalendarLayoutManager(private val calView: CalendarView, @Recycle
if (day == null) {
return dx
}
val offset = calculateOffset(day, view)
val offset = calculateDayViewOffsetInParent(day, view)
return dx - offset
}
}
Expand Down

0 comments on commit b130887

Please sign in to comment.