Skip to content

Commit ed40e12

Browse files
authored
Fix issue where day backgrounds weren't applied correctly (#234)
1 parent 7d4fd90 commit ed40e12

4 files changed

Lines changed: 56 additions & 49 deletions

File tree

core/src/main/java/com/alamkanak/weekview/CalendarRenderer.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,28 +120,28 @@ private class DayBackgroundDrawer(
120120
/**
121121
* Draws a day's background color in the corresponding bounds.
122122
*
123-
* @param day The [Calendar] indicating the date
123+
* @param date The [Calendar] indicating the date
124124
* @param startPixel The x-coordinate on which to start drawing the background
125125
* @param canvas The [Canvas] on which to draw the background
126126
*/
127127
private fun drawDayBackground(
128-
day: Calendar,
128+
date: Calendar,
129129
startPixel: Float,
130130
canvas: Canvas
131131
) {
132132
val actualStartPixel = max(startPixel, viewState.calendarGridBounds.left)
133133
val height = viewState.viewHeight.toFloat()
134134

135135
// If not specified, this will use the normal day background.
136-
val pastPaint = viewState.getPastBackgroundPaint(isWeekend = day.isWeekend)
137-
val futurePaint = viewState.getFutureBackgroundPaint(isWeekend = day.isWeekend)
136+
val pastPaint = viewState.getPastBackgroundPaint(date = date)
137+
val futurePaint = viewState.getFutureBackgroundPaint(date = date)
138138

139139
val startY = viewState.headerHeight + viewState.currentOrigin.y
140140
val endX = startPixel + viewState.dayWidth
141141

142142
when {
143-
day.isToday -> drawPastAndFutureRect(actualStartPixel, startY, endX, pastPaint, futurePaint, height, canvas)
144-
day.isBeforeToday -> canvas.drawRect(actualStartPixel, startY, endX, height, pastPaint)
143+
date.isToday -> drawPastAndFutureRect(actualStartPixel, startY, endX, pastPaint, futurePaint, height, canvas)
144+
date.isBeforeToday -> canvas.drawRect(actualStartPixel, startY, endX, height, pastPaint)
145145
else -> canvas.drawRect(actualStartPixel, startY, endX, height, futurePaint)
146146
}
147147
}

core/src/main/java/com/alamkanak/weekview/ViewState.kt

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -154,15 +154,15 @@ internal class ViewState {
154154
style = Paint.Style.STROKE
155155
}
156156

157-
val todayBackgroundPaint = Paint()
157+
var todayBackgroundPaint: Paint? = null
158158

159-
val futureBackgroundPaint = Paint()
159+
var futureBackgroundPaint: Paint? = null
160160

161-
val pastBackgroundPaint = Paint()
161+
var pastBackgroundPaint: Paint? = null
162162

163-
val futureWeekendBackgroundPaint = Paint()
163+
var futureWeekendBackgroundPaint: Paint? = null
164164

165-
val pastWeekendBackgroundPaint = Paint()
165+
var pastWeekendBackgroundPaint: Paint? = null
166166

167167
val timeColumnSeparatorPaint = Paint()
168168

@@ -393,12 +393,22 @@ internal class ViewState {
393393
currentOrigin.y = min(currentOrigin.y, 0f)
394394
}
395395

396-
fun getPastBackgroundPaint(isWeekend: Boolean): Paint {
397-
return if (isWeekend) pastWeekendBackgroundPaint else pastBackgroundPaint
396+
fun getPastBackgroundPaint(date: Calendar): Paint {
397+
val paint = when {
398+
date.isToday -> todayBackgroundPaint
399+
date.isWeekend -> pastWeekendBackgroundPaint
400+
else -> pastBackgroundPaint
401+
}
402+
return paint ?: dayBackgroundPaint
398403
}
399404

400-
fun getFutureBackgroundPaint(isWeekend: Boolean): Paint {
401-
return if (isWeekend) futureWeekendBackgroundPaint else futureBackgroundPaint
405+
fun getFutureBackgroundPaint(date: Calendar): Paint {
406+
val paint = when {
407+
date.isToday -> todayBackgroundPaint
408+
date.isWeekend -> futureWeekendBackgroundPaint
409+
else -> futureBackgroundPaint
410+
}
411+
return paint ?: dayBackgroundPaint
402412
}
403413

404414
private fun updateHourHeight(viewHeight: Int) {

core/src/main/java/com/alamkanak/weekview/ViewStateFactory.kt

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.alamkanak.weekview
33
import android.content.Context
44
import android.content.res.TypedArray
55
import android.graphics.Color
6+
import android.graphics.Paint
67
import android.graphics.Typeface
78
import android.os.Build
89
import android.util.AttributeSet
@@ -83,25 +84,12 @@ internal object ViewStateFactory {
8384
color = a.getColor(R.styleable.WeekView_dayBackgroundColor, context.windowBackground)
8485
}
8586

86-
viewState.todayBackgroundPaint.apply {
87-
color = a.getColor(R.styleable.WeekView_todayBackgroundColor, viewState.dayBackgroundPaint.color)
88-
}
89-
90-
viewState.pastBackgroundPaint.apply {
91-
color = a.getColor(R.styleable.WeekView_pastBackgroundColor, viewState.dayBackgroundPaint.color)
92-
}
93-
94-
viewState.futureBackgroundPaint.apply {
95-
color = a.getColor(R.styleable.WeekView_futureBackgroundColor, viewState.dayBackgroundPaint.color)
96-
}
97-
98-
viewState.pastWeekendBackgroundPaint.apply {
99-
color = a.getColor(R.styleable.WeekView_pastWeekendBackgroundColor, viewState.pastBackgroundPaint.color)
100-
}
87+
viewState.todayBackgroundPaint = a.paintFromColor(colorIndex = R.styleable.WeekView_todayBackgroundColor)
88+
viewState.pastBackgroundPaint = a.paintFromColor(colorIndex = R.styleable.WeekView_pastBackgroundColor)
89+
viewState.futureBackgroundPaint = a.paintFromColor(colorIndex = R.styleable.WeekView_futureBackgroundColor)
10190

102-
viewState.futureWeekendBackgroundPaint.apply {
103-
color = a.getColor(R.styleable.WeekView_futureWeekendBackgroundColor, viewState.futureBackgroundPaint.color)
104-
}
91+
viewState.pastWeekendBackgroundPaint = a.paintFromColor(colorIndex = R.styleable.WeekView_pastWeekendBackgroundColor)
92+
viewState.futureWeekendBackgroundPaint = a.paintFromColor(colorIndex = R.styleable.WeekView_futureWeekendBackgroundColor)
10593

10694
viewState.timeColumnSeparatorPaint.apply {
10795
color = a.getColor(R.styleable.WeekView_timeColumnSeparatorColor, context.lineColor)
@@ -229,6 +217,14 @@ internal object ViewStateFactory {
229217
}
230218
}
231219

220+
private fun TypedArray.paintFromColor(colorIndex: Int): Paint? {
221+
return if (hasValue(colorIndex)) getColor(colorIndex, 0).toPaint() else null
222+
}
223+
224+
internal fun Int.toPaint(): Paint {
225+
return Paint().apply { color = this@toPaint }
226+
}
227+
232228
private const val SANS = 1
233229
private const val SERIF = 2
234230
private const val MONOSPACE = 3

core/src/main/java/com/alamkanak/weekview/WeekView.kt

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -753,61 +753,62 @@ class WeekView @JvmOverloads constructor(
753753
}
754754

755755
/**
756-
* Returns the background color of the current date.
756+
* Returns the background color of the current date. If not explicitly set, WeekView will use
757+
* [dayBackgroundColor].
757758
*/
758759
@PublicApi
759760
var todayBackgroundColor: Int
760-
get() = viewState.todayBackgroundPaint.color
761+
get() = viewState.todayBackgroundPaint?.color ?: dayBackgroundColor
761762
set(value) {
762-
viewState.todayBackgroundPaint.color = value
763+
viewState.todayBackgroundPaint = value.toPaint()
763764
invalidate()
764765
}
765766

766767
/**
767-
* Returns the background color for past dates. If not explicitly set, WeekView will used
768+
* Returns the background color for past dates. If not explicitly set, WeekView will use
768769
* [dayBackgroundColor].
769770
*/
770771
@PublicApi
771772
var pastBackgroundColor: Int
772-
get() = viewState.pastBackgroundPaint.color
773+
get() = viewState.pastBackgroundPaint?.color ?: dayBackgroundColor
773774
set(value) {
774-
viewState.pastBackgroundPaint.color = value
775+
viewState.pastBackgroundPaint = value.toPaint()
775776
invalidate()
776777
}
777778

778779
/**
779-
* Returns the background color for past weekend dates. If not explicitly set, WeekView will
780-
* used [pastBackgroundColor].
780+
* Returns the background color for past weekend dates. If not explicitly set, WeekView will use
781+
* [dayBackgroundColor].
781782
*/
782783
@PublicApi
783784
var pastWeekendBackgroundColor: Int
784-
get() = viewState.pastWeekendBackgroundPaint.color
785+
get() = viewState.pastWeekendBackgroundPaint?.color ?: dayBackgroundColor
785786
set(value) {
786-
viewState.pastWeekendBackgroundPaint.color = value
787+
viewState.pastWeekendBackgroundPaint = value.toPaint()
787788
invalidate()
788789
}
789790

790791
/**
791-
* Returns the background color for future dates. If not explicitly set, WeekView will used
792+
* Returns the background color for future dates. If not explicitly set, WeekView will use
792793
* [dayBackgroundColor].
793794
*/
794795
@PublicApi
795796
var futureBackgroundColor: Int
796-
get() = viewState.futureBackgroundPaint.color
797+
get() = viewState.futureBackgroundPaint?.color ?: dayBackgroundColor
797798
set(value) {
798-
viewState.futureBackgroundPaint.color = value
799+
viewState.futureBackgroundPaint = value.toPaint()
799800
invalidate()
800801
}
801802

802803
/**
803804
* Returns the background color for future weekend dates. If not explicitly set, WeekView will
804-
* used [futureBackgroundColor].
805+
* use [dayBackgroundColor].
805806
*/
806807
@PublicApi
807808
var futureWeekendBackgroundColor: Int
808-
get() = viewState.futureWeekendBackgroundPaint.color
809+
get() = viewState.futureWeekendBackgroundPaint?.color ?: dayBackgroundColor
809810
set(value) {
810-
viewState.futureWeekendBackgroundPaint.color = value
811+
viewState.futureWeekendBackgroundPaint = value.toPaint()
811812
invalidate()
812813
}
813814

0 commit comments

Comments
 (0)