Skip to content

Commit 8bc63c7

Browse files
committed
Support for date time string interpreter added
1 parent 009aa54 commit 8bc63c7

File tree

2 files changed

+78
-39
lines changed

2 files changed

+78
-39
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.alamkanak.weekview;
2+
3+
import java.util.Calendar;
4+
5+
/**
6+
* Created by Raquib on 1/6/2015.
7+
*/
8+
public interface DateTimeInterpreter {
9+
String interpretDate(Calendar date);
10+
String interpretTime(int hour);
11+
}

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

Lines changed: 67 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import android.graphics.Rect;
1010
import android.graphics.RectF;
1111
import android.graphics.Typeface;
12+
import android.support.annotation.NonNull;
1213
import android.support.v4.view.GestureDetectorCompat;
1314
import android.support.v4.view.ViewCompat;
1415
import android.text.Layout;
@@ -25,6 +26,7 @@
2526
import android.widget.OverScroller;
2627
import android.widget.Scroller;
2728

29+
import java.text.SimpleDateFormat;
2830
import java.util.ArrayList;
2931
import java.util.Calendar;
3032
import java.util.Collections;
@@ -37,7 +39,9 @@
3739
*/
3840
public class WeekView extends View {
3941

42+
@Deprecated
4043
public static final int LENGTH_SHORT = 1;
44+
@Deprecated
4145
public static final int LENGTH_LONG = 2;
4246
private final Context mContext;
4347
private Calendar mToday;
@@ -91,7 +95,7 @@ public class WeekView extends View {
9195
private int mHeaderColumnBackgroundColor = Color.WHITE;
9296
private int mDefaultEventColor;
9397
private boolean mIsFirstDraw = true;
94-
private int mDayNameLength = LENGTH_LONG;
98+
@Deprecated private int mDayNameLength = LENGTH_LONG;
9599
private int mOverlappingEventGap = 0;
96100
private int mEventMarginVertical = 0;
97101
private float mXScrollingSpeed = 1f;
@@ -103,6 +107,7 @@ public class WeekView extends View {
103107
private EventLongPressListener mEventLongPressListener;
104108
private MonthChangeListener mMonthChangeListener;
105109
private TimeClickListener mTimeClickListener;
110+
private DateTimeInterpreter mDateTimeInterpreter;
106111

107112
private final GestureDetector.SimpleOnGestureListener mGestureListener = new GestureDetector.SimpleOnGestureListener() {
108113

@@ -193,7 +198,6 @@ public void onLongPress(MotionEvent e) {
193198
}
194199
};
195200

196-
197201
private enum Direction {
198202
NONE, HORIZONTAL, VERTICAL
199203
}
@@ -352,7 +356,10 @@ private void drawTimeColumnAndAxes(Canvas canvas) {
352356
float top = mHeaderTextHeight + mHeaderRowPadding * 2 + mCurrentOrigin.y + mHourHeight * i + mHeaderMarginBottom;
353357

354358
// 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.
355-
if (top < getHeight()) canvas.drawText(getTimeString(i), mTimeTextWidth + mHeaderColumnPadding, top + mTimeTextHeight, mTimeTextPaint);
359+
String time = getDateTimeInterpreter().interpretTime(i);
360+
if (time == null)
361+
throw new IllegalStateException("A DateTimeInterpreter must not return null time");
362+
if (top < getHeight()) canvas.drawText(time, mTimeTextWidth + mHeaderColumnPadding, top + mTimeTextHeight, mTimeTextPaint);
356363
}
357364
}
358365

@@ -456,7 +463,9 @@ private void drawHeaderRowAndEvents(Canvas canvas) {
456463
boolean sameDay = isSameDay(day, mToday);
457464

458465
// Draw the day labels.
459-
String dayLabel = String.format("%s %d/%02d", getDayName(day), day.get(Calendar.MONTH) + 1, day.get(Calendar.DAY_OF_MONTH));
466+
String dayLabel = getDateTimeInterpreter().interpretDate(day);
467+
if (dayLabel == null)
468+
throw new IllegalStateException("A DateTimeInterpreter must not return null date");
460469
canvas.drawText(dayLabel, startPixel + mWidthPerDay / 2, mHeaderTextHeight + mHeaderRowPadding, sameDay ? mTodayHeaderTextPaint : mHeaderTextPaint);
461470
startPixel += mWidthPerDay + mColumnGap;
462471
}
@@ -917,12 +926,54 @@ public void setEventLongPressListener(EventLongPressListener eventLongPressListe
917926
this.mEventLongPressListener = eventLongPressListener;
918927
}
919928

929+
public void setHourClickListener(TimeClickListener mTimeClickListener){
930+
this.mTimeClickListener = mTimeClickListener;
931+
}
932+
920933
public TimeClickListener getHourClickListener(){
921934
return mTimeClickListener;
922935
}
923936

924-
public void setHourClickListener(TimeClickListener mTimeClickListener){
925-
this.mTimeClickListener = mTimeClickListener;
937+
/**
938+
* Get the interpreter which provides the text to show in the header column and the header row.
939+
* @return The date, time interpreter.
940+
*/
941+
public @NonNull DateTimeInterpreter getDateTimeInterpreter() {
942+
if (mDateTimeInterpreter == null) {
943+
mDateTimeInterpreter = new DateTimeInterpreter() {
944+
@Override
945+
public String interpretDate(Calendar date) {
946+
SimpleDateFormat sdf;
947+
sdf = mDayNameLength == LENGTH_SHORT ? new SimpleDateFormat("EEEEE") : new SimpleDateFormat("EEE");
948+
try{
949+
String dayName = sdf.format(date.getTime()).toUpperCase();
950+
return String.format("%s %d/%02d", dayName, date.get(Calendar.MONTH) + 1, date.get(Calendar.DAY_OF_MONTH));
951+
}catch (Exception e){
952+
e.printStackTrace();
953+
return "";
954+
}
955+
}
956+
957+
@Override
958+
public String interpretTime(int hour) {
959+
String amPm;
960+
if (hour >= 0 && hour < 12) amPm = "AM";
961+
else amPm = "PM";
962+
if (hour == 0) hour = 12;
963+
if (hour > 12) hour -= 12;
964+
return String.format("%02d %s", hour, amPm);
965+
}
966+
};
967+
}
968+
return mDateTimeInterpreter;
969+
}
970+
971+
/**
972+
* Set the interpreter which provides the text to show in the header column and the header row.
973+
* @param dateTimeInterpreter The date, time interpreter.
974+
*/
975+
public void setDateTimeInterpreter(DateTimeInterpreter dateTimeInterpreter){
976+
this.mDateTimeInterpreter = dateTimeInterpreter;
926977
}
927978

928979

@@ -1123,16 +1174,26 @@ public void setDefaultEventColor(int defaultEventColor) {
11231174
invalidate();
11241175
}
11251176

1177+
/**
1178+
* <b>Note:</b> Use {@link #setDateTimeInterpreter(DateTimeInterpreter)} and
1179+
* {@link #getDateTimeInterpreter()} instead.
1180+
* @return
1181+
*/
1182+
@Deprecated
11261183
public int getDayNameLength() {
11271184
return mDayNameLength;
11281185
}
11291186

11301187
/**
11311188
* Set the length of the day name displayed in the header row. Example of short day names is
11321189
* 'M' for 'Monday' and example of long day names is 'Mon' for 'Monday'.
1190+
* <p>
1191+
* <b>Note:</b> Use {@link #setDateTimeInterpreter(DateTimeInterpreter)} instead.
1192+
* </p>
11331193
* @param length Supported values are {@link com.alamkanak.weekview.WeekView#LENGTH_SHORT} and
11341194
* {@link com.alamkanak.weekview.WeekView#LENGTH_LONG}.
11351195
*/
1196+
@Deprecated
11361197
public void setDayNameLength(int length) {
11371198
if (length != LENGTH_LONG && length != LENGTH_SHORT) {
11381199
throw new IllegalArgumentException("length parameter must be either LENGTH_LONG or LENGTH_SHORT");
@@ -1357,22 +1418,6 @@ private boolean containsValue(int[] list, int value) {
13571418
return false;
13581419
}
13591420

1360-
1361-
/**
1362-
* Converts an int (0-23) to time string (e.g. 12 PM).
1363-
* @param hour The time. Limit: 0-23.
1364-
* @return The string representation of the time.
1365-
*/
1366-
private String getTimeString(int hour) {
1367-
String amPm;
1368-
if (hour >= 0 && hour < 12) amPm = "AM";
1369-
else amPm = "PM";
1370-
if (hour == 0) hour = 12;
1371-
if (hour > 12) hour -= 12;
1372-
return String.format("%02d %s", hour, amPm);
1373-
}
1374-
1375-
13761421
/**
13771422
* Checks if two times are on the same day.
13781423
* @param dayOne The first day.
@@ -1383,21 +1428,4 @@ private boolean isSameDay(Calendar dayOne, Calendar dayTwo) {
13831428
return dayOne.get(Calendar.YEAR) == dayTwo.get(Calendar.YEAR) && dayOne.get(Calendar.DAY_OF_YEAR) == dayTwo.get(Calendar.DAY_OF_YEAR);
13841429
}
13851430

1386-
1387-
/**
1388-
* Get the day name of a given date.
1389-
* @param date The date.
1390-
* @return The first the characters of the day name.
1391-
*/
1392-
private String getDayName(Calendar date) {
1393-
int dayOfWeek = date.get(Calendar.DAY_OF_WEEK);
1394-
if (Calendar.MONDAY == dayOfWeek) return (mDayNameLength == LENGTH_SHORT ? "M" : "MON");
1395-
else if (Calendar.TUESDAY == dayOfWeek) return (mDayNameLength == LENGTH_SHORT ? "T" : "TUE");
1396-
else if (Calendar.WEDNESDAY == dayOfWeek) return (mDayNameLength == LENGTH_SHORT ? "W" : "WED");
1397-
else if (Calendar.THURSDAY == dayOfWeek) return (mDayNameLength == LENGTH_SHORT ? "T" : "THU");
1398-
else if (Calendar.FRIDAY == dayOfWeek) return (mDayNameLength == LENGTH_SHORT ? "F" : "FRI");
1399-
else if (Calendar.SATURDAY == dayOfWeek) return (mDayNameLength == LENGTH_SHORT ? "S" : "SAT");
1400-
else if (Calendar.SUNDAY == dayOfWeek) return (mDayNameLength == LENGTH_SHORT ? "S" : "SUN");
1401-
return "";
1402-
}
14031431
}

0 commit comments

Comments
 (0)