Skip to content

Commit 36103a2

Browse files
committed
Fix issue where goToHour() would work incorrectly when minHour was set
1 parent e0a2c5e commit 36103a2

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,24 +1114,34 @@ class WeekView<T : Any> @JvmOverloads constructor(
11141114
/**
11151115
* Scrolls to a specific hour.
11161116
*
1117-
* @param hour The hour to scroll to in 24-hour format. Supported values are 0-24.
1117+
* @param hour The hour to scroll to, in 24-hour format. Supported values are 0-24.
1118+
*
1119+
* @throws IllegalArgumentException Throws exception if the provided hour is smaller than
1120+
* [minHour] or larger than [maxHour].
11181121
*/
11191122
override fun goToHour(hour: Int) {
11201123
if (viewState.areDimensionsInvalid) {
11211124
viewState.scrollToHour = hour
11221125
return
11231126
}
11241127

1125-
val modifiedHour = min(hour, configWrapper.hoursPerDay)
1126-
var verticalOffset = configWrapper.hourHeight * modifiedHour
1128+
if (hour !in configWrapper.timeRange) {
1129+
throw IllegalArgumentException(
1130+
"The provided hour ($hour) is outside of the set time range " +
1131+
"(${configWrapper.minHour}${configWrapper.maxHour})"
1132+
)
1133+
}
11271134

1128-
val dayHeight = configWrapper.totalDayHeight
1129-
val viewHeight = height.toDouble()
1135+
val hourHeight = configWrapper.hourHeight
1136+
val desiredOffset = hourHeight * (hour - configWrapper.minHour)
11301137

1131-
val desiredOffset = dayHeight - viewHeight
1132-
verticalOffset = min(desiredOffset.toFloat(), verticalOffset)
1138+
// We make sure that WeekView doesn't "over-scroll" by limiting the offset to the total day
1139+
// height minus the height of WeekView, which would result in scrolling all the way to the
1140+
// bottom.
1141+
val maxOffset = configWrapper.totalDayHeight - height
1142+
val finalOffset = min(maxOffset, desiredOffset)
11331143

1134-
configWrapper.currentOrigin.y = -verticalOffset
1144+
configWrapper.currentOrigin.y = finalOffset * (-1)
11351145
invalidate()
11361146
}
11371147

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import java.util.Calendar
1010
import kotlin.math.max
1111
import kotlin.math.min
1212

13-
private const val DAYS_PER_WEEK = 7
14-
1513
internal class WeekViewConfigWrapper(
1614
private val view: WeekView<*>,
1715
private val config: WeekViewConfig
@@ -254,6 +252,9 @@ internal class WeekViewConfigWrapper(
254252
config.maxHour = value
255253
}
256254

255+
val timeRange: IntRange
256+
get() = minHour..maxHour
257+
257258
var xScrollingSpeed: Float
258259
get() = config.xScrollingSpeed
259260
set(value) {

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,6 @@ data class WeekViewEvent<T> internal constructor(
198198
private var data: T? = null
199199
) {
200200

201-
// private val event = WeekViewEvent(data = data)
202-
203201
private var id: Long? = null
204202
private var title: TextResource? = null
205203
private var location: TextResource? = null

0 commit comments

Comments
 (0)