Skip to content

Commit e0a2c5e

Browse files
committed
Simplify internal date manipulation
1 parent 71ef5b5 commit e0a2c5e

File tree

17 files changed

+190
-111
lines changed

17 files changed

+190
-111
lines changed

build.gradle

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,34 @@
33
buildscript {
44
ext {
55
buildConfig = [
6-
minSdk: 15,
6+
minSdk : 15,
77
minSdkJsr310: 26,
8-
compileSdk: 29,
9-
targetSdk: 29
8+
compileSdk : 29,
9+
targetSdk : 29
1010
]
1111
versions = [
12-
appCompat: '1.0.2',
13-
emoji: '1.0.0',
14-
jodaTime: '2.10.3',
12+
appCompat : '1.0.2',
13+
emoji : '1.0.0',
14+
jodaTime : '2.10.3',
1515
jodaTimeAndroid: '2.10.2',
16-
jUnit: '4.12',
17-
kotlin: '1.3.50',
18-
threeTen: '1.4.0',
19-
threeTenAbp: '1.2.1',
20-
mockitoCore: '2.28.2',
21-
mockitoInline: '2.28.2'
16+
jUnit : '4.12',
17+
kotlin : '1.3.50',
18+
threeTen : '1.4.0',
19+
threeTenAbp : '1.2.1',
20+
mockitoCore : '2.28.2',
21+
mockitoInline : '2.28.2'
2222
]
2323
libraries = [
24-
appCompat: "androidx.appcompat:appcompat:${versions.appCompat}",
25-
emoji: "androidx.emoji:emoji-bundled:${versions.emoji}",
26-
jodaTime: "joda-time:joda-time:${versions.jodaTime}",
24+
appCompat : "androidx.appcompat:appcompat:${versions.appCompat}",
25+
emoji : "androidx.emoji:emoji-bundled:${versions.emoji}",
26+
jodaTime : "joda-time:joda-time:${versions.jodaTime}",
2727
jodaTimeAndroid: "net.danlew:android.joda:${versions.jodaTimeAndroid}",
28-
jUnit: "junit:junit:${versions.jUnit}",
29-
kotlin: "org.jetbrains.kotlin:kotlin-stdlib-jdk7:${versions.kotlin}",
30-
threeTen: "org.threeten:threetenbp:${versions.threeTen}",
31-
threeTenAbp: "com.jakewharton.threetenabp:threetenabp:${versions.threeTenAbp}",
32-
mockitoCore: "org.mockito:mockito-core:${versions.mockitoCore}",
33-
mockitoInline: "org.mockito:mockito-inline:${versions.mockitoInline}"
28+
jUnit : "junit:junit:${versions.jUnit}",
29+
kotlin : "org.jetbrains.kotlin:kotlin-stdlib-jdk7:${versions.kotlin}",
30+
threeTen : "org.threeten:threetenbp:${versions.threeTen}",
31+
threeTenAbp : "com.jakewharton.threetenabp:threetenabp:${versions.threeTenAbp}",
32+
mockitoCore : "org.mockito:mockito-core:${versions.mockitoCore}",
33+
mockitoInline : "org.mockito:mockito-inline:${versions.mockitoInline}"
3434
]
3535
}
3636
repositories {
@@ -55,6 +55,13 @@ allprojects {
5555
options.addStringOption('charSet', 'UTF-8')
5656
}
5757

58+
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
59+
kotlinOptions {
60+
jvmTarget = '1.8'
61+
freeCompilerArgs = ['-XXLanguage:+InlineClasses']
62+
}
63+
}
64+
5865
apply plugin: 'org.jlleitschuh.gradle.ktlint'
5966

6067
repositories {

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

Lines changed: 67 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,25 @@ import java.util.Calendar
66
import java.util.Locale
77
import kotlin.math.roundToInt
88

9+
internal interface Duration {
10+
val inMillis: Int
11+
}
12+
13+
internal inline class Days(val days: Int) : Duration {
14+
override val inMillis: Int
15+
get() = days * (24 * 60 * 60 * 1_000)
16+
}
17+
18+
internal inline class Hours(val hours: Int) : Duration {
19+
override val inMillis: Int
20+
get() = hours * (60 * 60 * 1_000)
21+
}
22+
23+
internal inline class Millis(val millis: Int) : Duration {
24+
override val inMillis: Int
25+
get() = millis
26+
}
27+
928
internal val Calendar.hour: Int
1029
get() = get(Calendar.HOUR_OF_DAY)
1130

@@ -28,29 +47,65 @@ internal fun Calendar.isEqual(other: Calendar) = timeInMillis == other.timeInMil
2847

2948
internal fun Calendar.isNotEqual(other: Calendar) = isEqual(other).not()
3049

31-
internal fun Calendar.plusDays(days: Int): Calendar {
50+
internal operator fun Calendar.plus(days: Days): Calendar {
3251
return copy().apply {
33-
add(Calendar.DATE, days)
52+
add(Calendar.DATE, days.days)
3453
}
3554
}
3655

37-
internal fun Calendar.minusDays(days: Int): Calendar = plusDays(days * (-1))
56+
internal operator fun Calendar.plusAssign(days: Days) {
57+
add(Calendar.DATE, days.days)
58+
}
3859

39-
internal fun Calendar.plusHours(hours: Int): Calendar {
60+
internal operator fun Calendar.minus(days: Days): Calendar {
4061
return copy().apply {
41-
add(Calendar.HOUR_OF_DAY, hours)
62+
add(Calendar.DATE, days.days * (-1))
4263
}
4364
}
4465

45-
internal fun Calendar.minusHours(hours: Int): Calendar = plusHours(hours * (-1))
66+
internal operator fun Calendar.minusAssign(days: Days) {
67+
add(Calendar.DATE, days.days * (-1))
68+
}
4669

47-
internal fun Calendar.plusMillis(millis: Int): Calendar {
70+
internal operator fun Calendar.plus(hours: Hours): Calendar {
4871
return copy().apply {
49-
add(Calendar.MILLISECOND, millis)
72+
add(Calendar.HOUR_OF_DAY, hours.hours)
5073
}
5174
}
5275

53-
internal fun Calendar.minusMillis(millis: Int): Calendar = plusMillis(millis * (-1))
76+
internal operator fun Calendar.plusAssign(hours: Hours) {
77+
add(Calendar.HOUR_OF_DAY, hours.hours)
78+
}
79+
80+
internal operator fun Calendar.minus(hours: Hours): Calendar {
81+
return copy().apply {
82+
add(Calendar.HOUR_OF_DAY, hours.hours * (-1))
83+
}
84+
}
85+
86+
internal operator fun Calendar.minusAssign(hours: Hours) {
87+
add(Calendar.HOUR_OF_DAY, hours.hours * (-1))
88+
}
89+
90+
internal operator fun Calendar.plus(millis: Millis): Calendar {
91+
return copy().apply {
92+
add(Calendar.MILLISECOND, millis.millis)
93+
}
94+
}
95+
96+
internal operator fun Calendar.plusAssign(millis: Millis) {
97+
add(Calendar.MILLISECOND, millis.millis)
98+
}
99+
100+
internal operator fun Calendar.minus(millis: Millis): Calendar {
101+
return copy().apply {
102+
add(Calendar.MILLISECOND, millis.millis * (-1))
103+
}
104+
}
105+
106+
internal operator fun Calendar.minusAssign(millis: Millis) {
107+
add(Calendar.MILLISECOND, millis.millis * (-1))
108+
}
54109

55110
internal fun Calendar.isBefore(other: Calendar) = timeInMillis < other.timeInMillis
56111

@@ -131,7 +186,7 @@ internal fun firstDayOfYear(): Calendar {
131186
}
132187

133188
internal fun getDateRange(start: Int, end: Int): List<Calendar> {
134-
return (start..end).map { today().plusDays(it - 1) }
189+
return (start..end).map { today() + Days(it - 1) }
135190
}
136191

137192
internal val Calendar.isWeekend: Boolean
@@ -177,7 +232,8 @@ internal fun Calendar.copy(): Calendar = clone() as Calendar
177232
*/
178233
internal fun Calendar.isAtStartOfNextDay(startDate: Calendar): Boolean {
179234
return if (isEqual(atStartOfDay)) {
180-
minusMillis(1).isSameDate(startDate)
235+
val endOfPreviousDay = this - Millis(1)
236+
endOfPreviousDay.isSameDate(startDate)
181237
} else {
182238
false
183239
}

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import android.content.Context
44
import android.graphics.Typeface
55
import android.text.SpannableStringBuilder
66
import android.text.StaticLayout
7-
import android.text.TextPaint
87
import android.text.TextUtils
98
import android.text.TextUtils.TruncateAt
109
import android.text.style.StyleSpan
@@ -111,14 +110,10 @@ internal class TextFitter<T>(
111110
do {
112111
// The text doesn't fit into the chip, so we need to gradually
113112
// reduce its size until it does
114-
textPaint.reduceSize()
113+
textPaint.textSize -= 1
115114
textLayout = TextLayoutBuilder.build(text, textPaint, width)
116115
} while (availableHeight < textLayout.height)
117116

118117
return textLayout
119118
}
120-
121-
private fun TextPaint.reduceSize() {
122-
textSize -= 1
123-
}
124119
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,8 @@ class WeekView<T : Any> @JvmOverloads constructor(
181181
val daysScrolled = configWrapper.currentOrigin.x / totalDayWidth
182182
val delta = daysScrolled.roundToInt() * (-1)
183183

184-
val firstVisibleDate = today().plusDays(delta)
185-
val lastVisibleDate = firstVisibleDate.plusDays(visibleDays - 1)
184+
val firstVisibleDate = today() + Days(delta)
185+
val lastVisibleDate = firstVisibleDate + Days(visibleDays - 1)
186186

187187
viewState.firstVisibleDate = firstVisibleDate
188188
viewState.lastVisibleDate = lastVisibleDate

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ internal class WeekViewConfig(
245245
SANS -> Typeface.SANS_SERIF
246246
SERIF -> Typeface.SERIF
247247
MONOSPACE -> Typeface.MONOSPACE
248-
else -> typeface
248+
else -> Typeface.DEFAULT
249249
}
250250
}
251251

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ internal class WeekViewConfigWrapper(
231231
val minX: Float
232232
get() {
233233
return maxDate?.let {
234-
val date = it.minusDays(numberOfVisibleDays - 1)
234+
val date = it - Days(numberOfVisibleDays - 1)
235235
getXOriginForDate(date)
236236
} ?: Float.NEGATIVE_INFINITY
237237
}
@@ -551,7 +551,7 @@ internal class WeekViewConfigWrapper(
551551

552552
val desired = if (now.hour > 0) {
553553
// Add some padding above the current time (and thus: the now line)
554-
now.minusHours(1)
554+
now - Hours(1)
555555
} else {
556556
now.atStartOfDay
557557
}
@@ -580,10 +580,10 @@ internal class WeekViewConfigWrapper(
580580
return if (date.isBefore(minDate)) {
581581
minDate
582582
} else if (date.isAfter(maxDate)) {
583-
maxDate.plusDays(1 - numberOfVisibleDays)
583+
maxDate + Days(1 - numberOfVisibleDays)
584584
} else if (numberOfVisibleDays >= 7 && showFirstDayOfWeekFirst) {
585585
val diff = computeDifferenceWithFirstDayOfWeek(date)
586-
date.minusDays(diff)
586+
date - Days(diff)
587587
} else {
588588
date
589589
}

0 commit comments

Comments
 (0)