Skip to content

Commit

Permalink
Some fixes and added timeColumnResolution
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoobergs committed Feb 12, 2017
1 parent 56aba4f commit d498da8
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 34 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ You can customize the look of the `WeekView` in xml. Use the following attribute
- `showDistinctPastFutureColor`
- `showDistinctWeekendColor`
- `showFirstDayOfWeekFirst`
- `showHalfHours`
- `showNowLine`
- `textSize`
- `timeColumnResolution`
- `todayBackgroundColor`
- `todayHeaderTextColor`
- `verticalFlingEnabled`
Expand Down
69 changes: 38 additions & 31 deletions library/src/main/java/com/alamkanak/weekview/WeekView.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@
import java.util.List;
import java.util.Locale;

import static com.alamkanak.weekview.WeekViewUtil.*;
import static com.alamkanak.weekview.WeekViewUtil.daysBetween;
import static com.alamkanak.weekview.WeekViewUtil.getPassedMinutesInDay;
import static com.alamkanak.weekview.WeekViewUtil.isSameDay;
import static com.alamkanak.weekview.WeekViewUtil.today;

/**
* Created by Raquib-ul-Alam Kanak on 7/21/2014.
Expand Down Expand Up @@ -163,7 +166,7 @@ private enum Direction {
private float mZoomFocusPoint = 0;
private boolean mZoomFocusPointEnabled = true;
private int mScrollDuration = 250;
private boolean mShowHalfHours = false;
private int mTimeColumnResolution = 60;
private Typeface mTypeface = Typeface.DEFAULT_BOLD;
private int mMinTime = 0;
private int mMaxTime = 24;
Expand Down Expand Up @@ -312,7 +315,7 @@ public boolean onSingleTapConfirmed(MotionEvent e) {
// If the tap was on an empty space, then trigger the callback.
if ((mEmptyViewClickListener != null || mAddEventClickListener != null) && e.getX() > mHeaderColumnWidth && e.getY() > (mHeaderHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom)) {
Calendar selectedTime = getTimeFromPoint(e.getX(), e.getY());
List<EventRect> tempEventRects = mEventRects;
List<EventRect> tempEventRects = new ArrayList<>(mEventRects);
mEventRects = new ArrayList<EventRect>();
if (selectedTime != null) {
if(mNewEventRect != null) {
Expand All @@ -334,10 +337,11 @@ public boolean onSingleTapConfirmed(MotionEvent e) {
endTime.add(Calendar.MINUTE, Math.min(mNewEventLengthInMinutes, (24-selectedTime.get(Calendar.HOUR_OF_DAY))*60 - selectedTime.get(Calendar.MINUTE)));
WeekViewEvent newEvent = new WeekViewEvent(mNewEventId, "", null, selectedTime, endTime);

int marginTop = mHourHeight * mMinTime;
float top = selectedTime.get(Calendar.HOUR_OF_DAY) * 60;
top = mHourHeight * 24 * top / 1440 + mCurrentOrigin.y + mHeaderHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom + mTimeTextHeight / 2 + mEventMarginVertical;
top = mHourHeight * top / 60 + mCurrentOrigin.y + mHeaderHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom + mTimeTextHeight / 2 + mEventMarginVertical - marginTop;
float bottom = endTime.get(Calendar.HOUR_OF_DAY) * 60;
bottom = mHourHeight * 24 * bottom / 1440 + mCurrentOrigin.y + mHeaderHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom + mTimeTextHeight / 2 - mEventMarginVertical;
bottom = mHourHeight * bottom / 60 + mCurrentOrigin.y + mHeaderHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom + mTimeTextHeight / 2 - mEventMarginVertical - marginTop;

// Calculate left and right.
float left = 0;
Expand All @@ -353,11 +357,11 @@ top < getHeight() &&
newEvent.setColor(mNewEventColor);
mNewEventRect = new EventRect(newEvent, newEvent, dayRectF);
tempEventRects.add(mNewEventRect);
invalidate();
computePositionOfEvents(tempEventRects);
}
}
}
computePositionOfEvents(tempEventRects);
invalidate();
}
return super.onSingleTapConfirmed(e);
}
Expand Down Expand Up @@ -453,7 +457,7 @@ public WeekView(Context context, AttributeSet attrs, int defStyleAttr) {
mZoomFocusPoint = a.getFraction(R.styleable.WeekView_zoomFocusPoint, 1, 1, mZoomFocusPoint);
mZoomFocusPointEnabled = a.getBoolean(R.styleable.WeekView_zoomFocusPointEnabled, mZoomFocusPointEnabled);
mScrollDuration = a.getInt(R.styleable.WeekView_scrollDuration, mScrollDuration);
mShowHalfHours = a.getBoolean(R.styleable.WeekView_showHalfHours, mShowHalfHours);
mTimeColumnResolution = a.getInt(R.styleable.WeekView_timeColumnResolution, mTimeColumnResolution);
mAutoLimitTime = a.getBoolean(R.styleable.WeekView_autoLimitTime, mAutoLimitTime);
mMinTime = a.getInt(R.styleable.WeekView_minTime, mMinTime);
mMaxTime = a.getInt(R.styleable.WeekView_maxTime, mMaxTime);
Expand All @@ -480,7 +484,7 @@ private void init() {
mTimeTextPaint.setTextSize(mTextSize);
mTimeTextPaint.setColor(mHeaderColumnTextColor);
Rect rect = new Rect();
final String exampleTime = mShowHalfHours ? "00:00 PM" : "00 PM";
final String exampleTime = (mTimeColumnResolution % 60 != 0) ? "00:00 PM" : "00 PM";
mTimeTextPaint.getTextBounds(exampleTime, 0, exampleTime.length(), rect);
mTimeTextWidth = mTimeTextPaint.measureText(exampleTime);
mTimeTextHeight = rect.height();
Expand Down Expand Up @@ -595,11 +599,11 @@ private float getXOriginForDate(Calendar date) {
}

private int getNumberOfPeriods(){
return (mMaxTime - mMinTime) * (mShowHalfHours ? 2 : 1);
return (int) ((mMaxTime - mMinTime) * (60.0 / mTimeColumnResolution));
}

private float getYMinLimit() {
return -(mHourHeight * 24
return -(mHourHeight * (mMaxTime - mMinTime)
+ mHeaderHeight
+ mHeaderRowPadding *2
+ mHeaderMarginBottom
Expand Down Expand Up @@ -709,15 +713,18 @@ private void drawTimeColumnAndAxes(Canvas canvas) {
float timeSpacing;
int minutes;
int hour;
if (mShowHalfHours) {
timeSpacing = mHourHeight / 2f;
hour = mMinTime + i / 2;
minutes = i%2 * 30;
} else {

float timesPerHour = (float) 60.0 / mTimeColumnResolution;

//if (mShowHalfHours) {
timeSpacing = mHourHeight / timesPerHour;
hour = mMinTime + i / (int) (timesPerHour);
minutes = i % ((int) timesPerHour) * (60 / (int) timesPerHour);
/*} else {
timeSpacing = mHourHeight;
hour = mMinTime + i;
minutes = 0;
}
}*/

