From 41369041cfb0575fe194ffc2f4e7ca83d7a4aa3f Mon Sep 17 00:00:00 2001 From: tibbi Date: Tue, 23 May 2017 22:07:27 +0200 Subject: [PATCH] add some logic to adding all-day events to lines --- .../calendar/fragments/WeekFragment.kt | 49 +++++++++++++++++-- 1 file changed, 45 insertions(+), 4 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/WeekFragment.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/WeekFragment.kt index 556c52395..cff1a517b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/WeekFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/WeekFragment.kt @@ -48,6 +48,7 @@ class WeekFragment : Fragment(), WeeklyCalendar { private var todayColumnIndex = -1 private var events = ArrayList() private var allDayHolders = ArrayList() + private var allDayRows = ArrayList>() lateinit var inflater: LayoutInflater lateinit var mView: View @@ -61,6 +62,7 @@ class WeekFragment : Fragment(), WeeklyCalendar { mWeekTimestamp = arguments.getInt(WEEK_START_TIMESTAMP) primaryColor = context.config.primaryColor mRes = resources + allDayRows.add(HashSet()) mView = inflater.inflate(R.layout.fragment_week, container, false).apply { week_events_scrollview.setOnScrollviewListener(object : MyScrollView.ScrollViewListener { @@ -231,11 +233,11 @@ class WeekFragment : Fragment(), WeeklyCalendar { initGrid() allDayHolders.clear() + allDayRows.clear() + allDayRows.add(HashSet()) week_all_day_holder?.removeAllViews() - val allDaysLine = inflater.inflate(R.layout.all_day_events_holder_line, null, false) as RelativeLayout - week_all_day_holder.addView(allDaysLine) - allDayHolders.add(allDaysLine) + addNewLine() val fullHeight = mRes.getDimension(R.dimen.weekly_view_events_height) val minuteHeight = fullHeight / (24 * 60) @@ -283,6 +285,12 @@ class WeekFragment : Fragment(), WeeklyCalendar { addCurrentTimeIndicator(minuteHeight) } + private fun addNewLine() { + val allDaysLine = inflater.inflate(R.layout.all_day_events_holder_line, null, false) as RelativeLayout + week_all_day_holder.addView(allDaysLine) + allDayHolders.add(allDaysLine) + } + fun addCurrentTimeIndicator(minuteHeight: Float) { if (todayColumnIndex != -1) { val minutes = DateTime().minuteOfDay @@ -330,7 +338,40 @@ class WeekFragment : Fragment(), WeeklyCalendar { if (activity == null) return - allDayHolders[0].addView(this) + var doesEventFit: Boolean + val cnt = allDayRows.size - 1 + var wasEventHandled = false + var drawAtLine = 0 + for (index in 0..cnt) { + doesEventFit = true + drawAtLine = index + val row = allDayRows[index] + for (i in firstDayIndex..firstDayIndex + daysCnt) { + if (row.contains(i)) { + doesEventFit = false + } + } + + for (dayIndex in firstDayIndex..firstDayIndex + daysCnt) { + if (doesEventFit) { + row.add(dayIndex) + wasEventHandled = true + } else if (index == cnt) { + if (allDayRows.size == index + 1) { + allDayRows.add(HashSet()) + addNewLine() + drawAtLine++ + wasEventHandled = true + } + allDayRows.last().add(dayIndex) + } + } + if (wasEventHandled) { + break + } + } + + allDayHolders[drawAtLine].addView(this) (layoutParams as RelativeLayout.LayoutParams).apply { topMargin = mRes.getDimension(R.dimen.tiny_margin).toInt() leftMargin = getColumnWithId(firstDayIndex).x.toInt()