Skip to content

Commit 4308eb5

Browse files
committed
manually define the default start and end time via xml
1 parent 9f1c434 commit 4308eb5

File tree

8 files changed

+66
-54
lines changed

8 files changed

+66
-54
lines changed

app/src/main/res/layout/activity_sample.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
android:id="@+id/week_view"
1616
android:layout_width="match_parent"
1717
android:layout_height="wrap_content"
18-
app:accent_color="@color/colorAccent">
18+
app:accent_color="@color/colorAccent"
19+
app:end_hour="15"
20+
app:start_hour="8">
1921

2022
<TextView
2123
android:layout_width="wrap_content"

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ buildscript {
1717
}
1818

1919
plugins {
20-
id "org.jlleitschuh.gradle.ktlint" version "11.0.0"
20+
id "org.jlleitschuh.gradle.ktlint" version "11.6.1"
2121
}
2222

2323
subprojects {

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,19 @@ import android.content.SharedPreferences
44

55
/**
66
* Persists the WeekViewConfig.
7+
* This makes it possible to keep the same zoom level across views and sessions.
78
*
89
* Created by Tobias Schürg on 01.03.2018.
910
*/
10-
class WeekViewConfig(val prefs: SharedPreferences) {
11+
class WeekViewConfig(private val prefs: SharedPreferences) {
1112

12-
private val SCALING_FACTOR = "scaling_facor"
13-
14-
var scalingFactor: Float = prefs.getFloat(SCALING_FACTOR, 1f)
13+
var scalingFactor: Float = prefs.getFloat(PREFS_KEY_SCALING_FACTOR, 1f)
1514
set(value) {
1615
field = value
17-
prefs.edit().putFloat(SCALING_FACTOR, value).apply()
16+
prefs.edit().putFloat(PREFS_KEY_SCALING_FACTOR, value).apply()
1817
}
18+
19+
companion object {
20+
private const val PREFS_KEY_SCALING_FACTOR = "awv_scaling_factor"
21+
}
1922
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ internal object DayOfWeekUtil {
3030
}
3131

3232
when (firstDayOfTheWeek) {
33-
3433
DayOfWeek.MONDAY -> {
3534
// mo: 0, fr:4, su:6
3635
val column = day.value

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

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import de.tobiasschuerg.weekview.util.ViewHelper
2020
import de.tobiasschuerg.weekview.util.dipToPixelF
2121
import de.tobiasschuerg.weekview.util.dipToPixelI
2222
import de.tobiasschuerg.weekview.util.toLocalString
23+
import kotlin.math.roundToInt
2324

2425
/** this view is only constructed during runtime. */
2526
@SuppressLint("ViewConstructor")
@@ -31,8 +32,8 @@ class EventView(
3132

3233
) : View(context) {
3334

34-
private val TAG = javaClass.simpleName
35-
private val CORNER_RADIUS_PX = context.dipToPixelF(2f)
35+
private val tag = javaClass.simpleName
36+
private val cornerRadiusPx = context.dipToPixelF(2f)
3637

3738
private val textPaint: Paint by lazy { Paint().apply { isAntiAlias = true } }
3839

@@ -54,15 +55,15 @@ class EventView(
5455

5556
background = PaintDrawable().apply {
5657
paint.color = event.backgroundColor
57-
setCornerRadius(CORNER_RADIUS_PX)
58+
setCornerRadius(cornerRadiusPx)
5859
}
5960

6061
/** Calculate weights above & below. */
61-
if (config.showTimeStart) weightStartTime = 1 else weightStartTime = 0
62-
if (config.showUpperText) weightUpperText = 1 else weightUpperText = 0
63-
if (config.showSubtitle) weightSubTitle = 1 else weightSubTitle = 0
64-
if (config.showLowerText) weightLowerText = 1 else weightLowerText = 0
65-
if (config.showTimeEnd) weightEndTime = 1 else weightEndTime = 0
62+
weightStartTime = if (config.showTimeStart) 1 else 0
63+
weightUpperText = if (config.showUpperText) 1 else 0
64+
weightSubTitle = if (config.showSubtitle) 1 else 0
65+
weightLowerText = if (config.showLowerText) 1 else 0
66+
weightEndTime = if (config.showTimeEnd) 1 else 0
6667

6768
weightSum = weightStartTime + weightUpperText + weightSubTitle + weightLowerText + weightEndTime + weightTitle
6869

@@ -71,14 +72,14 @@ class EventView(
7172

7273
// TODO: clean up
7374
override fun onDraw(canvas: Canvas) {
74-
Log.d(TAG, "Drawing ${event.title}")
75+
Log.d(tag, "Drawing ${event.title}")
7576

7677
// only for debugging
7778
if (Debug.isDebuggerConnected()) {
7879
for (i in 0..weightSum) {
7980
val content = height - (paddingTop + paddingBottom)
8081
val y = content * i / weightSum + paddingTop
81-
canvas.drawLine(0f, y.toFloat(), canvas.width.toFloat(), y.toFloat(), textPaint)
82+
canvas.drawLine(0f, y.toFloat(), width.toFloat(), y.toFloat(), textPaint)
8283
}
8384
}
8485

@@ -94,7 +95,9 @@ class EventView(
9495
canvas.drawText(eventName, (width / 2 - textBounds.centerX()).toFloat(), subjectY.toFloat(), textPaint)
9596

9697
textPaint.textSize = TextHelper.fitText(
97-
"123456", maxTextSize, width / 2,
98+
"123456",
99+
maxTextSize,
100+
width / 2,
98101
getY(position = 1, bounds = textBounds) - getY(position = 0, bounds = textBounds)
99102
)
100103

@@ -137,16 +140,16 @@ class EventView(
137140
private fun getY(position: Int, weight: Int = 1, bounds: Rect): Int {
138141
val content = height - (paddingTop + paddingBottom)
139142
val y = (content * (position + 0.5f * weight) / weightSum) + paddingTop
140-
return Math.round(y) - bounds.centerY()
143+
return y.roundToInt() - bounds.centerY()
141144
}
142145

143146
private var measureCount = 0
144147
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
145-
Log.v(TAG, "Measuring ${event.title} ${measureCount++}")
148+
Log.v(tag, "Measuring ${event.title} ${measureCount++}")
146149
if (BuildConfig.DEBUG) {
147150
val debugWidth = ViewHelper.debugMeasureSpec(widthMeasureSpec)
148151
val debugHeight = ViewHelper.debugMeasureSpec(heightMeasureSpec)
149-
Log.v(TAG, "-> width: $debugWidth\n-> height: $debugHeight")
152+
Log.v(tag, "-> width: $debugWidth\n-> height: $debugHeight")
150153
}
151154

152155
val desiredHeightDp = event.duration.toMinutes() * scalingFactor
@@ -157,17 +160,21 @@ class EventView(
157160
}
158161

159162
override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
160-
Log.d(TAG, "Laying out ${event.title}: changed[$changed] ($left, $top),($right, $bottom)")
163+
Log.d(tag, "Laying out ${event.title}: changed[$changed] ($left, $top),($right, $bottom)")
161164
super.onLayout(changed, left, top, right, bottom)
162165
}
163166

164167
override fun onAttachedToWindow() {
165168
super.onAttachedToWindow()
166169
val anim = ScaleAnimation(
167-
0f, 1f, // Start and end values for the X axis scaling
168-
0f, 1f, // Start and end values for the Y axis scaling
169-
Animation.RELATIVE_TO_SELF, 0.5f, // Pivot point of X scaling
170-
Animation.RELATIVE_TO_SELF, 0.5f
170+
0f,
171+
1f, // Start and end values for the X axis scaling
172+
0f,
173+
1f, // Start and end values for the Y axis scaling
174+
Animation.RELATIVE_TO_SELF,
175+
0.5f, // Pivot point of X scaling
176+
Animation.RELATIVE_TO_SELF,
177+
0.5f
171178
) // Pivot point of Y scaling
172179
anim.fillAfter = true // Needed to keep the result of the animation
173180
anim.duration = 1000

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

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,19 @@ internal class WeekBackgroundView constructor(context: Context) : View(context)
5757
remove(DayOfWeek.SUNDAY)
5858
}
5959

60-
var startTime: LocalTime = LocalTime.of(10, 0)
61-
private set
62-
private var endTime: LocalTime = LocalTime.of(13, 0)
63-
6460
var scalingFactor = 1f
6561
/**
6662
* Updated the scaling factor and redraws the view.
6763
*/
6864
set(value) {
6965
field = value
7066
requestLayout()
71-
// invalidate()
67+
}
68+
69+
var defaultTimeSpan = TimeSpan.of(LocalTime.of(10, 0), Duration.ofHours(4))
70+
set(value) {
71+
field = value
72+
requestLayout()
7273
}
7374

7475
fun setAccentColor(color: Int) {
@@ -91,9 +92,9 @@ internal class WeekBackgroundView constructor(context: Context) : View(context)
9192
}
9293

9394
private fun drawNowIndicator(canvas: Canvas) {
94-
if (startTime.isBefore(LocalTime.now()) && endTime.isAfter(LocalTime.now())) {
95+
if (defaultTimeSpan.start.isBefore(LocalTime.now()) && defaultTimeSpan.endExclusive.isAfter(LocalTime.now())) {
9596
Log.v(TAG, "Drawing now indicator")
96-
val nowOffset = Duration.between(startTime, LocalTime.now())
97+
val nowOffset = Duration.between(defaultTimeSpan.start, LocalTime.now())
9798

9899
val minutes = nowOffset.toMinutes()
99100
val y = topOffsetPx + context.dipToPixelF(minutes * scalingFactor)
@@ -104,10 +105,10 @@ internal class WeekBackgroundView constructor(context: Context) : View(context)
104105

105106
private fun Canvas.drawHorizontalDividers() {
106107
Log.d(TAG, "Drawing horizontal dividers")
107-
var localTime = startTime
108+
var localTime = defaultTimeSpan.start
108109
var last = LocalTime.MIN
109-
while (localTime.isBefore(endTime) && !last.isAfter(localTime)) {
110-
val offset = Duration.between(startTime, localTime)
110+
while (localTime.isBefore(defaultTimeSpan.endExclusive) && !last.isAfter(localTime)) {
111+
val offset = Duration.between(defaultTimeSpan.start, localTime)
111112
Log.v(TAG, "Offset $offset")
112113
val y = topOffsetPx + context.dipToPixelF(offset.toMinutes() * scalingFactor)
113114
drawLine(0f, y, width.toFloat(), y, paintDivider)
@@ -125,7 +126,7 @@ internal class WeekBackgroundView constructor(context: Context) : View(context)
125126
last = localTime
126127
localTime = localTime.plusHours(1)
127128
}
128-
val offset = Duration.between(startTime, localTime)
129+
val offset = Duration.between(defaultTimeSpan.start, localTime)
129130
Log.v(TAG, "Offset + $offset")
130131
drawLine(0f, bottom.toFloat(), width.toFloat(), bottom.toFloat(), paintDivider)
131132
}
@@ -216,28 +217,20 @@ internal class WeekBackgroundView constructor(context: Context) : View(context)
216217
}
217218

218219
fun updateTimes(timeSpan: TimeSpan) {
219-
var timesHaveChanged = false
220-
if (timeSpan.start.isBefore(startTime)) {
221-
startTime = timeSpan.start.truncatedTo(ChronoUnit.HOURS)
222-
timesHaveChanged = true
220+
if (timeSpan.start.isBefore(defaultTimeSpan.start)) {
221+
defaultTimeSpan = defaultTimeSpan.copy(start = timeSpan.start.truncatedTo(ChronoUnit.HOURS))
223222
}
224-
if (timeSpan.endExclusive.isAfter(endTime)) {
225-
if (timeSpan.endExclusive.isBefore(LocalTime.of(23, 0))) {
226-
this.endTime = timeSpan.endExclusive.truncatedTo(ChronoUnit.HOURS).plusHours(1)
223+
if (timeSpan.endExclusive.isAfter(defaultTimeSpan.endExclusive)) {
224+
defaultTimeSpan = if (timeSpan.endExclusive.isBefore(LocalTime.of(23, 0))) {
225+
defaultTimeSpan.copy(endExclusive = timeSpan.endExclusive.truncatedTo(ChronoUnit.HOURS).plusHours(1))
227226
} else {
228-
this.endTime = LocalTime.MAX
227+
defaultTimeSpan.copy(endExclusive = LocalTime.MAX)
229228
}
230-
timesHaveChanged = true
231-
}
232-
if (this.startTime.isAfter(this.endTime)) throw IllegalArgumentException()
233-
234-
if (timesHaveChanged) {
235-
requestLayout()
236229
}
237230
}
238231

239232
private fun getDurationMinutes(): Long {
240-
return Duration.between(startTime, endTime).toMinutes()
233+
return Duration.between(defaultTimeSpan.start, defaultTimeSpan.endExclusive).toMinutes()
241234
}
242235

243236
companion object {

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import de.tobiasschuerg.weekview.data.WeekData
1717
import de.tobiasschuerg.weekview.data.WeekViewConfig
1818
import de.tobiasschuerg.weekview.util.Animation
1919
import de.tobiasschuerg.weekview.util.DayOfWeekUtil
20+
import de.tobiasschuerg.weekview.util.TimeSpan
2021
import de.tobiasschuerg.weekview.util.dipToPixelF
2122
import org.threeten.bp.DayOfWeek
2223
import org.threeten.bp.Duration
@@ -48,12 +49,15 @@ class WeekView(context: Context, attributeSet: AttributeSet) :
4849
init {
4950
val arr = context.obtainStyledAttributes(attributeSet, R.styleable.WeekView)
5051
accentColor = arr.getColor(R.styleable.WeekView_accent_color, Color.BLUE)
52+
val start = arr.getInt(R.styleable.WeekView_start_hour, 10)
53+
val end = arr.getInt(R.styleable.WeekView_end_hour, 14)
5154
arr.recycle() // Do this when done.
5255

5356
val prefs = context.getSharedPreferences("ts_week_view", Context.MODE_PRIVATE)
5457
weekViewConfig = WeekViewConfig(prefs)
5558

5659
backgroundView = WeekBackgroundView(context)
60+
backgroundView.defaultTimeSpan = TimeSpan(LocalTime.of(start, 0), LocalTime.of(end, 0))
5761
backgroundView.setAccentColor(accentColor)
5862
backgroundView.scalingFactor = weekViewConfig.scalingFactor
5963

@@ -143,6 +147,7 @@ class WeekView(context: Context, attributeSet: AttributeSet) :
143147
backgroundView.days.add(DayOfWeek.SATURDAY)
144148
}
145149
}
150+
146151
DayOfWeek.SUNDAY -> {
147152
Log.i(TAG, "Enabling Saturday")
148153
if (!backgroundView.days.contains(DayOfWeek.SATURDAY)) {
@@ -153,6 +158,7 @@ class WeekView(context: Context, attributeSet: AttributeSet) :
153158
backgroundView.days.add(DayOfWeek.SUNDAY)
154159
}
155160
}
161+
156162
else -> {
157163
// nothing to do, just add the event
158164
}
@@ -243,7 +249,7 @@ class WeekView(context: Context, attributeSet: AttributeSet) :
243249
}
244250

245251
eventView.scalingFactor = weekViewConfig.scalingFactor
246-
val startTime = backgroundView.startTime
252+
val startTime = backgroundView.defaultTimeSpan.start
247253
val lessonStart = eventView.event.timeSpan.start
248254
val offset = Duration.between(startTime, lessonStart)
249255

library/src/main/res/values/attr.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@
22
<resources>
33
<declare-styleable name="WeekView">
44
<attr name="accent_color" format="color" />
5+
<attr name="start_hour" format="integer" />
6+
<attr name="end_hour" format="integer" />
57
</declare-styleable>
68
</resources>

0 commit comments

Comments
 (0)