Skip to content

Commit 3ed0eb4

Browse files
committed
filter by limit time
1 parent ed450d8 commit 3ed0eb4

File tree

2 files changed

+75
-8
lines changed

2 files changed

+75
-8
lines changed

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

+74-8
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ private enum Direction {
144144
private boolean mVerticalFlingEnabled = true;
145145
private int mAllDayEventHeight = 100;
146146
private int mScrollDuration = 250;
147+
private int mStartTime = 0;
148+
private int mEndTime = 24;
149+
private boolean autoLimitTime = false;
147150

148151
// Listeners.
149152
private EventClickListener mEventClickListener;
@@ -353,6 +356,7 @@ public WeekView(Context context, AttributeSet attrs, int defStyleAttr) {
353356
mVerticalFlingEnabled = a.getBoolean(R.styleable.WeekView_verticalFlingEnabled, mVerticalFlingEnabled);
354357
mAllDayEventHeight = a.getDimensionPixelSize(R.styleable.WeekView_allDayEventHeight, mAllDayEventHeight);
355358
mScrollDuration = a.getInt(R.styleable.WeekView_scrollDuration, mScrollDuration);
359+
autoLimitTime = a.getBoolean(R.styleable.WeekView_autoLimitTime, autoLimitTime);
356360
} finally {
357361
a.recycle();
358362
}
@@ -534,7 +538,7 @@ private void drawTimeColumnAndAxes(Canvas canvas) {
534538
canvas.clipRect(0, mHeaderHeight + mHeaderRowPadding * 2, mHeaderColumnWidth, getHeight(), Region.Op.REPLACE);
535539

536540
for (int i = 0; i < 24; i++) {
537-
float top = mHeaderHeight + mHeaderRowPadding * 2 + mCurrentOrigin.y + mHourHeight * i + mHeaderMarginBottom;
541+
float top = mHeaderHeight + mHeaderRowPadding * 2 + mCurrentOrigin.y + mHourHeight * (i-mStartTime) + mHeaderMarginBottom;
538542

539543
// 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.
540544
String time = getDateTimeInterpreter().interpretTime(i);
@@ -592,8 +596,8 @@ else if (mNewHourHeight > mMaxHourHeight)
592596
}
593597

594598
// If the new mCurrentOrigin.y is invalid, make it valid.
595-
if (mCurrentOrigin.y < getHeight() - mHourHeight * 24 - mHeaderHeight - mHeaderRowPadding * 2 - mHeaderMarginBottom - mTimeTextHeight/2)
596-
mCurrentOrigin.y = getHeight() - mHourHeight * 24 - mHeaderHeight - mHeaderRowPadding * 2 - mHeaderMarginBottom - mTimeTextHeight/2;
599+
if (mCurrentOrigin.y < getHeight() - mHourHeight * (mEndTime-mStartTime) - mHeaderHeight - mHeaderRowPadding * 2 - mHeaderMarginBottom - mTimeTextHeight/2)
600+
mCurrentOrigin.y = getHeight() - mHourHeight * (mEndTime-mStartTime) - mHeaderHeight - mHeaderRowPadding * 2 - mHeaderMarginBottom - mTimeTextHeight/2;
597601

598602
// Don't put an "else if" because it will trigger a glitch when completely zoomed out and
599603
// scrolling vertically.
@@ -684,7 +688,7 @@ else if (day.before(today)) {
684688
// Prepare the separator lines for hours.
685689
int i = 0;
686690
for (int hourNumber = 0; hourNumber < 24; hourNumber++) {
687-
float top = mHeaderHeight + mHeaderRowPadding * 2 + mCurrentOrigin.y + mHourHeight * hourNumber + mTimeTextHeight/2 + mHeaderMarginBottom;
691+
float top = mHeaderHeight + mHeaderRowPadding * 2 + mCurrentOrigin.y + mHourHeight * (hourNumber-mStartTime) + mTimeTextHeight/2 + mHeaderMarginBottom;
688692
if (top > mHeaderHeight + mHeaderRowPadding * 2 + mTimeTextHeight/2 + mHeaderMarginBottom - mHourSeparatorHeight && top < getHeight() && startPixel + mWidthPerDay - start > 0){
689693
hourLines[i * 4] = start;
690694
hourLines[i * 4 + 1] = top;
@@ -697,6 +701,12 @@ else if (day.before(today)) {
697701
// Draw the lines for hours.
698702
canvas.drawLines(hourLines, mHourSeparatorPaint);
699703

704+
// Limit time events
705+
// Only calculate on visible days
706+
if(dayNumber <= leftDaysWithGaps + mNumberOfVisibleDays && autoLimitTime && mNumberOfVisibleDays == 1) {
707+
limitEventTime(day);
708+
}
709+
700710
// Draw the events.
701711
drawEvents(day, startPixel, canvas);
702712

@@ -771,6 +781,39 @@ private Calendar getTimeFromPoint(float x, float y){
771781
return null;
772782
}
773783

784+
/**
785+
* limit current time of event by update mStartTime & mEndTime
786+
* find smallest of start time & latest of end time
787+
* */
788+
private void limitEventTime(Calendar date){
789+
if (mEventRects != null && mEventRects.size() > 0) {
790+
Calendar startTime = null;
791+
Calendar endTime = null;
792+
793+
for (EventRect eventRect: mEventRects) {
794+
if (isSameDay(eventRect.event.getStartTime(), date) && !eventRect.event.isAllDay()) {
795+
796+
if(startTime==null || startTime.after(eventRect.event.getStartTime())){
797+
startTime = eventRect.event.getStartTime();
798+
}
799+
800+
if(endTime==null || endTime.before(eventRect.event.getEndTime())){
801+
endTime = eventRect.event.getEndTime();
802+
}
803+
}
804+
}
805+
806+
if(startTime!=null && endTime !=null && startTime.before(endTime)) {
807+
mStartTime = Math.max(0,startTime.get(Calendar.HOUR_OF_DAY));
808+
mEndTime = Math.min(24,endTime.get(Calendar.HOUR_OF_DAY));
809+
return;
810+
}
811+
}
812+
813+
mStartTime = 0;
814+
mEndTime = 24;
815+
}
816+
774817
/**
775818
* Draw all the events of a particular day.
776819
* @param date The day.
@@ -782,12 +825,13 @@ private void drawEvents(Calendar date, float startFromPixel, Canvas canvas) {
782825
for (int i = 0; i < mEventRects.size(); i++) {
783826
if (isSameDay(mEventRects.get(i).event.getStartTime(), date) && !mEventRects.get(i).event.isAllDay()){
784827

828+
int marginTop = mHourHeight * mStartTime;
785829
// Calculate top.
786-
float top = mHourHeight * 24 * mEventRects.get(i).top / 1440 + mCurrentOrigin.y + mHeaderHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom + mTimeTextHeight/2 + mEventMarginVertical;
830+
float top = mHourHeight * 24 * mEventRects.get(i).top / 1440 + mCurrentOrigin.y + mHeaderHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom + mTimeTextHeight/2 + mEventMarginVertical-marginTop;
787831

788832
// Calculate bottom.
789833
float bottom = mEventRects.get(i).bottom;
790-
bottom = mHourHeight * 24 * bottom / 1440 + mCurrentOrigin.y + mHeaderHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom + mTimeTextHeight/2 - mEventMarginVertical;
834+
bottom = mHourHeight * 24 * bottom / 1440 + mCurrentOrigin.y + mHeaderHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom + mTimeTextHeight/2 - mEventMarginVertical - marginTop;
791835

792836
// Calculate left and right.
793837
float left = startFromPixel + mEventRects.get(i).left * mWidthPerDay;
@@ -1154,7 +1198,6 @@ else if (!isEventsCollide(eventRect.event, column.get(column.size()-1).event)) {
11541198
}
11551199
}
11561200

1157-
11581201
// Calculate left and right position for all the events.
11591202
// Get the maxRowCount by looking in all columns.
11601203
int maxRowCount = 0;
@@ -1184,7 +1227,6 @@ else if (!isEventsCollide(eventRect.event, column.get(column.size()-1).event)) {
11841227
}
11851228
}
11861229

1187-
11881230
/**
11891231
* Checks if two events overlap.
11901232
* @param event1 The first event.
@@ -1676,6 +1718,30 @@ public void setShowDistinctWeekendColor(boolean showDistinctWeekendColor) {
16761718
invalidate();
16771719
}
16781720

1721+
/**
1722+
* auto calculate limit time on events in day.
1723+
* @see #limitEventTime(Calendar)
1724+
* */
1725+
public void setAutoLimitTime(boolean isAuto){
1726+
this.autoLimitTime = isAuto;
1727+
invalidate();
1728+
}
1729+
1730+
/**
1731+
* set fix visible time.
1732+
* @param startHour limit time display on top (between 0~24)
1733+
* @param endHour limit time display at bottom (between 0~24 and > startHour)
1734+
* */
1735+
public void setLimitTime(int startHour, int endHour){
1736+
if(endHour <= startHour || startHour < 0 || endHour > 24){
1737+
throw new IllegalArgumentException("endHour must larger startHour");
1738+
}
1739+
this.mStartTime = startHour;
1740+
this.mEndTime = endHour;
1741+
this.autoLimitTime = false;
1742+
invalidate();
1743+
}
1744+
16791745
/**
16801746
* Whether past and future days should have two different background colors. The past and
16811747
* future day colors are defined by the attributes `futureBackgroundColor` and

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

+1
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,6 @@
5151
<attr name="verticalFlingEnabled" format="boolean"/>
5252
<attr name="allDayEventHeight" format="dimension"/>
5353
<attr name="scrollDuration" format="integer"/>
54+
<attr name="autoLimitTime" format="boolean"/>
5455
</declare-styleable>
5556
</resources>

0 commit comments

Comments
 (0)