Skip to content

Commit

Permalink
filter by limit time
Browse files Browse the repository at this point in the history
  • Loading branch information
khacpv committed Jun 20, 2016
1 parent ed450d8 commit 3ed0eb4
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 8 deletions.
82 changes: 74 additions & 8 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 @@ -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);
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 Down Expand Up @@ -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;
Expand All @@ -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);

Expand Down Expand Up @@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
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>

0 comments on commit 3ed0eb4

Please sign in to comment.