From 14e9277dd2340b53f87420a5ca03b504d1f8e4b4 Mon Sep 17 00:00:00 2001 From: marunjar Date: Sat, 2 Jan 2016 08:22:53 +0100 Subject: [PATCH 01/18] add EmptyViewLongPressListener add Listener to sample app --- .../com/alamkanak/weekview/sample/MainActivity.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) 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 8ae323f34..74ddc6d3a 100644 --- a/sample/src/main/java/com/alamkanak/weekview/sample/MainActivity.java +++ b/sample/src/main/java/com/alamkanak/weekview/sample/MainActivity.java @@ -25,7 +25,8 @@ * Website: http://alamkanak.github.io/ */ public class MainActivity extends ActionBarActivity implements MonthLoader.MonthChangeListener, - WeekView.EventClickListener, WeekView.EventLongPressListener { + WeekView.EventClickListener, WeekView.EventLongPressListener, + WeekView.EmptyViewLongPressListener { private static final int TYPE_DAY_VIEW = 1; private static final int TYPE_THREE_DAY_VIEW = 2; @@ -51,6 +52,9 @@ protected void onCreate(Bundle savedInstanceState) { // Set long press listener for events. mWeekView.setEventLongPressListener(this); + // Set long press listener for empty view + mWeekView.setEmptyViewLongPressListener(this); + // Set up a date time interpreter to interpret how the date and time will be formatted in // the week view. This is optional. setupDateTimeInterpreter(false); @@ -260,4 +264,9 @@ public void onEventClick(WeekViewEvent event, RectF eventRect) { public void onEventLongPress(WeekViewEvent event, RectF eventRect) { Toast.makeText(MainActivity.this, "Long pressed event: " + event.getName(), Toast.LENGTH_SHORT).show(); } + + @Override + public void onEmptyViewLongPress(Calendar time) { + Toast.makeText(MainActivity.this, "Empty view long pressed: " + getEventTitle(time), Toast.LENGTH_SHORT).show(); + } } From 4ed40a1f8c31bbbe6bad0053e69e19311fe800c0 Mon Sep 17 00:00:00 2001 From: marunjar Date: Sat, 2 Jan 2016 08:58:22 +0100 Subject: [PATCH 02/18] slightly less instances of RectF simplify condition if event should be drawn: there were some duplicates instanciate new RectF only if event is visible --- .../main/java/com/alamkanak/weekview/WeekView.java | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/library/src/main/java/com/alamkanak/weekview/WeekView.java b/library/src/main/java/com/alamkanak/weekview/WeekView.java index 21f34b57e..f5674c793 100755 --- a/library/src/main/java/com/alamkanak/weekview/WeekView.java +++ b/library/src/main/java/com/alamkanak/weekview/WeekView.java @@ -748,15 +748,13 @@ private void drawEvents(Calendar date, float startFromPixel, Canvas canvas) { right -= mOverlappingEventGap; // Draw the event and the event name on top of it. - RectF eventRectF = new RectF(left, top, right, bottom); - if (bottom > mHeaderTextHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom + mTimeTextHeight/2 && left < right && - eventRectF.right > mHeaderColumnWidth && - eventRectF.left < getWidth() && - eventRectF.bottom > mHeaderTextHeight + mHeaderRowPadding * 2 + mTimeTextHeight / 2 + mHeaderMarginBottom && - eventRectF.top < getHeight() && - left < right + if (left < right && + left < getWidth() && + top < getHeight() && + right > mHeaderColumnWidth && + bottom > mHeaderTextHeight + mHeaderRowPadding * 2 + mTimeTextHeight / 2 + mHeaderMarginBottom ) { - mEventRects.get(i).rectF = eventRectF; + mEventRects.get(i).rectF = new RectF(left, top, right, bottom); mEventBackgroundPaint.setColor(mEventRects.get(i).event.getColor() == 0 ? mDefaultEventColor : mEventRects.get(i).event.getColor()); canvas.drawRoundRect(mEventRects.get(i).rectF, mEventCornerRadius, mEventCornerRadius, mEventBackgroundPaint); drawEventTitle(mEventRects.get(i).event, mEventRects.get(i).rectF, canvas, top, left); From 4363c63e681f071d91797a3ad5a310eb1b3e5707 Mon Sep 17 00:00:00 2001 From: marunjar Date: Sat, 2 Jan 2016 21:30:40 +0100 Subject: [PATCH 03/18] fix wrong value assigned to EventPadding from xml --- library/src/main/java/com/alamkanak/weekview/WeekView.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/src/main/java/com/alamkanak/weekview/WeekView.java b/library/src/main/java/com/alamkanak/weekview/WeekView.java index 3e662cef3..87a83b540 100755 --- a/library/src/main/java/com/alamkanak/weekview/WeekView.java +++ b/library/src/main/java/com/alamkanak/weekview/WeekView.java @@ -324,7 +324,7 @@ public WeekView(Context context, AttributeSet attrs, int defStyleAttr) { mTodayHeaderTextColor = a.getColor(R.styleable.WeekView_todayHeaderTextColor, mTodayHeaderTextColor); mEventTextSize = a.getDimensionPixelSize(R.styleable.WeekView_eventTextSize, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, mEventTextSize, context.getResources().getDisplayMetrics())); mEventTextColor = a.getColor(R.styleable.WeekView_eventTextColor, mEventTextColor); - mEventPadding = a.getDimensionPixelSize(R.styleable.WeekView_hourSeparatorHeight, mEventPadding); + mEventPadding = a.getDimensionPixelSize(R.styleable.WeekView_eventPadding, mEventPadding); mHeaderColumnBackgroundColor = a.getColor(R.styleable.WeekView_headerColumnBackground, mHeaderColumnBackgroundColor); mDayNameLength = a.getInteger(R.styleable.WeekView_dayNameLength, mDayNameLength); mOverlappingEventGap = a.getDimensionPixelSize(R.styleable.WeekView_overlappingEventGap, mOverlappingEventGap); From 4a21f272f4f7477701bae99cbc474ef3c4293f4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=BE=D0=BB=D0=B0=D0=B9?= Date: Tue, 26 May 2015 15:04:39 +0300 Subject: [PATCH 04/18] Update Paints Update Paints in setters --- .../src/main/java/com/alamkanak/weekview/WeekView.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/library/src/main/java/com/alamkanak/weekview/WeekView.java b/library/src/main/java/com/alamkanak/weekview/WeekView.java index 87a83b540..13378f6af 100755 --- a/library/src/main/java/com/alamkanak/weekview/WeekView.java +++ b/library/src/main/java/com/alamkanak/weekview/WeekView.java @@ -1357,6 +1357,8 @@ public int getHeaderColumnTextColor() { public void setHeaderColumnTextColor(int headerColumnTextColor) { mHeaderColumnTextColor = headerColumnTextColor; + mHeaderTextPaint.setColor(mHeaderColumnTextColor); + mTimeTextPaint.setColor(mHeaderColumnTextColor); invalidate(); } @@ -1375,6 +1377,7 @@ public int getHeaderRowBackgroundColor() { public void setHeaderRowBackgroundColor(int headerRowBackgroundColor) { mHeaderRowBackgroundColor = headerRowBackgroundColor; + mHeaderBackgroundPaint.setColor(mHeaderRowBackgroundColor); invalidate(); } @@ -1384,6 +1387,7 @@ public int getDayBackgroundColor() { public void setDayBackgroundColor(int dayBackgroundColor) { mDayBackgroundColor = dayBackgroundColor; + mDayBackgroundPaint.setColor(mDayBackgroundColor); invalidate(); } @@ -1393,6 +1397,7 @@ public int getHourSeparatorColor() { public void setHourSeparatorColor(int hourSeparatorColor) { mHourSeparatorColor = hourSeparatorColor; + mHourSeparatorPaint.setColor(mHourSeparatorColor); invalidate(); } @@ -1402,6 +1407,7 @@ public int getTodayBackgroundColor() { public void setTodayBackgroundColor(int todayBackgroundColor) { mTodayBackgroundColor = todayBackgroundColor; + mTodayBackgroundPaint.setColor(mTodayBackgroundColor); invalidate(); } @@ -1411,6 +1417,7 @@ public int getHourSeparatorHeight() { public void setHourSeparatorHeight(int hourSeparatorHeight) { mHourSeparatorHeight = hourSeparatorHeight; + mHourSeparatorPaint.setStrokeWidth(mHourSeparatorHeight); invalidate(); } @@ -1420,6 +1427,7 @@ public int getTodayHeaderTextColor() { public void setTodayHeaderTextColor(int todayHeaderTextColor) { mTodayHeaderTextColor = todayHeaderTextColor; + mTodayHeaderTextPaint.setColor(mTodayHeaderTextColor); invalidate(); } @@ -1439,6 +1447,7 @@ public int getEventTextColor() { public void setEventTextColor(int eventTextColor) { mEventTextColor = eventTextColor; + mEventTextPaint.setColor(mEventTextColor); invalidate(); } @@ -1457,6 +1466,7 @@ public int getHeaderColumnBackgroundColor() { public void setHeaderColumnBackgroundColor(int headerColumnBackgroundColor) { mHeaderColumnBackgroundColor = headerColumnBackgroundColor; + mHeaderColumnBackgroundPaint.setColor(mHeaderColumnBackgroundColor); invalidate(); } From a028e2870de0e73edaa853247914ed598334c600 Mon Sep 17 00:00:00 2001 From: Glenn Schmidt Date: Tue, 28 Oct 2014 17:36:29 +1100 Subject: [PATCH 05/18] Ability to turn off fling gesture Fling can be disabled for the horizontal or vertical axis. --- .../java/com/alamkanak/weekview/WeekView.java | 26 +++++++++++++++++++ library/src/main/res/values/attrs.xml | 4 ++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/library/src/main/java/com/alamkanak/weekview/WeekView.java b/library/src/main/java/com/alamkanak/weekview/WeekView.java index 13378f6af..3d3b02c10 100755 --- a/library/src/main/java/com/alamkanak/weekview/WeekView.java +++ b/library/src/main/java/com/alamkanak/weekview/WeekView.java @@ -136,6 +136,8 @@ private enum Direction { private boolean mShowDistinctWeekendColor = false; private boolean mShowNowLine = false; private boolean mShowDistinctPastFutureColor = false; + private boolean mHorizontalFlingEnabled = true; + private boolean mVerticalFlingEnabled = true; // Listeners. private EventClickListener mEventClickListener; @@ -210,6 +212,11 @@ public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float ve if (mIsZooming) return true; + if ((mCurrentFlingDirection == Direction.HORIZONTAL && !mHorizontalFlingEnabled) + || (mCurrentFlingDirection == Direction.VERTICAL && !mVerticalFlingEnabled)) { + return false; + } + mScroller.forceFinished(true); mCurrentFlingDirection = mCurrentScrollDirection; @@ -331,10 +338,13 @@ public WeekView(Context context, AttributeSet attrs, int defStyleAttr) { mEventMarginVertical = a.getDimensionPixelSize(R.styleable.WeekView_eventMarginVertical, mEventMarginVertical); mXScrollingSpeed = a.getFloat(R.styleable.WeekView_xScrollingSpeed, mXScrollingSpeed); mEventCornerRadius = a.getDimensionPixelSize(R.styleable.WeekView_eventCornerRadius, mEventCornerRadius); + mShowDistinctPastFutureColor = a.getBoolean(R.styleable.WeekView_showDistinctPastFutureColor, mShowDistinctPastFutureColor); mShowDistinctWeekendColor = a.getBoolean(R.styleable.WeekView_showDistinctWeekendColor, mShowDistinctWeekendColor); mShowNowLine = a.getBoolean(R.styleable.WeekView_showNowLine, mShowNowLine); + mHorizontalFlingEnabled = a.getBoolean(R.styleable.WeekView_horizontalFlingEnabled, mHorizontalFlingEnabled); + mVerticalFlingEnabled = a.getBoolean(R.styleable.WeekView_verticalFlingEnabled, mVerticalFlingEnabled); } finally { a.recycle(); } @@ -1674,6 +1684,22 @@ public void setNowLineThickness(int nowLineThickness) { invalidate(); } + public boolean isHorizontalFlingEnabled() { + return mHorizontalFlingEnabled; + } + + public void setHorizontalFlingEnabled(boolean enabled) { + mHorizontalFlingEnabled = enabled; + } + + public boolean isVerticalFlingEnabled() { + return mVerticalFlingEnabled; + } + + public void setVerticalFlingEnabled(boolean enabled) { + mVerticalFlingEnabled = enabled; + } + ///////////////////////////////////////////////////////////////// // // Functions related to scrolling. diff --git a/library/src/main/res/values/attrs.xml b/library/src/main/res/values/attrs.xml index fccad1635..6db63dd96 100644 --- a/library/src/main/res/values/attrs.xml +++ b/library/src/main/res/values/attrs.xml @@ -46,5 +46,7 @@ + + - \ No newline at end of file + From c70875bbe6bc478e0570c4f032a9aa0a7e2f1bdf Mon Sep 17 00:00:00 2001 From: Alam Kanak Date: Sun, 3 Jan 2016 18:32:53 +0600 Subject: [PATCH 06/18] Add new activity for asynchronous example --- sample/src/main/AndroidManifest.xml | 9 + .../weekview/sample/AsynchronousActivity.java | 13 + .../weekview/sample/BaseActivity.java | 159 +++++++++++ .../weekview/sample/BasicActivity.java | 120 ++++++++ .../weekview/sample/MainActivity.java | 261 +----------------- sample/src/main/res/layout/activity_base.xml | 27 ++ sample/src/main/res/layout/activity_main.xml | 38 ++- sample/src/main/res/values/strings.xml | 2 + sample/src/main/res/values/styles.xml | 7 + 9 files changed, 367 insertions(+), 269 deletions(-) create mode 100644 sample/src/main/java/com/alamkanak/weekview/sample/AsynchronousActivity.java create mode 100644 sample/src/main/java/com/alamkanak/weekview/sample/BaseActivity.java create mode 100644 sample/src/main/java/com/alamkanak/weekview/sample/BasicActivity.java create mode 100644 sample/src/main/res/layout/activity_base.xml diff --git a/sample/src/main/AndroidManifest.xml b/sample/src/main/AndroidManifest.xml index fc8fd159c..5728678b3 100644 --- a/sample/src/main/AndroidManifest.xml +++ b/sample/src/main/AndroidManifest.xml @@ -12,9 +12,18 @@ android:label="@string/app_name" > + + + + + diff --git a/sample/src/main/java/com/alamkanak/weekview/sample/AsynchronousActivity.java b/sample/src/main/java/com/alamkanak/weekview/sample/AsynchronousActivity.java new file mode 100644 index 000000000..e5c91f30a --- /dev/null +++ b/sample/src/main/java/com/alamkanak/weekview/sample/AsynchronousActivity.java @@ -0,0 +1,13 @@ +package com.alamkanak.weekview.sample; + +import com.alamkanak.weekview.WeekViewEvent; + +import java.util.List; + +public class AsynchronousActivity extends BaseActivity { + + @Override + public List onMonthChange(int newYear, int newMonth) { + return null; + } +} diff --git a/sample/src/main/java/com/alamkanak/weekview/sample/BaseActivity.java b/sample/src/main/java/com/alamkanak/weekview/sample/BaseActivity.java new file mode 100644 index 000000000..1308f4ccd --- /dev/null +++ b/sample/src/main/java/com/alamkanak/weekview/sample/BaseActivity.java @@ -0,0 +1,159 @@ +package com.alamkanak.weekview.sample; + +import android.graphics.RectF; +import android.os.Bundle; +import android.support.v7.app.AppCompatActivity; +import android.util.TypedValue; +import android.view.Menu; +import android.view.MenuItem; +import android.widget.Toast; + +import com.alamkanak.weekview.DateTimeInterpreter; +import com.alamkanak.weekview.MonthLoader; +import com.alamkanak.weekview.WeekView; +import com.alamkanak.weekview.WeekViewEvent; + +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Locale; + +/** + * Created by alam on 1/3/16. + */ +public abstract class BaseActivity extends AppCompatActivity implements WeekView.EventClickListener, MonthLoader.MonthChangeListener, WeekView.EventLongPressListener, WeekView.EmptyViewLongPressListener { + private static final int TYPE_DAY_VIEW = 1; + private static final int TYPE_THREE_DAY_VIEW = 2; + private static final int TYPE_WEEK_VIEW = 3; + private int mWeekViewType = TYPE_THREE_DAY_VIEW; + private WeekView mWeekView; + + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_base); + + // Get a reference for the week view in the layout. + mWeekView = (WeekView) findViewById(R.id.weekView); + + // Show a toast message about the touched event. + mWeekView.setOnEventClickListener(this); + + // The week view has infinite scrolling horizontally. We have to provide the events of a + // month every time the month changes on the week view. + mWeekView.setMonthChangeListener(this); + + // Set long press listener for events. + mWeekView.setEventLongPressListener(this); + + // Set long press listener for empty view + mWeekView.setEmptyViewLongPressListener(this); + + // Set up a date time interpreter to interpret how the date and time will be formatted in + // the week view. This is optional. + setupDateTimeInterpreter(false); + } + + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + getMenuInflater().inflate(R.menu.main, menu); + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + int id = item.getItemId(); + setupDateTimeInterpreter(id == R.id.action_week_view); + switch (id){ + case R.id.action_today: + mWeekView.goToToday(); + return true; + case R.id.action_day_view: + if (mWeekViewType != TYPE_DAY_VIEW) { + item.setChecked(!item.isChecked()); + mWeekViewType = TYPE_DAY_VIEW; + mWeekView.setNumberOfVisibleDays(1); + + // Lets change some dimensions to best fit the view. + mWeekView.setColumnGap((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, getResources().getDisplayMetrics())); + mWeekView.setTextSize((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 12, getResources().getDisplayMetrics())); + mWeekView.setEventTextSize((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 12, getResources().getDisplayMetrics())); + } + return true; + case R.id.action_three_day_view: + if (mWeekViewType != TYPE_THREE_DAY_VIEW) { + item.setChecked(!item.isChecked()); + mWeekViewType = TYPE_THREE_DAY_VIEW; + mWeekView.setNumberOfVisibleDays(3); + + // Lets change some dimensions to best fit the view. + mWeekView.setColumnGap((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, getResources().getDisplayMetrics())); + mWeekView.setTextSize((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 12, getResources().getDisplayMetrics())); + mWeekView.setEventTextSize((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 12, getResources().getDisplayMetrics())); + } + return true; + case R.id.action_week_view: + if (mWeekViewType != TYPE_WEEK_VIEW) { + item.setChecked(!item.isChecked()); + mWeekViewType = TYPE_WEEK_VIEW; + mWeekView.setNumberOfVisibleDays(7); + + // Lets change some dimensions to best fit the view. + mWeekView.setColumnGap((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, getResources().getDisplayMetrics())); + mWeekView.setTextSize((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 10, getResources().getDisplayMetrics())); + mWeekView.setEventTextSize((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 10, getResources().getDisplayMetrics())); + } + return true; + } + + return super.onOptionsItemSelected(item); + } + + /** + * Set up a date time interpreter which will show short date values when in week view and long + * date values otherwise. + * @param shortDate True if the date values should be short. + */ + private void setupDateTimeInterpreter(final boolean shortDate) { + mWeekView.setDateTimeInterpreter(new DateTimeInterpreter() { + @Override + public String interpretDate(Calendar date) { + SimpleDateFormat weekdayNameFormat = new SimpleDateFormat("EEE", Locale.getDefault()); + String weekday = weekdayNameFormat.format(date.getTime()); + SimpleDateFormat format = new SimpleDateFormat(" M/d", Locale.getDefault()); + + // All android api level do not have a standard way of getting the first letter of + // the week day name. Hence we get the first char programmatically. + // Details: http://stackoverflow.com/questions/16959502/get-one-letter-abbreviation-of-week-day-of-a-date-in-java#answer-16959657 + if (shortDate) + weekday = String.valueOf(weekday.charAt(0)); + return weekday.toUpperCase() + format.format(date.getTime()); + } + + @Override + public String interpretTime(int hour) { + return hour > 11 ? (hour - 12) + " PM" : (hour == 0 ? "12 AM" : hour + " AM"); + } + }); + } + + protected String getEventTitle(Calendar time) { + return String.format("Event of %02d:%02d %s/%d", time.get(Calendar.HOUR_OF_DAY), time.get(Calendar.MINUTE), time.get(Calendar.MONTH)+1, time.get(Calendar.DAY_OF_MONTH)); + } + + @Override + public void onEventClick(WeekViewEvent event, RectF eventRect) { + Toast.makeText(this, "Clicked " + event.getName(), Toast.LENGTH_SHORT).show(); + } + + @Override + public void onEventLongPress(WeekViewEvent event, RectF eventRect) { + Toast.makeText(this, "Long pressed event: " + event.getName(), Toast.LENGTH_SHORT).show(); + } + + @Override + public void onEmptyViewLongPress(Calendar time) { + Toast.makeText(this, "Empty view long pressed: " + getEventTitle(time), Toast.LENGTH_SHORT).show(); + } +} diff --git a/sample/src/main/java/com/alamkanak/weekview/sample/BasicActivity.java b/sample/src/main/java/com/alamkanak/weekview/sample/BasicActivity.java new file mode 100644 index 000000000..d2fc1dbae --- /dev/null +++ b/sample/src/main/java/com/alamkanak/weekview/sample/BasicActivity.java @@ -0,0 +1,120 @@ +package com.alamkanak.weekview.sample; + +import com.alamkanak.weekview.WeekViewEvent; + +import java.util.ArrayList; +import java.util.Calendar; +import java.util.List; + +/** + * Created by alam on 1/3/16. + */ +public class BasicActivity extends BaseActivity { + + @Override + public List onMonthChange(int newYear, int newMonth) { + // Populate the week view with some events. + List events = new ArrayList(); + + Calendar startTime = Calendar.getInstance(); + startTime.set(Calendar.HOUR_OF_DAY, 3); + startTime.set(Calendar.MINUTE, 0); + startTime.set(Calendar.MONTH, newMonth-1); + startTime.set(Calendar.YEAR, newYear); + Calendar endTime = (Calendar) startTime.clone(); + endTime.add(Calendar.HOUR, 1); + endTime.set(Calendar.MONTH, newMonth-1); + WeekViewEvent event = new WeekViewEvent(1, getEventTitle(startTime), startTime, endTime); + event.setColor(getResources().getColor(R.color.event_color_01)); + events.add(event); + + startTime = Calendar.getInstance(); + startTime.set(Calendar.HOUR_OF_DAY, 3); + 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, 4); + endTime.set(Calendar.MINUTE, 30); + endTime.set(Calendar.MONTH, newMonth-1); + event = new WeekViewEvent(10, getEventTitle(startTime), startTime, endTime); + event.setColor(getResources().getColor(R.color.event_color_02)); + events.add(event); + + startTime = Calendar.getInstance(); + startTime.set(Calendar.HOUR_OF_DAY, 4); + startTime.set(Calendar.MINUTE, 20); + startTime.set(Calendar.MONTH, newMonth-1); + startTime.set(Calendar.YEAR, newYear); + endTime = (Calendar) startTime.clone(); + endTime.set(Calendar.HOUR_OF_DAY, 5); + endTime.set(Calendar.MINUTE, 0); + event = new WeekViewEvent(10, getEventTitle(startTime), startTime, endTime); + event.setColor(getResources().getColor(R.color.event_color_03)); + events.add(event); + + startTime = Calendar.getInstance(); + startTime.set(Calendar.HOUR_OF_DAY, 5); + startTime.set(Calendar.MINUTE, 30); + startTime.set(Calendar.MONTH, newMonth-1); + startTime.set(Calendar.YEAR, newYear); + endTime = (Calendar) startTime.clone(); + endTime.add(Calendar.HOUR_OF_DAY, 2); + endTime.set(Calendar.MONTH, newMonth-1); + event = new WeekViewEvent(2, getEventTitle(startTime), startTime, endTime); + event.setColor(getResources().getColor(R.color.event_color_02)); + events.add(event); + + startTime = Calendar.getInstance(); + startTime.set(Calendar.HOUR_OF_DAY, 5); + startTime.set(Calendar.MINUTE, 0); + startTime.set(Calendar.MONTH, newMonth-1); + startTime.set(Calendar.YEAR, newYear); + startTime.add(Calendar.DATE, 1); + endTime = (Calendar) startTime.clone(); + endTime.add(Calendar.HOUR_OF_DAY, 3); + endTime.set(Calendar.MONTH, newMonth - 1); + event = new WeekViewEvent(3, getEventTitle(startTime), startTime, endTime); + event.setColor(getResources().getColor(R.color.event_color_03)); + events.add(event); + + startTime = Calendar.getInstance(); + startTime.set(Calendar.DAY_OF_MONTH, 15); + startTime.set(Calendar.HOUR_OF_DAY, 3); + startTime.set(Calendar.MINUTE, 0); + startTime.set(Calendar.MONTH, newMonth-1); + startTime.set(Calendar.YEAR, newYear); + endTime = (Calendar) startTime.clone(); + endTime.add(Calendar.HOUR_OF_DAY, 3); + event = new WeekViewEvent(4, getEventTitle(startTime), startTime, endTime); + event.setColor(getResources().getColor(R.color.event_color_04)); + events.add(event); + + startTime = Calendar.getInstance(); + startTime.set(Calendar.DAY_OF_MONTH, 1); + startTime.set(Calendar.HOUR_OF_DAY, 3); + startTime.set(Calendar.MINUTE, 0); + startTime.set(Calendar.MONTH, newMonth-1); + startTime.set(Calendar.YEAR, newYear); + endTime = (Calendar) startTime.clone(); + endTime.add(Calendar.HOUR_OF_DAY, 3); + event = new WeekViewEvent(5, getEventTitle(startTime), startTime, endTime); + event.setColor(getResources().getColor(R.color.event_color_01)); + events.add(event); + + startTime = Calendar.getInstance(); + startTime.set(Calendar.DAY_OF_MONTH, startTime.getActualMaximum(Calendar.DAY_OF_MONTH)); + startTime.set(Calendar.HOUR_OF_DAY, 15); + startTime.set(Calendar.MINUTE, 0); + startTime.set(Calendar.MONTH, newMonth-1); + startTime.set(Calendar.YEAR, newYear); + endTime = (Calendar) startTime.clone(); + endTime.add(Calendar.HOUR_OF_DAY, 3); + event = new WeekViewEvent(5, 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 74ddc6d3a..54fd785d7 100644 --- a/sample/src/main/java/com/alamkanak/weekview/sample/MainActivity.java +++ b/sample/src/main/java/com/alamkanak/weekview/sample/MainActivity.java @@ -1,272 +1,37 @@ package com.alamkanak.weekview.sample; -import android.graphics.RectF; +import android.content.Intent; import android.os.Bundle; -import android.support.v7.app.ActionBarActivity; -import android.util.TypedValue; -import android.view.Menu; -import android.view.MenuItem; -import android.widget.Toast; - -import com.alamkanak.weekview.DateTimeInterpreter; -import com.alamkanak.weekview.MonthLoader; -import com.alamkanak.weekview.WeekView; -import com.alamkanak.weekview.WeekViewEvent; - -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Calendar; -import java.util.List; -import java.util.Locale; +import android.support.v7.app.AppCompatActivity; +import android.view.View; /** * Created by Raquib-ul-Alam Kanak on 7/21/2014. * Website: http://alamkanak.github.io/ */ -public class MainActivity extends ActionBarActivity implements MonthLoader.MonthChangeListener, - WeekView.EventClickListener, WeekView.EventLongPressListener, - WeekView.EmptyViewLongPressListener { - - private static final int TYPE_DAY_VIEW = 1; - private static final int TYPE_THREE_DAY_VIEW = 2; - private static final int TYPE_WEEK_VIEW = 3; - private int mWeekViewType = TYPE_THREE_DAY_VIEW; - private WeekView mWeekView; +public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - // Get a reference for the week view in the layout. - mWeekView = (WeekView) findViewById(R.id.weekView); - - // Show a toast message about the touched event. - mWeekView.setOnEventClickListener(this); - - // The week view has infinite scrolling horizontally. We have to provide the events of a - // month every time the month changes on the week view. - mWeekView.setMonthChangeListener(this); - - // Set long press listener for events. - mWeekView.setEventLongPressListener(this); - - // Set long press listener for empty view - mWeekView.setEmptyViewLongPressListener(this); - - // Set up a date time interpreter to interpret how the date and time will be formatted in - // the week view. This is optional. - setupDateTimeInterpreter(false); - } - - - @Override - public boolean onCreateOptionsMenu(Menu menu) { - getMenuInflater().inflate(R.menu.main, menu); - return true; - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - int id = item.getItemId(); - setupDateTimeInterpreter(id == R.id.action_week_view); - switch (id){ - case R.id.action_today: - mWeekView.goToToday(); - return true; - case R.id.action_day_view: - if (mWeekViewType != TYPE_DAY_VIEW) { - item.setChecked(!item.isChecked()); - mWeekViewType = TYPE_DAY_VIEW; - mWeekView.setNumberOfVisibleDays(1); - - // Lets change some dimensions to best fit the view. - mWeekView.setColumnGap((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, getResources().getDisplayMetrics())); - mWeekView.setTextSize((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 12, getResources().getDisplayMetrics())); - mWeekView.setEventTextSize((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 12, getResources().getDisplayMetrics())); - } - return true; - case R.id.action_three_day_view: - if (mWeekViewType != TYPE_THREE_DAY_VIEW) { - item.setChecked(!item.isChecked()); - mWeekViewType = TYPE_THREE_DAY_VIEW; - mWeekView.setNumberOfVisibleDays(3); - - // Lets change some dimensions to best fit the view. - mWeekView.setColumnGap((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, getResources().getDisplayMetrics())); - mWeekView.setTextSize((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 12, getResources().getDisplayMetrics())); - mWeekView.setEventTextSize((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 12, getResources().getDisplayMetrics())); - } - return true; - case R.id.action_week_view: - if (mWeekViewType != TYPE_WEEK_VIEW) { - item.setChecked(!item.isChecked()); - mWeekViewType = TYPE_WEEK_VIEW; - mWeekView.setNumberOfVisibleDays(7); - - // Lets change some dimensions to best fit the view. - mWeekView.setColumnGap((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, getResources().getDisplayMetrics())); - mWeekView.setTextSize((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 10, getResources().getDisplayMetrics())); - mWeekView.setEventTextSize((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 10, getResources().getDisplayMetrics())); - } - return true; - } - - return super.onOptionsItemSelected(item); - } - - /** - * Set up a date time interpreter which will show short date values when in week view and long - * date values otherwise. - * @param shortDate True if the date values should be short. - */ - private void setupDateTimeInterpreter(final boolean shortDate) { - mWeekView.setDateTimeInterpreter(new DateTimeInterpreter() { + findViewById(R.id.buttonBasic).setOnClickListener(new View.OnClickListener() { @Override - public String interpretDate(Calendar date) { - SimpleDateFormat weekdayNameFormat = new SimpleDateFormat("EEE", Locale.getDefault()); - String weekday = weekdayNameFormat.format(date.getTime()); - SimpleDateFormat format = new SimpleDateFormat(" M/d", Locale.getDefault()); - - // All android api level do not have a standard way of getting the first letter of - // the week day name. Hence we get the first char programmatically. - // Details: http://stackoverflow.com/questions/16959502/get-one-letter-abbreviation-of-week-day-of-a-date-in-java#answer-16959657 - if (shortDate) - weekday = String.valueOf(weekday.charAt(0)); - return weekday.toUpperCase() + format.format(date.getTime()); + public void onClick(View v) { + Intent intent = new Intent(MainActivity.this, BasicActivity.class); + startActivity(intent); } + }); + findViewById(R.id.buttonAsynchronous).setOnClickListener(new View.OnClickListener() { @Override - public String interpretTime(int hour) { - return hour > 11 ? (hour - 12) + " PM" : (hour == 0 ? "12 AM" : hour + " AM"); + public void onClick(View v) { + Intent intent = new Intent(MainActivity.this, AsynchronousActivity.class); + startActivity(intent); } }); } - @Override - public List onMonthChange(int newYear, int newMonth) { - - // Populate the week view with some events. - List events = new ArrayList(); - - Calendar startTime = Calendar.getInstance(); - startTime.set(Calendar.HOUR_OF_DAY, 3); - startTime.set(Calendar.MINUTE, 0); - startTime.set(Calendar.MONTH, newMonth-1); - startTime.set(Calendar.YEAR, newYear); - Calendar endTime = (Calendar) startTime.clone(); - endTime.add(Calendar.HOUR, 1); - endTime.set(Calendar.MONTH, newMonth-1); - WeekViewEvent event = new WeekViewEvent(1, getEventTitle(startTime), startTime, endTime); - event.setColor(getResources().getColor(R.color.event_color_01)); - events.add(event); - - startTime = Calendar.getInstance(); - startTime.set(Calendar.HOUR_OF_DAY, 3); - 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, 4); - endTime.set(Calendar.MINUTE, 30); - endTime.set(Calendar.MONTH, newMonth-1); - event = new WeekViewEvent(10, getEventTitle(startTime), startTime, endTime); - event.setColor(getResources().getColor(R.color.event_color_02)); - events.add(event); - - startTime = Calendar.getInstance(); - startTime.set(Calendar.HOUR_OF_DAY, 4); - startTime.set(Calendar.MINUTE, 20); - startTime.set(Calendar.MONTH, newMonth-1); - startTime.set(Calendar.YEAR, newYear); - endTime = (Calendar) startTime.clone(); - endTime.set(Calendar.HOUR_OF_DAY, 5); - endTime.set(Calendar.MINUTE, 0); - event = new WeekViewEvent(10, getEventTitle(startTime), startTime, endTime); - event.setColor(getResources().getColor(R.color.event_color_03)); - events.add(event); - - startTime = Calendar.getInstance(); - startTime.set(Calendar.HOUR_OF_DAY, 5); - startTime.set(Calendar.MINUTE, 30); - startTime.set(Calendar.MONTH, newMonth-1); - startTime.set(Calendar.YEAR, newYear); - endTime = (Calendar) startTime.clone(); - endTime.add(Calendar.HOUR_OF_DAY, 2); - endTime.set(Calendar.MONTH, newMonth-1); - event = new WeekViewEvent(2, getEventTitle(startTime), startTime, endTime); - event.setColor(getResources().getColor(R.color.event_color_02)); - events.add(event); - - startTime = Calendar.getInstance(); - startTime.set(Calendar.HOUR_OF_DAY, 5); - startTime.set(Calendar.MINUTE, 0); - startTime.set(Calendar.MONTH, newMonth-1); - startTime.set(Calendar.YEAR, newYear); - startTime.add(Calendar.DATE, 1); - endTime = (Calendar) startTime.clone(); - endTime.add(Calendar.HOUR_OF_DAY, 3); - endTime.set(Calendar.MONTH, newMonth - 1); - event = new WeekViewEvent(3, getEventTitle(startTime), startTime, endTime); - event.setColor(getResources().getColor(R.color.event_color_03)); - events.add(event); - - startTime = Calendar.getInstance(); - startTime.set(Calendar.DAY_OF_MONTH, 15); - startTime.set(Calendar.HOUR_OF_DAY, 3); - startTime.set(Calendar.MINUTE, 0); - startTime.set(Calendar.MONTH, newMonth-1); - startTime.set(Calendar.YEAR, newYear); - endTime = (Calendar) startTime.clone(); - endTime.add(Calendar.HOUR_OF_DAY, 3); - event = new WeekViewEvent(4, getEventTitle(startTime), startTime, endTime); - event.setColor(getResources().getColor(R.color.event_color_04)); - events.add(event); - - startTime = Calendar.getInstance(); - startTime.set(Calendar.DAY_OF_MONTH, 1); - startTime.set(Calendar.HOUR_OF_DAY, 3); - startTime.set(Calendar.MINUTE, 0); - startTime.set(Calendar.MONTH, newMonth-1); - startTime.set(Calendar.YEAR, newYear); - endTime = (Calendar) startTime.clone(); - endTime.add(Calendar.HOUR_OF_DAY, 3); - event = new WeekViewEvent(5, getEventTitle(startTime), startTime, endTime); - event.setColor(getResources().getColor(R.color.event_color_01)); - events.add(event); - - startTime = Calendar.getInstance(); - startTime.set(Calendar.DAY_OF_MONTH, startTime.getActualMaximum(Calendar.DAY_OF_MONTH)); - startTime.set(Calendar.HOUR_OF_DAY, 15); - startTime.set(Calendar.MINUTE, 0); - startTime.set(Calendar.MONTH, newMonth-1); - startTime.set(Calendar.YEAR, newYear); - endTime = (Calendar) startTime.clone(); - endTime.add(Calendar.HOUR_OF_DAY, 3); - event = new WeekViewEvent(5, getEventTitle(startTime), startTime, endTime); - event.setColor(getResources().getColor(R.color.event_color_02)); - events.add(event); - - return events; - } - - private String getEventTitle(Calendar time) { - return String.format("Event of %02d:%02d %s/%d", time.get(Calendar.HOUR_OF_DAY), time.get(Calendar.MINUTE), time.get(Calendar.MONTH)+1, time.get(Calendar.DAY_OF_MONTH)); - } - - @Override - public void onEventClick(WeekViewEvent event, RectF eventRect) { - Toast.makeText(MainActivity.this, "Clicked " + event.getName(), Toast.LENGTH_SHORT).show(); - } - - @Override - public void onEventLongPress(WeekViewEvent event, RectF eventRect) { - Toast.makeText(MainActivity.this, "Long pressed event: " + event.getName(), Toast.LENGTH_SHORT).show(); - } - - @Override - public void onEmptyViewLongPress(Calendar time) { - Toast.makeText(MainActivity.this, "Empty view long pressed: " + getEventTitle(time), Toast.LENGTH_SHORT).show(); - } } diff --git a/sample/src/main/res/layout/activity_base.xml b/sample/src/main/res/layout/activity_base.xml new file mode 100644 index 000000000..28379f29e --- /dev/null +++ b/sample/src/main/res/layout/activity_base.xml @@ -0,0 +1,27 @@ + + + + + diff --git a/sample/src/main/res/layout/activity_main.xml b/sample/src/main/res/layout/activity_main.xml index e22f0c0a2..e55c3b5b5 100644 --- a/sample/src/main/res/layout/activity_main.xml +++ b/sample/src/main/res/layout/activity_main.xml @@ -1,27 +1,23 @@ - + tools:context=".MainActivity" + android:orientation="vertical"> - + android:layout_height="wrap_content" + android:text="Basic Example" + style="@style/Button"/> - + +