From f794be6116d2885d27480869f2a7be9c303c4496 Mon Sep 17 00:00:00 2001 From: tibbi Date: Tue, 27 Mar 2018 13:36:01 +0200 Subject: [PATCH] add week numbers to monthly view when appropriate --- .../calendar/fragments/MonthFragment.kt | 18 +++++++++-- .../calendar/views/MonthView.kt | 32 ++++++++++++++----- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/MonthFragment.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/MonthFragment.kt index 6497b26e8..52983243c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/MonthFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/MonthFragment.kt @@ -31,6 +31,7 @@ import org.joda.time.DateTime class MonthFragment : Fragment(), MonthlyCalendar { private var mTextColor = 0 private var mSundayFirst = false + private var mShowWeekNumbers = false private var mDayCode = "" private var mPackageName = "" private var mDayLabelHeight = 0 @@ -50,7 +51,7 @@ class MonthFragment : Fragment(), MonthlyCalendar { mHolder = view.month_calendar_holder mDayCode = arguments!!.getString(DAY_CODE) mConfig = context!!.config - mSundayFirst = mConfig.isSundayFirst + storeStateVariables() setupButtons() setupLabels() @@ -61,24 +62,35 @@ class MonthFragment : Fragment(), MonthlyCalendar { override fun onPause() { super.onPause() - mSundayFirst = context!!.config.isSundayFirst + storeStateVariables() } override fun onResume() { super.onResume() if (mConfig.isSundayFirst != mSundayFirst) { - mSundayFirst = mConfig.isSundayFirst setupLabels() } + if (mConfig.showWeekNumbers != mShowWeekNumbers) { + mLastHash = -1L + } + mCalendar!!.apply { mTargetDate = Formatter.getDateTimeFromCode(mDayCode) getDays(false) // prefill the screen asap, even if without events } + storeStateVariables() updateCalendar() } + private fun storeStateVariables() { + mConfig.apply { + mSundayFirst = isSundayFirst + mShowWeekNumbers = showWeekNumbers + } + } + fun updateCalendar() { mCalendar?.updateMonthlyCalendar(Formatter.getDateTimeFromCode(mDayCode)) } 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 ee2ea01ec..d3d3a2af3 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/views/MonthView.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/views/MonthView.kt @@ -40,6 +40,7 @@ class MonthView(context: Context, attrs: AttributeSet, defStyle: Int) : View(con private var currDayOfWeek = 0 private var smallPadding = 0 private var maxEventsPerDay = 0 + private var horizontalOffset = 0 private var showWeekNumbers = false private var allEvents = ArrayList() private var bgRectF = RectF() @@ -79,9 +80,10 @@ class MonthView(context: Context, attrs: AttributeSet, defStyle: Int) : View(con fun updateDays(newDays: ArrayList) { days = newDays + showWeekNumbers = context.config.showWeekNumbers + horizontalOffset = if (showWeekNumbers) eventTitleHeight * 2 else 0 initWeekDayLetters() setupCurrentDayOfWeekIndex() - showWeekNumbers = context.config.showWeekNumbers groupAllEvents() invalidate() } @@ -110,11 +112,12 @@ class MonthView(context: Context, attrs: AttributeSet, defStyle: Int) : View(con super.onDraw(canvas) dayVerticalOffsets.clear() dayEventsCount.clear() - if (dayWidth == 0f || dayHeight == 0f) { - measureDaySize(canvas) - } + measureDaySize(canvas) addWeekDayLetters(canvas) + if (showWeekNumbers) { + addWeekNumbers(canvas) + } var curId = 0 for (y in 0 until ROW_COUNT) { @@ -123,7 +126,7 @@ class MonthView(context: Context, attrs: AttributeSet, defStyle: Int) : View(con if (day != null) { dayVerticalOffsets.put(day.indexOnMonthView, dayVerticalOffsets[day.indexOnMonthView] + weekDaysLetterHeight) val verticalOffset = dayVerticalOffsets[day.indexOnMonthView] - val xPos = x * dayWidth + val xPos = x * dayWidth + horizontalOffset val yPos = y * dayHeight + verticalOffset val xPosCenter = xPos + dayWidth / 2 if (day.isToday) { @@ -142,7 +145,7 @@ 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 + val xPos = horizontalOffset + (i + 1) * dayWidth - dayWidth / 2 var weekDayLetterPaint = paint if (i == currDayOfWeek) { weekDayLetterPaint = getColoredPaint(primaryColor) @@ -151,8 +154,21 @@ class MonthView(context: Context, attrs: AttributeSet, defStyle: Int) : View(con } } + private fun addWeekNumbers(canvas: Canvas) { + val weekNumberPaint = Paint(paint) + weekNumberPaint.textAlign = Paint.Align.RIGHT + + for (i in 0 until ROW_COUNT) { + // fourth day of the week matters + val id = "${days[i * 7 + 3].weekOfYear}:" + + val yPos = i * dayHeight + weekDaysLetterHeight + canvas.drawText(id, horizontalOffset.toFloat(), yPos + paint.textSize, weekNumberPaint) + } + } + private fun measureDaySize(canvas: Canvas) { - dayWidth = canvas.width / 7f + dayWidth = (canvas.width - horizontalOffset) / 7f dayHeight = (canvas.height - weekDaysLetterHeight) / ROW_COUNT.toFloat() val availableHeightForEvents = dayHeight.toInt() - weekDaysLetterHeight maxEventsPerDay = availableHeightForEvents / eventTitleHeight @@ -166,7 +182,7 @@ class MonthView(context: Context, attrs: AttributeSet, defStyle: Int) : View(con private fun drawEvent(event: MonthViewEvent, canvas: Canvas) { val verticalOffset = dayVerticalOffsets[event.startDayIndex] - val xPos = event.startDayIndex % 7 * dayWidth + val xPos = event.startDayIndex % 7 * dayWidth + horizontalOffset val yPos = (event.startDayIndex / 7) * dayHeight val xPosCenter = xPos + dayWidth / 2