Skip to content

Commit 69e47e5

Browse files
committed
Fix issue where header height wasn't updated correctly
1 parent ef2a323 commit 69e47e5

File tree

6 files changed

+119
-112
lines changed

6 files changed

+119
-112
lines changed

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

Lines changed: 1 addition & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package com.alamkanak.weekview
22

33
import android.graphics.Canvas
44
import android.graphics.Paint
5-
import android.graphics.Rect
65
import android.graphics.RectF
76
import java.util.Calendar
87
import kotlin.math.max
@@ -21,8 +20,7 @@ internal class CalendarRenderer(
2120
DayBackgroundDrawer(viewState),
2221
BackgroundGridDrawer(viewState),
2322
SingleEventsDrawer(viewState, eventChipsCache),
24-
NowLineDrawer(viewState),
25-
HeaderRowDrawer(viewState)
23+
NowLineDrawer(viewState)
2624
)
2725

2826
override fun render(canvas: Canvas) {
@@ -305,73 +303,3 @@ private class NowLineDrawer(
305303
drawCircle(actualStartPixel, lineStartY, adjustedRadius, viewState.nowDotPaint)
306304
}
307305
}
308-
309-
private class HeaderRowDrawer(
310-
private val viewState: ViewState
311-
) : Drawer {
312-
313-
override fun draw(canvas: Canvas) {
314-
val width = viewState.viewWidth.toFloat()
315-
316-
val backgroundPaint = if (viewState.showHeaderRowBottomShadow) {
317-
viewState.headerBackgroundPaint.withShadow(
318-
radius = viewState.headerRowBottomShadowRadius,
319-
color = viewState.headerRowBottomShadowColor
320-
)
321-
} else viewState.headerBackgroundPaint
322-
323-
canvas.drawRect(0f, 0f, width, viewState.headerHeight, backgroundPaint)
324-
325-
if (viewState.showWeekNumber) {
326-
canvas.drawWeekNumber(viewState)
327-
}
328-
329-
if (viewState.showHeaderRowBottomLine) {
330-
val y = viewState.headerHeight - viewState.headerRowBottomLineWidth
331-
canvas.drawLine(0f, y, width, y, viewState.headerRowBottomLinePaint)
332-
}
333-
}
334-
335-
private fun Canvas.drawWeekNumber(state: ViewState) {
336-
val weekNumber = state.dateRange.first().weekOfYear.toString()
337-
338-
val bounds = state.weekNumberBounds
339-
val textPaint = state.weekNumberTextPaint
340-
341-
val textHeight = textPaint.textHeight
342-
val textOffset = (textHeight / 2f).roundToInt() - textPaint.descent().roundToInt()
343-
344-
val width = textPaint.getTextBounds("52").width() * 2.5f
345-
val height = textHeight * 1.5f
346-
347-
val backgroundRect = RectF(
348-
bounds.centerX() - width / 2f,
349-
bounds.centerY() - height / 2f,
350-
bounds.centerX() + width / 2f,
351-
bounds.centerY() + height / 2f
352-
)
353-
354-
drawRect(bounds, state.headerBackgroundPaint)
355-
356-
val backgroundPaint = state.weekNumberBackgroundPaint
357-
val radius = state.weekNumberBackgroundCornerRadius.toFloat()
358-
drawRoundRect(backgroundRect, radius, radius, backgroundPaint)
359-
360-
drawText(weekNumber, bounds.centerX(), bounds.centerY() + textOffset, textPaint)
361-
}
362-
}
363-
364-
private val Paint.textHeight: Int
365-
get() = (descent() - ascent()).roundToInt()
366-
367-
private fun Paint.getTextBounds(text: String): Rect {
368-
val rect = Rect()
369-
getTextBounds(text, 0, text.length, rect)
370-
return rect
371-
}
372-
373-
private fun Paint.withShadow(radius: Int, color: Int): Paint {
374-
return Paint(this).apply {
375-
setShadowLayer(radius.toFloat(), 0f, 0f, color)
376-
}
377-
}

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

Lines changed: 84 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.alamkanak.weekview
22

33
import android.graphics.Canvas
4+
import android.graphics.Paint
5+
import android.graphics.Rect
46
import android.graphics.RectF
57
import android.text.SpannableStringBuilder
68
import android.text.StaticLayout
@@ -23,22 +25,26 @@ internal class HeaderRenderer(
2325
labelLayouts = dateLabelLayouts
2426
)
2527

