Skip to content

Commit 69d4883

Browse files
authored
Add option for header row bottom shadow and tweak now line dot behavior (alamkanak#147)
1 parent 9bc3f03 commit 69d4883

File tree

9 files changed

+78
-18
lines changed

9 files changed

+78
-18
lines changed

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,15 @@ internal class HeaderRowDrawer<T : Any>(
1616
canvas: Canvas
1717
) {
1818
val width = view.width.toFloat()
19-
canvas.drawRect(0f, 0f, width, config.headerHeight, config.headerBackgroundPaint)
19+
20+
val backgroundPaint = if (config.showHeaderRowBottomShadow) {
21+
config.headerBackgroundPaint.withShadow(
22+
radius = config.headerRowBottomShadowRadius,
23+
color = config.headerRowBottomShadowColor
24+
)
25+
} else config.headerBackgroundPaint
26+
27+
canvas.drawRect(0f, 0f, width, config.headerHeight, backgroundPaint)
2028

2129
if (config.showWeekNumber) {
2230
canvas.drawWeekNumber(drawingContext)
@@ -57,11 +65,17 @@ internal class HeaderRowDrawer<T : Any>(
5765
}
5866
}
5967

60-
internal val Paint.textHeight: Int
68+
private val Paint.textHeight: Int
6169
get() = (descent() - ascent()).roundToInt()
6270

63-
internal fun Paint.getTextBounds(text: String): Rect {
71+
private fun Paint.getTextBounds(text: String): Rect {
6472
val rect = Rect()
6573
getTextBounds(text, 0, text.length, rect)
6674
return rect
6775
}
76+
77+
private fun Paint.withShadow(radius: Int, color: Int): Paint {
78+
return Paint(this).apply {
79+
setShadowLayer(radius.toFloat(), 0f, 0f, color)
80+
}
81+
}

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ internal class NowLineDrawer(
2222
.map { (_, startPixel) -> startPixel }
2323
.firstOrNull() ?: return
2424

25-
drawLine(startPixel, canvas)
25+
canvas.drawLine(startPixel)
2626
}
2727

28-
private fun drawLine(startPixel: Float, canvas: Canvas) {
28+
private fun Canvas.drawLine(startPixel: Float) {
2929
val top = config.headerHeight + config.currentOrigin.y
3030
val now = now()
3131

@@ -35,17 +35,24 @@ internal class NowLineDrawer(
3535

3636
val startX = max(startPixel, config.timeColumnWidth)
3737
val endX = startPixel + config.totalDayWidth
38-
canvas.drawLine(startX, verticalOffset, endX, verticalOffset, config.nowLinePaint)
38+
drawLine(startX, verticalOffset, endX, verticalOffset, config.nowLinePaint)
3939

4040
if (config.showNowLineDot) {
41-
drawDot(startPixel, verticalOffset, canvas)
41+
drawDot(startPixel, verticalOffset)
4242
}
4343
}
4444

45-
private fun drawDot(startPixel: Float, lineStartY: Float, canvas: Canvas) {
46-
// We use a margin to prevent the dot from sticking on the left side of the screen
45+
private fun Canvas.drawDot(startPixel: Float, lineStartY: Float) {
4746
val dotRadius = config.nowDotPaint.strokeWidth
48-
val dotMargin = 32f
49-
canvas.drawCircle(startPixel + dotMargin, lineStartY, dotRadius, config.nowDotPaint)
47+
val actualStartPixel = max(startPixel, config.timeColumnWidth)
48+
49+
val fullLineWidth = config.totalDayWidth
50+
val actualEndPixel = startPixel + fullLineWidth
51+
52+
val currentlyDisplayedWidth = actualEndPixel - actualStartPixel
53+
val currentlyDisplayedPortion = currentlyDisplayedWidth / fullLineWidth
54+
55+
val adjustedRadius = currentlyDisplayedPortion * dotRadius
56+
drawCircle(actualStartPixel, lineStartY, adjustedRadius, config.nowDotPaint)
5057
}
5158
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ class WeekView<T : Any> @JvmOverloads constructor(
8585
if (isAccessibilityHelperActive) {
8686
ViewCompat.setAccessibilityDelegate(this, accessibilityTouchHelper)
8787
}
88+
89+
setLayerType(LAYER_TYPE_SOFTWARE, null)
8890
}
8991

9092
// Be careful when changing the order of the drawers, as that might cause
@@ -93,11 +95,11 @@ class WeekView<T : Any> @JvmOverloads constructor(
9395
DayBackgroundDrawer(this, configWrapper),
9496
BackgroundGridDrawer(this, configWrapper),
9597
SingleEventsDrawer(context, configWrapper, eventChipCache),
96-
NowLineDrawer(configWrapper),
9798
TimeColumnDrawer(this, configWrapper),
9899
HeaderRowDrawer(this, configWrapper),
99100
DayLabelDrawer(configWrapper, cache),
100-
AllDayEventsDrawer(context, configWrapper, cache)
101+
AllDayEventsDrawer(context, configWrapper, cache),
102+
NowLineDrawer(configWrapper)
101103
)
102104

103105
override fun onDraw(canvas: Canvas) {

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ internal class WeekViewConfig(
2323
var headerRowBottomLineColor: Int = 0
2424
var headerRowBottomLineWidth: Int = 0
2525

26+
// Header bottom shadow
27+
var showHeaderRowBottomShadow: Boolean = false
28+
var headerRowBottomShadowColor: Int = 0
29+
var headerRowBottomShadowRadius: Int = 0
30+
2631
// Time column
2732
var timeColumnTextColor: Int = 0
2833
var timeColumnBackgroundColor: Int = 0
@@ -139,6 +144,11 @@ internal class WeekViewConfig(
139144
headerRowBottomLineColor = getColor(R.styleable.WeekView_headerRowBottomLineColor, Defaults.GRID_COLOR)
140145
headerRowBottomLineWidth = getDimensionPixelSize(R.styleable.WeekView_headerRowBottomLineWidth, 1)
141146

147+
// Header bottom shadow
148+
showHeaderRowBottomShadow = a.getBoolean(R.styleable.WeekView_showHeaderRowBottomShadow, false)
149+
headerRowBottomShadowColor = a.getColor(R.styleable.WeekView_headerRowBottomShadowColor, Color.LTGRAY)
150+
headerRowBottomShadowRadius = a.getDimensionPixelSize(R.styleable.WeekView_headerRowBottomShadowRadius, 2)
151+
142152
// Time column
143153
timeColumnTextColor = getColor(R.styleable.WeekView_timeColumnTextColor, Color.BLACK)
144154
timeColumnBackgroundColor = getColor(R.styleable.WeekView_timeColumnBackgroundColor, Color.WHITE)

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,24 @@ internal class WeekViewConfigWrapper(
330330
val headerRowBottomLineWidth: Float
331331
get() = if (showHeaderRowBottomLine) headerRowBottomLinePaint.strokeWidth else 0f
332332

333+
var showHeaderRowBottomShadow: Boolean
334+
get() = config.showHeaderRowBottomShadow
335+
set(value) {
336+
config.showHeaderRowBottomShadow = value
337+
}
338+
339+
var headerRowBottomShadowColor: Int
340+
get() = config.headerRowBottomShadowColor
341+
set(value) {
342+
config.headerRowBottomShadowColor = value
343+
}
344+
345+
var headerRowBottomShadowRadius: Int
346+
get() = config.headerRowBottomShadowRadius
347+
set(value) {
348+
config.headerRowBottomShadowRadius = value
349+
}
350+
333351
var showWeekNumber: Boolean
334352
get() = config.showWeekNumber
335353
set(value) {

core/src/main/res/values/attrs.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
<attr name="headerRowBottomLineColor" format="color" />
2424
<attr name="headerRowBottomLineWidth" format="dimension" />
2525

26+
<!-- Header bottom shadow -->
27+
<attr name="showHeaderRowBottomShadow" format="boolean" />
28+
<attr name="headerRowBottomShadowColor" format="color" />
29+
<attr name="headerRowBottomShadowRadius" format="dimension" />
30+
2631
<!-- Time column -->
2732
<attr name="timeColumnTextColor" format="color" />
2833
<attr name="timeColumnBackgroundColor" format="color" />

sample/src/main/res/layout/activity_basic.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
app:eventPaddingHorizontal="6dp"
2020
app:eventPaddingVertical="2dp"
2121
app:eventTextColor="@android:color/white"
22-
app:headerRowBottomLineColor="@color/header_bottom_line"
23-
app:headerRowBottomLineWidth="1dp"
22+
app:headerRowBottomShadowColor="@color/light_gray"
23+
app:headerRowBottomShadowRadius="4dp"
2424
app:headerRowPadding="12dp"
2525
app:hourHeight="60dp"
2626
app:nowLineColor="@color/accent"
@@ -30,7 +30,7 @@
3030
app:numberOfVisibleDays="3"
3131
app:overlappingEventGap="1dp"
3232
app:showCurrentTimeFirst="true"
33-
app:showHeaderRowBottomLine="true"
33+
app:showHeaderRowBottomShadow="true"
3434
app:showNowLine="true"
3535
app:showNowLineDot="true"
3636
app:showTimeColumnSeparator="true"
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<com.google.android.material.appbar.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
34
android:layout_width="match_parent"
4-
android:layout_height="wrap_content">
5+
android:layout_height="wrap_content"
6+
app:elevation="0dp">
57

68
<com.google.android.material.appbar.MaterialToolbar
79
android:id="@+id/toolbar"
810
android:layout_width="match_parent"
9-
android:layout_height="wrap_content" />
11+
android:layout_height="wrap_content"
12+
android:background="@android:color/white" />
1013

1114
</com.google.android.material.appbar.AppBarLayout>

sample/src/main/res/values/colors.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@
1010
<color name="accent">#2789e4</color>
1111
<color name="divider">#E2E2E2</color>
1212
<color name="dark_gray">#aeaeae</color>
13+
<color name="light_gray">#CCCCCC</color>
1314
</resources>

0 commit comments

Comments
 (0)