From 3ed0eb41145a308e2cf8bd1af76cc1d2c791c092 Mon Sep 17 00:00:00 2001 From: khacpv Date: Fri, 17 Jun 2016 18:21:05 +0700 Subject: [PATCH 1/2] filter by limit time --- .../java/com/alamkanak/weekview/WeekView.java | 82 +++++++++++++++++-- library/src/main/res/values/attrs.xml | 1 + 2 files changed, 75 insertions(+), 8 deletions(-) diff --git a/library/src/main/java/com/alamkanak/weekview/WeekView.java b/library/src/main/java/com/alamkanak/weekview/WeekView.java index da3fa71a0..37ec2d125 100755 --- a/library/src/main/java/com/alamkanak/weekview/WeekView.java +++ b/library/src/main/java/com/alamkanak/weekview/WeekView.java @@ -144,6 +144,9 @@ private enum Direction { private boolean mVerticalFlingEnabled = true; private int mAllDayEventHeight = 100; private int mScrollDuration = 250; + private int mStartTime = 0; + private int mEndTime = 24; + private boolean autoLimitTime = false; // Listeners. private EventClickListener mEventClickListener; @@ -353,6 +356,7 @@ public WeekView(Context context, AttributeSet attrs, int defStyleAttr) { mVerticalFlingEnabled = a.getBoolean(R.styleable.WeekView_verticalFlingEnabled, mVerticalFlingEnabled); mAllDayEventHeight = a.getDimensionPixelSize(R.styleable.WeekView_allDayEventHeight, mAllDayEventHeight); mScrollDuration = a.getInt(R.styleable.WeekView_scrollDuration, mScrollDuration); + autoLimitTime = a.getBoolean(R.styleable.WeekView_autoLimitTime, autoLimitTime); } finally { a.recycle(); } @@ -534,7 +538,7 @@ private void drawTimeColumnAndAxes(Canvas canvas) { canvas.clipRect(0, mHeaderHeight + mHeaderRowPadding * 2, mHeaderColumnWidth, getHeight(), Region.Op.REPLACE); for (int i = 0; i < 24; i++) { - float top = mHeaderHeight + mHeaderRowPadding * 2 + mCurrentOrigin.y + mHourHeight * i + mHeaderMarginBottom; + float top = mHeaderHeight + mHeaderRowPadding * 2 + mCurrentOrigin.y + mHourHeight * (i-mStartTime) + mHeaderMarginBottom; // Draw the text if its y position is not outside of the visible area. The pivot point of the text is the point at the bottom-right corner. String time = getDateTimeInterpreter().interpretTime(i); @@ -592,8 +596,8 @@ else if (mNewHourHeight > mMaxHourHeight) } // If the new mCurrentOrigin.y is invalid, make it valid. - if (mCurrentOrigin.y < getHeight() - mHourHeight * 24 - mHeaderHeight - mHeaderRowPadding * 2 - mHeaderMarginBottom - mTimeTextHeight/2) - mCurrentOrigin.y = getHeight() - mHourHeight * 24 - mHeaderHeight - mHeaderRowPadding * 2 - mHeaderMarginBottom - mTimeTextHeight/2; + if (mCurrentOrigin.y < getHeight() - mHourHeight * (mEndTime-mStartTime) - mHeaderHeight - mHeaderRowPadding * 2 - mHeaderMarginBottom - mTimeTextHeight/2) + mCurrentOrigin.y = getHeight() - mHourHeight * (mEndTime-mStartTime) - mHeaderHeight - mHeaderRowPadding * 2 - mHeaderMarginBottom - mTimeTextHeight/2; // Don't put an "else if" because it will trigger a glitch when completely zoomed out and // scrolling vertically. @@ -684,7 +688,7 @@ else if (day.before(today)) { // Prepare the separator lines for hours. int i = 0; for (int hourNumber = 0; hourNumber < 24; hourNumber++) { - float top = mHeaderHeight + mHeaderRowPadding * 2 + mCurrentOrigin.y + mHourHeight * hourNumber + mTimeTextHeight/2 + mHeaderMarginBottom; + float top = mHeaderHeight + mHeaderRowPadding * 2 + mCurrentOrigin.y + mHourHeight * (hourNumber-mStartTime) + mTimeTextHeight/2 + mHeaderMarginBottom; if (top > mHeaderHeight + mHeaderRowPadding * 2 + mTimeTextHeight/2 + mHeaderMarginBottom - mHourSeparatorHeight && top < getHeight() && startPixel + mWidthPerDay - start > 0){ hourLines[i * 4] = start; hourLines[i * 4 + 1] = top; @@ -697,6 +701,12 @@ else if (day.before(today)) { // Draw the lines for hours. canvas.drawLines(hourLines, mHourSeparatorPaint); + // Limit time events + // Only calculate on visible days + if(dayNumber <= leftDaysWithGaps + mNumberOfVisibleDays && autoLimitTime && mNumberOfVisibleDays == 1) { + limitEventTime(day); + } + // Draw the events. drawEvents(day, startPixel, canvas); @@ -771,6 +781,39 @@ private Calendar getTimeFromPoint(float x, float y){ return null; } + /** + * limit current time of event by update mStartTime & mEndTime + * find smallest of start time & latest of end time + * */ + private void limitEventTime(Calendar date){ + if (mEventRects != null && mEventRects.size() > 0) { + Calendar startTime = null; + Calendar endTime = null; + + for (EventRect eventRect: mEventRects) { + if (isSameDay(eventRect.event.getStartTime(), date) && !eventRect.event.isAllDay()) { + + if(startTime==null || startTime.after(eventRect.event.getStartTime())){ + startTime = eventRect.event.getStartTime(); + } + + if(endTime==null || endTime.before(eventRect.event.getEndTime())){ + endTime = eventRect.event.getEndTime(); + } + } + } + + if(startTime!=null && endTime !=null && startTime.before(endTime)) { + mStartTime = Math.max(0,startTime.get(Calendar.HOUR_OF_DAY)); + mEndTime = Math.min(24,endTime.get(Calendar.HOUR_OF_DAY)); + return; + } + } + + mStartTime = 0; + mEndTime = 24; + } + /** * Draw all the events of a particular day. * @param date The day. @@ -782,12 +825,13 @@ private void drawEvents(Calendar date, float startFromPixel, Canvas canvas) { for (int i = 0; i < mEventRects.size(); i++) { if (isSameDay(mEventRects.get(i).event.getStartTime(), date) && !mEventRects.get(i).event.isAllDay()){ + int marginTop = mHourHeight * mStartTime; // Calculate top. - float top = mHourHeight * 24 * mEventRects.get(i).top / 1440 + mCurrentOrigin.y + mHeaderHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom + mTimeTextHeight/2 + mEventMarginVertical; + float top = mHourHeight * 24 * mEventRects.get(i).top / 1440 + mCurrentOrigin.y + mHeaderHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom + mTimeTextHeight/2 + mEventMarginVertical-marginTop; // Calculate bottom. float bottom = mEventRects.get(i).bottom; - bottom = mHourHeight * 24 * bottom / 1440 + mCurrentOrigin.y + mHeaderHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom + mTimeTextHeight/2 - mEventMarginVertical; + bottom = mHourHeight * 24 * bottom / 1440 + mCurrentOrigin.y + mHeaderHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom + mTimeTextHeight/2 - mEventMarginVertical - marginTop; // Calculate left and right. float left = startFromPixel + mEventRects.get(i).left * mWidthPerDay; @@ -1154,7 +1198,6 @@ else if (!isEventsCollide(eventRect.event, column.get(column.size()-1).event)) { } } - // Calculate left and right position for all the events. // Get the maxRowCount by looking in all columns. int maxRowCount = 0; @@ -1184,7 +1227,6 @@ else if (!isEventsCollide(eventRect.event, column.get(column.size()-1).event)) { } } - /** * Checks if two events overlap. * @param event1 The first event. @@ -1676,6 +1718,30 @@ public void setShowDistinctWeekendColor(boolean showDistinctWeekendColor) { invalidate(); } + /** + * auto calculate limit time on events in day. + * @see #limitEventTime(Calendar) + * */ + public void setAutoLimitTime(boolean isAuto){ + this.autoLimitTime = isAuto; + invalidate(); + } + + /** + * set fix visible time. + * @param startHour limit time display on top (between 0~24) + * @param endHour limit time display at bottom (between 0~24 and > startHour) + * */ + public void setLimitTime(int startHour, int endHour){ + if(endHour <= startHour || startHour < 0 || endHour > 24){ + throw new IllegalArgumentException("endHour must larger startHour"); + } + this.mStartTime = startHour; + this.mEndTime = endHour; + this.autoLimitTime = false; + invalidate(); + } + /** * Whether past and future days should have two different background colors. The past and * future day colors are defined by the attributes `futureBackgroundColor` and diff --git a/library/src/main/res/values/attrs.xml b/library/src/main/res/values/attrs.xml index a37a3717b..e1b62e05e 100644 --- a/library/src/main/res/values/attrs.xml +++ b/library/src/main/res/values/attrs.xml @@ -51,5 +51,6 @@ + From 82771f78804ca99989e386f780faf206814d3c64 Mon Sep 17 00:00:00 2001 From: khacpv Date: Mon, 4 Jul 2016 13:02:35 +0700 Subject: [PATCH 2/2] fix display current time line & onEmptyViewClicked --- .../java/com/alamkanak/weekview/WeekView.java | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/library/src/main/java/com/alamkanak/weekview/WeekView.java b/library/src/main/java/com/alamkanak/weekview/WeekView.java index 37ec2d125..7ec9b0e39 100755 --- a/library/src/main/java/com/alamkanak/weekview/WeekView.java +++ b/library/src/main/java/com/alamkanak/weekview/WeekView.java @@ -537,7 +537,7 @@ private void drawTimeColumnAndAxes(Canvas canvas) { // Clip to paint in left column only. canvas.clipRect(0, mHeaderHeight + mHeaderRowPadding * 2, mHeaderColumnWidth, getHeight(), Region.Op.REPLACE); - for (int i = 0; i < 24; i++) { + for (int i = mStartTime; i < mEndTime; i++) { float top = mHeaderHeight + mHeaderRowPadding * 2 + mCurrentOrigin.y + mHourHeight * (i-mStartTime) + mHeaderMarginBottom; // Draw the text if its y position is not outside of the visible area. The pivot point of the text is the point at the bottom-right corner. @@ -613,7 +613,7 @@ else if (mNewHourHeight > mMaxHourHeight) // Prepare to iterate for each day. Calendar day = (Calendar) today.clone(); - day.add(Calendar.HOUR, 6); + day.add(Calendar.HOUR_OF_DAY, 6); // Prepare to iterate for each hour to draw the hour lines. int lineCount = (int) ((getHeight() - mHeaderHeight - mHeaderRowPadding * 2 - @@ -687,7 +687,7 @@ else if (day.before(today)) { // Prepare the separator lines for hours. int i = 0; - for (int hourNumber = 0; hourNumber < 24; hourNumber++) { + for (int hourNumber = mStartTime; hourNumber < mEndTime; hourNumber++) { float top = mHeaderHeight + mHeaderRowPadding * 2 + mCurrentOrigin.y + mHourHeight * (hourNumber-mStartTime) + mTimeTextHeight/2 + mHeaderMarginBottom; if (top > mHeaderHeight + mHeaderRowPadding * 2 + mTimeTextHeight/2 + mHeaderMarginBottom - mHourSeparatorHeight && top < getHeight() && startPixel + mWidthPerDay - start > 0){ hourLines[i * 4] = start; @@ -714,8 +714,9 @@ else if (day.before(today)) { if (mShowNowLine && sameDay){ float startY = mHeaderHeight + mHeaderRowPadding * 2 + mTimeTextHeight/2 + mHeaderMarginBottom + mCurrentOrigin.y; Calendar now = Calendar.getInstance(); - float beforeNow = (now.get(Calendar.HOUR_OF_DAY) + now.get(Calendar.MINUTE)/60.0f) * mHourHeight; - canvas.drawLine(start, startY + beforeNow, startPixel + mWidthPerDay, startY + beforeNow, mNowLinePaint); + float beforeNow = (now.get(Calendar.HOUR_OF_DAY) - mStartTime + now.get(Calendar.MINUTE)/60.0f) * mHourHeight; + float top = startY + beforeNow; + canvas.drawLine(start, top, startPixel + mWidthPerDay, top, mNowLinePaint); } // In the next iteration, start from the next day. @@ -772,7 +773,7 @@ private Calendar getTimeFromPoint(float x, float y){ - mHeaderRowPadding * 2 - mTimeTextHeight/2 - mHeaderMarginBottom; int hour = (int)(pixelsFromZero / mHourHeight); int minute = (int) (60 * (pixelsFromZero - hour * mHourHeight) / mHourHeight); - day.add(Calendar.HOUR, hour); + day.add(Calendar.HOUR_OF_DAY, hour + mStartTime); day.set(Calendar.MINUTE, minute); return day; } @@ -805,7 +806,7 @@ private void limitEventTime(Calendar date){ if(startTime!=null && endTime !=null && startTime.before(endTime)) { mStartTime = Math.max(0,startTime.get(Calendar.HOUR_OF_DAY)); - mEndTime = Math.min(24,endTime.get(Calendar.HOUR_OF_DAY)); + mEndTime = Math.min(24,endTime.get(Calendar.HOUR_OF_DAY)+1); return; } }