// Calculate the top of the rectangle where the time text will go
float top = mHeaderHeight + mHeaderRowPadding * 2 + mCurrentOrigin.y + timeSpacing * i + mHeaderMarginBottom;
Expand All @@ -742,7 +749,7 @@ private void drawHeaderRowAndEvents(Canvas canvas) {
Calendar today = today();

if (mAreDimensionsInvalid) {
mEffectiveMinHourHeight = Math.max(mMinHourHeight, (int) ((getHeight() - mHeaderHeight - mHeaderRowPadding * 2 - mHeaderMarginBottom) / 24));
mEffectiveMinHourHeight = Math.max(mMinHourHeight, (int) ((getHeight() - mHeaderHeight - mHeaderRowPadding * 2 - mHeaderMarginBottom) / (mMaxTime - mMinTime)));

mAreDimensionsInvalid = false;
if (mScrollToDay != null)
Expand Down Expand Up @@ -1028,11 +1035,11 @@ private void drawEvents(Calendar date, float startFromPixel, Canvas canvas) {

int marginTop = mHourHeight * mMinTime;
// Calculate top.
float top = mHourHeight * 24 * mEventRects.get(i).top / 1440 + mCurrentOrigin.y + mHeaderHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom + mTimeTextHeight/2 + mEventMarginVertical - marginTop;
float top = mHourHeight * mEventRects.get(i).top / 60 + 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 - marginTop;
bottom = mHourHeight * bottom / 60 + mCurrentOrigin.y + mHeaderHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom + mTimeTextHeight/2 - mEventMarginVertical - marginTop;

// Calculate left and right.
float left = startFromPixel + mEventRects.get(i).left * mWidthPerDay;
Expand All @@ -1055,7 +1062,7 @@ top < getHeight() &&
canvas.drawRoundRect(mEventRects.get(i).rectF, mEventCornerRadius, mEventCornerRadius, mEventBackgroundPaint);
float topToUse = top;
if(mEventRects.get(i).event.getStartTime().get(Calendar.HOUR_OF_DAY) < mMinTime)
topToUse = mHourHeight * 24 * getPassedMinutesInDay(mMinTime, 0) / 1440 + mCurrentOrigin.y + mHeaderHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom + mTimeTextHeight/2 + mEventMarginVertical - marginTop;
topToUse = mHourHeight * getPassedMinutesInDay(mMinTime, 0) / 60 + mCurrentOrigin.y + mHeaderHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom + mTimeTextHeight/2 + mEventMarginVertical - marginTop;

if(mEventRects.get(i).event.getId() != mNewEventId)
drawEventTitle(mEventRects.get(i).event, mEventRects.get(i).rectF, canvas, topToUse, left);
Expand Down Expand Up @@ -1560,12 +1567,12 @@ public ScrollListener getScrollListener(){
return mScrollListener;
}

public void setShowHalfHours(boolean showHalfHours){
mShowHalfHours = showHalfHours;
public void setTimeColumnResolution(int resolution){
mTimeColumnResolution = resolution;
}

public boolean isShowHalfHours(){
return mShowHalfHours;
public int getTimeColumnResolution(){
return mTimeColumnResolution;
}

public void setAddEventClickListener(AddEventClickListener addEventClickListener){
Expand Down Expand Up @@ -1605,7 +1612,7 @@ public String interpretTime(int hour, int minutes) {
if (DateFormat.is24HourFormat(getContext())) {
sdf = new SimpleDateFormat("HH:mm", Locale.getDefault());
} else {
if (mShowHalfHours) {
if ((mTimeColumnResolution % 60 != 0)) {
sdf = new SimpleDateFormat("hh:mm a", Locale.getDefault());
} else {
sdf = new SimpleDateFormat("hh a", Locale.getDefault());
Expand Down Expand Up @@ -2454,13 +2461,13 @@ public void goToHour(double hour){
}

int verticalOffset = 0;
if (hour > 24)
verticalOffset = mHourHeight * 24;
else if (hour > 0)
if (hour > mMaxTime)
verticalOffset = mHourHeight * (mMaxTime - mMinTime);
else if (hour > mMinTime)
verticalOffset = (int) (mHourHeight * hour);

if (verticalOffset > mHourHeight * 24 - getHeight() + mHeaderHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom)
verticalOffset = (int)(mHourHeight * 24 - getHeight() + mHeaderHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom);
if (verticalOffset > mHourHeight * (mMaxTime - mMinTime) - getHeight() + mHeaderHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom)
verticalOffset = (int)(mHourHeight * (mMaxTime - mMinTime) - getHeight() + mHeaderHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom);

mCurrentOrigin.y = -verticalOffset;
invalidate();
Expand Down
2 changes: 1 addition & 1 deletion library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@
<attr name="showDistinctPastFutureColor" format="boolean"/>
<attr name="showDistinctWeekendColor" format="boolean"/>
<attr name="showFirstDayOfWeekFirst" format="boolean"/>
<attr name="showHalfHours" format="boolean" />
<attr name="showNowLine" format="boolean"/>
<attr name="textSize" format="dimension"/>
<attr name="timeColumnResolution" format="integer" />
<attr name="todayBackgroundColor" format="color"/>
<attr name="todayHeaderTextColor" format="color"/>
<attr name="verticalFlingEnabled" format="boolean"/>
Expand Down
2 changes: 1 addition & 1 deletion sample/src/main/res/layout/activity_base.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@
app:headerColumnBackground="#ffffffff"
app:todayHeaderTextColor="@color/accent"
app:newEventTimeResolutionInMinutes="15"
app:showHalfHours="true" />
app:timeColumnResolution="60" />

</RelativeLayout>

0 comments on commit d498da8

Please sign in to comment.