Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter hours to specific range (i.e. opening hours) #341

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 81 additions & 14 deletions library/src/main/java/com/alamkanak/weekview/WeekView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -533,8 +537,8 @@ 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++) {
float top = mHeaderHeight + mHeaderRowPadding * 2 + mCurrentOrigin.y + mHourHeight * i + mHeaderMarginBottom;
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.
String time = getDateTimeInterpreter().interpretTime(i);
Expand Down Expand Up @@ -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.
Expand All @@ -609,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 -
Expand Down Expand Up @@ -683,8 +687,8 @@ 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;
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;
hourLines[i * 4 + 1] = top;
Expand All @@ -697,15 +701,22 @@ 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);

// Draw the line at the current time.
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.
Expand Down Expand Up @@ -762,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;
}
Expand All @@ -771,6 +782,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)+1);
return;
}
}

mStartTime = 0;
mEndTime = 24;
}

/**
* Draw all the events of a particular day.
* @param date The day.
Expand All @@ -782,12 +826,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;
Expand Down Expand Up @@ -1154,7 +1199,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;
Expand Down Expand Up @@ -1184,7 +1228,6 @@ else if (!isEventsCollide(eventRect.event, column.get(column.size()-1).event)) {
}
}


/**
* Checks if two events overlap.
* @param event1 The first event.
Expand Down Expand Up @@ -1676,6 +1719,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
Expand Down
1 change: 1 addition & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,6 @@
<attr name="verticalFlingEnabled" format="boolean"/>
<attr name="allDayEventHeight" format="dimension"/>
<attr name="scrollDuration" format="integer"/>
<attr name="autoLimitTime" format="boolean"/>
</declare-styleable>
</resources>