Skip to content

Commit 7cb7cd8

Browse files
-added a way to put text on the left of the all-day events.
-Minor bugs fixes -Minor optimizations to make drawing and scrolling smoother
1 parent ae17dee commit 7cb7cd8

File tree

7 files changed

+76
-54
lines changed

7 files changed

+76
-54
lines changed

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

Lines changed: 64 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -77,23 +77,16 @@ class WeekView @JvmOverloads constructor(context: Context, attrs: AttributeSet?
7777
mCurrentOrigin.x = 0f
7878
invalidate()
7979
}
80-
private val mTimeTextPaint: Paint = Paint(Paint.ANTI_ALIAS_FLAG)
81-
private var mTimeTextWidth: Float = 0f
82-
private var mTimeTextHeight: Float = 0f
80+
private val mTimeTextPaint = TextPaint(Paint.ANTI_ALIAS_FLAG)
8381
private val mHeaderWeekDayTitleTextPaint = TextPaint(Paint.ANTI_ALIAS_FLAG)
84-
private var mHeaderWeekDayTitleTextHeight: Float = 0f
85-
private var mHeaderHeight: Float = 0f
8682
private val mHeaderWeekDaySubtitleTextPaint = TextPaint(Paint.ANTI_ALIAS_FLAG)
87-
private var mHeaderWeekDaySubtitleTextHeight: Float = 0f
8883
private val mHeaderWeekDayTitleTodayTextPaint = TextPaint(Paint.ANTI_ALIAS_FLAG)
8984
private val mHeaderWeekDaySubtitleTodayTextPaint = TextPaint(Paint.ANTI_ALIAS_FLAG)
90-
private var mGestureDetector: GestureDetectorCompat? = null
91-
private var mScroller: OverScroller? = null
92-
private val mCurrentOrigin = PointF(0f, 0f)
93-
private var mCurrentScrollDirection = Direction.NONE
85+
private val mEventTextPaint = TextPaint(Paint.ANTI_ALIAS_FLAG or Paint.LINEAR_TEXT_FLAG)
86+
private val sideTitleTextPaint = TextPaint(Paint.ANTI_ALIAS_FLAG)
87+
private val allDaySideTitleTextPaint = TextPaint(Paint.ANTI_ALIAS_FLAG)
9488
private val mEmptyEventPaint = Paint()
9589
private val mHeaderBackgroundPaint: Paint = Paint()
96-
private var mWidthPerDay: Float = 0f
9790
private val mDayBackgroundPaint: Paint = Paint()
9891
private val mHourSeparatorPaint = Paint()
9992
private val mTodayColumnBackgroundPaint: Paint = Paint()
@@ -104,11 +97,20 @@ class WeekView @JvmOverloads constructor(context: Context, attrs: AttributeSet?
10497
private val mNowLinePaint = Paint()
10598
private val mEventBackgroundPaint = Paint()
10699
private val mNewEventBackgroundPaint = Paint()
100+
private var containsAllDayEvent: Boolean = false
101+
private var mTimeTextWidth: Float = 0f
102+
private var mTimeTextHeight: Float = 0f
103+
private var mHeaderWeekDayTitleTextHeight: Float = 0f
104+
private var mHeaderHeight: Float = 0f
105+
private var mHeaderWeekDaySubtitleTextHeight: Float = 0f
106+
private var mGestureDetector: GestureDetectorCompat? = null
107+
private var mScroller: OverScroller? = null
108+
private val mCurrentOrigin = PointF(0f, 0f)
109+
private var mCurrentScrollDirection = Direction.NONE
110+
private var mWidthPerDay: Float = 0f
107111
private var mHeaderColumnWidth: Float = 0f
108112
private var mEventRects: MutableList<EventRect>? = null
109113
private var mEvents: MutableList<WeekViewEvent>? = null
110-
private val mEventTextPaint = TextPaint(Paint.ANTI_ALIAS_FLAG or Paint.LINEAR_TEXT_FLAG)
111-
private val mHeaderColumnBackgroundPaint: Paint = Paint()
112114
private var mFetchedPeriod = -1 // the middle period the calendar has fetched.
113115
private var mRefreshEvents = false
114116
private var mCurrentFlingDirection = Direction.NONE
@@ -139,8 +141,6 @@ class WeekView @JvmOverloads constructor(context: Context, attrs: AttributeSet?
139141
private var mDistanceMin: Float = 0f
140142
private var mOffsetValueToSecureScreen = 9
141143
private var mStartOriginForScroll = 0f
142-
143-
// Attributes and their default values.
144144
private var mNewHourHeight = -1
145145
var minHourHeight = 0
146146
//no minimum specified (will be dynamic, based on screen)
@@ -152,7 +152,6 @@ class WeekView @JvmOverloads constructor(context: Context, attrs: AttributeSet?
152152
var newEventLengthInMinutes = 60
153153
var newEventTimeResolutionInMinutes = 15
154154
var isShowFirstDayOfWeekFirst = false
155-
156155
private var mIsFirstDraw = true
157156
private var mAreDimensionsInvalid = true
158157
/**
@@ -176,7 +175,7 @@ class WeekView @JvmOverloads constructor(context: Context, attrs: AttributeSet?
176175
/**
177176
* the height of AllDay-events.
178177
*/
179-
var allDayEventHeight = 100
178+
var allDayEventHeight = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40f, resources.displayMetrics).toInt()
180179
/**
181180
* If you set this to false the `zoomFocusPoint` won't take effect any more while zooming.
182181
* The zoom will always be focused at the center of your gesture.
@@ -193,6 +192,13 @@ class WeekView @JvmOverloads constructor(context: Context, attrs: AttributeSet?
193192
init()
194193
}
195194
}
195+
var allDaySideTitleText: String? = null
196+
set(value) {
197+
if (value == field)
198+
return
199+
field = value
200+
invalidate()
201+
}
196202

197203
private var mMinTime = 0
198204
private var mMaxTime = 24
@@ -245,8 +251,6 @@ class WeekView @JvmOverloads constructor(context: Context, attrs: AttributeSet?
245251
field = value
246252
invalidate()
247253
}
248-
private val sideTitleTextPaint = Paint(Paint.ANTI_ALIAS_FLAG)
249-
250254

251255
private val mGestureListener = object : GestureDetector.SimpleOnGestureListener() {
252256
override fun onDown(e: MotionEvent): Boolean {
@@ -633,7 +637,7 @@ class WeekView @JvmOverloads constructor(context: Context, attrs: AttributeSet?
633637
return
634638
field = value
635639
mTimeTextPaint.textSize = value
636-
sideTitleTextPaint.textSize = value
640+
allDaySideTitleTextPaint.textSize = value
637641
invalidate()
638642
}
639643

@@ -678,9 +682,21 @@ class WeekView @JvmOverloads constructor(context: Context, attrs: AttributeSet?
678682
mHeaderWeekDaySubtitleTextPaint.color = value
679683
mTimeTextPaint.color = value
680684
sideTitleTextPaint.color = value
685+
allDaySideTitleTextPaint.color = value
686+
invalidate()
687+
}
688+
689+
@ColorInt
690+
var allDaySideTitleTextColor = headerColumnTextColor
691+
set(value) {
692+
if (field == value)
693+
return
694+
field = value
695+
allDaySideTitleTextPaint.color = value
681696
invalidate()
682697
}
683698

699+
684700
@ColorInt
685701
var headerRowBackgroundColor: Int = Color.WHITE
686702
set(value) {
@@ -768,16 +784,6 @@ class WeekView @JvmOverloads constructor(context: Context, attrs: AttributeSet?
768784
invalidate()
769785
}
770786

771-
@ColorInt
772-
var headerColumnBackgroundColor: Int = Color.WHITE
773-
set(value) {
774-
if (field == value)
775-
return
776-
field = value
777-
mHeaderColumnBackgroundPaint.color = value
778-
invalidate()
779-
}
780-
781787
@ColorInt
782788
var defaultEventColor: Int = 0
783789
set(value) {
@@ -1030,7 +1036,6 @@ class WeekView @JvmOverloads constructor(context: Context, attrs: AttributeSet?
10301036
newEventLengthInMinutes = a.getInt(R.styleable.WeekView_newEventLengthInMinutes, newEventLengthInMinutes)
10311037
newEventTimeResolutionInMinutes = a.getInt(R.styleable.WeekView_newEventTimeResolutionInMinutes, newEventTimeResolutionInMinutes)
10321038
eventPadding = a.getDimensionPixelSize(R.styleable.WeekView_eventPadding, eventPadding)
1033-
headerColumnBackgroundColor = a.getColor(R.styleable.WeekView_headerColumnBackground, headerColumnBackgroundColor)
10341039
dayNameLength = a.getInteger(R.styleable.WeekView_dayNameLength, dayNameLength)
10351040
overlappingEventGap = a.getDimensionPixelSize(R.styleable.WeekView_overlappingEventGap, overlappingEventGap)
10361041
spaceBetweenWeekDaysAndAllDayEvents = a.getDimensionPixelSize(R.styleable.WeekView_spaceBetweenWeekDaysAndAllDayEvents, spaceBetweenWeekDaysAndAllDayEvents)
@@ -1063,6 +1068,7 @@ class WeekView @JvmOverloads constructor(context: Context, attrs: AttributeSet?
10631068
mHeaderWeekDayTitleTextPaint.textAlign = Paint.Align.CENTER
10641069
mHeaderWeekDaySubtitleTextPaint.textAlign = Paint.Align.CENTER
10651070
sideTitleTextPaint.textAlign = Paint.Align.CENTER
1071+
allDaySideTitleTextPaint.textAlign = Paint.Align.CENTER
10661072
mTimeTextPaint.textAlign = Paint.Align.RIGHT
10671073
mEventTextPaint.style = Paint.Style.FILL
10681074
mHourSeparatorPaint.style = Paint.Style.STROKE
@@ -1095,10 +1101,14 @@ class WeekView @JvmOverloads constructor(context: Context, attrs: AttributeSet?
10951101
initTextTimeWidth()
10961102

10971103
//handle sideTitleTextPaint
1098-
sideTitleTextPaint.textSize = textSize
10991104
sideTitleTextPaint.color = headerColumnTextColor
11001105
sideTitleTextPaint.typeface = typeface
11011106

1107+
//handle allDaySideTitleTextPaint
1108+
allDaySideTitleTextPaint.textSize = textSize
1109+
allDaySideTitleTextPaint.color = allDaySideTitleTextColor
1110+
allDaySideTitleTextPaint.typeface = typeface
1111+
11021112
// Measure settings for header row.
11031113
//TODO measure the text that will actually be used, based on the locale and dates. Important because various characters might look different.
11041114
val sampleText = "ABCDEFGHIKLMNOPQRSTUVWXYZabcdefghiklmnopqrstuvwxyz0123456789"
@@ -1151,9 +1161,6 @@ class WeekView @JvmOverloads constructor(context: Context, attrs: AttributeSet?
11511161
// Prepare empty event background color.
11521162
mNewEventBackgroundPaint.color = Color.rgb(60, 147, 217)
11531163

1154-
// Prepare header column background color.
1155-
mHeaderColumnBackgroundPaint.color = headerColumnBackgroundColor
1156-
11571164
// Prepare event text size and color.
11581165
mEventTextPaint.color = eventTextColor
11591166
mEventTextPaint.textSize = eventTextSize
@@ -1229,20 +1236,20 @@ class WeekView @JvmOverloads constructor(context: Context, attrs: AttributeSet?
12291236

12301237
private fun calculateHeaderHeight() {
12311238
//Make sure the header is the right size (depends on AllDay events)
1232-
var containsAllDayEvent = false
1239+
containsAllDayEvent = false
12331240
if (mEventRects != null && mEventRects!!.size > 0) {
1241+
val day = firstVisibleDay!!.clone() as Calendar
12341242
for (dayNumber in 0 until realNumberOfVisibleDays) {
1235-
val day = firstVisibleDay!!.clone() as Calendar
1236-
day.add(Calendar.DATE, dayNumber)
12371243
for (i in mEventRects!!.indices) {
1238-
if (isSameDay(mEventRects!![i].event.startTime!!, day) && mEventRects!![i].event.isAllDay) {
1244+
val event = mEventRects!![i].event
1245+
if (isSameDay(event.startTime!!, day) && event.isAllDay) {
12391246
containsAllDayEvent = true
12401247
break
12411248
}
12421249
}
1243-
if (containsAllDayEvent) {
1250+
if (containsAllDayEvent)
12441251
break
1245-
}
1252+
day.add(Calendar.DATE, 1)
12461253
}
12471254
}
12481255
mHeaderHeight = if (containsAllDayEvent) {
@@ -1255,9 +1262,6 @@ class WeekView @JvmOverloads constructor(context: Context, attrs: AttributeSet?
12551262
}
12561263

12571264
private fun drawTimeColumnAndAxes(canvas: Canvas) {
1258-
// Draw the background color for the header column.
1259-
canvas.drawRect(0f, mHeaderHeight + weekDaysHeaderRowPadding * 2, mHeaderColumnWidth, height.toFloat(), mHeaderColumnBackgroundPaint)
1260-
canvas.restore()
12611265
canvas.save()
12621266
// Clip to paint in left column only.
12631267
canvas.clipRect(0.0f, mHeaderHeight + weekDaysHeaderRowPadding * 2, mHeaderColumnWidth, height.toFloat())
@@ -1490,6 +1494,7 @@ class WeekView @JvmOverloads constructor(context: Context, attrs: AttributeSet?
14901494
canvas.save()
14911495
canvas.clipRect(mHeaderColumnWidth, 0f, width.toFloat(), mHeaderHeight + weekDaysHeaderRowPadding * 2)
14921496

1497+
14931498
// Draw the header background.
14941499
if (enableDrawHeaderBackgroundOnlyOnWeekDays)
14951500
canvas.drawRect(0f, 0f, width.toFloat(), headerTitleAndSubtitleTextHeight + weekDaysHeaderRowPadding * 2, mHeaderBackgroundPaint)
@@ -1524,11 +1529,24 @@ class WeekView @JvmOverloads constructor(context: Context, attrs: AttributeSet?
15241529
canvas.drawText(subtitleText, startPixel + mWidthPerDay / 2, headerTitleAndSubtitleTextHeight + weekDaysHeaderRowPadding,
15251530
if (isToday) mHeaderWeekDaySubtitleTodayTextPaint else mHeaderWeekDaySubtitleTextPaint)
15261531
}
1527-
drawAllDayEvents(day, startPixel, canvas)
1532+
if (containsAllDayEvent)
1533+
drawAllDayEvents(day, startPixel, canvas)
15281534
startPixel += mWidthPerDay + columnGap
15291535
day.add(Calendar.DAY_OF_YEAR, 1)
15301536
}
15311537
}
1538+
canvas.restore()
1539+
//draw text on the left of the all-day events
1540+
if (containsAllDayEvent && !TextUtils.isEmpty(allDaySideTitleText)) {
1541+
canvas.save()
1542+
val headerTitleAndSubtitleTextHeight = mHeaderWeekDayTitleTextHeight + (if (isSubtitleHeaderEnabled) mHeaderWeekDaySubtitleTextHeight + spaceBetweenHeaderWeekDayTitleAndSubtitle else 0.0f)
1543+
val weekDaysHeight = headerTitleAndSubtitleTextHeight + weekDaysHeaderRowPadding * 2
1544+
val top = weekDaysHeight + spaceBetweenWeekDaysAndAllDayEvents + mTimeTextHeight / 2
1545+
val bottom = top + allDayEventHeight
1546+
canvas.clipRect(0f, 0f, top, bottom)
1547+
canvas.drawText(allDaySideTitleText, mHeaderColumnWidth / 2, (top+bottom)/2, allDaySideTitleTextPaint)
1548+
canvas.restore()
1549+
}
15321550
}
15331551

15341552
/**
@@ -1647,7 +1665,7 @@ class WeekView @JvmOverloads constructor(context: Context, attrs: AttributeSet?
16471665
* @param canvas The canvas to draw upon.
16481666
*/
16491667
private fun drawAllDayEvents(date: Calendar, startFromPixel: Float, canvas: Canvas) {
1650-
if (mEventRects != null && mEventRects!!.size > 0) {
1668+
if (mEventRects != null && mEventRects!!.isNotEmpty()) {
16511669
val headerTitleAndSubtitleTextHeight = mHeaderWeekDayTitleTextHeight + (if (isSubtitleHeaderEnabled) mHeaderWeekDaySubtitleTextHeight + spaceBetweenHeaderWeekDayTitleAndSubtitle else 0.0f)
16521670

16531671
for (i in mEventRects!!.indices) {

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
</attr>
2626
<attr name="futureBackgroundColor" format="color"/>
2727
<attr name="futureWeekendBackgroundColor" format="color"/>
28-
<attr name="headerColumnBackground" format="color"/>
2928
<attr name="headerColumnPadding" format="dimension"/>
3029
<attr name="headerColumnTextColor" format="color"/>
3130
<attr name="headerRowBackgroundColor" format="color"/>

sample/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
<activity
2525
android:name=".WholeViewSnappingActivity" android:label="@string/title_activity_whole_view_snap"
26-
android:screenOrientation="userLandscape" android:theme="@style/AppTheme.FullScreen"/>
26+
android:screenOrientation="reverseLandscape" android:theme="@style/AppTheme.FullScreen"/>
2727
</application>
2828

2929
</manifest>

sample/src/main/java/com/alamkanak/weekview/sample/MainActivity.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class MainActivity : AppCompatActivity() {
2424
buttonWholeViewSnap.setOnClickListener {
2525
startActivity(Intent(this@MainActivity, WholeViewSnappingActivity::class.java))
2626
}
27-
startActivity(Intent(this@MainActivity, WholeViewSnappingActivity::class.java))
28-
finish()
27+
// startActivity(Intent(this@MainActivity, WholeViewSnappingActivity::class.java))
28+
// finish()
2929
}
3030
}

sample/src/main/java/com/alamkanak/weekview/sample/WholeViewSnappingActivity.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.alamkanak.weekview.sample
22

3+
import android.graphics.RectF
34
import android.os.Bundle
45
import android.text.format.DateFormat
56
import android.util.TypedValue
@@ -9,6 +10,7 @@ import android.view.View
910
import com.alamkanak.weekview.DateTimeInterpreter
1011
import com.alamkanak.weekview.WeekDaySubtitleInterpreter
1112
import com.alamkanak.weekview.WeekView
13+
import com.alamkanak.weekview.WeekViewEvent
1214
import kotlinx.android.synthetic.main.activity_base.*
1315
import java.text.SimpleDateFormat
1416
import java.util.*
@@ -18,7 +20,6 @@ import java.util.*
1820
*/
1921
class WholeViewSnappingActivity : BasicActivity() {
2022
override fun onCreate(savedInstanceState: Bundle?) {
21-
// window.setBackgroundDrawable(ColorDrawable(0))
2223
super.onCreate(savedInstanceState)
2324

2425
weekView.isShowNowLine = true
@@ -29,6 +30,7 @@ class WholeViewSnappingActivity : BasicActivity() {
2930
weekView.hourSeparatorHeight = weekView.columnGap
3031
weekView.isScrollNumberOfVisibleDays = true
3132
weekView.dropListener = null
33+
weekView.allDaySideTitleText = getString(R.string.all_day)
3234
setDayViewType(TYPE_WEEK_VIEW)
3335
val cal = Calendar.getInstance()
3436
val currentHour = cal.get(Calendar.HOUR_OF_DAY) + cal.get(Calendar.MINUTE) / 60.0
@@ -47,6 +49,10 @@ class WholeViewSnappingActivity : BasicActivity() {
4749

4850
override fun interpretDate(date: Calendar): String = dateFormatTitle.format(date.time)
4951
}
52+
weekView.eventClickListener = object : WeekView.EventClickListener {
53+
override fun onEventClick(event: WeekViewEvent, eventRect: RectF) {
54+
}
55+
}
5056
}
5157

5258
override fun setupDateTimeInterpreter(shortDate: Boolean) {

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
xmlns:app="http://schemas.android.com/apk/res-auto"
44
android:layout_width="match_parent"
55
android:layout_height="match_parent"
6-
tools:context=".BaseActivity"
7-
android:background="#fff">
6+
tools:context=".BaseActivity">
87

98
<com.alamkanak.weekview.WeekView
109
android:id="@+id/weekView"
@@ -19,7 +18,6 @@
1918
app:headerRowBackgroundColor="@color/toolbar"
2019
app:dayBackgroundColor="#05000000"
2120
app:todayColumnBackgroundColor="#1848adff"
22-
app:headerColumnBackground="#ffffffff"
2321
app:todayHeaderTextColor="@color/accent"
2422
app:newEventTimeResolutionInMinutes="15"
2523
app:timeColumnResolution="60"

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@
1010
<string name="async_error">Could not download events from the internet</string>
1111
<string name="drag_me">Drag Me</string>
1212
<string name="title_activity_whole_view_snap">Whole view snap</string>
13+
<string name="all_day">All day</string>
1314
</resources>

0 commit comments

Comments
 (0)