Skip to content

Commit f0c75c7

Browse files
committed
Replaced int days by DayOfWeek. closes #15 and probably also fixes #17
1 parent 694ae29 commit f0c75c7

File tree

7 files changed

+222
-130
lines changed

7 files changed

+222
-130
lines changed

app/src/main/java/de/tobiasschuerg/weekview/sample/SampleActivity.kt

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ import de.tobiasschuerg.weekview.data.Event
1414
import de.tobiasschuerg.weekview.data.EventConfig
1515
import de.tobiasschuerg.weekview.data.WeekData
1616
import de.tobiasschuerg.weekview.view.EventView
17-
import kotlinx.android.synthetic.main.activity_sample.week_view_foo
17+
import kotlinx.android.synthetic.main.activity_sample.*
18+
import org.threeten.bp.DayOfWeek
1819
import org.threeten.bp.LocalDate
1920
import org.threeten.bp.LocalTime
20-
import java.util.Calendar
21-
import java.util.Random
21+
import java.util.*
2222
import kotlin.math.absoluteValue
2323

2424
class SampleActivity : AppCompatActivity() {
@@ -30,17 +30,22 @@ class SampleActivity : AppCompatActivity() {
3030
private val minEventLength = 30
3131
private val maxEventLength = 90
3232

33+
3334
private val data: WeekData by lazy {
3435
WeekData().apply {
3536
var startTime: LocalTime
36-
(1..7).filter { it != Calendar.SATURDAY }.map {
37-
startTime = LocalTime.of(8 + random.nextInt(1), random.nextInt(60))
38-
while (startTime.isBefore(LocalTime.of(15, 0))) {
39-
val endTime = startTime.plusMinutes(minEventLength + random.nextInt(maxEventLength - minEventLength).toLong())
40-
this.add(createSampleEntry(it, startTime, endTime))
41-
startTime = endTime.plusMinutes(5 + random.nextInt(95).toLong())
42-
}
43-
}
37+
DayOfWeek.values()
38+
// Filter the weekend
39+
.filter { it != DayOfWeek.SATURDAY }
40+
.filter { it != DayOfWeek.SUNDAY }
41+
.map { dayOfWeek ->
42+
startTime = LocalTime.of(8 + random.nextInt(1), random.nextInt(60))
43+
while (startTime.isBefore(LocalTime.of(15, 0))) {
44+
val endTime = startTime.plusMinutes(minEventLength + random.nextInt(maxEventLength - minEventLength).toLong())
45+
this.add(createSampleEntry(dayOfWeek, startTime, endTime))
46+
startTime = endTime.plusMinutes(5 + random.nextInt(95).toLong())
47+
}
48+
}
4449
earliestStart = LocalTime.MIN
4550
}
4651
}
@@ -78,7 +83,7 @@ class SampleActivity : AppCompatActivity() {
7883
}
7984
}
8085

81-
private fun createSampleEntry(day: Int, startTime: LocalTime, endTime: LocalTime): Event.Single {
86+
private fun createSampleEntry(day: DayOfWeek, startTime: LocalTime, endTime: LocalTime): Event.Single {
8287
val name = titles[random.nextInt(titles.size)]
8388
val subTitle = subTitles[random.nextInt(subTitles.size)]
8489
return Event.Single(
@@ -118,7 +123,7 @@ class SampleActivity : AppCompatActivity() {
118123
override fun onOptionsItemSelected(item: MenuItem?): Boolean {
119124
val startTime = LocalTime.of(8 + random.nextInt(8), random.nextInt(60))
120125
val endTime = startTime.plusMinutes((30 + random.nextInt(60)).toLong())
121-
val day = random.nextInt(7) + 1
126+
val day = DayOfWeek.values().asList().shuffled().first()
122127
val newEvents = listOf(createSampleEntry(day, startTime, endTime))
123128
newEvents.forEach { data.add(it) }
124129
week_view_foo.addLessonsToTimetable(data)

library/src/main/java/de/tobiasschuerg/weekview/data/Event.kt

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package de.tobiasschuerg.weekview.data
22

3+
import org.threeten.bp.DayOfWeek
34
import org.threeten.bp.Duration
45
import org.threeten.bp.LocalDate
56
import org.threeten.bp.LocalTime
@@ -12,21 +13,21 @@ sealed class Event {
1213
abstract val shortTitle: String
1314

1415
data class Single(
15-
override val id: Long,
16-
override val date: LocalDate,
17-
override val title: String,
18-
override val shortTitle: String,
19-
val subTitle: String?,
16+
override val id: Long,
17+
override val date: LocalDate,
18+
override val title: String,
19+
override val shortTitle: String,
20+
val subTitle: String?,
2021

21-
val day: Int,
22-
val startTime: LocalTime,
23-
val endTime: LocalTime,
22+
val day: DayOfWeek,
23+
val startTime: LocalTime,
24+
val endTime: LocalTime,
2425

25-
val upperText: String?,
26-
val lowerText: String?,
26+
val upperText: String?,
27+
val lowerText: String?,
2728

28-
val textColor: Int,
29-
val backgroundColor: Int
29+
val textColor: Int,
30+
val backgroundColor: Int
3031
) : Event() {
3132
val duration: Duration = Duration.between(startTime, endTime)
3233
}

library/src/main/java/de/tobiasschuerg/weekview/util/DayHelper.kt

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package de.tobiasschuerg.weekview.util
2+
3+
import org.threeten.bp.DayOfWeek
4+
import org.threeten.bp.temporal.WeekFields
5+
import java.util.*
6+
7+
internal object DayOfWeekUtil {
8+
9+
/**
10+
* Creates a list of all week days with a given start day.
11+
*/
12+
fun createList(firstDay: DayOfWeek = WeekFields.of(Locale.getDefault()).firstDayOfWeek): List<DayOfWeek> {
13+
return (0..6L).toList().map { firstDay.plus(it) }
14+
}
15+
16+
/**
17+
* Maps a [DayOfWeek] to the corresponding column.
18+
* Considers the first day of the week for the given [Locale],
19+
* as well as if a day is 'enabled' and skips it.
20+
*/
21+
fun mapDayToColumn(calendarDay: DayOfWeek, saturdayEnabled: Boolean, sundayEnabled: Boolean): Int {
22+
val firstDayOfTheWeek = WeekFields.of(Locale.getDefault()).firstDayOfWeek
23+
24+
if (calendarDay == DayOfWeek.SATURDAY && !saturdayEnabled) {
25+
throw java.lang.IllegalStateException("Passed saturday although it is disabled")
26+
}
27+
28+
if (calendarDay == DayOfWeek.SUNDAY && !sundayEnabled) {
29+
throw java.lang.IllegalStateException("Passed sunday although it is disabled")
30+
}
31+
32+
when (firstDayOfTheWeek) {
33+
34+
DayOfWeek.MONDAY -> {
35+
// mo: 0, fr:4, su:6
36+
val column = calendarDay.value
37+
return if (!saturdayEnabled && calendarDay == DayOfWeek.SUNDAY) {
38+
5
39+
} else {
40+
column - 1
41+
}
42+
}
43+
44+
DayOfWeek.SATURDAY -> {
45+
// sa: 0, su: 1, fr: 6,
46+
if (saturdayEnabled) {
47+
return if (sundayEnabled) {
48+
when (calendarDay) {
49+
DayOfWeek.SATURDAY -> 0
50+
DayOfWeek.SUNDAY -> 1
51+
else -> calendarDay.value + 1
52+
}
53+
} else {
54+
return when (calendarDay) {
55+
DayOfWeek.SATURDAY -> 0
56+
else -> calendarDay.value
57+
}
58+
}
59+
} else {
60+
return if (sundayEnabled) {
61+
when (calendarDay) {
62+
DayOfWeek.SUNDAY -> 0
63+
else -> calendarDay.value
64+
}
65+
} else {
66+
return calendarDay.value - 1
67+
}
68+
}
69+
}
70+
71+
DayOfWeek.SUNDAY -> {
72+
return if (sundayEnabled) {
73+
// su: 0, mo: 1 fr: 5, sa: 6
74+
if (calendarDay == DayOfWeek.SUNDAY) {
75+
0
76+
} else {
77+
calendarDay.value
78+
}
79+
} else {
80+
// mo: 0 fr: 4, sa: 5, su: -1
81+
calendarDay.value - 1
82+
}
83+
}
84+
else -> throw IllegalStateException("$firstDayOfTheWeek das is not supported as start day")
85+
}
86+
}
87+
88+
}

library/src/main/java/de/tobiasschuerg/weekview/view/WeekBackgroundView.kt

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@ import android.graphics.Paint
77
import android.graphics.Rect
88
import android.util.Log
99
import android.view.View
10-
import de.tobiasschuerg.weekview.util.DayHelper
10+
import de.tobiasschuerg.weekview.util.DayOfWeekUtil
1111
import de.tobiasschuerg.weekview.util.dipToPixelF
1212
import de.tobiasschuerg.weekview.util.dipToPixelI
1313
import de.tobiasschuerg.weekview.util.toLocalString
14+
import org.threeten.bp.DayOfWeek
1415
import org.threeten.bp.Duration
1516
import org.threeten.bp.LocalTime
17+
import org.threeten.bp.format.TextStyle
1618
import org.threeten.bp.temporal.ChronoUnit
17-
import java.text.DateFormatSymbols
18-
import java.util.Calendar
19+
import org.threeten.bp.temporal.WeekFields
20+
import java.util.*
1921
import kotlin.math.roundToInt
2022

2123
internal class WeekBackgroundView constructor(context: Context) : View(context) {
@@ -47,12 +49,12 @@ internal class WeekBackgroundView constructor(context: Context) : View(context)
4749

4850
private var drawCount = 0
4951

50-
val days: MutableList<Int> = DayHelper.createListStartingOn()
51-
.toMutableList()
52-
.apply {
53-
remove(Calendar.SATURDAY)
54-
remove(Calendar.SUNDAY)
55-
}
52+
val days: MutableList<DayOfWeek> = DayOfWeekUtil.createList()
53+
.toMutableList()
54+
.apply {
55+
remove(DayOfWeek.SATURDAY)
56+
remove(DayOfWeek.SUNDAY)
57+
}
5658

5759
var startTime: LocalTime = LocalTime.of(10, 0)
5860
private set
@@ -123,7 +125,7 @@ internal class WeekBackgroundView constructor(context: Context) : View(context)
123125

124126
private fun Canvas.drawColumnsWithHeaders() {
125127
Log.v(TAG, "Drawing vertical dividers on canvas")
126-
val todayDay: Int = Calendar.getInstance().get(Calendar.DAY_OF_WEEK)
128+
val todayDay: DayOfWeek = WeekFields.of(Locale.getDefault()).firstDayOfWeek
127129
for ((column, dayId) in days.withIndex()) {
128130
drawLeftColumnDivider(column)
129131
drawWeekDayName(dayId, column)
@@ -146,20 +148,20 @@ internal class WeekBackgroundView constructor(context: Context) : View(context)
146148
drawRect(rect, accentPaint)
147149
}
148150

149-
private fun Canvas.drawWeekDayName(dayId: Int, column: Int) {
150-
val shortName = DateFormatSymbols().shortWeekdays[dayId]
151+
private fun Canvas.drawWeekDayName(day: DayOfWeek, column: Int) {
152+
val shortName = day.getDisplayName(TextStyle.SHORT, Locale.getDefault())
151153
val xLabel = (getColumnStart(column, false) + getColumnEnd(column, false)) / 2
152154
drawText(shortName, xLabel.toFloat(), topOffsetPx / 2 + mPaintLabels.descent(), mPaintLabels)
153155
}
154156

155157
private fun drawMultiLineText(canvas: Canvas, text: String, initialX: Float, initialY: Float, paint: Paint) {
156158
var currentY = initialY
157159
text.split(" ")
158-
.dropLastWhile(String::isEmpty)
159-
.forEach {
160-
canvas.drawText(it, initialX, currentY, paint)
161-
currentY += (-paint.ascent() + paint.descent()).toInt()
162-
}
160+
.dropLastWhile(String::isEmpty)
161+
.forEach {
162+
canvas.drawText(it, initialX, currentY, paint)
163+
currentY += (-paint.ascent() + paint.descent()).toInt()
164+
}
163165
}
164166

165167
/**

0 commit comments

Comments
 (0)