26-
private val dateLabelDrawer = DayLabelsDrawer(
27-
viewState = viewState,
28-
dateLabelLayouts = dateLabelLayouts
29-
)
30-
3128
private val eventsUpdater = AllDayEventsUpdater(
3229
viewState = viewState,
3330
eventsLabelLayouts = allDayEventLabels,
3431
eventChipsCache = eventChipsCache
3532
)
3633

34+
private val dateLabelDrawer = DayLabelsDrawer(
35+
viewState = viewState,
36+
dateLabelLayouts = dateLabelLayouts
37+
)
38+
3739
private val eventsDrawer = AllDayEventsDrawer(
3840
viewState = viewState,
3941
allDayEventLayouts = allDayEventLabels
4042
)
4143

44+
private val headerRowDrawer = HeaderRowDrawer(
45+
viewState = viewState
46+
)
47+
4248
override fun onSizeChanged(width: Int, height: Int) {
4349
allDayEventLabels.clear()
4450
dateLabelLayouts.clear()
@@ -53,10 +59,11 @@ internal class HeaderRenderer(
5359
if (eventsUpdater.isRequired()) {
5460
eventsUpdater.update()
5561
}
56-
eventsDrawer.draw(canvas)
57-
5862
headerRowUpdater.update()
63+
64+
headerRowDrawer.draw(canvas)
5965
dateLabelDrawer.draw(canvas)
66+
eventsDrawer.draw(canvas)
6067
}
6168
}
6269

@@ -290,3 +297,73 @@ internal class AllDayEventsDrawer(
290297
}
291298
}
292299
}
300+
301+
private class HeaderRowDrawer(
302+
private val viewState: ViewState
303+
) : Drawer {
304+
305+
override fun draw(canvas: Canvas) {
306+
val width = viewState.viewWidth.toFloat()
307+
308+
val backgroundPaint = if (viewState.showHeaderRowBottomShadow) {
309+
viewState.headerBackgroundPaint.withShadow(
310+
radius = viewState.headerRowBottomShadowRadius,
311+
color = viewState.headerRowBottomShadowColor
312+
)
313+
} else viewState.headerBackgroundPaint
314+
315+
canvas.drawRect(0f, 0f, width, viewState.headerHeight, backgroundPaint)
316+
317+
if (viewState.showWeekNumber) {
318+
canvas.drawWeekNumber(viewState)
319+
}
320+
321+
if (viewState.showHeaderRowBottomLine) {
322+
val y = viewState.headerHeight - viewState.headerRowBottomLineWidth
323+
canvas.drawLine(0f, y, width, y, viewState.headerRowBottomLinePaint)
324+
}
325+
}
326+
327+
private fun Canvas.drawWeekNumber(state: ViewState) {
328+
val weekNumber = state.dateRange.first().weekOfYear.toString()
329+
330+
val bounds = state.weekNumberBounds
331+
val textPaint = state.weekNumberTextPaint
332+
333+
val textHeight = textPaint.textHeight
334+
val textOffset = (textHeight / 2f).roundToInt() - textPaint.descent().roundToInt()
335+
336+
val width = textPaint.getTextBounds("52").width() * 2.5f
337+
val height = textHeight * 1.5f
338+
339+
val backgroundRect = RectF(
340+
bounds.centerX() - width / 2f,
341+
bounds.centerY() - height / 2f,
342+
bounds.centerX() + width / 2f,
343+
bounds.centerY() + height / 2f
344+
)
345+
346+
drawRect(bounds, state.headerBackgroundPaint)
347+
348+
val backgroundPaint = state.weekNumberBackgroundPaint
349+
val radius = state.weekNumberBackgroundCornerRadius.toFloat()
350+
drawRoundRect(backgroundRect, radius, radius, backgroundPaint)
351+
352+
drawText(weekNumber, bounds.centerX(), bounds.centerY() + textOffset, textPaint)
353+
}
354+
}
355+
356+
private val Paint.textHeight: Int
357+
get() = (descent() - ascent()).roundToInt()
358+
359+
private fun Paint.getTextBounds(text: String): Rect {
360+
val rect = Rect()
361+
getTextBounds(text, 0, text.length, rect)
362+
return rect
363+
}
364+
365+
private fun Paint.withShadow(radius: Int, color: Int): Paint {
366+
return Paint(this).apply {
367+
setShadowLayer(radius.toFloat(), 0f, 0f, color)
368+
}
369+
}
Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.alamkanak.weekview
22

3-
import android.animation.Animator
43
import android.animation.ValueAnimator as AndroidValueAnimator
54
import android.view.animation.DecelerateInterpolator
65

