add all-day events in week fragment in a dynamically added relative layout

This commit is contained in:
tibbi 2017-05-21 23:48:04 +02:00
parent 0d96caf25d
commit 6801f9bcbf
5 changed files with 66 additions and 57 deletions

View File

@ -9,7 +9,6 @@ import android.os.Bundle
import android.support.v4.app.Fragment import android.support.v4.app.Fragment
import android.view.* import android.view.*
import android.widget.ImageView import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout import android.widget.RelativeLayout
import android.widget.TextView import android.widget.TextView
import com.simplemobiletools.calendar.R import com.simplemobiletools.calendar.R
@ -47,7 +46,8 @@ class WeekFragment : Fragment(), WeeklyCalendar {
private var clickStartTime = 0L private var clickStartTime = 0L
private var selectedGrid: View? = null private var selectedGrid: View? = null
private var todayColumnIndex = -1 private var todayColumnIndex = -1
private var events: List<Event> = ArrayList() private var events = ArrayList<Event>()
private var allDayHolders = ArrayList<RelativeLayout>()
lateinit var inflater: LayoutInflater lateinit var inflater: LayoutInflater
lateinit var mView: View lateinit var mView: View
@ -167,7 +167,7 @@ class WeekFragment : Fragment(), WeeklyCalendar {
private fun initGrid() { private fun initGrid() {
(0..6).map { getColumnWithId(it) } (0..6).map { getColumnWithId(it) }
.forEachIndexed { index, layout -> .forEachIndexed { index, layout ->
activity.runOnUiThread { layout.removeAllViews() } layout.removeAllViews()
layout.setOnTouchListener { view, motionEvent -> layout.setOnTouchListener { view, motionEvent ->
checkGridClick(motionEvent, index, layout) checkGridClick(motionEvent, index, layout)
true true
@ -211,7 +211,7 @@ class WeekFragment : Fragment(), WeeklyCalendar {
} }
} }
override fun updateWeeklyCalendar(events: List<Event>) { override fun updateWeeklyCalendar(events: ArrayList<Event>) {
this.events = events this.events = events
updateEvents() updateEvents()
} }
@ -220,13 +220,26 @@ class WeekFragment : Fragment(), WeeklyCalendar {
if (mWasDestroyed) if (mWasDestroyed)
return return
activity.runOnUiThread {
if (context != null)
addEvents()
}
}
private fun addEvents() {
val filtered = context.getFilteredEvents(events) val filtered = context.getFilteredEvents(events)
initGrid() initGrid()
allDayHolders.clear()
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)
val fullHeight = mRes.getDimension(R.dimen.weekly_view_events_height) val fullHeight = mRes.getDimension(R.dimen.weekly_view_events_height)
val minuteHeight = fullHeight / (24 * 60) val minuteHeight = fullHeight / (24 * 60)
val minimalHeight = mRes.getDimension(R.dimen.weekly_view_minimal_event_height).toInt() val minimalHeight = mRes.getDimension(R.dimen.weekly_view_minimal_event_height).toInt()
activity.runOnUiThread { mView.week_all_day_holder.removeAllViews() }
var hadAllDayEvent = false var hadAllDayEvent = false
val sorted = filtered.sortedWith(compareBy({ it.startTS }, { it.endTS }, { it.title }, { it.description })) val sorted = filtered.sortedWith(compareBy({ it.startTS }, { it.endTS }, { it.title }, { it.description }))
@ -246,13 +259,11 @@ class WeekFragment : Fragment(), WeeklyCalendar {
(inflater.inflate(R.layout.week_event_marker, null, false) as TextView).apply { (inflater.inflate(R.layout.week_event_marker, null, false) as TextView).apply {
background = ColorDrawable(MainActivity.eventTypeColors.get(event.eventType, primaryColor)) background = ColorDrawable(MainActivity.eventTypeColors.get(event.eventType, primaryColor))
text = event.title text = event.title
activity.runOnUiThread { layout.addView(this)
layout.addView(this) y = startMinutes * minuteHeight
y = startMinutes * minuteHeight (layoutParams as RelativeLayout.LayoutParams).apply {
(layoutParams as RelativeLayout.LayoutParams).apply { width = layout.width - 1
width = layout.width - 1 minHeight = if (event.startTS == event.endTS) minimalHeight else (duration * minuteHeight).toInt() - 1
minHeight = if (event.startTS == event.endTS) minimalHeight else (duration * minuteHeight).toInt() - 1
}
} }
setOnClickListener { setOnClickListener {
Intent(activity.applicationContext, EventActivity::class.java).apply { Intent(activity.applicationContext, EventActivity::class.java).apply {
@ -278,17 +289,15 @@ class WeekFragment : Fragment(), WeeklyCalendar {
val todayColumn = getColumnWithId(todayColumnIndex) val todayColumn = getColumnWithId(todayColumnIndex)
(inflater.inflate(R.layout.week_now_marker, null, false) as ImageView).apply { (inflater.inflate(R.layout.week_now_marker, null, false) as ImageView).apply {
setColorFilter(primaryColor, PorterDuff.Mode.SRC_IN) setColorFilter(primaryColor, PorterDuff.Mode.SRC_IN)
activity.runOnUiThread { mView.week_events_holder.addView(this, 0)
mView.week_events_holder.addView(this, 0) val extraWidth = (todayColumn.width * 0.3).toInt()
val extraWidth = (todayColumn.width * 0.3).toInt() val markerHeight = resources.getDimension(R.dimen.weekly_view_now_height).toInt()
val markerHeight = resources.getDimension(R.dimen.weekly_view_now_height).toInt() (layoutParams as RelativeLayout.LayoutParams).apply {
(layoutParams as RelativeLayout.LayoutParams).apply { width = todayColumn.width + extraWidth
width = todayColumn.width + extraWidth height = markerHeight
height = markerHeight
}
x = todayColumn.x - extraWidth / 2
y = minutes * minuteHeight - markerHeight / 2
} }
x = todayColumn.x - extraWidth / 2
y = minutes * minuteHeight - markerHeight / 2
} }
} }
} }
@ -318,35 +327,33 @@ class WeekFragment : Fragment(), WeeklyCalendar {
val firstDayIndex = (startDateTimeInWeek.dayOfWeek - if (context.config.isSundayFirst) 0 else 1) % 7 val firstDayIndex = (startDateTimeInWeek.dayOfWeek - if (context.config.isSundayFirst) 0 else 1) % 7
val daysCnt = Days.daysBetween(Formatter.getDateTimeFromTS(minTS), Formatter.getDateTimeFromTS(maxTS)).days val daysCnt = Days.daysBetween(Formatter.getDateTimeFromTS(minTS), Formatter.getDateTimeFromTS(maxTS)).days
activity.runOnUiThread { if (activity == null)
if (activity == null) return
return@runOnUiThread
mView.week_all_day_holder.addView(this) allDayHolders[0].addView(this)
(layoutParams as LinearLayout.LayoutParams).apply { (layoutParams as RelativeLayout.LayoutParams).apply {
topMargin = mRes.getDimension(R.dimen.tiny_margin).toInt() topMargin = mRes.getDimension(R.dimen.tiny_margin).toInt()
leftMargin = getColumnWithId(firstDayIndex).x.toInt() leftMargin = getColumnWithId(firstDayIndex).x.toInt()
bottomMargin = 1 bottomMargin = 1
width = getColumnWithId(Math.min(firstDayIndex + daysCnt, 6)).right - leftMargin - 1 width = getColumnWithId(Math.min(firstDayIndex + daysCnt, 6)).right - leftMargin - 1
}
mView.week_top_holder.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
if (activity == null)
return
mView.week_top_holder.viewTreeObserver.removeOnGlobalLayoutListener(this)
if (isFragmentVisible) {
(activity as MainActivity).updateHoursTopMargin(mView.week_top_holder.height)
}
if (!wasExtraHeightAdded) {
maxScrollY += mView.week_all_day_holder.height
wasExtraHeightAdded = true
}
}
})
} }
mView.week_top_holder.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
if (activity == null)
return
mView.week_top_holder.viewTreeObserver.removeOnGlobalLayoutListener(this)
if (isFragmentVisible) {
(activity as MainActivity).updateHoursTopMargin(mView.week_top_holder.height)
}
if (!wasExtraHeightAdded) {
maxScrollY += mView.week_all_day_holder.height
wasExtraHeightAdded = true
}
}
})
setOnClickListener { setOnClickListener {
Intent(activity.applicationContext, EventActivity::class.java).apply { Intent(activity.applicationContext, EventActivity::class.java).apply {
putExtra(EVENT_ID, event.id) putExtra(EVENT_ID, event.id)

View File

@ -7,17 +7,13 @@ import com.simplemobiletools.calendar.models.Event
import java.util.* import java.util.*
class WeeklyCalendarImpl(val mCallback: WeeklyCalendar, val mContext: Context) { class WeeklyCalendarImpl(val mCallback: WeeklyCalendar, val mContext: Context) {
var mEvents: List<Event> var mEvents = ArrayList<Event>()
init {
mEvents = ArrayList<Event>()
}
fun updateWeeklyCalendar(weekStartTS: Int) { fun updateWeeklyCalendar(weekStartTS: Int) {
val startTS = weekStartTS val startTS = weekStartTS
val endTS = startTS + WEEK_SECONDS val endTS = startTS + WEEK_SECONDS
mContext.dbHelper.getEvents(startTS, endTS) { mContext.dbHelper.getEvents(startTS, endTS) {
mEvents = it mEvents = it as ArrayList<Event>
mCallback.updateWeeklyCalendar(it) mCallback.updateWeeklyCalendar(it)
} }
} }

View File

@ -3,5 +3,5 @@ package com.simplemobiletools.calendar.interfaces
import com.simplemobiletools.calendar.models.Event import com.simplemobiletools.calendar.models.Event
interface WeeklyCalendar { interface WeeklyCalendar {
fun updateWeeklyCalendar(events: List<Event>) fun updateWeeklyCalendar(events: ArrayList<Event>)
} }

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

View File

@ -169,7 +169,8 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_below="@+id/week_letters_holder" android:layout_below="@+id/week_letters_holder"
android:orientation="vertical"/> android:orientation="vertical">
</LinearLayout>
</RelativeLayout> </RelativeLayout>
</RelativeLayout> </RelativeLayout>