Skip to content

Commit

Permalink
Add setMonthMargins and setMonthPadding methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
kizitonwose committed Jul 25, 2020
1 parent e88ce73 commit 4cd3cdb
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 14 deletions.
65 changes: 53 additions & 12 deletions library/src/main/java/com/kizitonwose/calendarview/CalendarView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ open class CalendarView : RecyclerView {
get() = !isVertical

private var configJob: Job? = null
private var updatingMonthConfig = false
private var internalConfigUpdate = false

constructor(context: Context) : super(context)

Expand Down Expand Up @@ -446,12 +446,21 @@ open class CalendarView : RecyclerView {
private val calendarAdapter: CalendarAdapter
get() = adapter as CalendarAdapter

private fun updateAdapterViewConfig() {
if (adapter != null) {
calendarAdapter.viewConfig =
ViewConfig(dayViewResource, monthHeaderResource, monthFooterResource, monthViewClass)
invalidateViewHolders()
}
}

private fun invalidateViewHolders() {
// This does not remove visible views.
// recycledViewPool.clear()

// This removes all views but is internal.
// removeAndRecycleViews()
if (internalConfigUpdate) return

if (adapter == null || layoutManager == null) return
val state = layoutManager?.onSaveInstanceState()
Expand All @@ -461,7 +470,7 @@ open class CalendarView : RecyclerView {
}

private fun updateAdapterMonthConfig(config: MonthConfig? = null) {
if (updatingMonthConfig) return
if (internalConfigUpdate) return
if (adapter != null) {
calendarAdapter.monthConfig = config ?: MonthConfig(
outDateStyle,
Expand Down Expand Up @@ -489,12 +498,12 @@ open class CalendarView : RecyclerView {
hasBoundaries: Boolean = this.hasBoundaries
) {
configJob?.cancel()
updatingMonthConfig = true
internalConfigUpdate = true
this.inDateStyle = inDateStyle
this.outDateStyle = outDateStyle
this.maxRowCount = maxRowCount
this.hasBoundaries = hasBoundaries
updatingMonthConfig = false
internalConfigUpdate = false
updateAdapterMonthConfig()
}

Expand All @@ -512,12 +521,12 @@ open class CalendarView : RecyclerView {
completion: Completion? = null
) {
configJob?.cancel()
updatingMonthConfig = true
internalConfigUpdate = true
this.inDateStyle = inDateStyle
this.outDateStyle = outDateStyle
this.maxRowCount = maxRowCount
this.hasBoundaries = hasBoundaries
updatingMonthConfig = false
internalConfigUpdate = false
configJob = GlobalScope.launch {
val monthConfig = generateMonthConfig(job)
withContext(Main) {
Expand All @@ -527,12 +536,44 @@ open class CalendarView : RecyclerView {
}
}

private fun updateAdapterViewConfig() {
if (adapter != null) {
calendarAdapter.viewConfig =
ViewConfig(dayViewResource, monthHeaderResource, monthFooterResource, monthViewClass)
invalidateViewHolders()
}
/**
* Set the [monthPaddingStart], [monthPaddingTop], [monthPaddingEnd] and [monthPaddingBottom]
* values without invalidating the view holders multiple times which would happen if these
* values were set individually.
*/
fun setMonthPadding(
@Px start: Int = monthPaddingStart,
@Px top: Int = monthPaddingTop,
@Px end: Int = monthPaddingEnd,
@Px bottom: Int = monthPaddingBottom
) {
internalConfigUpdate = true
monthPaddingStart = start
monthPaddingTop = top
monthPaddingEnd = end
monthPaddingBottom = bottom
internalConfigUpdate = false
invalidateViewHolders()
}

/**
* Set the [monthMarginStart], [monthMarginTop], [monthMarginEnd] and [monthMarginBottom]
* values without invalidating the view holders multiple times which would happen if these
* values were set individually.
*/
fun setMonthMargins(
@Px start: Int = monthMarginStart,
@Px top: Int = monthMarginTop,
@Px end: Int = monthMarginEnd,
@Px bottom: Int = monthMarginBottom
) {
internalConfigUpdate = true
monthMarginStart = start
monthMarginTop = top
monthMarginEnd = end
monthMarginBottom = bottom
internalConfigUpdate = false
invalidateViewHolders()
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ import com.kizitonwose.calendarview.ui.ViewContainer
import com.kizitonwose.calendarview.utils.yearMonth
import com.kizitonwose.calendarviewsample.*
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Assert.*
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import java.lang.Thread.sleep
import java.time.DayOfWeek
import java.time.LocalDate
import java.time.YearMonth

Expand Down Expand Up @@ -309,6 +309,24 @@ class CalenderViewTests {
assertTrue(calendarView.findFirstVisibleMonth() == targetVisibleCalMonth)
}

@Test
fun completionBlocksAreCalledOnTheMainThread() {
val calendarView = CalendarView(homeScreenRule.activity)
homeScreenRule.runOnUiThread {
val threadName = Thread.currentThread().name
calendarView.setupAsync(YearMonth.now(), YearMonth.now().plusMonths(10), DayOfWeek.SUNDAY) {
assertTrue(threadName == Thread.currentThread().name)
calendarView.updateMonthConfigurationAsync {
assertTrue(threadName == Thread.currentThread().name)
calendarView.updateMonthRangeAsync {
assertTrue(threadName == Thread.currentThread().name)
}
}
}
}
sleep(3000)
}

private inline fun <reified T : Fragment> findFragment(): T {
return homeScreenRule.activity.supportFragmentManager
.findFragmentByTag(T::class.java.simpleName) as T
Expand Down

0 comments on commit 4cd3cdb

Please sign in to comment.