Skip to content

Commit 8339769

Browse files
authored
Replace DateTimeInterpreter with lambdas (alamkanak#149)
* Replace DateTimeInterpreter with setDateFormatter and setTimeFormatter * Remove DefaultDateTimeInterpreterTest
1 parent 80ee1d9 commit 8339769

File tree

8 files changed

+42
-136
lines changed

8 files changed

+42
-136
lines changed

core/src/main/java/com/alamkanak/weekview/CalendarExtensions.kt

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -250,18 +250,15 @@ internal fun Calendar.isAtStartOfNextDay(startDate: Calendar): Boolean {
250250
}
251251
}
252252

253-
internal fun getDefaultDateFormat(numberOfDays: Int): SimpleDateFormat {
254-
return when (numberOfDays) {
255-
1 -> SimpleDateFormat("EEEE M/dd", Locale.getDefault()) // full weekday
256-
in 2..6 -> SimpleDateFormat("EEE M/dd", Locale.getDefault()) // first three characters
257-
else -> SimpleDateFormat("EEEEE M/dd", Locale.getDefault()) // first character
258-
}
253+
internal fun defaultDateFormatter(
254+
numberOfDays: Int
255+
): SimpleDateFormat = when (numberOfDays) {
256+
1 -> SimpleDateFormat("EEEE M/dd", Locale.getDefault()) // full weekday
257+
in 2..6 -> SimpleDateFormat("EEE M/dd", Locale.getDefault()) // first three characters
258+
else -> SimpleDateFormat("EEEEE M/dd", Locale.getDefault()) // first character
259259
}
260260

261-
internal fun getDefaultTimeFormat(is24HourFormat: Boolean): SimpleDateFormat {
262-
val format = if (is24HourFormat) "HH:mm" else "hh a"
263-
return SimpleDateFormat(format, Locale.getDefault())
264-
}
261+
internal fun defaultTimeFormatter(): SimpleDateFormat = SimpleDateFormat("hh a", Locale.getDefault())
265262

266263
internal fun Calendar.format(
267264
format: Int = java.text.DateFormat.MEDIUM
Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package com.alamkanak.weekview
22

3-
import android.content.Context
4-
import android.text.format.DateFormat
53
import java.util.Calendar
6-
import java.util.Locale
74

85
interface DateTimeInterpreter {
96
fun onSetNumberOfDays(days: Int) {
@@ -12,41 +9,3 @@ interface DateTimeInterpreter {
129
fun interpretDate(date: Calendar): String
1310
fun interpretTime(hour: Int): String
1411
}
15-
16-
internal class DefaultDateTimeInterpreter(
17-
dateFormatProvider: DateFormatProvider,
18-
numberOfDays: Int
19-
) : DateTimeInterpreter {
20-
21-
private var sdfDate = getDefaultDateFormat(numberOfDays)
22-
private val sdfTime = getDefaultTimeFormat(dateFormatProvider.is24HourFormat)
23-
24-
// This calendar is only used for interpreting the time. To avoid issues with time changes,
25-
// we always use the first day of the year
26-
private val calendar = firstDayOfYear()
27-
28-
override fun onSetNumberOfDays(days: Int) {
29-
sdfDate = getDefaultDateFormat(days)
30-
}
31-
32-
override fun interpretDate(date: Calendar): String {
33-
return sdfDate.format(date.time).toUpperCase(Locale.getDefault())
34-
}
35-
36-
override fun interpretTime(hour: Int): String {
37-
val time = calendar.withTime(hour, minutes = 0)
38-
return sdfTime.format(time.time)
39-
}
40-
}
41-
42-
internal interface DateFormatProvider {
43-
val is24HourFormat: Boolean
44-
}
45-
46-
internal class RealDateFormatProvider(
47-
private val context: Context
48-
) : DateFormatProvider {
49-
50-
override val is24HourFormat: Boolean
51-
get() = DateFormat.is24HourFormat(context)
52-
}

core/src/main/java/com/alamkanak/weekview/DayLabelDrawer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ internal class DayLabelDrawer<T>(
5151
}
5252

5353
private fun provideAndCacheDayLabel(key: Int, day: Calendar): String {
54-
return config.dateTimeInterpreter.interpretDate(day).also {
54+
return config.dateFormatter(day).also {
5555
cache.dayLabelCache.put(key, it)
5656
}
5757
}

core/src/main/java/com/alamkanak/weekview/MultiLineDayLabelHeightUpdater.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ internal class MultiLineDayLabelHeightUpdater<T>(
6464
}
6565

6666
private fun provideAndCacheDayLabel(key: Int, day: Calendar): String {
67-
return config.dateTimeInterpreter.interpretDate(day).also {
67+
return config.dateFormatter(day).also {
6868
cache.dayLabelCache.put(key, it)
6969
}
7070
}

core/src/main/java/com/alamkanak/weekview/TimeColumnDrawer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ internal class TimeColumnDrawer(
1616

1717
private fun cacheTimeLabels() = with(config) {
1818
for (hour in timeRange step timeColumnHoursInterval) {
19-
timeLabelCache.put(hour, dateTimeInterpreter.interpretTime(hour))
19+
timeLabelCache.put(hour, timeFormatter(hour))
2020
}
2121
}
2222

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,13 +1418,28 @@ class WeekView<T : Any> @JvmOverloads constructor(
14181418
}
14191419

14201420
@PublicApi
1421+
@Deprecated("Use setDateFormatter() and setTimeFormatter() instead.")
14211422
var dateTimeInterpreter: DateTimeInterpreter
1422-
get() = configWrapper.dateTimeInterpreter
1423+
get() = object : DateTimeInterpreter {
1424+
override fun interpretDate(date: Calendar): String = configWrapper.dateFormatter(date)
1425+
override fun interpretTime(hour: Int): String = configWrapper.timeFormatter(hour)
1426+
}
14231427
set(value) {
1424-
configWrapper.dateTimeInterpreter = value
1428+
setDateFormatter { value.interpretDate(it) }
1429+
setTimeFormatter { value.interpretTime(it) }
14251430
clearCaches()
14261431
}
14271432

1433+
@PublicApi
1434+
fun setDateFormatter(formatter: (Calendar) -> String) {
1435+
configWrapper.dateFormatter = formatter
1436+
}
1437+
1438+
@PublicApi
1439+
fun setTimeFormatter(formatter: (Int) -> String) {
1440+
configWrapper.timeFormatter = formatter
1441+
}
1442+
14281443
private fun clearCaches() {
14291444
drawers
14301445
.filterIsInstance(CachingDrawer::class.java)

core/src/main/java/com/alamkanak/weekview/WeekViewConfigWrapper.kt

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ import java.util.Calendar
1111
import kotlin.math.max
1212
import kotlin.math.min
1313

14+
typealias DateFormatter = (Calendar) -> String
15+
typealias TimeFormatter = (Int) -> String
16+
1417
internal class WeekViewConfigWrapper(
1518
private val view: WeekView<*>,
1619
private val config: WeekViewConfig
1720
) {
1821

19-
private val context = view.context
20-
2122
var timeTextPaint: Paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
2223
textAlign = Paint.Align.RIGHT
2324
textSize = config.timeColumnTextSize.toFloat()
@@ -151,13 +152,19 @@ internal class WeekViewConfigWrapper(
151152
var minDate: Calendar? = null
152153
var maxDate: Calendar? = null
153154

154-
private var internalDateTimeInterpreter: DateTimeInterpreter =
155-
DefaultDateTimeInterpreter(RealDateFormatProvider(context), numberOfVisibleDays)
155+
var dateFormatter: DateFormatter = { date ->
156+
defaultDateFormatter(numberOfDays = numberOfVisibleDays).format(date.time)
157+
}
158+
159+
private var _timeFormatter: TimeFormatter = { hour ->
160+
val date = now().withTime(hour = hour, minutes = 0)
161+
defaultTimeFormatter().format(date.time)
162+
}
156163

157-
var dateTimeInterpreter: DateTimeInterpreter
158-
get() = internalDateTimeInterpreter
164+
var timeFormatter: TimeFormatter
165+
get() = _timeFormatter
159166
set(value) {
160-
internalDateTimeInterpreter = value
167+
_timeFormatter = value
161168
initTextTimeWidth()
162169
}
163170

@@ -752,7 +759,7 @@ internal class WeekViewConfigWrapper(
752759
*/
753760
private fun initTextTimeWidth() {
754761
timeTextWidth = (0 until hoursPerDay)
755-
.map { dateTimeInterpreter.interpretTime(it) }
762+
.map { _timeFormatter(it) }
756763
.map { timeTextPaint.measureText(it) }
757764
.max() ?: 0f
758765
}

core/src/test/java/com/alamkanak/weekview/DefaultDateTimeInterpreterTest.kt

Lines changed: 0 additions & 72 deletions
This file was deleted.

0 commit comments

Comments
 (0)