small cleanup at MonthView

This commit is contained in:
tibbi 2018-03-25 23:04:35 +02:00
parent 0ad79384aa
commit d12949fb2c
1 changed files with 22 additions and 18 deletions

View File

@ -84,25 +84,11 @@ class MonthView(context: Context, attrs: AttributeSet, defStyle: Int) : View(con
override fun onDraw(canvas: Canvas) { override fun onDraw(canvas: Canvas) {
super.onDraw(canvas) super.onDraw(canvas)
dayVerticalOffsets.clear() dayVerticalOffsets.clear()
if (dayWidth == 0f) { if (dayWidth == 0f || dayHeight == 0f) {
dayWidth = canvas.width / 7f measureDaySize(canvas)
} }
if (dayHeight == 0f) { addWeekDayLetters(canvas)
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)
}
var curId = 0 var curId = 0
for (y in 0 until ROW_COUNT) { 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 maxTS = Math.min(endDateTime.seconds(), Int.MAX_VALUE)
val daysCnt = Days.daysBetween(Formatter.getDateTimeFromTS(minTS).toLocalDate(), Formatter.getDateTimeFromTS(maxTS).toLocalDate()).days + 1 val daysCnt = Days.daysBetween(Formatter.getDateTimeFromTS(minTS).toLocalDate(), Formatter.getDateTimeFromTS(maxTS).toLocalDate()).days + 1
// background rectangle // event background rectangle
val backgroundY = yPos + verticalOffset val backgroundY = yPos + verticalOffset
val bgLeft = xPos + smallPadding val bgLeft = xPos + smallPadding
val bgTop = backgroundY + smallPadding - eventTitleHeight 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) { 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) 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)) canvas.drawText(title, 0, ellipsized.length, x + smallPadding * 2, y, getEventTitlePaint(eventColor))