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 acac56d02..fecc85416 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/MonthFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/MonthFragment.kt @@ -166,7 +166,7 @@ class MonthFragment : Fragment(), MonthlyCalendar { } private fun updateDays(days: ArrayList) { - mHolder.month_view.updateDays(days) + mHolder.month_view_wrapper.updateDays(days) /*val displayWeekNumbers = mConfig.displayWeekNumbers val len = days.size 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 57169879a..619c911a7 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/views/MonthView.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/views/MonthView.kt @@ -36,13 +36,14 @@ class MonthView(context: Context, attrs: AttributeSet, defStyle: Int) : View(con weakTextColor = textColor.adjustAlpha(LOW_ALPHA) val normalTextSize = resources.getDimensionPixelSize(R.dimen.normal_text_size).toFloat() + weekDaysLetterHeight = 2 * normalTextSize.toInt() + paint = Paint(Paint.ANTI_ALIAS_FLAG).apply { color = textColor textSize = normalTextSize textAlign = Paint.Align.CENTER } - weekDaysLetterHeight = 2 * normalTextSize.toInt() initWeekDayLetters() setupCurrentDayOfWeekIndex() } @@ -57,11 +58,11 @@ class MonthView(context: Context, attrs: AttributeSet, defStyle: Int) : View(con override fun onDraw(canvas: Canvas) { super.onDraw(canvas) if (dayWidth == 0f) { - dayWidth = (canvas.width / 7).toFloat() + dayWidth = canvas.width / 7f } if (dayHeight == 0f) { - dayHeight = ((canvas.height - weekDaysLetterHeight) / 6).toFloat() + dayHeight = (canvas.height - weekDaysLetterHeight) / 6f } for (i in 0..6) { @@ -75,10 +76,10 @@ class MonthView(context: Context, attrs: AttributeSet, defStyle: Int) : View(con var curId = 0 for (y in 0..5) { - for (x in 1..7) { + for (x in 0..6) { val day = days.getOrNull(curId) if (day != null) { - val xPos = x * dayWidth - dayWidth / 2 + val xPos = x * dayWidth + dayWidth / 2 val yPos = y * dayHeight + weekDaysLetterHeight if (day.isToday) { canvas.drawCircle(xPos, yPos + paint.textSize * 0.7f, paint.textSize * 0.75f, getCirclePaint(day)) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/views/MonthViewWrapper.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/views/MonthViewWrapper.kt new file mode 100644 index 000000000..b412dc780 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/views/MonthViewWrapper.kt @@ -0,0 +1,88 @@ +package com.simplemobiletools.calendar.views + +import android.content.Context +import android.util.AttributeSet +import android.view.LayoutInflater +import android.widget.FrameLayout +import com.simplemobiletools.calendar.R +import com.simplemobiletools.calendar.models.DayMonthly +import com.simplemobiletools.commons.extensions.onGlobalLayout +import kotlinx.android.synthetic.main.month_view.view.* + +// used in the Monthly view fragment, 1 view per screen +class MonthViewWrapper(context: Context, attrs: AttributeSet, defStyle: Int) : FrameLayout(context, attrs, defStyle) { + private var dayWidth = 0f + private var dayHeight = 0f + private var weekDaysLetterHeight = 0 + private var wereViewsAdded = false + private var days = ArrayList() + private var inflater: LayoutInflater + private var monthView: MonthView + + constructor(context: Context, attrs: AttributeSet) : this(context, attrs, 0) + + init { + val normalTextSize = resources.getDimensionPixelSize(R.dimen.normal_text_size).toFloat() + weekDaysLetterHeight = 2 * normalTextSize.toInt() + + inflater = LayoutInflater.from(context) + monthView = inflater.inflate(R.layout.month_view, this).month_view + + onGlobalLayout { + measureSizes() + if (!wereViewsAdded && days.isNotEmpty()) { + addViews() + monthView.updateDays(days) + } + } + } + + fun updateDays(newDays: ArrayList) { + days = newDays + if (dayWidth != 0f) { + addViews() + monthView.updateDays(days) + } + } + + private fun measureSizes() { + if (dayWidth == 0f) { + dayWidth = width / 7f + } + + if (dayHeight == 0f) { + dayHeight = (height - weekDaysLetterHeight) / 6f + } + } + + private fun addViews() { + removeAllViews() + monthView = inflater.inflate(R.layout.month_view, this).month_view + wereViewsAdded = true + var curId = 0 + for (y in 0..5) { + for (x in 0..6) { + val day = days.getOrNull(curId) + if (day != null) { + val xPos = x * dayWidth + val yPos = y * dayHeight + weekDaysLetterHeight + addViewBackground(xPos, yPos, day) + } + curId++ + } + } + } + + private fun addViewBackground(xPos: Float, yPos: Float, day: DayMonthly) { + inflater.inflate(R.layout.month_view_background, this, false).apply { + layoutParams.width = dayWidth.toInt() + layoutParams.height = dayHeight.toInt() + x = xPos + y = yPos + setOnClickListener { + + } + addView(this) + } + } +} diff --git a/app/src/main/res/layout/fragment_month.xml b/app/src/main/res/layout/fragment_month.xml index 6cccd9c7d..401c4883f 100644 --- a/app/src/main/res/layout/fragment_month.xml +++ b/app/src/main/res/layout/fragment_month.xml @@ -8,10 +8,10 @@ - diff --git a/app/src/main/res/layout/month_view.xml b/app/src/main/res/layout/month_view.xml new file mode 100644 index 000000000..0294b179d --- /dev/null +++ b/app/src/main/res/layout/month_view.xml @@ -0,0 +1,6 @@ + + diff --git a/app/src/main/res/layout/month_view_background.xml b/app/src/main/res/layout/month_view_background.xml new file mode 100644 index 000000000..f7eb272d5 --- /dev/null +++ b/app/src/main/res/layout/month_view_background.xml @@ -0,0 +1,7 @@ + +