Skip to content

Commit 86b3813

Browse files
authored
Consolidate all view state information in new ViewState data class (alamkanak#161)
1 parent c5b74af commit 86b3813

31 files changed

+1366
-1767
lines changed

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

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,32 @@ import android.text.StaticLayout
66
import kotlin.math.roundToInt
77

88
internal class AllDayEventsUpdater<T : Any>(
9-
private val view: WeekView<T>,
10-
private val config: WeekViewConfigWrapper,
9+
private val viewState: ViewState,
1110
private val cache: WeekViewCache<T>,
1211
private val chipsCache: EventChipsCache<T>
1312
) : Updater {
1413

15-
private val boundsCalculator = EventChipBoundsCalculator<T>(config)
14+
private val boundsCalculator = EventChipBoundsCalculator<T>(viewState)
1615
private val spannableStringBuilder = SpannableStringBuilder()
1716

1817
private var previousHorizontalOrigin: Float? = null
1918
private var dummyTextLayout: StaticLayout? = null
2019

21-
override fun isRequired(drawingContext: DrawingContext): Boolean {
22-
val didScrollHorizontally = previousHorizontalOrigin != config.currentOrigin.x
23-
val dateRange = drawingContext.dateRange
20+
override fun isRequired(): Boolean {
21+
val didScrollHorizontally = previousHorizontalOrigin != viewState.currentOrigin.x
22+
val dateRange = viewState.dateRange
2423
val containsNewChips = chipsCache.allDayEventChipsInDateRange(dateRange).any { it.bounds == null }
2524
return didScrollHorizontally || containsNewChips
2625
}
2726

28-
override fun update(drawingContext: DrawingContext) {
27+
override fun update() {
2928
cache.clearAllDayEventLayouts()
3029

31-
val datesWithStartPixels = drawingContext.dateRangeWithStartPixels
30+
val datesWithStartPixels = viewState.dateRangeWithStartPixels
3231
for ((date, startPixel) in datesWithStartPixels) {
3332
// If we use a horizontal margin in the day view, we need to offset the start pixel.
3433
val modifiedStartPixel = when {
35-
config.isSingleDay -> startPixel + config.eventMarginHorizontal.toFloat()
34+
viewState.isSingleDay -> startPixel + viewState.eventMarginHorizontal.toFloat()
3635
else -> startPixel
3736
}
3837

@@ -47,7 +46,7 @@ internal class AllDayEventsUpdater<T : Any>(
4746
.map { it.height().roundToInt() }
4847
.max() ?: 0
4948

50-
config.updateAllDayEventHeight(maximumChipHeight)
49+
viewState.updateAllDayEventHeight(maximumChipHeight)
5150
}
5251

5352
private fun calculateTextLayout(
@@ -71,8 +70,8 @@ internal class AllDayEventsUpdater<T : Any>(
7170
val event = eventChip.event
7271
val bounds = checkNotNull(eventChip.bounds)
7372

74-
val fullHorizontalPadding = config.eventPaddingHorizontal * 2
75-
val fullVerticalPadding = config.eventPaddingVertical * 2
73+
val fullHorizontalPadding = viewState.eventPaddingHorizontal * 2
74+
val fullVerticalPadding = viewState.eventPaddingVertical * 2
7675

7776
val width = bounds.width() - fullHorizontalPadding
7877
val height = bounds.height() - fullVerticalPadding
@@ -106,7 +105,7 @@ internal class AllDayEventsUpdater<T : Any>(
106105
val text = spannableStringBuilder.build()
107106
val availableWidth = width.toInt()
108107

109-
val textPaint = config.getTextPaint(event)
108+
val textPaint = viewState.getTextPaint(event)
110109
val textLayout = text.toTextLayout(textPaint, availableWidth)
111110
val lineHeight = textLayout.height / textLayout.lineCount
112111

@@ -124,7 +123,7 @@ internal class AllDayEventsUpdater<T : Any>(
124123
event: ResolvedWeekViewEvent<T>
125124
): StaticLayout {
126125
if (dummyTextLayout == null) {
127-
val textPaint = config.getTextPaint(event)
126+
val textPaint = viewState.getTextPaint(event)
128127
dummyTextLayout = "".toTextLayout(textPaint, width = 0)
129128
}
130129
return checkNotNull(dummyTextLayout)
@@ -135,9 +134,9 @@ internal class AllDayEventsUpdater<T : Any>(
135134
availableWidth: Int,
136135
existingTextLayout: StaticLayout
137136
): StaticLayout {
138-
val textPaint = config.getTextPaint(event)
137+
val textPaint = viewState.getTextPaint(event)
139138
val bounds = checkNotNull(bounds)
140-
val width = bounds.width().roundToInt() - (config.eventPaddingHorizontal * 2)
139+
val width = bounds.width().roundToInt() - (viewState.eventPaddingHorizontal * 2)
141140

142141
val ellipsized = text.ellipsized(textPaint, availableWidth)
143142
val isTooSmallForText = width < 0
@@ -152,8 +151,8 @@ internal class AllDayEventsUpdater<T : Any>(
152151

153152
private val RectF.isValidEventBounds: Boolean
154153
get() = (left < right &&
155-
left < view.width &&
156-
top < view.height &&
157-
right > config.timeColumnWidth &&
154+
left < viewState.viewWidth &&
155+
top < viewState.viewHeight &&
156+
right > viewState.timeColumnWidth &&
158157
bottom > 0)
159158
}

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

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,65 +5,61 @@ import kotlin.math.max
55
import kotlin.math.roundToInt
66

77
internal class BackgroundGridDrawer(
8-
private val view: WeekView<*>,
9-
private val config: WeekViewConfigWrapper
8+
private val viewState: ViewState
109
) : Drawer {
1110

1211
private lateinit var hourLines: FloatArray
1312

14-
override fun draw(
15-
drawingContext: DrawingContext,
16-
canvas: Canvas
17-
) {
18-
drawingContext.startPixels.forEach { startPixel ->
19-
val startX = max(startPixel, config.timeColumnWidth)
13+
override fun draw(canvas: Canvas) {
14+
viewState.startPixels.forEach { startPixel ->
15+
val startX = max(startPixel, viewState.timeColumnWidth)
2016
drawGrid(startX, startPixel, canvas)
2117
}
2218
}
2319

2420
private fun createHourLines(): FloatArray {
25-
val headerHeight = config.getTotalHeaderHeight()
26-
val gridHeight = view.height - headerHeight.toInt()
27-
val linesPerDay = (gridHeight / config.hourHeight) + 1
28-
val overallLines = linesPerDay.roundToInt() * (config.numberOfVisibleDays + 1)
21+
val headerHeight = viewState.getTotalHeaderHeight()
22+
val gridHeight = viewState.viewHeight - headerHeight.toInt()
23+
val linesPerDay = (gridHeight / viewState.hourHeight) + 1
24+
val overallLines = linesPerDay.roundToInt() * (viewState.numberOfVisibleDays + 1)
2925
return FloatArray(overallLines * 4) // 4 lines make a cube in the grid
3026
}
3127

3228
private fun drawGrid(startX: Float, startPixel: Float, canvas: Canvas) {
33-
if (config.showHourSeparators) {
29+
if (viewState.showHourSeparators) {
3430
hourLines = createHourLines()
3531
drawHourLines(startX, startPixel, canvas)
3632
}
3733

38-
if (config.showDaySeparators) {
34+
if (viewState.showDaySeparators) {
3935
drawDaySeparators(startPixel, canvas)
4036
}
4137
}
4238

4339
private fun drawDaySeparators(startPixel: Float, canvas: Canvas) {
44-
val days = config.numberOfVisibleDays
45-
val widthPerDay = config.totalDayWidth
46-
val top = config.headerHeight
40+
val days = viewState.numberOfVisibleDays
41+
val widthPerDay = viewState.totalDayWidth
42+
val top = viewState.headerHeight
4743

4844
for (i in 0 until days) {
4945
val start = startPixel + widthPerDay * (i + 1)
50-
canvas.drawLine(start, top, start, top + view.height, config.daySeparatorPaint)
46+
canvas.drawLine(start, top, start, top + viewState.viewHeight, viewState.daySeparatorPaint)
5147
}
5248
}
5349

5450
private fun drawHourLines(startX: Float, startPixel: Float, canvas: Canvas) {
55-
val hourStep = config.timeColumnHoursInterval
51+
val hourStep = viewState.timeColumnHoursInterval
5652
var lineIndex = 0
5753

58-
for (hour in hourStep until config.hoursPerDay step hourStep) {
59-
val heightOfHour = (config.hourHeight * hour)
60-
val top = config.headerHeight + config.currentOrigin.y + heightOfHour
54+
for (hour in hourStep until viewState.hoursPerDay step hourStep) {
55+
val heightOfHour = (viewState.hourHeight * hour)
56+
val top = viewState.headerHeight + viewState.currentOrigin.y + heightOfHour
6157

62-
val widthPerDay = config.totalDayWidth
63-
val separatorWidth = config.hourSeparatorPaint.strokeWidth
58+
val widthPerDay = viewState.totalDayWidth
59+
val separatorWidth = viewState.hourSeparatorPaint.strokeWidth
6460

65-
val isNotHiddenByHeader = top > config.headerHeight - separatorWidth
66-
val isWithinVisibleRange = top < view.height
61+
val isNotHiddenByHeader = top > viewState.headerHeight - separatorWidth
62+
val isWithinVisibleRange = top < viewState.viewHeight
6763
val isVisibleHorizontally = startPixel + widthPerDay - startX > 0
6864

6965
if (isNotHiddenByHeader && isWithinVisibleRange && isVisibleHorizontally) {
@@ -75,6 +71,6 @@ internal class BackgroundGridDrawer(
7571
}
7672
}
7773

78-
canvas.drawLines(hourLines, config.hourSeparatorPaint)
74+
canvas.drawLines(hourLines, viewState.hourSeparatorPaint)
7975
}
8076
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ internal fun firstDayOfYear(): Calendar {
194194
}
195195
}
196196

197-
internal fun WeekViewConfigWrapper.createDateRange(start: Int, end: Int): List<Calendar> {
197+
internal fun ViewState.createDateRange(start: Int, end: Int): List<Calendar> {
198198
val firstDate = today()
199199
firstDate.firstDayOfWeek = firstDayOfWeek
200200
return (start..end).map { firstDate + Days(it - 1) }

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

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,11 @@ import java.util.Calendar
77
import kotlin.math.max
88

99
internal class DayBackgroundDrawer(
10-
private val view: WeekView<*>,
11-
private val config: WeekViewConfigWrapper
10+
private val viewState: ViewState
1211
) : Drawer {
1312

14-
override fun draw(
15-
drawingContext: DrawingContext,
16-
canvas: Canvas
17-
) {
18-
drawingContext.dateRangeWithStartPixels.forEach { (date, startPixel) ->
13+
override fun draw(canvas: Canvas) {
14+
viewState.dateRangeWithStartPixels.forEach { (date, startPixel) ->
1915
drawDayBackground(date, startPixel, canvas)
2016
}
2117
}
@@ -32,32 +28,32 @@ internal class DayBackgroundDrawer(
3228
startPixel: Float,
3329
canvas: Canvas
3430
) {
35-
val endPixel = startPixel + config.widthPerDay
36-
val isCompletelyHiddenByTimeColumn = endPixel <= config.timeColumnWidth
31+
val endPixel = startPixel + viewState.widthPerDay
32+
val isCompletelyHiddenByTimeColumn = endPixel <= viewState.timeColumnWidth
3733
if (isCompletelyHiddenByTimeColumn) {
3834
return
3935
}
4036

41-
val actualStartPixel = max(startPixel, config.timeColumnWidth)
42-
val height = view.height.toFloat()
37+
val actualStartPixel = max(startPixel, viewState.timeColumnWidth)
38+
val height = viewState.viewHeight.toFloat()
4339

44-
if (config.showDistinctPastFutureColor) {
45-
val useWeekendColor = day.isWeekend && config.showDistinctWeekendColor
46-
val pastPaint = config.getPastBackgroundPaint(useWeekendColor)
47-
val futurePaint = config.getFutureBackgroundPaint(useWeekendColor)
40+
if (viewState.showDistinctPastFutureColor) {
41+
val useWeekendColor = day.isWeekend && viewState.showDistinctWeekendColor
42+
val pastPaint = viewState.getPastBackgroundPaint(useWeekendColor)
43+
val futurePaint = viewState.getFutureBackgroundPaint(useWeekendColor)
4844

49-
val startY = config.headerHeight + config.currentOrigin.y
50-
val endX = startPixel + config.widthPerDay
45+
val startY = viewState.headerHeight + viewState.currentOrigin.y
46+
val endX = startPixel + viewState.widthPerDay
5147

5248
when {
5349
day.isToday -> drawPastAndFutureRect(actualStartPixel, startY, endX, pastPaint, futurePaint, height, canvas)
5450
day.isBeforeToday -> canvas.drawRect(actualStartPixel, startY, endX, height, pastPaint)
5551
else -> canvas.drawRect(actualStartPixel, startY, endX, height, futurePaint)
5652
}
5753
} else {
58-
val todayPaint = config.getDayBackgroundPaint(day.isToday)
59-
val right = startPixel + config.widthPerDay
60-
canvas.drawRect(actualStartPixel, config.headerHeight, right, height, todayPaint)
54+
val todayPaint = viewState.getDayBackgroundPaint(day.isToday)
55+
val right = startPixel + viewState.widthPerDay
56+
canvas.drawRect(actualStartPixel, viewState.headerHeight, right, height, todayPaint)
6157
}
6258
}
6359

@@ -71,10 +67,10 @@ internal class DayBackgroundDrawer(
7167
canvas: Canvas
7268
) {
7369
val now = now()
74-
val hour = now.hour - config.minHour
70+
val hour = now.hour - viewState.minHour
7571
val hourFraction = now.minute / MINUTES_PER_HOUR
7672

77-
val beforeNow = (hour + hourFraction) * config.hourHeight
73+
val beforeNow = (hour + hourFraction) * viewState.hourHeight
7874
canvas.drawRect(startX, startY, endX, startY + beforeNow, pastPaint)
7975
canvas.drawRect(startX, startY + beforeNow, endX, height, futurePaint)
8076
}

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,18 @@ import android.graphics.Canvas
44
import java.util.Calendar
55

66
internal class DayLabelsDrawer<T>(
7-
private val config: WeekViewConfigWrapper,
7+
private val viewState: ViewState,
88
private val cache: WeekViewCache<T>
99
) : Drawer {
1010

11-
override fun draw(
12-
drawingContext: DrawingContext,
13-
canvas: Canvas
14-
) {
15-
val left = config.timeColumnWidth
11+
override fun draw(canvas: Canvas) {
12+
val left = viewState.timeColumnWidth
1613
val top = 0f
1714
val right = canvas.width.toFloat()
18-
val bottom = config.getTotalHeaderHeight()
15+
val bottom = viewState.getTotalHeaderHeight()
1916

2017
canvas.drawInRect(left, top, right, bottom) {
21-
drawingContext.dateRangeWithStartPixels.forEach { (date, startPixel) ->
18+
viewState.dateRangeWithStartPixels.forEach { (date, startPixel) ->
2219
drawLabel(date, startPixel, this)
2320
}
2421
}
@@ -29,8 +26,8 @@ internal class DayLabelsDrawer<T>(
2926
val textLayout = cache.dateLabelLayouts[key]
3027

3128
canvas.withTranslation(
32-
x = startPixel + config.widthPerDay / 2,
33-
y = config.headerRowPadding.toFloat()
29+
x = startPixel + viewState.widthPerDay / 2,
30+
y = viewState.headerRowPadding.toFloat()
3431
) {
3532
textLayout.draw(this)
3633
}

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

Lines changed: 0 additions & 26 deletions
This file was deleted.

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

Lines changed: 0 additions & 46 deletions
This file was deleted.

0 commit comments

Comments
 (0)