Skip to content

show half hour marks on the left-hand column #193

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

Open
wants to merge 1 commit 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
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
*/
public interface DateTimeInterpreter {
String interpretDate(Calendar date);
String interpretTime(int hour);
String interpretTime(int hour, int minutes);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I create a new method with interpretTime(int hour, minutes) and leave the original interpretTime(int hour) to not break people?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, preferably.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hold on, that doesn't actually help. Just by adding the interpretTime(int hour, int minutes) to the interface will break an client implementing that interface. Only if we had a base class and people extended that, could we then add a method to the interface and not break consumer.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe have a "subinterface"?

Because a breaking change for this small behaviour change is maybe not what we want.

}
54 changes: 45 additions & 9 deletions library/src/main/java/com/alamkanak/weekview/WeekView.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ private enum Direction {
private boolean mShowDistinctPastFutureColor = false;
private boolean mHorizontalFlingEnabled = true;
private boolean mVerticalFlingEnabled = true;
private boolean showHalfHours = false;
private int mAllDayEventHeight = 100;
private int mScrollDuration = 250;

Expand Down Expand Up @@ -351,6 +352,7 @@ public WeekView(Context context, AttributeSet attrs, int defStyleAttr) {
mShowNowLine = a.getBoolean(R.styleable.WeekView_showNowLine, mShowNowLine);
mHorizontalFlingEnabled = a.getBoolean(R.styleable.WeekView_horizontalFlingEnabled, mHorizontalFlingEnabled);
mVerticalFlingEnabled = a.getBoolean(R.styleable.WeekView_verticalFlingEnabled, mVerticalFlingEnabled);
showHalfHours = a.getBoolean(R.styleable.WeekView_showHalfHours, showHalfHours);
mAllDayEventHeight = a.getDimensionPixelSize(R.styleable.WeekView_allDayEventHeight, mAllDayEventHeight);
mScrollDuration = a.getInt(R.styleable.WeekView_scrollDuration, mScrollDuration);
} finally {
Expand All @@ -374,7 +376,9 @@ private void init() {
mTimeTextPaint.setTextSize(mTextSize);
mTimeTextPaint.setColor(mHeaderColumnTextColor);
Rect rect = new Rect();
mTimeTextPaint.getTextBounds("00 PM", 0, "00 PM".length(), rect);
final String exampleTime = showHalfHours ? "00:00 PM" : "00 PM";
mTimeTextPaint.getTextBounds(exampleTime, 0, exampleTime.length(), rect);
mTimeTextWidth = mTimeTextPaint.measureText(exampleTime);
mTimeTextHeight = rect.height();
mHeaderMarginBottom = mTimeTextHeight / 2;
initTextTimeWidth();
Expand All @@ -384,7 +388,7 @@ private void init() {
mHeaderTextPaint.setColor(mHeaderColumnTextColor);
mHeaderTextPaint.setTextAlign(Paint.Align.CENTER);
mHeaderTextPaint.setTextSize(mTextSize);
mHeaderTextPaint.getTextBounds("00 PM", 0, "00 PM".length(), rect);
mHeaderTextPaint.getTextBounds(exampleTime, 0, exampleTime.length(), rect);
mHeaderTextHeight = rect.height();
mHeaderTextPaint.setTypeface(Typeface.DEFAULT_BOLD);

Expand Down Expand Up @@ -479,7 +483,7 @@ private void initTextTimeWidth() {
mTimeTextWidth = 0;
for (int i = 0; i < 24; i++) {
// Measure time string and get max width.
String time = getDateTimeInterpreter().interpretTime(i);
String time = getDateTimeInterpreter().interpretTime(i, 0);
if (time == null)
throw new IllegalStateException("A DateTimeInterpreter must not return null time");
mTimeTextWidth = Math.max(mTimeTextWidth, mTimeTextPaint.measureText(time));
Expand Down Expand Up @@ -533,11 +537,34 @@ 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;
int numPeriodsInDay = showHalfHours ? 48 : 24;
for (int i = 0; i < numPeriodsInDay; i++) {
// If we are showing half hours (eg. 5:30am), space the times out by half the hour height
// and need to provide 30 minutes on each odd period, otherwise, minutes is always 0.
int timeSpacing;
int minutes;
int hour;
if (showHalfHours) {
timeSpacing = mHourHeight / 2;
hour = i / 2;
if (i % 2 == 0) {
minutes = 0;
} else {
minutes = 30;
}
} else {
timeSpacing = mHourHeight;
hour = i;
minutes = 0;
}

// Calculate the top of the rectangle where the time text will go
float top = mHeaderTextHeight + mHeaderRowPadding * 2 + mCurrentOrigin.y + mHourHeight * i + mHeaderMarginBottom;

// Get the time to be displayed, as a String.
String time = getDateTimeInterpreter().interpretTime(hour, minutes);

// 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);
if (time == null)
throw new IllegalStateException("A DateTimeInterpreter must not return null time");
if (top < getHeight()) canvas.drawText(time, mTimeTextWidth + mHeaderColumnPadding, top + mTimeTextHeight, mTimeTextPaint);
Expand Down Expand Up @@ -1311,13 +1338,22 @@ public String interpretDate(Calendar date) {
}

@Override
public String interpretTime(int hour) {
public String interpretTime(int hour, int minutes) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.MINUTE, minutes);

try {
SimpleDateFormat sdf = DateFormat.is24HourFormat(getContext()) ? new SimpleDateFormat("HH:mm", Locale.getDefault()) : new SimpleDateFormat("hh a", Locale.getDefault());
SimpleDateFormat sdf;
if (DateFormat.is24HourFormat(getContext())) {
sdf = new SimpleDateFormat("HH:mm", Locale.getDefault());
} else {
if (showHalfHours) {
sdf = new SimpleDateFormat("hh:mm a", Locale.getDefault());
} else {
sdf = new SimpleDateFormat("hh a", Locale.getDefault());
}
}
return sdf.format(calendar.getTime());
} catch (Exception e) {
e.printStackTrace();
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 @@ -49,6 +49,7 @@
<attr name="nowLineThickness" format="dimension"/>
<attr name="horizontalFlingEnabled" format="boolean"/>
<attr name="verticalFlingEnabled" format="boolean"/>
<attr name="showHalfHours" format="boolean" />
<attr name="allDayEventHeight" format="dimension"/>
<attr name="scrollDuration" format="integer"/>
</declare-styleable>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,17 @@ public String interpretDate(Calendar date) {
}

@Override
public String interpretTime(int hour) {
return hour > 11 ? (hour - 12) + " PM" : (hour == 0 ? "12 AM" : hour + " AM");
public String interpretTime(int hour, int minutes) {
String strMinutes = String.format("%02d", minutes);
if (hour > 11) {
return (hour - 12) + ":" + strMinutes + " PM";
} else {
if (hour == 0) {
return "12:" + strMinutes + " AM";
} else {
return hour + ":" + strMinutes + " AM";
}
}
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,19 @@ public List<? extends WeekViewEvent> onMonthChange(int newYear, int newMonth) {
event.setColor(getResources().getColor(R.color.event_color_01));
events.add(event);

startTime = Calendar.getInstance();
startTime.set(Calendar.HOUR_OF_DAY, 18);
startTime.set(Calendar.MINUTE, 30);
startTime.set(Calendar.MONTH, newMonth-1);
startTime.set(Calendar.YEAR, newYear);
endTime = (Calendar) startTime.clone();
endTime.set(Calendar.HOUR_OF_DAY, 19);
endTime.set(Calendar.MINUTE, 30);
endTime.set(Calendar.MONTH, newMonth-1);
event = new WeekViewEvent(22, getEventTitle(startTime), startTime, endTime);
event.setColor(getResources().getColor(R.color.event_color_02));
events.add(event);

return events;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import android.support.v7.app.AppCompatActivity;
import android.view.View;


/**
* The launcher activity of the sample app. It contains the links to visit all the example screens.
* Created by Raquib-ul-Alam Kanak on 7/21/2014.
Expand Down Expand Up @@ -34,5 +33,4 @@ public void onClick(View v) {
}
});
}

}
3 changes: 2 additions & 1 deletion sample/src/main/res/layout/activity_base.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
app:dayBackgroundColor="#05000000"
app:todayBackgroundColor="#1848adff"
app:headerColumnBackground="#ffffffff"
app:todayHeaderTextColor="@color/accent" />
app:todayHeaderTextColor="@color/accent"
app:showHalfHours="false" />

</RelativeLayout>