From d12949fb2c74120d7ad59c3396ef3972d2850c9e Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 25 Mar 2018 23:04:35 +0200 Subject: [PATCH] small cleanup at MonthView --- .../calendar/views/MonthView.kt | 40 ++++++++++--------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/views/MonthView.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/views/MonthView.kt index ac0137271..3b7f527ff 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/views/MonthView.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/views/MonthView.kt @@ -84,25 +84,11 @@ class MonthView(context: Context, attrs: AttributeSet, defStyle: Int) : View(con override fun onDraw(canvas: Canvas) { super.onDraw(canvas) dayVerticalOffsets.clear() - if (dayWidth == 0f) { - dayWidth = canvas.width / 7f + if (dayWidth == 0f || dayHeight == 0f) { + measureDaySize(canvas) } - if (dayHeight == 0f) { - dayHeight = (canvas.height - weekDaysLetterHeight) / ROW_COUNT.toFloat() - availableHeightForEvents = dayHeight.toInt() - weekDaysLetterHeight - maxEventsPerDay = availableHeightForEvents / eventTitleHeight - } - - // week day letters - for (i in 0..6) { - val xPos = (i + 1) * dayWidth - dayWidth / 2 - var weekDayLetterPaint = paint - if (i == currDayOfWeek) { - weekDayLetterPaint = getColoredPaint(primaryColor) - } - canvas.drawText(dayLetters[i], xPos, weekDaysLetterHeight / 2f, weekDayLetterPaint) - } + addWeekDayLetters(canvas) var curId = 0 for (y in 0 until ROW_COUNT) { @@ -133,7 +119,7 @@ class MonthView(context: Context, attrs: AttributeSet, defStyle: Int) : View(con val maxTS = Math.min(endDateTime.seconds(), Int.MAX_VALUE) val daysCnt = Days.daysBetween(Formatter.getDateTimeFromTS(minTS).toLocalDate(), Formatter.getDateTimeFromTS(maxTS).toLocalDate()).days + 1 - // background rectangle + // event background rectangle val backgroundY = yPos + verticalOffset val bgLeft = xPos + smallPadding val bgTop = backgroundY + smallPadding - eventTitleHeight @@ -152,6 +138,24 @@ class MonthView(context: Context, attrs: AttributeSet, defStyle: Int) : View(con } } + private fun addWeekDayLetters(canvas: Canvas) { + for (i in 0..6) { + val xPos = (i + 1) * dayWidth - dayWidth / 2 + var weekDayLetterPaint = paint + if (i == currDayOfWeek) { + weekDayLetterPaint = getColoredPaint(primaryColor) + } + canvas.drawText(dayLetters[i], xPos, weekDaysLetterHeight / 2f, weekDayLetterPaint) + } + } + + private fun measureDaySize(canvas: Canvas) { + dayWidth = canvas.width / 7f + dayHeight = (canvas.height - weekDaysLetterHeight) / ROW_COUNT.toFloat() + availableHeightForEvents = dayHeight.toInt() - weekDaysLetterHeight + maxEventsPerDay = availableHeightForEvents / eventTitleHeight + } + private fun drawEventTitle(title: String, canvas: Canvas, x: Float, y: Float, eventColor: Int, daysCnt: Int) { val ellipsized = TextUtils.ellipsize(title, eventTitlePaint, dayWidth * daysCnt - smallPadding * 4, TextUtils.TruncateAt.END) canvas.drawText(title, 0, ellipsized.length, x + smallPadding * 2, y, getEventTitlePaint(eventColor))