Skip to content

Commit 0be8d77

Browse files
fixed various issues with all-day events.
added CTOR for all-day event that takes a single day.
1 parent 9d6f070 commit 0be8d77

File tree

11 files changed

+141
-158
lines changed

11 files changed

+141
-158
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ There is also a [sample app](https://github.com/quivr/Android-Week-View/tree/mas
4545
License
4646
----------
4747

48-
Copyright 2014 Raquib-ul-Alam
49-
5048
Licensed under the Apache License, Version 2.0 (the "License");
5149
you may not use this file except in compliance with the License.
5250
You may obtain a copy of the License at

library/src/main/java/com/alamkanak/weekview/DateTimeInterpreter.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ package com.alamkanak.weekview
22

33
import java.util.*
44

5-
/**
6-
* Created by Raquib on 1/6/2015.
7-
*/
85
interface DateTimeInterpreter {
96
fun interpretDate(date: Calendar): String
107

library/src/main/java/com/alamkanak/weekview/WeekView.kt

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ import com.alamkanak.weekview.WeekViewUtil.today
2525
import java.text.SimpleDateFormat
2626
import java.util.*
2727

28-
/**
29-
* Created by Raquib-ul-Alam Kanak on 7/21/2014.
30-
* Website: http://alamkanak.github.io/
31-
*/
3228
class WeekView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : View(context, attrs, defStyleAttr) {
3329
//region fields and properties
3430
private var mHomeDate: Calendar? = null
@@ -366,7 +362,7 @@ class WeekView @JvmOverloads constructor(context: Context, attrs: AttributeSet?
366362
val reversedEventRects = mEventRects
367363
reversedEventRects!!.reverse()
368364
for (eventRect in reversedEventRects) {
369-
if (newEventIdentifier != eventRect.event.identifier && eventRect.rectF != null && e.x > eventRect.rectF!!.left && e.x < eventRect.rectF!!.right && e.y > eventRect.rectF!!.top && e.y < eventRect.rectF!!.bottom) {
365+
if (newEventIdentifier != eventRect.event.id && eventRect.rectF != null && e.x > eventRect.rectF!!.left && e.x < eventRect.rectF!!.right && e.y > eventRect.rectF!!.top && e.y < eventRect.rectF!!.bottom) {
370366
eventClickListener!!.onEventClick(eventRect.originalEvent, eventRect.rectF!!)
371367
playSoundEffect(SoundEffectConstants.CLICK)
372368
return super.onSingleTapConfirmed(e)
@@ -433,11 +429,7 @@ class WeekView @JvmOverloads constructor(context: Context, attrs: AttributeSet?
433429
val right = left + mWidthPerDay
434430

435431
// Add the new event if its bounds are valid
436-
if (left < right &&
437-
left < width &&
438-
top < height &&
439-
right > mHeaderColumnWidth &&
440-
bottom > 0) {
432+
if (left < right && left < width && top < height && right > mHeaderColumnWidth && bottom > 0) {
441433
val dayRectF = RectF(left, top, right, bottom - mCurrentOrigin.y)
442434
newEvent.color = newEventColor
443435
mNewEventRect = EventRect(newEvent, newEvent, dayRectF)
@@ -1672,7 +1664,7 @@ class WeekView @JvmOverloads constructor(context: Context, attrs: AttributeSet?
16721664
if (mEventRects!![i].event.startTime!!.get(Calendar.HOUR_OF_DAY) < mMinTime)
16731665
topToUse = hourHeight * getPassedMinutesInDay(mMinTime, 0) / 60 + eventsTop
16741666

1675-
if (newEventIdentifier != mEventRects!![i].event.identifier)
1667+
if (newEventIdentifier != mEventRects!![i].event.id)
16761668
drawEventTitle(mEventRects!![i].event, mEventRects!![i].rectF!!, canvas, topToUse, left)
16771669
else
16781670
drawEmptyImage(mEventRects!![i].event, mEventRects!![i].rectF!!, canvas, topToUse, left)
@@ -1774,7 +1766,7 @@ class WeekView @JvmOverloads constructor(context: Context, attrs: AttributeSet?
17741766
var availableLineCount = availableHeight / lineHeight
17751767
do {
17761768
// Ellipsize text to fit into event rect.
1777-
if (newEventIdentifier != event.identifier)
1769+
if (newEventIdentifier != event.id)
17781770
textLayout = StaticLayout(TextUtils.ellipsize(bob, mEventTextPaint, (availableLineCount * availableWidth).toFloat(), TextUtils.TruncateAt.END), mEventTextPaint, (rect.right - originalLeft - (eventPadding * 2).toFloat()).toInt(), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0f, false)
17791771

17801772
// Reduce line count.
@@ -1812,7 +1804,6 @@ class WeekView @JvmOverloads constructor(context: Context, attrs: AttributeSet?
18121804
* stored in "originalEvent". But the event that corresponds to rectangle the rectangle
18131805
* instance will be stored in "event".
18141806
*/
1815-
private inner class EventRect
18161807
/**
18171808
* Create a new instance of event rect. An EventRect is actually the rectangle that is drawn
18181809
* on the calendar for a given event. There may be more than one rectangle for a single
@@ -1825,11 +1816,15 @@ class WeekView @JvmOverloads constructor(context: Context, attrs: AttributeSet?
18251816
* @param originalEvent The original event that was passed by the user.
18261817
* @param rectF The rectangle.
18271818
*/
1828-
(var event: WeekViewEvent, var originalEvent: WeekViewEvent, var rectF: RectF?) {
1819+
private inner class EventRect(var event: WeekViewEvent, var originalEvent: WeekViewEvent, var rectF: RectF?) {
18291820
var left: Float = 0f
18301821
var width: Float = 0f
18311822
var top: Float = 0f
18321823
var bottom: Float = 0f
1824+
override fun toString(): String {
1825+
return "EventRect(left=$left, width=$width, top=$top, bottom=$bottom, rectF=$rectF, event=$event, originalEvent=$originalEvent)"
1826+
}
1827+
18331828
}
18341829

18351830

@@ -1960,7 +1955,7 @@ class WeekView @JvmOverloads constructor(context: Context, attrs: AttributeSet?
19601955

19611956
outerLoop@ for (collisionGroup in collisionGroups) {
19621957
for (groupEvent in collisionGroup) {
1963-
if (isEventsCollide(groupEvent.event, eventRect.event) && groupEvent.event.isAllDay == eventRect.event.isAllDay) {
1958+
if (isEventsCollide(groupEvent.event, eventRect.event)) { //&& groupEvent.event.isAllDay == eventRect.event.isAllDay) {
19641959
collisionGroup.add(eventRect)
19651960
isPlaced = true
19661961
break@outerLoop
@@ -2045,13 +2040,15 @@ class WeekView @JvmOverloads constructor(context: Context, attrs: AttributeSet?
20452040
* @return true if the events overlap.
20462041
*/
20472042
private fun isEventsCollide(event1: WeekViewEvent, event2: WeekViewEvent): Boolean {
2043+
if (event1.isAllDay != event2.isAllDay)
2044+
return false
20482045
val start1 = event1.startTime!!.timeInMillis
2049-
val end1 = event1.endTime!!.timeInMillis
20502046
val start2 = event2.startTime!!.timeInMillis
2047+
val end1 = event1.endTime!!.timeInMillis
20512048
val end2 = event2.endTime!!.timeInMillis
2052-
2049+
if (event1.isAllDay)
2050+
return !(start1 > end2 || end1 < start2)
20532051
val minOverlappingMillis = (minOverlappingMinutes * 60 * 1000).toLong()
2054-
20552052
return !(start1 + minOverlappingMillis >= end2 || end1 <= start2 + minOverlappingMillis)
20562053
}
20572054

library/src/main/java/com/alamkanak/weekview/WeekViewEvent.kt

Lines changed: 86 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -5,68 +5,57 @@ import android.support.annotation.ColorInt
55
import com.alamkanak.weekview.WeekViewUtil.isSameDay
66
import java.util.*
77

8-
/**
9-
* Created by Raquib-ul-Alam Kanak on 7/21/2014.
10-
* Website: http://april-shower.com
11-
*/
128
class WeekViewEvent {
13-
var identifier: String? = null
9+
var id: String? = null
1410
var startTime: Calendar? = null
11+
set(value) {
12+
field = value
13+
resetDatesForAllDayIfNeeded()
14+
}
1515
var endTime: Calendar? = null
16+
set(value) {
17+
field = value
18+
resetDatesForAllDayIfNeeded()
19+
}
1620
var name: String? = null
1721
var location: String? = null
1822
@ColorInt
1923
@get:ColorInt
2024
var color: Int = 0
2125
var isAllDay: Boolean = false
22-
var shader: Shader? = null
23-
24-
var id: Long
25-
@Deprecated("")
26-
get() = java.lang.Long.parseLong(identifier)
27-
@Deprecated("")
28-
set(id) {
29-
this.identifier = id.toString()
26+
set(value) {
27+
field = value
28+
resetDatesForAllDayIfNeeded()
3029
}
30+
var shader: Shader? = null
3131

3232
constructor()
3333

34-
/**
35-
* Initializes the event for week view.
36-
*
37-
* @param id The id of the event as String.
38-
* @param name Name of the event.
39-
* @param startYear Year when the event starts.
40-
* @param startMonth Month when the event starts.
41-
* @param startDay Day when the event starts.
42-
* @param startHour Hour (in 24-hour format) when the event starts.
43-
* @param startMinute Minute when the event starts.
44-
* @param endYear Year when the event ends.
45-
* @param endMonth Month when the event ends.
46-
* @param endDay Day when the event ends.
47-
* @param endHour Hour (in 24-hour format) when the event ends.
48-
* @param endMinute Minute when the event ends.
49-
*/
50-
constructor(id: String, name: String, startYear: Int, startMonth: Int, startDay: Int, startHour: Int, startMinute: Int, endYear: Int, endMonth: Int, endDay: Int, endHour: Int, endMinute: Int) {
51-
this.identifier = id
52-
53-
startTime = Calendar.getInstance().apply {
54-
set(Calendar.YEAR, startYear)
55-
set(Calendar.MONTH, startMonth - 1)
56-
set(Calendar.DAY_OF_MONTH, startDay)
57-
set(Calendar.HOUR_OF_DAY, startHour)
58-
set(Calendar.MINUTE, startMinute)
59-
}
60-
endTime = Calendar.getInstance().apply {
61-
set(Calendar.YEAR, endYear)
62-
set(Calendar.MONTH, endMonth - 1)
63-
set(Calendar.DAY_OF_MONTH, endDay)
64-
set(Calendar.HOUR_OF_DAY, endHour)
65-
set(Calendar.MINUTE, endMinute)
34+
private fun resetDatesForAllDayIfNeeded() {
35+
if (!isAllDay)
36+
return
37+
when {
38+
startTime == null && endTime != null -> {
39+
WeekViewUtil.resetTime(endTime!!)
40+
startTime = endTime
41+
}
42+
startTime != null && endTime == null -> {
43+
WeekViewUtil.resetTime(startTime!!)
44+
endTime = startTime
45+
}
46+
startTime != null && endTime != null -> {
47+
WeekViewUtil.resetTime(startTime!!)
48+
if (!WeekViewUtil.isSameDay(startTime!!, endTime!!))
49+
WeekViewUtil.resetTime(endTime!!)
50+
else if (endTime !== startTime)
51+
endTime = startTime
52+
}
6653
}
67-
this.name = name
6854
}
6955

56+
/**CTOR for a single, all day event*/
57+
constructor(id: String?, name: String?, location: String? = null, allDayTime: Calendar, shader: Shader? = null) : this(id, name, location, allDayTime, allDayTime, true, shader)
58+
7059
/**
7160
* Initializes the event for week view.
7261
*
@@ -79,11 +68,13 @@ class WeekViewEvent {
7968
* @param shader the Shader of the event rectangle
8069
*/
8170
@JvmOverloads constructor(id: String?, name: String?, location: String?, startTime: Calendar, endTime: Calendar, allDay: Boolean = false, shader: Shader? = null) {
82-
this.identifier = id
71+
this.id = id
8372
this.name = name
8473
this.location = location
74+
this.isAllDay = allDay
8575
this.startTime = startTime
8676
this.endTime = endTime
77+
this.startTime = startTime
8778
this.isAllDay = allDay
8879
this.shader = shader
8980
}
@@ -101,73 +92,73 @@ class WeekViewEvent {
10192
override fun equals(other: Any?): Boolean {
10293
if (this === other) return true
10394
if (other == null || javaClass != other.javaClass) return false
104-
10595
val that = other as WeekViewEvent?
106-
107-
return identifier == that!!.identifier
96+
return id == that!!.id
10897
}
10998

11099
override fun hashCode(): Int {
111-
return identifier!!.hashCode()
100+
return id!!.hashCode()
112101
}
113102

114-
115103
fun splitWeekViewEvents(): MutableList<WeekViewEvent> {
116104
//This function splits the WeekViewEvent in WeekViewEvents by day
105+
if (this.endTime == null || isSameDay(this.startTime!!, this.endTime!!)) {
106+
val events = ArrayList<WeekViewEvent>(1)
107+
events.add(this)
108+
return events
109+
}
117110
val events = ArrayList<WeekViewEvent>()
118111
// The first millisecond of the next day is still the same day. (no need to split events for this).
119112
var endTime = this.endTime!!.clone() as Calendar
120113
endTime.add(Calendar.MILLISECOND, -1)
121-
if (!isSameDay(this.startTime!!, endTime)) {
122-
endTime = this.startTime!!.clone() as Calendar
123-
endTime.set(Calendar.HOUR_OF_DAY, 23)
124-
endTime.set(Calendar.MINUTE, 59)
125-
val event1 = WeekViewEvent(this.identifier!!, this.name, this.location, this.startTime!!, endTime, this.isAllDay)
126-
event1.color = this.color
127-
events.add(event1)
128-
129-
// Add other days.
130-
if (!isSameDay(this.startTime!!, this.endTime!!)) {
131-
val otherDay = this.startTime!!.clone() as Calendar
114+
endTime = this.startTime!!.clone() as Calendar
115+
endTime.set(Calendar.HOUR_OF_DAY, 23)
116+
endTime.set(Calendar.MINUTE, 59)
117+
val event1 = WeekViewEvent(this.id!!, this.name, this.location, this.startTime!!, endTime, this.isAllDay)
118+
event1.color = this.color
119+
events.add(event1)
120+
121+
// Add other days.
122+
if (!isSameDay(this.startTime!!, this.endTime!!)) {
123+
val otherDay = this.startTime!!.clone() as Calendar
124+
otherDay.add(Calendar.DATE, 1)
125+
while (!isSameDay(otherDay, this.endTime!!)) {
126+
val overDay = otherDay.clone() as Calendar
127+
overDay.set(Calendar.HOUR_OF_DAY, 0)
128+
overDay.set(Calendar.MINUTE, 0)
129+
val endOfOverDay = overDay.clone() as Calendar
130+
endOfOverDay.set(Calendar.HOUR_OF_DAY, 23)
131+
endOfOverDay.set(Calendar.MINUTE, 59)
132+
val eventMore = WeekViewEvent(this.id!!, this.name, null, overDay, endOfOverDay, this.isAllDay)
133+
eventMore.color = this.color
134+
events.add(eventMore)
135+
136+
// Add next day.
132137
otherDay.add(Calendar.DATE, 1)
133-
while (!isSameDay(otherDay, this.endTime!!)) {
134-
val overDay = otherDay.clone() as Calendar
135-
overDay.set(Calendar.HOUR_OF_DAY, 0)
136-
overDay.set(Calendar.MINUTE, 0)
137-
val endOfOverDay = overDay.clone() as Calendar
138-
endOfOverDay.set(Calendar.HOUR_OF_DAY, 23)
139-
endOfOverDay.set(Calendar.MINUTE, 59)
140-
val eventMore = WeekViewEvent(this.identifier!!, this.name, null, overDay, endOfOverDay, this.isAllDay)
141-
eventMore.color = this.color
142-
events.add(eventMore)
143-
144-
// Add next day.
145-
otherDay.add(Calendar.DATE, 1)
146-
}
147-
// Add last day.
148-
val startTime = this.endTime!!.clone() as Calendar
149-
startTime.set(Calendar.HOUR_OF_DAY, 0)
150-
startTime.set(Calendar.MINUTE, 0)
151-
val event2 = WeekViewEvent(this.identifier!!, this.name, this.location, startTime, this.endTime!!, this.isAllDay)
152-
event2.color = this.color
153-
events.add(event2)
154138
}
155-
} else {
156-
events.add(this)
139+
// Add last day.
140+
val startTime = this.endTime!!.clone() as Calendar
141+
startTime.set(Calendar.HOUR_OF_DAY, 0)
142+
startTime.set(Calendar.MINUTE, 0)
143+
val event2 = WeekViewEvent(this.id!!, this.name, this.location, startTime, this.endTime!!, this.isAllDay)
144+
event2.color = this.color
145+
events.add(event2)
157146
}
158147

159148
return events
160149
}
161150

162151
override fun toString(): String {
163-
if (isAllDay)
164-
return "WeekViewEvent(identifier=$identifier, time=${WeekViewUtil.calendarToString(startTime, false)}, name=$name, location=$location, color=$color, isAllDay=$isAllDay, shader=$shader)"
165-
val startTimeText = if (startTime == null) null
166-
else startTime!!.get(Calendar.YEAR).toString() + "-" + (startTime!!.get(Calendar.MONTH) + 1).toString() + "-" + startTime!!.get(Calendar.DAY_OF_MONTH).toString() + " " +
167-
startTime!!.get(Calendar.HOUR_OF_DAY).toString() + ":" + startTime!!.get(Calendar.MINUTE) + ":" + startTime!!.get(Calendar.SECOND) + "." + startTime!!.get(Calendar.MILLISECOND)
168-
val endTimeText = if (endTime == null) null
169-
else endTime!!.get(Calendar.YEAR).toString() + "-" + (endTime!!.get(Calendar.MONTH) + 1).toString() + "-" + endTime!!.get(Calendar.DAY_OF_MONTH).toString() + " " +
170-
endTime!!.get(Calendar.HOUR_OF_DAY).toString() + ":" + endTime!!.get(Calendar.MINUTE) + ":" + endTime!!.get(Calendar.SECOND) + "." + endTime!!.get(Calendar.MILLISECOND)
171-
return "WeekViewEvent(identifier=$identifier, startTime=$startTimeText, endTime=$endTimeText, name=$name, location=$location, color=$color, isAllDay=$isAllDay, shader=$shader)"
152+
val colorStr = "#${Integer.toHexString(color)}"
153+
val startTimeStr = WeekViewUtil.calendarToString(startTime, !isAllDay)
154+
if (isAllDay) {
155+
if (endTime != null || WeekViewUtil.isSameDay(startTime!!, endTime!!))
156+
return "allDayEvent(id=$id, time=$startTimeStr..${WeekViewUtil.calendarToString(startTime, false)}, name=$name, location=$location, color=$colorStr ,shader=$shader)"
157+
return "allDayEvent(id=$id, time=$startTimeStr, name=$name, location=$location, color=$colorStr ,shader=$shader)"
158+
}
159+
if (endTime != null)
160+
return "normalEvent(id=$id, startTime=$startTimeStr, name=$name, location=$location, color=$colorStr , shader=$shader)"
161+
val endTimeStr = WeekViewUtil.calendarToString(endTime, true)
162+
return "normalEvent(id=$id, startTime=$colorStr, endTime=$endTimeStr, name=$name, location=$location, color=$colorStr , shader=$shader)"
172163
}
173164
}

library/src/main/java/com/alamkanak/weekview/WeekViewUtil.kt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ import android.text.format.DateFormat
66
import java.text.SimpleDateFormat
77
import java.util.*
88

9-
/**
10-
* Created by jesse on 6/02/2016.
11-
*/
129
object WeekViewUtil {
1310

1411

@@ -142,9 +139,20 @@ object WeekViewUtil {
142139
sb.append(get(Calendar.YEAR).toString()).append('-').append((get(Calendar.MONTH) + 1).toString())
143140
.append('-').append(get(Calendar.DAY_OF_MONTH).toString())
144141
if (includeTime)
145-
sb.append(get(Calendar.HOUR_OF_DAY).toString()).append(':').append(get(Calendar.MINUTE).toString()).append(':')
142+
sb.append(" ").append(get(Calendar.HOUR_OF_DAY).toString()).append(':').append(get(Calendar.MINUTE).toString()).append(':')
146143
.append(get(Calendar.SECOND).toString()).append('.').append(get(Calendar.MILLISECOND).toString())
147144
}
148145
return sb.toString()
149146
}
147+
148+
@JvmStatic
149+
fun resetTime(cal: Calendar) {
150+
with(cal)
151+
{
152+
set(java.util.Calendar.HOUR_OF_DAY, 0)
153+
set(java.util.Calendar.SECOND, 0)
154+
set(java.util.Calendar.MINUTE, 0)
155+
set(java.util.Calendar.MILLISECOND, 0)
156+
}
157+
}
150158
}

0 commit comments

Comments
 (0)