Skip to content

Commit

Permalink
show half hour marks on the left-hand column
Browse files Browse the repository at this point in the history
  • Loading branch information
dgrant committed Dec 22, 2015
1 parent 2f1e6e3 commit 528565c
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 15 deletions.
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);
}
55 changes: 45 additions & 10 deletions library/src/main/java/com/alamkanak/weekview/WeekView.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ public class WeekView extends View {
private boolean mShowDistinctWeekendColor = false;
private boolean mShowNowLine = false;
private boolean mShowDistinctPastFutureColor = false;
private boolean showHalfHours = false;

// Listeners.
private EventClickListener mEventClickListener;
Expand Down Expand Up @@ -303,7 +304,7 @@ public WeekView(Context context, AttributeSet attrs, int defStyleAttr) {
mShowDistinctPastFutureColor = a.getBoolean(R.styleable.WeekView_showDistinctPastFutureColor, mShowDistinctPastFutureColor);
mShowDistinctWeekendColor = a.getBoolean(R.styleable.WeekView_showDistinctWeekendColor, mShowDistinctWeekendColor);
mShowNowLine = a.getBoolean(R.styleable.WeekView_showNowLine, mShowNowLine);

showHalfHours = a.getBoolean(R.styleable.WeekView_showHalfHours, showHalfHours);
} finally {
a.recycle();
}
Expand All @@ -322,7 +323,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 @@ -332,7 +335,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 @@ -427,7 +430,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 @@ -455,11 +458,34 @@ private void drawTimeColumnAndAxes(Canvas canvas) {
// Draw the background color for the header column.
canvas.drawRect(0, mHeaderTextHeight + mHeaderRowPadding * 2, mHeaderColumnWidth, getHeight(), mHeaderColumnBackgroundPaint);

for (int i = 0; i < 24; i++) {
float top = mHeaderTextHeight + 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 + timeSpacing * 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 @@ -1201,13 +1227,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
3 changes: 2 additions & 1 deletion library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,6 @@
<attr name="showNowLine" format="boolean"/>
<attr name="nowLineColor" format="color"/>
<attr name="nowLineThickness" format="dimension"/>
<attr name="showHalfHours" format="boolean" />
</declare-styleable>
</resources>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.graphics.RectF;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MenuItem;
Expand All @@ -13,6 +14,7 @@
import com.alamkanak.weekview.WeekView;
import com.alamkanak.weekview.WeekViewEvent;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
Expand Down Expand Up @@ -134,8 +136,18 @@ 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 Expand Up @@ -244,6 +256,19 @@ public List<WeekViewEvent> onMonthChange(int newYear, int newMonth) {
event.setColor(getResources().getColor(R.color.event_color_02));
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
3 changes: 2 additions & 1 deletion sample/src/main/res/layout/activity_main.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>

0 comments on commit 528565c

Please sign in to comment.