add week numbers to monthly view when appropriate
This commit is contained in:
parent
e48156c24b
commit
f794be6116
|
@ -31,6 +31,7 @@ import org.joda.time.DateTime
|
||||||
class MonthFragment : Fragment(), MonthlyCalendar {
|
class MonthFragment : Fragment(), MonthlyCalendar {
|
||||||
private var mTextColor = 0
|
private var mTextColor = 0
|
||||||
private var mSundayFirst = false
|
private var mSundayFirst = false
|
||||||
|
private var mShowWeekNumbers = false
|
||||||
private var mDayCode = ""
|
private var mDayCode = ""
|
||||||
private var mPackageName = ""
|
private var mPackageName = ""
|
||||||
private var mDayLabelHeight = 0
|
private var mDayLabelHeight = 0
|
||||||
|
@ -50,7 +51,7 @@ class MonthFragment : Fragment(), MonthlyCalendar {
|
||||||
mHolder = view.month_calendar_holder
|
mHolder = view.month_calendar_holder
|
||||||
mDayCode = arguments!!.getString(DAY_CODE)
|
mDayCode = arguments!!.getString(DAY_CODE)
|
||||||
mConfig = context!!.config
|
mConfig = context!!.config
|
||||||
mSundayFirst = mConfig.isSundayFirst
|
storeStateVariables()
|
||||||
|
|
||||||
setupButtons()
|
setupButtons()
|
||||||
setupLabels()
|
setupLabels()
|
||||||
|
@ -61,24 +62,35 @@ class MonthFragment : Fragment(), MonthlyCalendar {
|
||||||
|
|
||||||
override fun onPause() {
|
override fun onPause() {
|
||||||
super.onPause()
|
super.onPause()
|
||||||
mSundayFirst = context!!.config.isSundayFirst
|
storeStateVariables()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onResume() {
|
override fun onResume() {
|
||||||
super.onResume()
|
super.onResume()
|
||||||
if (mConfig.isSundayFirst != mSundayFirst) {
|
if (mConfig.isSundayFirst != mSundayFirst) {
|
||||||
mSundayFirst = mConfig.isSundayFirst
|
|
||||||
setupLabels()
|
setupLabels()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (mConfig.showWeekNumbers != mShowWeekNumbers) {
|
||||||
|
mLastHash = -1L
|
||||||
|
}
|
||||||
|
|
||||||
mCalendar!!.apply {
|
mCalendar!!.apply {
|
||||||
mTargetDate = Formatter.getDateTimeFromCode(mDayCode)
|
mTargetDate = Formatter.getDateTimeFromCode(mDayCode)
|
||||||
getDays(false) // prefill the screen asap, even if without events
|
getDays(false) // prefill the screen asap, even if without events
|
||||||
}
|
}
|
||||||
|
|
||||||
|
storeStateVariables()
|
||||||
updateCalendar()
|
updateCalendar()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun storeStateVariables() {
|
||||||
|
mConfig.apply {
|
||||||
|
mSundayFirst = isSundayFirst
|
||||||
|
mShowWeekNumbers = showWeekNumbers
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun updateCalendar() {
|
fun updateCalendar() {
|
||||||
mCalendar?.updateMonthlyCalendar(Formatter.getDateTimeFromCode(mDayCode))
|
mCalendar?.updateMonthlyCalendar(Formatter.getDateTimeFromCode(mDayCode))
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,6 +40,7 @@ class MonthView(context: Context, attrs: AttributeSet, defStyle: Int) : View(con
|
||||||
private var currDayOfWeek = 0
|
private var currDayOfWeek = 0
|
||||||
private var smallPadding = 0
|
private var smallPadding = 0
|
||||||
private var maxEventsPerDay = 0
|
private var maxEventsPerDay = 0
|
||||||
|
private var horizontalOffset = 0
|
||||||
private var showWeekNumbers = false
|
private var showWeekNumbers = false
|
||||||
private var allEvents = ArrayList<MonthViewEvent>()
|
private var allEvents = ArrayList<MonthViewEvent>()
|
||||||
private var bgRectF = RectF()
|
private var bgRectF = RectF()
|
||||||
|
@ -79,9 +80,10 @@ class MonthView(context: Context, attrs: AttributeSet, defStyle: Int) : View(con
|
||||||
|
|
||||||
fun updateDays(newDays: ArrayList<DayMonthly>) {
|
fun updateDays(newDays: ArrayList<DayMonthly>) {
|
||||||
days = newDays
|
days = newDays
|
||||||
|
showWeekNumbers = context.config.showWeekNumbers
|
||||||
|
horizontalOffset = if (showWeekNumbers) eventTitleHeight * 2 else 0
|
||||||
initWeekDayLetters()
|
initWeekDayLetters()
|
||||||
setupCurrentDayOfWeekIndex()
|
setupCurrentDayOfWeekIndex()
|
||||||
showWeekNumbers = context.config.showWeekNumbers
|
|
||||||
groupAllEvents()
|
groupAllEvents()
|
||||||
invalidate()
|
invalidate()
|
||||||
}
|
}
|
||||||
|
@ -110,11 +112,12 @@ class MonthView(context: Context, attrs: AttributeSet, defStyle: Int) : View(con
|
||||||
super.onDraw(canvas)
|
super.onDraw(canvas)
|
||||||
dayVerticalOffsets.clear()
|
dayVerticalOffsets.clear()
|
||||||
dayEventsCount.clear()
|
dayEventsCount.clear()
|
||||||
if (dayWidth == 0f || dayHeight == 0f) {
|
|
||||||
measureDaySize(canvas)
|
measureDaySize(canvas)
|
||||||
}
|
|
||||||
|
|
||||||
addWeekDayLetters(canvas)
|
addWeekDayLetters(canvas)
|
||||||
|
if (showWeekNumbers) {
|
||||||
|
addWeekNumbers(canvas)
|
||||||
|
}
|
||||||
|
|
||||||
var curId = 0
|
var curId = 0
|
||||||
for (y in 0 until ROW_COUNT) {
|
for (y in 0 until ROW_COUNT) {
|
||||||
|
@ -123,7 +126,7 @@ class MonthView(context: Context, attrs: AttributeSet, defStyle: Int) : View(con
|
||||||
if (day != null) {
|
if (day != null) {
|
||||||
dayVerticalOffsets.put(day.indexOnMonthView, dayVerticalOffsets[day.indexOnMonthView] + weekDaysLetterHeight)
|
dayVerticalOffsets.put(day.indexOnMonthView, dayVerticalOffsets[day.indexOnMonthView] + weekDaysLetterHeight)
|
||||||
val verticalOffset = dayVerticalOffsets[day.indexOnMonthView]
|
val verticalOffset = dayVerticalOffsets[day.indexOnMonthView]
|
||||||
val xPos = x * dayWidth
|
val xPos = x * dayWidth + horizontalOffset
|
||||||
val yPos = y * dayHeight + verticalOffset
|
val yPos = y * dayHeight + verticalOffset
|
||||||
val xPosCenter = xPos + dayWidth / 2
|
val xPosCenter = xPos + dayWidth / 2
|
||||||
if (day.isToday) {
|
if (day.isToday) {
|
||||||
|
@ -142,7 +145,7 @@ class MonthView(context: Context, attrs: AttributeSet, defStyle: Int) : View(con
|
||||||
|
|
||||||
private fun addWeekDayLetters(canvas: Canvas) {
|
private fun addWeekDayLetters(canvas: Canvas) {
|
||||||
for (i in 0..6) {
|
for (i in 0..6) {
|
||||||
val xPos = (i + 1) * dayWidth - dayWidth / 2
|
val xPos = horizontalOffset + (i + 1) * dayWidth - dayWidth / 2
|
||||||
var weekDayLetterPaint = paint
|
var weekDayLetterPaint = paint
|
||||||
if (i == currDayOfWeek) {
|
if (i == currDayOfWeek) {
|
||||||
weekDayLetterPaint = getColoredPaint(primaryColor)
|
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) {
|
private fun measureDaySize(canvas: Canvas) {
|
||||||
dayWidth = canvas.width / 7f
|
dayWidth = (canvas.width - horizontalOffset) / 7f
|
||||||
dayHeight = (canvas.height - weekDaysLetterHeight) / ROW_COUNT.toFloat()
|
dayHeight = (canvas.height - weekDaysLetterHeight) / ROW_COUNT.toFloat()
|
||||||
val availableHeightForEvents = dayHeight.toInt() - weekDaysLetterHeight
|
val availableHeightForEvents = dayHeight.toInt() - weekDaysLetterHeight
|
||||||
maxEventsPerDay = availableHeightForEvents / eventTitleHeight
|
maxEventsPerDay = availableHeightForEvents / eventTitleHeight
|
||||||
|
@ -166,7 +182,7 @@ class MonthView(context: Context, attrs: AttributeSet, defStyle: Int) : View(con
|
||||||
|
|
||||||
private fun drawEvent(event: MonthViewEvent, canvas: Canvas) {
|
private fun drawEvent(event: MonthViewEvent, canvas: Canvas) {
|
||||||
val verticalOffset = dayVerticalOffsets[event.startDayIndex]
|
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 yPos = (event.startDayIndex / 7) * dayHeight
|
||||||
val xPosCenter = xPos + dayWidth / 2
|
val xPosCenter = xPos + dayWidth / 2
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue