Skip to content

Commit 9523d1b

Browse files
handled auto-refreshing of the view every minute, to reflect changes of current time&date. You can test it by setting the time of the day to be 23:59, and wait a minute...
cleaned code a bit.
1 parent 3438aad commit 9523d1b

File tree

3 files changed

+84
-75
lines changed

3 files changed

+84
-75
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ buildscript {
77
google()
88
}
99
dependencies {
10-
classpath 'com.android.tools.build:gradle:3.2.0-alpha13'
10+
classpath 'com.android.tools.build:gradle:3.2.0-alpha14'
1111
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
1212

1313
// NOTE: Do not place your application dependencies here; they belong

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

Lines changed: 82 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import android.content.Context
44
import android.graphics.*
55
import android.graphics.drawable.BitmapDrawable
66
import android.graphics.drawable.Drawable
7+
import android.os.Handler
78
import android.support.v4.view.GestureDetectorCompat
89
import android.support.v4.view.ViewCompat
910
import android.support.v4.view.animation.FastOutLinearInInterpolator
@@ -27,6 +28,7 @@ import java.util.*
2728
* Website: http://alamkanak.github.io/
2829
*/
2930
class WeekView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : View(context, attrs, defStyleAttr) {
31+
//region fields and properties
3032
private var mHomeDate: Calendar? = null
3133
/**
3234
* Get the earliest day that can be displayed. Will return null if no minimum date is set.
@@ -113,7 +115,6 @@ class WeekView @JvmOverloads constructor(context: Context, attrs: AttributeSet?
113115
private var mEventRects: MutableList<EventRect>? = null
114116
private var mEvents: MutableList<WeekViewEvent>? = null
115117
private val mEventTextPaint = TextPaint(Paint.ANTI_ALIAS_FLAG or Paint.LINEAR_TEXT_FLAG)
116-
private val mNewEventTextPaint: TextPaint? = null
117118
private val mHeaderColumnBackgroundPaint: Paint = Paint()
118119
private var mFetchedPeriod = -1 // the middle period the calendar has fetched.
119120
private var mRefreshEvents = false
@@ -141,7 +142,7 @@ class WeekView @JvmOverloads constructor(context: Context, attrs: AttributeSet?
141142
private var mSizeOfWeekView: Float = 0f
142143
private var mDistanceDone = 0f
143144
private var mDistanceMin: Float = 0f
144-
protected var mOffsetValueToSecureScreen = 9
145+
private var mOffsetValueToSecureScreen = 9
145146
private var mStartOriginForScroll = 0f
146147

147148
// Attributes and their default values.
@@ -347,14 +348,14 @@ class WeekView @JvmOverloads constructor(context: Context, attrs: AttributeSet?
347348
when (mCurrentScrollDirection) {
348349
WeekView.Direction.NONE -> {
349350
// Allow scrolling only in one direction.
350-
if (Math.abs(distanceX) > Math.abs(distanceY)) {
351+
mCurrentScrollDirection = if (Math.abs(distanceX) > Math.abs(distanceY)) {
351352
if (distanceX > 0) {
352-
mCurrentScrollDirection = Direction.LEFT
353+
Direction.LEFT
353354
} else {
354-
mCurrentScrollDirection = Direction.RIGHT
355+
Direction.RIGHT
355356
}
356357
} else {
357-
mCurrentScrollDirection = Direction.VERTICAL
358+
Direction.VERTICAL
358359
}
359360
}
360361
WeekView.Direction.LEFT -> {
@@ -379,30 +380,26 @@ class WeekView @JvmOverloads constructor(context: Context, attrs: AttributeSet?
379380
val minX = xMinLimit
380381
val maxX = xMaxLimit
381382

382-
if (e2.x < 0) {
383-
mDistanceDone = e2.x - e1.x
383+
mDistanceDone = if (e2.x < 0) {
384+
e2.x - e1.x
384385
} else {
385-
mDistanceDone = e1.x - e2.x
386+
e1.x - e2.x
386387
}
387388

388-
if (mCurrentOrigin.x - distanceX * xScrollingSpeed > maxX) {
389-
mCurrentOrigin.x = maxX
390-
} else if (mCurrentOrigin.x - distanceX * xScrollingSpeed < minX) {
391-
mCurrentOrigin.x = minX
392-
} else {
393-
mCurrentOrigin.x -= distanceX * xScrollingSpeed
389+
when {
390+
mCurrentOrigin.x - distanceX * xScrollingSpeed > maxX -> mCurrentOrigin.x = maxX
391+
mCurrentOrigin.x - distanceX * xScrollingSpeed < minX -> mCurrentOrigin.x = minX
392+
else -> mCurrentOrigin.x -= distanceX * xScrollingSpeed
394393
}
395394
ViewCompat.postInvalidateOnAnimation(this@WeekView)
396395
}
397396
WeekView.Direction.VERTICAL -> {
398397
val minY = yMinLimit
399398
val maxY = yMaxLimit
400-
if (mCurrentOrigin.y - distanceY > maxY) {
401-
mCurrentOrigin.y = maxY
402-
} else if (mCurrentOrigin.y - distanceY < minY) {
403-
mCurrentOrigin.y = minY
404-
} else {
405-
mCurrentOrigin.y -= distanceY
399+
when {
400+
mCurrentOrigin.y - distanceY > maxY -> mCurrentOrigin.y = maxY
401+
mCurrentOrigin.y - distanceY < minY -> mCurrentOrigin.y = minY
402+
else -> mCurrentOrigin.y -= distanceY
406403
}
407404
ViewCompat.postInvalidateOnAnimation(this@WeekView)
408405
}
@@ -576,16 +573,16 @@ class WeekView @JvmOverloads constructor(context: Context, attrs: AttributeSet?
576573

577574
private val xMinLimit: Float
578575
get() {
579-
if (maxDate == null) {
580-
return Integer.MIN_VALUE.toFloat()
576+
return if (maxDate == null) {
577+
Integer.MIN_VALUE.toFloat()
581578
} else {
582579
val date = maxDate!!.clone() as Calendar
583580
date.add(Calendar.DATE, 1 - realNumberOfVisibleDays)
584581
while (date.before(minDate)) {
585582
date.add(Calendar.DATE, 1)
586583
}
587584

588-
return getXOriginForDate(date)
585+
getXOriginForDate(date)
589586
}
590587
}
591588

@@ -1074,11 +1071,23 @@ class WeekView @JvmOverloads constructor(context: Context, attrs: AttributeSet?
10741071
val firstVisibleHour: Double
10751072
get() = (-mCurrentOrigin.y / mHourHeight).toDouble()
10761073

1074+
private val refreshRunnable: Runnable
1075+
private val uiHandler = Handler()
1076+
1077+
//endregion fields and properties
1078+
10771079
private enum class Direction {
10781080
NONE, LEFT, RIGHT, VERTICAL
10791081
}
10801082

10811083
init {
1084+
refreshRunnable = object : Runnable {
1085+
override fun run() {
1086+
invalidate()
1087+
uiHandler.postDelayed(this, 60L * 1000L)
1088+
}
1089+
}
1090+
uiHandler.postDelayed(refreshRunnable, 60L * 1000L)
10821091
// Get the attribute values (if any).
10831092
val a = context.theme.obtainStyledAttributes(attrs, R.styleable.WeekView, 0, 0)
10841093
try {
@@ -1237,6 +1246,11 @@ class WeekView @JvmOverloads constructor(context: Context, attrs: AttributeSet?
12371246
mScaleDetector = ScaleGestureDetector(context, WeekViewGestureListener())
12381247
}
12391248

1249+
override fun onDetachedFromWindow() {
1250+
super.onDetachedFromWindow()
1251+
uiHandler.removeCallbacks(refreshRunnable)
1252+
}
1253+
12401254
private fun resetHomeDate() {
12411255
var newHomeDate = today()
12421256

@@ -1313,10 +1327,10 @@ class WeekView @JvmOverloads constructor(context: Context, attrs: AttributeSet?
13131327
}
13141328
}
13151329
}
1316-
if (containsAllDayEvent) {
1317-
mHeaderHeight = mHeaderTextHeight + (allDayEventHeight + mHeaderMarginBottom)
1330+
mHeaderHeight = if (containsAllDayEvent) {
1331+
mHeaderTextHeight + (allDayEventHeight + mHeaderMarginBottom)
13181332
} else {
1319-
mHeaderHeight = mHeaderTextHeight
1333+
mHeaderTextHeight
13201334
}
13211335
}
13221336

@@ -1422,7 +1436,7 @@ class WeekView @JvmOverloads constructor(context: Context, attrs: AttributeSet?
14221436
var lineCount = ((height.toFloat() - mHeaderHeight - (mHeaderRowPadding * 2).toFloat() -
14231437
mHeaderMarginBottom) / mHourHeight).toInt() + 1
14241438

1425-
lineCount = lineCount * (realNumberOfVisibleDays + 1)
1439+
lineCount *= (realNumberOfVisibleDays + 1)
14261440

14271441
val hourLines = FloatArray(lineCount * 4)
14281442

@@ -1455,7 +1469,6 @@ class WeekView @JvmOverloads constructor(context: Context, attrs: AttributeSet?
14551469
}
14561470

14571471
for (dayNumber in leftDaysWithGaps + 1..leftDaysWithGaps + realNumberOfVisibleDays + 1) {
1458-
14591472
// Check if the day is today.
14601473
day = mHomeDate!!.clone() as Calendar
14611474
lastVisibleDay = day.clone() as Calendar
@@ -1485,16 +1498,15 @@ class WeekView @JvmOverloads constructor(context: Context, attrs: AttributeSet?
14851498
val pastPaint = if (isWeekend && mShowDistinctWeekendColor) mPastWeekendBackgroundPaint else mPastBackgroundPaint
14861499
val futurePaint = if (isWeekend && mShowDistinctWeekendColor) mFutureWeekendBackgroundPaint else mFutureBackgroundPaint
14871500
val startY = mHeaderHeight + (mHeaderRowPadding * 2).toFloat() + mTimeTextHeight / 2 + mHeaderMarginBottom + mCurrentOrigin.y
1488-
1489-
if (isToday) {
1490-
val now = Calendar.getInstance()
1491-
val beforeNow = (now.get(Calendar.HOUR_OF_DAY) - mMinTime + now.get(Calendar.MINUTE) / 60f) * mHourHeight
1492-
canvas.drawRect(start, startY, startPixel + mWidthPerDay, startY + beforeNow, pastPaint)
1493-
canvas.drawRect(start, startY + beforeNow, startPixel + mWidthPerDay, height.toFloat(), futurePaint)
1494-
} else if (day.before(today)) {
1495-
canvas.drawRect(start, startY, startPixel + mWidthPerDay, height.toFloat(), pastPaint)
1496-
} else {
1497-
canvas.drawRect(start, startY, startPixel + mWidthPerDay, height.toFloat(), futurePaint)
1501+
when {
1502+
isToday -> {
1503+
val now = Calendar.getInstance()
1504+
val beforeNow = (now.get(Calendar.HOUR_OF_DAY) - mMinTime + now.get(Calendar.MINUTE) / 60f) * mHourHeight
1505+
canvas.drawRect(start, startY, startPixel + mWidthPerDay, startY + beforeNow, pastPaint)
1506+
canvas.drawRect(start, startY + beforeNow, startPixel + mWidthPerDay, height.toFloat(), futurePaint)
1507+
}
1508+
day.before(today) -> canvas.drawRect(start, startY, startPixel + mWidthPerDay, height.toFloat(), pastPaint)
1509+
else -> canvas.drawRect(start, startY, startPixel + mWidthPerDay, height.toFloat(), futurePaint)
14981510
}
14991511
} else {
15001512
canvas.drawRect(start, mHeaderHeight + (mHeaderRowPadding * 2).toFloat() + mTimeTextHeight / 2 + mHeaderMarginBottom, startPixel + mWidthPerDay, height.toFloat(), if (isToday) mTodayBackgroundPaint else mDayBackgroundPaint)
@@ -2096,17 +2108,17 @@ class WeekView @JvmOverloads constructor(context: Context, attrs: AttributeSet?
20962108
* @param endHour limit time display at bottom (between 0~24 and larger than startHour)
20972109
*/
20982110
fun setLimitTime(startHour: Int, endHour: Int) {
2099-
if (endHour <= startHour) {
2100-
throw IllegalArgumentException("endHour must larger startHour.")
2101-
} else if (startHour < 0) {
2102-
throw IllegalArgumentException("startHour must be at least 0.")
2103-
} else if (endHour > 24) {
2104-
throw IllegalArgumentException("endHour can't be higher than 24.")
2111+
when {
2112+
endHour <= startHour -> throw IllegalArgumentException("endHour must larger startHour.")
2113+
startHour < 0 -> throw IllegalArgumentException("startHour must be at least 0.")
2114+
endHour > 24 -> throw IllegalArgumentException("endHour can't be higher than 24.")
2115+
else -> {
2116+
this.mMinTime = startHour
2117+
this.mMaxTime = endHour
2118+
recalculateHourHeight()
2119+
invalidate()
2120+
}
21052121
}
2106-
this.mMinTime = startHour
2107-
this.mMaxTime = endHour
2108-
recalculateHourHeight()
2109-
invalidate()
21102122
}
21112123

21122124
/**
@@ -2173,22 +2185,23 @@ class WeekView @JvmOverloads constructor(context: Context, attrs: AttributeSet?
21732185

21742186
if (mDistanceDone > mDistanceMin || mDistanceDone < -mDistanceMin || !mIsScrollNumberOfVisibleDays) {
21752187

2176-
if (!mIsScrollNumberOfVisibleDays && mCurrentFlingDirection != Direction.NONE) {
2177-
// snap to nearest day
2178-
leftDays = Math.round(leftDays).toDouble()
2179-
} else if (mCurrentScrollDirection == Direction.LEFT) {
2180-
// snap to last day
2181-
leftDays = Math.floor(leftDays)
2182-
mStartOriginForScroll -= mSizeOfWeekView
2183-
isPassed = true
2184-
} else if (mCurrentScrollDirection == Direction.RIGHT) {
2185-
// snap to next day
2186-
leftDays = Math.floor(leftDays)
2187-
mStartOriginForScroll += mSizeOfWeekView
2188-
isPassed = true
2189-
} else {
2190-
// snap to nearest day
2191-
leftDays = Math.round(leftDays).toDouble()
2188+
when {
2189+
!mIsScrollNumberOfVisibleDays && mCurrentFlingDirection != Direction.NONE -> // snap to nearest day
2190+
leftDays = Math.round(leftDays).toDouble()
2191+
mCurrentScrollDirection == Direction.LEFT -> {
2192+
// snap to last day
2193+
leftDays = Math.floor(leftDays)
2194+
mStartOriginForScroll -= mSizeOfWeekView
2195+
isPassed = true
2196+
}
2197+
mCurrentScrollDirection == Direction.RIGHT -> {
2198+
// snap to next day
2199+
leftDays = Math.floor(leftDays)
2200+
mStartOriginForScroll += mSizeOfWeekView
2201+
isPassed = true
2202+
}
2203+
else -> // snap to nearest day
2204+
leftDays = Math.round(leftDays).toDouble()
21922205
}
21932206

21942207

@@ -2355,16 +2368,10 @@ class WeekView @JvmOverloads constructor(context: Context, attrs: AttributeSet?
23552368
if (minDate != null && day.before(minDate)) {
23562369
return false
23572370
}
2358-
return if (maxDate != null && day.after(maxDate)) {
2359-
false
2360-
} else true
2371+
return !(maxDate != null && day.after(maxDate))
23612372
}
23622373

2363-
/////////////////////////////////////////////////////////////////
2364-
//
2365-
// Interfaces.
2366-
//
2367-
/////////////////////////////////////////////////////////////////
2374+
//region interfaces
23682375

23692376
interface DropListener {
23702377
/**
@@ -2496,6 +2503,8 @@ class WeekView @JvmOverloads constructor(context: Context, attrs: AttributeSet?
24962503
}
24972504
}
24982505

2506+
//endregion interfaces
2507+
24992508
companion object {
25002509
@Deprecated("")
25012510
val LENGTH_SHORT = 1

sample/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
android:name=".BasicActivity" android:label="@string/title_activity_basic"/>
1818
<activity
1919
android:name=".AsynchronousActivity" android:label="@string/title_activity_asynchronous"/>
20-
<activity
20+
<activity android:screenOrientation="userLandscape" android:theme="@style/AppTheme.FullScreen"
2121
android:name=".WholeViewSnappingActivity" android:label="@string/title_activity_whole_view_snap"/>
2222
</application>
2323

0 commit comments

Comments
 (0)