From 06836e72cafbebca739ac5e02bb0af6efe96f92a Mon Sep 17 00:00:00 2001 From: David Grant Date: Fri, 27 Mar 2015 11:22:28 -0700 Subject: [PATCH] show half hour marks on the left-hand column --- .../weekview/DateTimeInterpreter.java | 2 +- .../java/com/alamkanak/weekview/WeekView.java | 54 +++++++++++++++---- library/src/main/res/values/attrs.xml | 1 + .../weekview/sample/BaseActivity.java | 13 ++++- .../weekview/sample/BasicActivity.java | 13 +++++ .../weekview/sample/MainActivity.java | 2 - sample/src/main/res/layout/activity_base.xml | 3 +- 7 files changed, 73 insertions(+), 15 deletions(-) diff --git a/library/src/main/java/com/alamkanak/weekview/DateTimeInterpreter.java b/library/src/main/java/com/alamkanak/weekview/DateTimeInterpreter.java index 383c5570d..8dced7794 100644 --- a/library/src/main/java/com/alamkanak/weekview/DateTimeInterpreter.java +++ b/library/src/main/java/com/alamkanak/weekview/DateTimeInterpreter.java @@ -7,5 +7,5 @@ */ public interface DateTimeInterpreter { String interpretDate(Calendar date); - String interpretTime(int hour); + String interpretTime(int hour, int minutes); } diff --git a/library/src/main/java/com/alamkanak/weekview/WeekView.java b/library/src/main/java/com/alamkanak/weekview/WeekView.java index da3fa71a0..499c243ed 100755 --- a/library/src/main/java/com/alamkanak/weekview/WeekView.java +++ b/library/src/main/java/com/alamkanak/weekview/WeekView.java @@ -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; @@ -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 { @@ -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(); @@ -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); @@ -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)); @@ -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); @@ -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(); diff --git a/library/src/main/res/values/attrs.xml b/library/src/main/res/values/attrs.xml index a37a3717b..2f5d84801 100644 --- a/library/src/main/res/values/attrs.xml +++ b/library/src/main/res/values/attrs.xml @@ -49,6 +49,7 @@ + diff --git a/sample/src/main/java/com/alamkanak/weekview/sample/BaseActivity.java b/sample/src/main/java/com/alamkanak/weekview/sample/BaseActivity.java index e311d1943..667190612 100644 --- a/sample/src/main/java/com/alamkanak/weekview/sample/BaseActivity.java +++ b/sample/src/main/java/com/alamkanak/weekview/sample/BaseActivity.java @@ -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"; + } + } } }); } diff --git a/sample/src/main/java/com/alamkanak/weekview/sample/BasicActivity.java b/sample/src/main/java/com/alamkanak/weekview/sample/BasicActivity.java index 1ef9a8903..e035c6f8b 100644 --- a/sample/src/main/java/com/alamkanak/weekview/sample/BasicActivity.java +++ b/sample/src/main/java/com/alamkanak/weekview/sample/BasicActivity.java @@ -157,6 +157,19 @@ public List 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; } diff --git a/sample/src/main/java/com/alamkanak/weekview/sample/MainActivity.java b/sample/src/main/java/com/alamkanak/weekview/sample/MainActivity.java index 678396799..dc20a1477 100644 --- a/sample/src/main/java/com/alamkanak/weekview/sample/MainActivity.java +++ b/sample/src/main/java/com/alamkanak/weekview/sample/MainActivity.java @@ -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. @@ -34,5 +33,4 @@ public void onClick(View v) { } }); } - } diff --git a/sample/src/main/res/layout/activity_base.xml b/sample/src/main/res/layout/activity_base.xml index 5f68c319d..1327fbb21 100644 --- a/sample/src/main/res/layout/activity_base.xml +++ b/sample/src/main/res/layout/activity_base.xml @@ -22,6 +22,7 @@ app:dayBackgroundColor="#05000000" app:todayBackgroundColor="#1848adff" app:headerColumnBackground="#ffffffff" - app:todayHeaderTextColor="@color/accent" /> + app:todayHeaderTextColor="@color/accent" + app:showHalfHours="false" />