@@ -15,8 +14,7 @@ internal class ValueAnimator {
1514
fromValue: Float,
1615
toValue: Float,
1716
duration: Long = 300,
18-
onUpdate: (Float) -> Unit,
19-
onEnd: () -> Unit = {}
17+
onUpdate: (Float) -> Unit
2018
) {
2119
valueAnimator?.cancel()
2220

@@ -29,8 +27,6 @@ internal class ValueAnimator {
2927
onUpdate(value)
3028
}
3129

32-
addEndListener { onEnd() }
33-
3430
start()
3531
}
3632
}
@@ -39,15 +35,3 @@ internal class ValueAnimator {
3935
valueAnimator?.cancel()
4036
}
4137
}
42-
43-
private fun AndroidValueAnimator.addEndListener(listener: (AndroidValueAnimator) -> Unit) {
44-
addListener(object : Animator.AnimatorListener {
45-
override fun onAnimationStart(animator: Animator) {
46-
listener(animator as AndroidValueAnimator)
47-
}
48-
49-
override fun onAnimationEnd(animator: Animator) = Unit
50-
override fun onAnimationCancel(animator: Animator) = Unit
51-
override fun onAnimationRepeat(animator: Animator) = Unit
52-
})
53-
}

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import kotlin.math.min
1818
typealias DateFormatter = (Calendar) -> String
1919
typealias TimeFormatter = (Int) -> String
2020

21-
data class ViewState(
21+
internal data class ViewState(
2222
// View
2323
var viewWidth: Int = 0,
2424
var viewHeight: Int = 0,
@@ -805,11 +805,6 @@ private fun TypedArray.getInt(index: Int): Int? {
805805
return if (hasValue(index)) getInteger(index, 0) else null
806806
}
807807

808-
private fun Float.limit(
809-
minValue: Float,
810-
maxValue: Float
811-
): Float = min(max(this, minValue), maxValue)
812-
813808
private object Defaults {
814809
const val BACKGROUND_COLOR = Color.WHITE
815810
val PAST_BACKGROUND_COLOR = Color.rgb(227, 227, 227)

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

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ class WeekView @JvmOverloads constructor(
4444
eventChipsCache = eventChipsCache
4545
)
4646

47+
private val scroller = ValueAnimator()
48+
4749
private val renderers: List<Renderer> = listOf(
4850
TimeColumnRenderer(viewState),
4951
CalendarRenderer(viewState, eventChipsCache),
@@ -1110,9 +1112,24 @@ class WeekView @JvmOverloads constructor(
11101112
return
11111113
}
11121114

1113-
val diff = adjustedDate.daysFromToday
1114-
viewState.currentOrigin.x = diff.toFloat() * (-1f) * viewState.totalDayWidth
1115-
invalidate()
1115+
val destinationOffset = viewState.getXOriginForDate(date)
1116+
val adjustedDestinationOffset = destinationOffset.limit(
1117+
minValue = viewState.minX,
1118+
maxValue = viewState.maxX
1119+
)
1120+
1121+
scroller.animate(
1122+
fromValue = viewState.currentOrigin.x,
1123+
toValue = adjustedDestinationOffset,
1124+
onUpdate = {
1125+
viewState.currentOrigin.x = it
1126+
invalidate()
1127+
}
1128+
)
1129+
1130+
// val diff = adjustedDate.daysFromToday
1131+
// viewState.currentOrigin.x = diff.toFloat() * (-1f) * viewState.totalDayWidth
1132+
// invalidate()
11161133
}
11171134

11181135
/**
@@ -1144,10 +1161,16 @@ class WeekView @JvmOverloads constructor(
11441161
// height minus the height of WeekView, which would result in scrolling all the way to the
11451162
// bottom.
11461163
val maxOffset = viewState.totalDayHeight - height
1147-
val finalOffset = min(maxOffset, desiredOffset)
1148-
1149-
viewState.currentOrigin.y = finalOffset * (-1)
1150-
invalidate()
1164+
val finalOffset = min(maxOffset, desiredOffset) * (-1)
1165+
1166+
scroller.animate(
1167+
fromValue = viewState.currentOrigin.y,
1168+
toValue = finalOffset,
1169+
onUpdate = {
1170+
viewState.currentOrigin.y = it
1171+
invalidate()
1172+
}
1173+
)
11511174
}
11521175

11531176
/**

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,6 @@ internal class WeekViewGestureHandler(
247247

248248
private val Context.scaledTouchSlop: Int
249249
get() = ViewConfiguration.get(this).scaledTouchSlop
250-
251-
private fun Float.limit(minValue: Float, maxValue: Float): Float = min(max(this, minValue), maxValue)
252250
}
251+
252+
internal fun Float.limit(minValue: Float, maxValue: Float): Float = min(max(this, minValue), maxValue)

0 commit comments

Comments
 (0)