Skip to content

Commit 50979d8

Browse files
author
Jens Claes
committed
Merge branch 'color' into quivr-master
Conflicts: library/src/main/java/com/alamkanak/weekview/WeekView.java
2 parents ff55097 + 07d329a commit 50979d8

File tree

2 files changed

+92
-20
lines changed

2 files changed

+92
-20
lines changed

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

Lines changed: 85 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ public class WeekView extends View {
4545
@Deprecated
4646
public static final int LENGTH_LONG = 2;
4747
private final Context mContext;
48-
private Calendar mToday;
49-
private Calendar mStartDate;
5048
private Paint mTimeTextPaint;
5149
private float mTimeTextWidth;
5250
private float mTimeTextHeight;
@@ -62,6 +60,11 @@ public class WeekView extends View {
6260
private Paint mHourSeparatorPaint;
6361
private float mHeaderMarginBottom;
6462
private Paint mTodayBackgroundPaint;
63+
private Paint mFutureBackgroundPaint;
64+
private Paint mPastBackgroundPaint;
65+
private Paint mFutureWeekendBackgroundPaint;
66+
private Paint mPastWeekendBackgroundPaint;
67+
private Paint mNowLinePaint;
6568
private Paint mTodayHeaderTextPaint;
6669
private Paint mEventBackgroundPaint;
6770
private float mHeaderColumnWidth;
@@ -86,6 +89,13 @@ public class WeekView extends View {
8689
private int mHeaderRowPadding = 10;
8790
private int mHeaderRowBackgroundColor = Color.WHITE;
8891
private int mDayBackgroundColor = Color.rgb(245, 245, 245);
92+
private int mPastBackgroundColor = Color.rgb(227, 227, 227);
93+
private int mFutureBackgroundColor = Color.rgb(245, 245, 245);
94+
private int mPastWeekendBackgroundColor = 0;
95+
private int mFutureWeekendBackgroundColor = 0;
96+
private int mNowLineColor = Color.rgb(102, 102, 102);
97+
private int mNowLineThickness = 5;
98+
private boolean mUseNewColoring = false;
8999
private int mHourSeparatorColor = Color.rgb(230, 230, 230);
90100
private int mTodayBackgroundColor = Color.rgb(239, 247, 254);
91101
private int mHourSeparatorHeight = 2;
@@ -240,6 +250,13 @@ public WeekView(Context context, AttributeSet attrs, int defStyleAttr) {
240250
mHeaderRowPadding = a.getDimensionPixelSize(R.styleable.WeekView_headerRowPadding, mHeaderRowPadding);
241251
mHeaderRowBackgroundColor = a.getColor(R.styleable.WeekView_headerRowBackgroundColor, mHeaderRowBackgroundColor);
242252
mDayBackgroundColor = a.getColor(R.styleable.WeekView_dayBackgroundColor, mDayBackgroundColor);
253+
mFutureBackgroundColor = a.getColor(R.styleable.WeekView_futureBackgroundColor, mFutureBackgroundColor);
254+
mPastBackgroundColor = a.getColor(R.styleable.WeekView_pastBackgroundColor, mPastBackgroundColor);
255+
mFutureWeekendBackgroundColor = a.getColor(R.styleable.WeekView_futureWeekendBackgroundColor, mFutureBackgroundColor); // If not set, use the same color as in the week
256+
mPastWeekendBackgroundColor = a.getColor(R.styleable.WeekView_pastWeekendBackgroundColor, mPastBackgroundColor);
257+
mNowLineColor = a.getColor(R.styleable.WeekView_nowLineColor, mNowLineColor);
258+
mNowLineThickness = a.getDimensionPixelSize(R.styleable.WeekView_nowLineThickness, mNowLineThickness);
259+
mUseNewColoring = a.getBoolean(R.styleable.WeekView_useNewColoringStyle, mUseNewColoring);
243260
mHourSeparatorColor = a.getColor(R.styleable.WeekView_hourSeparatorColor, mHourSeparatorColor);
244261
mTodayBackgroundColor = a.getColor(R.styleable.WeekView_todayBackgroundColor, mTodayBackgroundColor);
245262
mHourSeparatorHeight = a.getDimensionPixelSize(R.styleable.WeekView_hourSeparatorHeight, mHourSeparatorHeight);
@@ -260,12 +277,6 @@ public WeekView(Context context, AttributeSet attrs, int defStyleAttr) {
260277
}
261278

262279
private void init() {
263-
// Get the date today.
264-
mToday = Calendar.getInstance();
265-
mToday.set(Calendar.HOUR_OF_DAY, 0);
266-
mToday.set(Calendar.MINUTE, 0);
267-
mToday.set(Calendar.SECOND, 0);
268-
269280
// Scrolling initialization.
270281
mGestureDetector = new GestureDetectorCompat(mContext, mGestureListener);
271282
mScroller = new OverScroller(mContext);
@@ -298,13 +309,26 @@ private void init() {
298309
// Prepare day background color paint.
299310
mDayBackgroundPaint = new Paint();
300311
mDayBackgroundPaint.setColor(mDayBackgroundColor);
312+
mFutureBackgroundPaint = new Paint();
313+
mFutureBackgroundPaint.setColor(mFutureBackgroundColor);
314+
mPastBackgroundPaint = new Paint();
315+
mPastBackgroundPaint.setColor(mPastBackgroundColor);
316+
mFutureWeekendBackgroundPaint = new Paint();
317+
mFutureWeekendBackgroundPaint.setColor(mFutureWeekendBackgroundColor);
318+
mPastWeekendBackgroundPaint = new Paint();
319+
mPastWeekendBackgroundPaint.setColor(mPastWeekendBackgroundColor);
301320

302321
// Prepare hour separator color paint.
303322
mHourSeparatorPaint = new Paint();
304323
mHourSeparatorPaint.setStyle(Paint.Style.STROKE);
305324
mHourSeparatorPaint.setStrokeWidth(mHourSeparatorHeight);
306325
mHourSeparatorPaint.setColor(mHourSeparatorColor);
307326

327+
// Prepare the "now" line color paint
328+
mNowLinePaint = new Paint();
329+
mNowLinePaint.setStrokeWidth(mNowLineThickness);
330+
mNowLinePaint.setColor(mNowLineColor);
331+
308332
// Prepare today background color paint.
309333
mTodayBackgroundPaint = new Paint();
310334
mTodayBackgroundPaint.setColor(mTodayBackgroundColor);
@@ -329,7 +353,6 @@ private void init() {
329353
mEventTextPaint.setStyle(Paint.Style.FILL);
330354
mEventTextPaint.setColor(mEventTextColor);
331355
mEventTextPaint.setTextSize(mEventTextSize);
332-
mStartDate = (Calendar) mToday.clone();
333356

334357
// Set default event color.
335358
mDefaultEventColor = Color.parseColor("#9fc6e7");
@@ -388,10 +411,12 @@ private void drawHeaderRowAndEvents(Canvas canvas) {
388411
mWidthPerDay = getWidth() - mHeaderColumnWidth - mColumnGap * (mNumberOfVisibleDays - 1);
389412
mWidthPerDay = mWidthPerDay/mNumberOfVisibleDays;
390413

414+
Calendar today = today();
415+
391416
// If the week view is being drawn for the first time, then consider the first day of week.
392417
if (mIsFirstDraw && mNumberOfVisibleDays >= 7) {
393-
if (mToday.get(Calendar.DAY_OF_WEEK) != mFirstDayOfWeek) {
394-
int difference = 7 + (mToday.get(Calendar.DAY_OF_WEEK) - mFirstDayOfWeek);
418+
if (today.get(Calendar.DAY_OF_WEEK) != mFirstDayOfWeek) {
419+
int difference = 7 + (today.get(Calendar.DAY_OF_WEEK) - mFirstDayOfWeek);
395420
mCurrentOrigin.x += (mWidthPerDay + mColumnGap) * difference;
396421
}
397422
mIsFirstDraw = false;
@@ -405,7 +430,7 @@ private void drawHeaderRowAndEvents(Canvas canvas) {
405430
float startPixel = startFromPixel;
406431

407432
// Prepare to iterate for each day.
408-
Calendar day = (Calendar) mToday.clone();
433+
Calendar day = (Calendar) today.clone();
409434
day.add(Calendar.HOUR, 6);
410435

411436
// Prepare to iterate for each hour to draw the hour lines.
@@ -423,7 +448,7 @@ private void drawHeaderRowAndEvents(Canvas canvas) {
423448

424449
// Iterate through each day.
425450
Calendar oldFirstVisibleDay = mFirstVisibleDay;
426-
mFirstVisibleDay = (Calendar) mToday.clone();
451+
mFirstVisibleDay = (Calendar) today.clone();
427452
mFirstVisibleDay.add(Calendar.DATE, leftDaysWithGaps);
428453
if(!mFirstVisibleDay.equals(oldFirstVisibleDay) && mScrolledListener != null){
429454
mScrolledListener.onFirstVisibleDayChanged(mFirstVisibleDay, oldFirstVisibleDay);
@@ -433,11 +458,11 @@ private void drawHeaderRowAndEvents(Canvas canvas) {
433458
dayNumber++) {
434459

435460
// Check if the day is today.
436-
day = (Calendar) mToday.clone();
461+
day = (Calendar) today.clone();
437462
mLastVisibleDay = (Calendar) day.clone();
438463
day.add(Calendar.DATE, dayNumber - 1);
439464
mLastVisibleDay.add(Calendar.DATE, dayNumber - 2);
440-
boolean sameDay = isSameDay(day, mToday);
465+
boolean sameDay = isSameDay(day, today);
441466

442467
// Get more events if necessary. We want to store the events 3 months beforehand. Get
443468
// events only when it is the first iteration of the loop.
@@ -448,8 +473,27 @@ private void drawHeaderRowAndEvents(Canvas canvas) {
448473

449474
// Draw background color for each day.
450475
float start = (startPixel < mHeaderColumnWidth ? mHeaderColumnWidth : startPixel);
451-
if (mWidthPerDay + startPixel - start> 0)
452-
canvas.drawRect(start, mHeaderTextHeight + mHeaderRowPadding * 2 + mTimeTextHeight/2 + mHeaderMarginBottom, startPixel + mWidthPerDay, getHeight(), sameDay ? mTodayBackgroundPaint : mDayBackgroundPaint);
476+
if (mWidthPerDay + startPixel - start> 0){
477+
if(mUseNewColoring){
478+
boolean isWeekend = (day.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || day.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY);
479+
Paint pastPaint = isWeekend ? mPastWeekendBackgroundPaint : mPastBackgroundPaint;
480+
Paint futurePaint = isWeekend ? mFutureWeekendBackgroundPaint : mFutureBackgroundPaint;
481+
float startY = mHeaderTextHeight + mHeaderRowPadding * 2 + mTimeTextHeight/2 + mHeaderMarginBottom + mCurrentOrigin.y;
482+
483+
if(sameDay){
484+
Calendar now = Calendar.getInstance();
485+
float beforeNow = (now.get(Calendar.HOUR_OF_DAY)+now.get(Calendar.MINUTE)/60.0f)*mHourHeight;
486+
canvas.drawRect(start, startY, startPixel + mWidthPerDay, startY+beforeNow, pastPaint);
487+
canvas.drawRect(start, startY+beforeNow, startPixel + mWidthPerDay, getHeight(), futurePaint);
488+
} else if(day.before(today)) {
489+
canvas.drawRect(start, startY, startPixel + mWidthPerDay, getHeight(), pastPaint);
490+
} else {
491+
canvas.drawRect(start, startY, startPixel + mWidthPerDay, getHeight(), futurePaint);
492+
}
493+
}else{
494+
canvas.drawRect(start, mHeaderTextHeight + mHeaderRowPadding * 2 + mTimeTextHeight/2 + mHeaderMarginBottom, startPixel + mWidthPerDay, getHeight(), sameDay ? mTodayBackgroundPaint : mDayBackgroundPaint);
495+
}
496+
}
453497

454498
// Prepare the separator lines for hours.
455499
int i = 0;
@@ -470,6 +514,14 @@ private void drawHeaderRowAndEvents(Canvas canvas) {
470514
// Draw the events.
471515
drawEvents(day, startPixel, canvas);
472516

517+
//Draw the line at the current time
518+
if(mUseNewColoring && sameDay){
519+
float startY = mHeaderTextHeight + mHeaderRowPadding * 2 + mTimeTextHeight/2 + mHeaderMarginBottom + mCurrentOrigin.y;
520+
Calendar now = Calendar.getInstance();
521+
float beforeNow = (now.get(Calendar.HOUR_OF_DAY)+now.get(Calendar.MINUTE)/60.0f)*mHourHeight;
522+
canvas.drawLine(start, startY+beforeNow, startPixel + mWidthPerDay, startY+beforeNow, mNowLinePaint);
523+
}
524+
473525
// In the next iteration, start from the next day.
474526
startPixel += mWidthPerDay + mColumnGap;
475527
}
@@ -481,9 +533,9 @@ private void drawHeaderRowAndEvents(Canvas canvas) {
481533
startPixel = startFromPixel;
482534
for (int dayNumber=leftDaysWithGaps+1; dayNumber <= leftDaysWithGaps + mNumberOfVisibleDays + 1; dayNumber++) {
483535
// Check if the day is today.
484-
day = (Calendar) mToday.clone();
536+
day = (Calendar) today.clone();
485537
day.add(Calendar.DATE, dayNumber - 1);
486-
boolean sameDay = isSameDay(day, mToday);
538+
boolean sameDay = isSameDay(day, today);
487539

488540
// Draw the day labels.
489541
String dayLabel = getDateTimeInterpreter().interpretDate(day);
@@ -511,7 +563,7 @@ private Calendar getTimeFromPoint(float x, float y){
511563
float start = (startPixel < mHeaderColumnWidth ? mHeaderColumnWidth : startPixel);
512564
if (mWidthPerDay + startPixel - start> 0
513565
&& x>start && x<startPixel + mWidthPerDay){
514-
Calendar day = (Calendar) mToday.clone();
566+
Calendar day = today();
515567
day.add(Calendar.DATE, dayNumber - 1);
516568
float pixelsFromZero = y - mCurrentOrigin.y - mHeaderTextHeight
517569
- mHeaderRowPadding * 2 - mTimeTextHeight/2 - mHeaderMarginBottom;
@@ -1498,4 +1550,17 @@ private boolean isSameDay(Calendar dayOne, Calendar dayTwo) {
14981550
return dayOne.get(Calendar.YEAR) == dayTwo.get(Calendar.YEAR) && dayOne.get(Calendar.DAY_OF_YEAR) == dayTwo.get(Calendar.DAY_OF_YEAR);
14991551
}
15001552

1553+
/**
1554+
* Returns a calendar instance at the start of this day
1555+
* @return the calendar instance
1556+
*/
1557+
private Calendar today(){
1558+
Calendar today = Calendar.getInstance();
1559+
today.set(Calendar.HOUR_OF_DAY, 0);
1560+
today.set(Calendar.MINUTE, 0);
1561+
today.set(Calendar.SECOND, 0);
1562+
today.set(Calendar.MILLISECOND, 0);
1563+
return today;
1564+
}
1565+
15011566
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@
2020
<attr name="noOfVisibleDays" format="integer"/>
2121
<attr name="headerRowBackgroundColor" format="color"/>
2222
<attr name="dayBackgroundColor" format="color"/>
23+
<attr name="futureBackgroundColor" format="color"/>
24+
<attr name="pastBackgroundColor" format="color"/>
25+
<attr name="futureWeekendBackgroundColor" format="color"/>
26+
<attr name="pastWeekendBackgroundColor" format="color"/>
27+
<attr name="nowLineColor" format="color"/>
28+
<attr name="nowLineThickness" format="dimension"/>
29+
<attr name="useNewColoringStyle" format="boolean"/>
2330
<attr name="hourSeparatorColor" format="color"/>
2431
<attr name="todayBackgroundColor" format="color"/>
2532
<attr name="todayHeaderTextColor" format="color"/>

0 commit comments

Comments
 (0)