From 39437ee90606f98d89519dfc2921abf80d89f8a8 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 10 Sep 2017 18:07:26 +0200 Subject: [PATCH] use different day colors depending on the event types at yearly view --- .../calendar/fragments/YearFragment.kt | 3 ++- .../calendar/helpers/YearlyCalendarImpl.kt | 25 +++++++++++------- .../calendar/interfaces/YearlyCalendar.kt | 3 ++- .../calendar/models/DayYearly.kt | 5 ++++ .../calendar/views/SmallMonthView.kt | 26 ++++++++++++------- 5 files changed, 41 insertions(+), 21 deletions(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/calendar/models/DayYearly.kt diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/YearFragment.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/YearFragment.kt index 1fdd892c8..720b245f6 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/YearFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/YearFragment.kt @@ -14,6 +14,7 @@ import com.simplemobiletools.calendar.helpers.YEAR_LABEL import com.simplemobiletools.calendar.helpers.YearlyCalendarImpl import com.simplemobiletools.calendar.interfaces.NavigationListener import com.simplemobiletools.calendar.interfaces.YearlyCalendar +import com.simplemobiletools.calendar.models.DayYearly import com.simplemobiletools.calendar.views.SmallMonthView import com.simplemobiletools.commons.extensions.updateTextColors import kotlinx.android.synthetic.main.fragment_year.view.* @@ -89,7 +90,7 @@ class YearFragment : Fragment(), YearlyCalendar { mListener = listener } - override fun updateYearlyCalendar(events: SparseArray>) { + override fun updateYearlyCalendar(events: SparseArray>) { if (!isAdded) return diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/YearlyCalendarImpl.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/YearlyCalendarImpl.kt index 51e94aac8..9c2ae2294 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/YearlyCalendarImpl.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/YearlyCalendarImpl.kt @@ -6,6 +6,7 @@ import com.simplemobiletools.calendar.extensions.dbHelper import com.simplemobiletools.calendar.extensions.getFilteredEvents import com.simplemobiletools.calendar.extensions.seconds import com.simplemobiletools.calendar.interfaces.YearlyCalendar +import com.simplemobiletools.calendar.models.DayYearly import com.simplemobiletools.calendar.models.Event import org.joda.time.DateTime import java.util.* @@ -23,34 +24,38 @@ class YearlyCalendarImpl(val callback: YearlyCalendar, val context: Context, val private fun gotEvents(events: MutableList) { val filtered = context.getFilteredEvents(events) - val arr = SparseArray>(12) + val arr = SparseArray>(12) - for ((id, startTS, endTS) in filtered) { - val startDateTime = Formatter.getDateTimeFromTS(startTS) - markDay(arr, startDateTime) + filtered.forEach { + val startDateTime = Formatter.getDateTimeFromTS(it.startTS) + markDay(arr, startDateTime, it) val startCode = Formatter.getDayCodeFromDateTime(startDateTime) - val endDateTime = Formatter.getDateTimeFromTS(endTS) + val endDateTime = Formatter.getDateTimeFromTS(it.endTS) val endCode = Formatter.getDayCodeFromDateTime(endDateTime) if (startCode != endCode) { var currDateTime = startDateTime while (Formatter.getDayCodeFromDateTime(currDateTime) != endCode) { currDateTime = currDateTime.plusDays(1) - markDay(arr, currDateTime) + markDay(arr, currDateTime, it) } } } callback.updateYearlyCalendar(arr) } - private fun markDay(arr: SparseArray>, dateTime: DateTime) { + private fun markDay(arr: SparseArray>, dateTime: DateTime, event: Event) { val month = dateTime.monthOfYear val day = dateTime.dayOfMonth - if (arr[month] == null) + if (arr[month] == null) { arr.put(month, ArrayList()) + for (i in 1..32) + arr.get(month).add(DayYearly()) + } - if (dateTime.year == year) - arr.get(month).add(day) + if (dateTime.year == year) { + arr.get(month)[day].addColor(event.color) + } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/interfaces/YearlyCalendar.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/interfaces/YearlyCalendar.kt index ef2de8b93..226fc2017 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/interfaces/YearlyCalendar.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/interfaces/YearlyCalendar.kt @@ -1,8 +1,9 @@ package com.simplemobiletools.calendar.interfaces import android.util.SparseArray +import com.simplemobiletools.calendar.models.DayYearly import java.util.* interface YearlyCalendar { - fun updateYearlyCalendar(events: SparseArray>) + fun updateYearlyCalendar(events: SparseArray>) } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/models/DayYearly.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/models/DayYearly.kt new file mode 100644 index 000000000..5a965a3c6 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/models/DayYearly.kt @@ -0,0 +1,5 @@ +package com.simplemobiletools.calendar.models + +data class DayYearly(var eventColors: HashSet = HashSet()) { + fun addColor(color: Int) = eventColors.add(color) +} diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/views/SmallMonthView.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/views/SmallMonthView.kt index 61cdd607f..51467e1ac 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/views/SmallMonthView.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/views/SmallMonthView.kt @@ -9,18 +9,18 @@ import android.view.View import com.simplemobiletools.calendar.R import com.simplemobiletools.calendar.extensions.config import com.simplemobiletools.calendar.helpers.MEDIUM_ALPHA +import com.simplemobiletools.calendar.models.DayYearly import com.simplemobiletools.commons.extensions.adjustAlpha import java.util.* class SmallMonthView(context: Context, attrs: AttributeSet, defStyle: Int) : View(context, attrs, defStyle) { private var paint: Paint - private var coloredPaint: Paint + private var todayCirclePaint: Paint private var dayWidth = 0f private var textColor = 0 - private var coloredTextColor = 0 private var days = 31 private var isLandscape = false - private var mEvents: ArrayList? = null + private var mEvents: ArrayList? = null var firstDay = 0 var todaysId = 0 @@ -32,7 +32,7 @@ class SmallMonthView(context: Context, attrs: AttributeSet, defStyle: Int) : Vie invalidate() } - fun setEvents(events: ArrayList?) { + fun setEvents(events: ArrayList?) { mEvents = events post { invalidate() } } @@ -51,7 +51,6 @@ class SmallMonthView(context: Context, attrs: AttributeSet, defStyle: Int) : Vie val baseColor = context.config.textColor textColor = baseColor.adjustAlpha(MEDIUM_ALPHA) - coloredTextColor = context.config.primaryColor.adjustAlpha(MEDIUM_ALPHA) paint = Paint(Paint.ANTI_ALIAS_FLAG).apply { color = textColor @@ -59,8 +58,8 @@ class SmallMonthView(context: Context, attrs: AttributeSet, defStyle: Int) : Vie textAlign = Paint.Align.RIGHT } - coloredPaint = Paint(paint) - coloredPaint.color = coloredTextColor + todayCirclePaint = Paint(paint) + todayCirclePaint.color = context.config.primaryColor.adjustAlpha(MEDIUM_ALPHA) isLandscape = resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE } @@ -82,7 +81,7 @@ class SmallMonthView(context: Context, attrs: AttributeSet, defStyle: Int) : Vie if (curId == todaysId) { val dividerConstant = if (isLandscape) 6 else 4 - canvas.drawCircle(x * dayWidth - dayWidth / dividerConstant, y * dayWidth - dayWidth / dividerConstant, dayWidth * 0.41f, coloredPaint) + canvas.drawCircle(x * dayWidth - dayWidth / dividerConstant, y * dayWidth - dayWidth / dividerConstant, dayWidth * 0.41f, todayCirclePaint) } } curId++ @@ -90,5 +89,14 @@ class SmallMonthView(context: Context, attrs: AttributeSet, defStyle: Int) : Vie } } - private fun getPaint(curId: Int) = if (mEvents?.contains(curId) == true) coloredPaint else paint + private fun getPaint(curId: Int): Paint { + val colors = mEvents?.get(curId)?.eventColors ?: HashSet() + if (colors.isNotEmpty()) { + val curPaint = Paint(paint) + curPaint.color = colors.first() + return curPaint + } + + return paint + } }