properly update the days at month view

This commit is contained in:
tibbi 2018-03-24 12:25:33 +01:00
parent ba07fbcfd3
commit e9ab1dbde8
7 changed files with 51 additions and 18 deletions

View File

@ -194,7 +194,7 @@ class WidgetMonthlyConfigureActivity : SimpleActivity(), MonthlyCalendar {
}
}
override fun updateMonthlyCalendar(context: Context, month: String, days: List<DayMonthly>, checkedEvents: Boolean) {
override fun updateMonthlyCalendar(context: Context, month: String, days: ArrayList<DayMonthly>, checkedEvents: Boolean) {
runOnUiThread {
mDays = days
top_value.text = month

View File

@ -83,7 +83,7 @@ class MonthFragment : Fragment(), MonthlyCalendar {
mCalendar?.updateMonthlyCalendar(Formatter.getDateTimeFromCode(mDayCode))
}
override fun updateMonthlyCalendar(context: Context, month: String, days: List<DayMonthly>, checkedEvents: Boolean) {
override fun updateMonthlyCalendar(context: Context, month: String, days: ArrayList<DayMonthly>, checkedEvents: Boolean) {
val newHash = month.hashCode() + days.hashCode().toLong()
if ((mLastHash != 0L && !checkedEvents) || mLastHash == newHash) {
return
@ -166,7 +166,9 @@ class MonthFragment : Fragment(), MonthlyCalendar {
}
}
private fun updateDays(days: List<DayMonthly>) {
private fun updateDays(days: ArrayList<DayMonthly>) {
mHolder.month_view.updateDays(days)
/*val displayWeekNumbers = mConfig.displayWeekNumbers
val len = days.size

View File

@ -47,7 +47,7 @@ class MonthlyCalendarImpl(val mCallback: MonthlyCalendar, val mContext: Context)
var isThisMonth = false
var isToday: Boolean
var value = prevMonthDays - firstDayIndex + 1
var curDay: DateTime = mTargetDate
var curDay = mTargetDate
for (i in 0 until DAYS_CNT) {
when {
@ -67,7 +67,7 @@ class MonthlyCalendarImpl(val mCallback: MonthlyCalendar, val mContext: Context)
}
}
isToday = isThisMonth && isToday(mTargetDate, value)
isToday = isToday(curDay, value)
val newDay = curDay.withDayOfMonth(value)
val dayCode = Formatter.getDayCodeFromDateTime(newDay)
@ -95,13 +95,13 @@ class MonthlyCalendarImpl(val mCallback: MonthlyCalendar, val mContext: Context)
var currDay = startDateTime
var dayCode = Formatter.getDayCodeFromDateTime(currDay)
var currDayEvents = (dayEvents[dayCode] ?: ArrayList()).apply { add(it) }
dayEvents.put(dayCode, currDayEvents)
dayEvents[dayCode] = currDayEvents
while (Formatter.getDayCodeFromDateTime(currDay) != endCode) {
currDay = currDay.plusDays(1)
dayCode = Formatter.getDayCodeFromDateTime(currDay)
currDayEvents = (dayEvents[dayCode] ?: ArrayList()).apply { add(it) }
dayEvents.put(dayCode, currDayEvents)
dayEvents[dayCode] = currDayEvents
}
}
@ -113,10 +113,8 @@ class MonthlyCalendarImpl(val mCallback: MonthlyCalendar, val mContext: Context)
}
private fun isToday(targetDate: DateTime, curDayInMonth: Int): Boolean {
return if (curDayInMonth > targetDate.dayOfMonth().maximumValue)
false
else
targetDate.withDayOfMonth(curDayInMonth).toString(Formatter.DAYCODE_PATTERN) == mToday
val targetMonthDays = targetDate.dayOfMonth().maximumValue
return targetDate.withDayOfMonth(Math.min(curDayInMonth, targetMonthDays)).toString(Formatter.DAYCODE_PATTERN) == mToday
}
private val monthName: String

View File

@ -149,7 +149,7 @@ class MyWidgetMonthlyProvider : AppWidgetProvider() {
}
private val monthlyCalendar = object : MonthlyCalendar {
override fun updateMonthlyCalendar(context: Context, month: String, days: List<DayMonthly>, checkedEvents: Boolean) {
override fun updateMonthlyCalendar(context: Context, month: String, days: ArrayList<DayMonthly>, checkedEvents: Boolean) {
val largerFontSize = context.config.getFontSize() + 3f
val textColor = context.config.widgetTextColor
val resources = context.resources

View File

@ -4,5 +4,5 @@ import android.content.Context
import com.simplemobiletools.calendar.models.DayMonthly
interface MonthlyCalendar {
fun updateMonthlyCalendar(context: Context, month: String, days: List<DayMonthly>, checkedEvents: Boolean)
fun updateMonthlyCalendar(context: Context, month: String, days: ArrayList<DayMonthly>, checkedEvents: Boolean)
}

View File

@ -7,6 +7,10 @@ import android.util.AttributeSet
import android.view.View
import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.extensions.config
import com.simplemobiletools.calendar.helpers.LOW_ALPHA
import com.simplemobiletools.calendar.models.DayMonthly
import com.simplemobiletools.commons.extensions.adjustAlpha
import com.simplemobiletools.commons.extensions.getContrastColor
// used in the Monthly view fragment, 1 view per screen
class MonthView(context: Context, attrs: AttributeSet, defStyle: Int) : View(context, attrs, defStyle) {
@ -14,12 +18,16 @@ class MonthView(context: Context, attrs: AttributeSet, defStyle: Int) : View(con
private var dayWidth = 0f
private var dayHeight = 0f
private var textColor = 0
private var days = 42
private var weakTextColor = 0
private var todayTextColor = 0
private var days = ArrayList<DayMonthly>()
constructor(context: Context, attrs: AttributeSet) : this(context, attrs, 0)
init {
textColor = context.config.textColor
weakTextColor = textColor.adjustAlpha(LOW_ALPHA)
todayTextColor = context.config.primaryColor.getContrastColor()
paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
color = textColor
@ -28,6 +36,11 @@ class MonthView(context: Context, attrs: AttributeSet, defStyle: Int) : View(con
}
}
fun updateDays(newDays: ArrayList<DayMonthly>) {
days = newDays
invalidate()
}
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
if (dayWidth == 0f) {
@ -38,14 +51,34 @@ class MonthView(context: Context, attrs: AttributeSet, defStyle: Int) : View(con
dayHeight = (canvas.height / 6).toFloat()
}
var curId = 1
var curId = 0
for (y in 0..5) {
for (x in 1..7) {
if (curId in 1..days) {
canvas.drawText(curId.toString(), x * dayWidth - dayWidth / 2, y * dayHeight + paint.textSize, paint)
val day = days.getOrNull(curId)
if (day != null) {
canvas.drawText(day.value.toString(), x * dayWidth - dayWidth / 2, y * dayHeight + paint.textSize, getPaint(day))
}
curId++
}
}
}
private fun getPaint(day: DayMonthly): Paint {
var paintColor = textColor
if (day.isToday) {
paintColor = paintColor.getContrastColor()
}
if (!day.isThisMonth) {
paintColor = paintColor.adjustAlpha(LOW_ALPHA)
}
return getColoredPaint(paintColor)
}
private fun getColoredPaint(color: Int): Paint {
val curPaint = Paint(paint)
curPaint.color = color
return curPaint
}
}

View File

@ -18,7 +18,7 @@
tools:ignore="UnknownIdInLayout"/>
<com.simplemobiletools.calendar.views.MonthView
android:id="@+id/month"
android:id="@+id/month_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/first_row"