@@ -5,68 +5,57 @@ import android.support.annotation.ColorInt
55import com.alamkanak.weekview.WeekViewUtil.isSameDay
66import java.util.*
77
8- /* *
9- * Created by Raquib-ul-Alam Kanak on 7/21/2014.
10- * Website: http://april-shower.com
11- */
128class 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}
0 commit comments