From e7f7e24bdf37935fa4e3fd34f15a1117bcca6714 Mon Sep 17 00:00:00 2001 From: tibbi Date: Thu, 14 Feb 2019 15:34:11 +0100 Subject: [PATCH] implementing the first part of solving weekly view event overlapping --- .../calendar/pro/fragments/WeekFragment.kt | 55 +++++++++++++++++-- .../calendar/pro/models/EventWeeklyView.kt | 5 ++ 2 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/calendar/pro/models/EventWeeklyView.kt diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/WeekFragment.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/WeekFragment.kt index 02ab3cbea..300703454 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/WeekFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/WeekFragment.kt @@ -5,6 +5,7 @@ import android.content.res.Resources import android.graphics.Rect import android.graphics.drawable.ColorDrawable import android.os.Bundle +import android.util.Range import android.view.LayoutInflater import android.view.MotionEvent import android.view.View @@ -24,6 +25,7 @@ import com.simplemobiletools.calendar.pro.helpers.Formatter import com.simplemobiletools.calendar.pro.interfaces.WeekFragmentListener import com.simplemobiletools.calendar.pro.interfaces.WeeklyCalendar import com.simplemobiletools.calendar.pro.models.Event +import com.simplemobiletools.calendar.pro.models.EventWeeklyView import com.simplemobiletools.calendar.pro.views.MyScrollView import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.helpers.DAY_SECONDS @@ -57,6 +59,7 @@ class WeekFragment : Fragment(), WeeklyCalendar { private var allDayHolders = ArrayList() private var allDayRows = ArrayList>() private var eventTypeColors = LongSparseArray() + private var eventTimeRanges = LinkedHashMap>() private lateinit var inflater: LayoutInflater private lateinit var mView: View @@ -242,6 +245,7 @@ class WeekFragment : Fragment(), WeeklyCalendar { initGrid() allDayHolders.clear() allDayRows.clear() + eventTimeRanges.clear() allDayRows.add(HashSet()) week_all_day_holder?.removeAllViews() @@ -250,22 +254,55 @@ class WeekFragment : Fragment(), WeeklyCalendar { val fullHeight = mRes.getDimension(R.dimen.weekly_view_events_height) val minuteHeight = fullHeight / (24 * 60) val minimalHeight = mRes.getDimension(R.dimen.weekly_view_minimal_event_height).toInt() + val density = Math.round(context!!.resources.displayMetrics.density) var hadAllDayEvent = false val replaceDescription = mConfig.replaceDescription val sorted = events.sortedWith(compareBy({ it.startTS }, { it.endTS }, { it.title }, { if (replaceDescription) it.location else it.description })) for (event in sorted) { - if (event.getIsAllDay() || Formatter.getDayCodeFromTS(event.startTS) != Formatter.getDayCodeFromTS(event.endTS)) { + val startDateTime = Formatter.getDateTimeFromTS(event.startTS) + val endDateTime = Formatter.getDateTimeFromTS(event.endTS) + if (!event.getIsAllDay() && Formatter.getDayCodeFromDateTime(startDateTime) == Formatter.getDayCodeFromDateTime(endDateTime)) { + val startMinutes = startDateTime.minuteOfDay + val duration = endDateTime.minuteOfDay - startMinutes + val range = Range(startMinutes, startMinutes + duration) + val eventWeekly = EventWeeklyView(event.id!!, range) + + val dayCode = Formatter.getDayCodeFromDateTime(startDateTime) + if (!eventTimeRanges.containsKey(dayCode)) { + eventTimeRanges[dayCode] = ArrayList() + } + + eventTimeRanges[dayCode]?.add(eventWeekly) + } + } + + for (event in sorted) { + val startDateTime = Formatter.getDateTimeFromTS(event.startTS) + val endDateTime = Formatter.getDateTimeFromTS(event.endTS) + if (event.getIsAllDay() || Formatter.getDayCodeFromDateTime(startDateTime) != Formatter.getDayCodeFromDateTime(endDateTime)) { hadAllDayEvent = true addAllDayEvent(event) } else { - val startDateTime = Formatter.getDateTimeFromTS(event.startTS) - val endDateTime = Formatter.getDateTimeFromTS(event.endTS) val dayOfWeek = startDateTime.plusDays(if (mConfig.isSundayFirst) 1 else 0).dayOfWeek - 1 val layout = getColumnWithId(dayOfWeek) val startMinutes = startDateTime.minuteOfDay val duration = endDateTime.minuteOfDay - startMinutes + val range = Range(startMinutes, startMinutes + duration) + + val dayCode = Formatter.getDayCodeFromDateTime(startDateTime) + var overlappingEvents = 0 + var currentEventOverlapIndex = 0 + eventTimeRanges[dayCode]!!.forEachIndexed { index, eventWeeklyView -> + if (eventWeeklyView.range.contains(range)) { + overlappingEvents++ + } + + if (eventWeeklyView.id == event.id) { + currentEventOverlapIndex = index + } + } (inflater.inflate(R.layout.week_event_marker, null, false) as TextView).apply { var backgroundColor = eventTypeColors.get(event.eventType, primaryColor) @@ -282,11 +319,21 @@ class WeekFragment : Fragment(), WeeklyCalendar { y = startMinutes * minuteHeight (layoutParams as RelativeLayout.LayoutParams).apply { width = layout.width - 1 + width /= overlappingEvents + x = width * currentEventOverlapIndex.toFloat() + if (overlappingEvents > 1) { + if (currentEventOverlapIndex + 1 == overlappingEvents) { + x += density + } else { + width -= density + } + } + minHeight = if (event.startTS == event.endTS) minimalHeight else (duration * minuteHeight).toInt() - 1 } setOnClickListener { Intent(context, EventActivity::class.java).apply { - putExtra(EVENT_ID, event.id) + putExtra(EVENT_ID, event.id!!) putExtra(EVENT_OCCURRENCE_TS, event.startTS) startActivity(this) } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/models/EventWeeklyView.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/models/EventWeeklyView.kt new file mode 100644 index 000000000..c57b55d5a --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/models/EventWeeklyView.kt @@ -0,0 +1,5 @@ +package com.simplemobiletools.calendar.pro.models + +import android.util.Range + +data class EventWeeklyView(val id: Long, val range: Range)