From 0a8606c373f175825d40531f8057be1c9186103b Mon Sep 17 00:00:00 2001 From: tibbi Date: Fri, 4 May 2018 21:36:28 +0200 Subject: [PATCH] dim past events on the monthly views when appropriate --- .../calendar/activities/MainActivity.kt | 4 +++- .../calendar/helpers/DBHelper.kt | 20 ++++++++++--------- .../helpers/MyWidgetMonthlyProvider.kt | 3 ++- .../calendar/models/DayMonthly.kt | 2 -- .../calendar/models/Event.kt | 5 ++++- .../calendar/models/MonthViewEvent.kt | 2 +- .../calendar/views/MonthView.kt | 20 ++++++++++--------- app/src/main/res/layout/activity_settings.xml | 2 +- 8 files changed, 33 insertions(+), 25 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/MainActivity.kt index fa6ff0292..c1afe10cc 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/MainActivity.kt @@ -63,6 +63,7 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener { private var mStoredDayCode = "" private var mStoredIsSundayFirst = false private var mStoredUse24HourFormat = false + private var mStoredDimPastEvents = true override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -103,7 +104,7 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener { override fun onResume() { super.onResume() if (mStoredTextColor != config.textColor || mStoredBackgroundColor != config.backgroundColor || mStoredPrimaryColor != config.primaryColor - || mStoredDayCode != Formatter.getTodayCode(applicationContext)) { + || mStoredDayCode != Formatter.getTodayCode(applicationContext) || mStoredDimPastEvents != config.dimPastEvents) { updateViewPager() } @@ -199,6 +200,7 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener { mStoredPrimaryColor = primaryColor mStoredBackgroundColor = backgroundColor mStoredUse24HourFormat = use24HourFormat + mStoredDimPastEvents = dimPastEvents } mStoredDayCode = Formatter.getTodayCode(applicationContext) } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt index edbb5dc81..7e96d07db 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt @@ -674,7 +674,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont events.addAll(getRepeatableEventsFor(fromTS, toTS, eventId)) - events.addAll(getAllDayEvents(fromTS, toTS, eventId)) + events.addAll(getAllDayEvents(fromTS, eventId)) val filtered = events.distinct().filterNot { it.ignoreEventOccurrences.contains(Formatter.getDayCodeFromTS(it.startTS).toInt()) } as MutableList callback(filtered) @@ -709,11 +709,11 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont if (event.repeatInterval.isXWeeklyRepetition()) { if (event.startTS.isTsOnProperDay(event)) { if (isOnProperWeek(event, startTimes)) { - events.add(event.copy()) + events.add(event.copy(isPastEvent = event.getIsPastEvent())) } } } else { - events.add(event.copy()) + events.add(event.copy(isPastEvent = event.getIsPastEvent())) } } @@ -721,14 +721,14 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont if (event.repeatInterval.isXWeeklyRepetition()) { if (event.endTS >= toTS && event.startTS.isTsOnProperDay(event)) { if (isOnProperWeek(event, startTimes)) { - events.add(event.copy()) + events.add(event.copy(isPastEvent = event.getIsPastEvent())) } } } else { val dayCode = Formatter.getDayCodeFromTS(fromTS) val endDayCode = Formatter.getDayCodeFromTS(event.endTS) if (dayCode == endDayCode) { - events.add(event.copy()) + events.add(event.copy(isPastEvent = event.getIsPastEvent())) } } } @@ -745,7 +745,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont if (event.startTS.isTsOnProperDay(event)) { if (isOnProperWeek(event, startTimes)) { if (event.endTS >= fromTS) { - events.add(event.copy()) + events.add(event.copy(isPastEvent = event.getIsPastEvent())) } event.repeatLimit++ } @@ -757,7 +757,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont val dayCode = Formatter.getDayCodeFromTS(fromTS) val endDayCode = Formatter.getDayCodeFromTS(event.endTS) if (dayCode == endDayCode) { - events.add(event.copy()) + events.add(event.copy(isPastEvent = event.getIsPastEvent())) } } event.repeatLimit++ @@ -767,7 +767,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont return events } - private fun getAllDayEvents(fromTS: Int, toTS: Int, eventId: Int = -1): List { + private fun getAllDayEvents(fromTS: Int, eventId: Int = -1): List { val events = ArrayList() var selection = "($COL_FLAGS & $FLAG_ALL_DAY) != 0" if (eventId != -1) @@ -914,9 +914,11 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont repeatRule = REPEAT_MONTH_SAME_DAY } + val isPastEvent = endTS < System.currentTimeMillis() / 1000 + val event = Event(id, startTS, endTS, title, description, reminder1Minutes, reminder2Minutes, reminder3Minutes, repeatInterval, importId, flags, repeatLimit, repeatRule, eventType, ignoreEventOccurrences, offset, isDstIncluded, - 0, lastUpdated, source, color, location) + 0, lastUpdated, source, color, location, isPastEvent) events.add(event) } while (cursor.moveToNext()) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/MyWidgetMonthlyProvider.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/MyWidgetMonthlyProvider.kt index cf707ea24..6acb39b8d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/MyWidgetMonthlyProvider.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/MyWidgetMonthlyProvider.kt @@ -84,6 +84,7 @@ class MyWidgetMonthlyProvider : AppWidgetProvider() { private fun updateDays(context: Context, views: RemoteViews, days: List) { val displayWeekNumbers = context.config.showWeekNumbers val textColor = context.config.widgetTextColor + val dimPastEvents = context.config.dimPastEvents val smallerFontSize = context.config.getFontSize() - 3f val res = context.resources val len = days.size @@ -117,7 +118,7 @@ class MyWidgetMonthlyProvider : AppWidgetProvider() { var backgroundColor = it.color var eventTextColor = backgroundColor.getContrastColor() - if (!day.isThisMonth) { + if (!day.isThisMonth || (dimPastEvents && it.getIsPastEvent())) { eventTextColor = eventTextColor.adjustAlpha(0.25f) backgroundColor = backgroundColor.adjustAlpha(0.25f) } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/models/DayMonthly.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/models/DayMonthly.kt index 55e9d57a4..8633d330f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/models/DayMonthly.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/models/DayMonthly.kt @@ -2,6 +2,4 @@ package com.simplemobiletools.calendar.models data class DayMonthly(val value: Int, val isThisMonth: Boolean, val isToday: Boolean, val code: String, val weekOfYear: Int, var dayEvents: ArrayList, var indexOnMonthView: Int) { - - fun hasEvent() = dayEvents.isNotEmpty() } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/models/Event.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/models/Event.kt index 8f9a2e3ba..ee30b8e80 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/models/Event.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/models/Event.kt @@ -12,7 +12,8 @@ data class Event(var id: Int = 0, var startTS: Int = 0, var endTS: Int = 0, var var importId: String = "", var flags: Int = 0, var repeatLimit: Int = 0, var repeatRule: Int = 0, var eventType: Int = DBHelper.REGULAR_EVENT_TYPE_ID, var ignoreEventOccurrences: ArrayList = ArrayList(), var offset: String = "", var isDstIncluded: Boolean = false, var parentId: Int = 0, var lastUpdated: Long = 0L, - var source: String = SOURCE_SIMPLE_CALENDAR, var color: Int = 0, var location: String = "") : Serializable { + var source: String = SOURCE_SIMPLE_CALENDAR, var color: Int = 0, var location: String = "", var isPastEvent: Boolean = false) + : Serializable { companion object { private val serialVersionUID = -32456795132345616L @@ -111,4 +112,6 @@ data class Event(var id: Int = 0, var startTS: Int = 0, var endTS: Int = 0, var } fun getCalDAVCalendarId() = if (source.startsWith(CALDAV)) (source.split("-").lastOrNull() ?: "0").toString().toInt() else 0 + + fun getIsPastEvent() = endTS < System.currentTimeMillis() / 1000 } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/models/MonthViewEvent.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/models/MonthViewEvent.kt index e68b819b3..0d9d8c8ab 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/models/MonthViewEvent.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/models/MonthViewEvent.kt @@ -1,4 +1,4 @@ package com.simplemobiletools.calendar.models data class MonthViewEvent(val id: Int, val title: String, val startTS: Int, val color: Int, val startDayIndex: Int, val daysCnt: Int, val originalStartDayIndex: Int, - val isAllDay: Boolean) + val isAllDay: Boolean, val isPastEvent: Boolean) 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 9b0ef9579..1de5995ac 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/views/MonthView.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/views/MonthView.kt @@ -43,6 +43,7 @@ class MonthView(context: Context, attrs: AttributeSet, defStyle: Int) : View(con private var maxEventsPerDay = 0 private var horizontalOffset = 0 private var showWeekNumbers = false + private var dimPastEvents = true private var allEvents = ArrayList() private var bgRectF = RectF() private var dayLetters = ArrayList() @@ -55,6 +56,7 @@ class MonthView(context: Context, attrs: AttributeSet, defStyle: Int) : View(con primaryColor = context.getAdjustedPrimaryColor() textColor = context.config.textColor showWeekNumbers = context.config.showWeekNumbers + dimPastEvents = context.config.dimPastEvents smallPadding = resources.displayMetrics.density.toInt() val normalTextSize = resources.getDimensionPixelSize(R.dimen.normal_text_size) @@ -103,7 +105,7 @@ class MonthView(context: Context, attrs: AttributeSet, defStyle: Int) : View(con val daysCnt = getEventLastingDaysCount(event) if (lastEvent == null || lastEvent.startDayIndex + daysCnt <= day.indexOnMonthView) { val monthViewEvent = MonthViewEvent(event.id, event.title, event.startTS, event.color, day.indexOnMonthView, - daysCnt, day.indexOnMonthView, event.getIsAllDay()) + daysCnt, day.indexOnMonthView, event.getIsAllDay(), event.isPastEvent) allEvents.add(monthViewEvent) } } @@ -236,16 +238,16 @@ class MonthView(context: Context, attrs: AttributeSet, defStyle: Int) : View(con bgRectF.set(bgLeft, bgTop, bgRight, bgBottom) canvas.drawRoundRect(bgRectF, BG_CORNER_RADIUS, BG_CORNER_RADIUS, getEventBackgroundColor(event, startDayIndex, endDayIndex)) - drawEventTitle(event.title, canvas, xPos, yPos + verticalOffset, bgRight - bgLeft - smallPadding, event.color, startDayIndex, endDayIndex) + drawEventTitle(event, canvas, xPos, yPos + verticalOffset, bgRight - bgLeft - smallPadding, startDayIndex, endDayIndex) for (i in 0 until Math.min(event.daysCnt, 7 - event.startDayIndex % 7)) { dayVerticalOffsets.put(event.startDayIndex + i, verticalOffset + eventTitleHeight + smallPadding * 2) } } - private fun drawEventTitle(title: String, canvas: Canvas, x: Float, y: Float, availableWidth: Float, eventColor: Int, startDay: DayMonthly, endDay: DayMonthly) { - val ellipsized = TextUtils.ellipsize(title, eventTitlePaint, availableWidth - smallPadding, TextUtils.TruncateAt.END) - canvas.drawText(title, 0, ellipsized.length, x + smallPadding * 2, y, getEventTitlePaint(eventColor, startDay, endDay)) + private fun drawEventTitle(event: MonthViewEvent, canvas: Canvas, x: Float, y: Float, availableWidth: Float, startDay: DayMonthly, endDay: DayMonthly) { + val ellipsized = TextUtils.ellipsize(event.title, eventTitlePaint, availableWidth - smallPadding, TextUtils.TruncateAt.END) + canvas.drawText(event.title, 0, ellipsized.length, x + smallPadding * 2, y, getEventTitlePaint(event, startDay, endDay)) } private fun getTextPaint(startDay: DayMonthly): Paint { @@ -269,16 +271,16 @@ class MonthView(context: Context, attrs: AttributeSet, defStyle: Int) : View(con private fun getEventBackgroundColor(event: MonthViewEvent, startDay: DayMonthly, endDay: DayMonthly): Paint { var paintColor = event.color - if (!startDay.isThisMonth && !endDay.isThisMonth) { + if ((!startDay.isThisMonth && !endDay.isThisMonth) || (dimPastEvents && event.isPastEvent)) { paintColor = paintColor.adjustAlpha(LOW_ALPHA) } return getColoredPaint(paintColor) } - private fun getEventTitlePaint(color: Int, startDay: DayMonthly, endDay: DayMonthly): Paint { - var paintColor = color.getContrastColor() - if (!startDay.isThisMonth && !endDay.isThisMonth) { + private fun getEventTitlePaint(event: MonthViewEvent, startDay: DayMonthly, endDay: DayMonthly): Paint { + var paintColor = event.color.getContrastColor() + if ((!startDay.isThisMonth && !endDay.isThisMonth) || (dimPastEvents && event.isPastEvent)) { paintColor = paintColor.adjustAlpha(LOW_ALPHA) } diff --git a/app/src/main/res/layout/activity_settings.xml b/app/src/main/res/layout/activity_settings.xml index 58683dcaa..b04fb405e 100644 --- a/app/src/main/res/layout/activity_settings.xml +++ b/app/src/main/res/layout/activity_settings.xml @@ -681,7 +681,7 @@ android:layout_marginLeft="@dimen/bigger_margin" android:layout_marginStart="@dimen/bigger_margin" android:layout_marginTop="@dimen/activity_margin" - android:text="@string/event_reminders" + android:text="@string/events" android:textAllCaps="true" android:textSize="@dimen/smaller_text_size"/>