diff --git a/library/src/main/java/com/alamkanak/weekview/DateTimeInterpreter.java b/library/src/main/java/com/alamkanak/weekview/DateTimeInterpreter.java index 383c5570d..c2097cfb7 100644 --- a/library/src/main/java/com/alamkanak/weekview/DateTimeInterpreter.java +++ b/library/src/main/java/com/alamkanak/weekview/DateTimeInterpreter.java @@ -6,6 +6,8 @@ * Created by Raquib on 1/6/2015. */ public interface DateTimeInterpreter { + String interpretDate(Calendar date); String interpretTime(int hour); + } diff --git a/library/src/main/java/com/alamkanak/weekview/MonthLoader.java b/library/src/main/java/com/alamkanak/weekview/MonthLoader.java index 3b799127a..4b6cc6bf6 100644 --- a/library/src/main/java/com/alamkanak/weekview/MonthLoader.java +++ b/library/src/main/java/com/alamkanak/weekview/MonthLoader.java @@ -1,8 +1,15 @@ package com.alamkanak.weekview; +import java.util.ArrayList; import java.util.Calendar; import java.util.List; +/** + * This class is responsible for loading {@link WeekViewEvent}s into {@link WeekView}. It can handle + * both concrete {@link WeekViewEvent} objects and {@link WeekViewDisplayable} objects. The latter is + * an interface that can be implemented in one's actual data class and handles the conversion to a + * {@link WeekViewEvent}. + */ public class MonthLoader implements WeekViewLoader { private MonthChangeListener mOnMonthChangeListener; @@ -22,7 +29,16 @@ public double toWeekViewPeriodIndex(Calendar instance){ public List onLoad(int periodIndex) { int newYear = periodIndex / 12; int newMonth = periodIndex % 12 + 1; - return mOnMonthChangeListener.onMonthChange(newYear, newMonth); + + List displayableItems = + mOnMonthChangeListener.onMonthChange(newYear, newMonth); + + List events = new ArrayList<>(); + for (WeekViewDisplayable displayableItem : displayableItems) { + events.add(displayableItem.toWeekViewEvent()); + } + + return events; } public MonthChangeListener getOnMonthChangeListener() { @@ -34,17 +50,14 @@ public void setOnMonthChangeListener(MonthChangeListener onMonthChangeListener) } public interface MonthChangeListener { + /** - * Very important interface, it's the base to load events in the calendar. - * This method is called three times: once to load the previous month, once to load the - * next month and once to load the current month.
- * That's why you can have three times the same event at the same place if you mess - * up with the configuration - * @param newYear : year of the events required by the view. - * @param newMonth : month of the events required by the view
1 based (not - * like JAVA API) --> January = 1 and December = 12. - * @return a list of the events happening during the specified month. + * Called when the month displayed in the {@link WeekView} changes. + * @param newYear The year that is now being displayed + * @param newMonth The month that is now being displayed + * @return The list of {@link WeekViewDisplayable} of the provided month */ - List onMonthChange(int newYear, int newMonth); + List onMonthChange(int newYear, int newMonth); + } } diff --git a/library/src/main/java/com/alamkanak/weekview/WeekView.java b/library/src/main/java/com/alamkanak/weekview/WeekView.java index a704ea617..2dcf1a582 100755 --- a/library/src/main/java/com/alamkanak/weekview/WeekView.java +++ b/library/src/main/java/com/alamkanak/weekview/WeekView.java @@ -1237,13 +1237,15 @@ public EventClickListener getEventClickListener() { return mEventClickListener; } - public @Nullable MonthLoader.MonthChangeListener getMonthChangeListener() { - if (mWeekViewLoader instanceof MonthLoader) + @Nullable + public MonthLoader.MonthChangeListener getMonthChangeListener() { + if (mWeekViewLoader instanceof MonthLoader) { return ((MonthLoader) mWeekViewLoader).getOnMonthChangeListener(); + } return null; } - public void setMonthChangeListener(MonthLoader.MonthChangeListener monthChangeListener) { + public void setMonthChangeListener(@Nullable MonthLoader.MonthChangeListener monthChangeListener) { this.mWeekViewLoader = new MonthLoader(monthChangeListener); } diff --git a/library/src/main/java/com/alamkanak/weekview/WeekViewDisplayable.java b/library/src/main/java/com/alamkanak/weekview/WeekViewDisplayable.java new file mode 100644 index 000000000..fee4628f9 --- /dev/null +++ b/library/src/main/java/com/alamkanak/weekview/WeekViewDisplayable.java @@ -0,0 +1,16 @@ +package com.alamkanak.weekview; + +/** + * This interface can be implemented by classes that should be displayed in {@link WeekView}. + * Instead of having to provide a list of {@link WeekViewEvent}s, you can provide a list of elements + * of your class. The conversion to {@link WeekViewEvent} will happen in the background. + */ +public interface WeekViewDisplayable { + + /** + * Returns a {@link WeekViewEvent} for use in {@link WeekView}. + * @return A {@link WeekViewEvent} + */ + public WeekViewEvent toWeekViewEvent(); + +} diff --git a/library/src/main/java/com/alamkanak/weekview/WeekViewEvent.java b/library/src/main/java/com/alamkanak/weekview/WeekViewEvent.java index 75bce6a47..f93be451c 100644 --- a/library/src/main/java/com/alamkanak/weekview/WeekViewEvent.java +++ b/library/src/main/java/com/alamkanak/weekview/WeekViewEvent.java @@ -12,7 +12,7 @@ * Created by Raquib-ul-Alam Kanak on 7/21/2014. * Website: http://april-shower.com */ -public class WeekViewEvent implements Comparable { +public class WeekViewEvent implements WeekViewDisplayable, Comparable { private long mId; private Calendar mStartTime; @@ -193,9 +193,9 @@ public int hashCode() { return (int) (mId ^ (mId >>> 32)); } - public List splitWeekViewEvents(){ + public List splitWeekViewEvents() { //This function splits the WeekViewEvent in WeekViewEvents by day - List events = new ArrayList(); + List events = new ArrayList<>(); // The first millisecond of the next day is still the same day. (no need to split events for this). Calendar endTime = (Calendar) this.getEndTime().clone(); @@ -245,4 +245,8 @@ public List splitWeekViewEvents(){ return events; } + @Override + public WeekViewEvent toWeekViewEvent() { + return this; + } } diff --git a/library/src/main/java/com/alamkanak/weekview/WeekViewUtil.java b/library/src/main/java/com/alamkanak/weekview/WeekViewUtil.java index 8171d4800..f71da569a 100644 --- a/library/src/main/java/com/alamkanak/weekview/WeekViewUtil.java +++ b/library/src/main/java/com/alamkanak/weekview/WeekViewUtil.java @@ -29,7 +29,7 @@ public static boolean isSameDay(Calendar dayOne, Calendar dayTwo) { * Returns a calendar instance at the start of this day * @return the calendar instance */ - public static Calendar today(){ + public static Calendar today() { Calendar today = Calendar.getInstance(); today.set(Calendar.HOUR_OF_DAY, 0); today.set(Calendar.MINUTE, 0); diff --git a/sample/src/main/AndroidManifest.xml b/sample/src/main/AndroidManifest.xml index b5b511c12..bee55ba4d 100644 --- a/sample/src/main/AndroidManifest.xml +++ b/sample/src/main/AndroidManifest.xml @@ -1,6 +1,6 @@ + package="com.alamkanak.weekview.sample"> @@ -8,23 +8,22 @@ android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" - android:theme="@style/AppTheme" > + android:theme="@style/AppTheme"> + android:label="@string/app_name"> + - + android:label="@string/title_activity_basic" /> - + android:label="@string/title_activity_asynchronous" /> - + \ No newline at end of file diff --git a/sample/src/main/java/com/alamkanak/weekview/sample/AsynchronousActivity.java b/sample/src/main/java/com/alamkanak/weekview/sample/AsynchronousActivity.java index 6feda05f9..c602bf88c 100644 --- a/sample/src/main/java/com/alamkanak/weekview/sample/AsynchronousActivity.java +++ b/sample/src/main/java/com/alamkanak/weekview/sample/AsynchronousActivity.java @@ -2,6 +2,7 @@ import android.widget.Toast; +import com.alamkanak.weekview.WeekViewDisplayable; import com.alamkanak.weekview.WeekViewEvent; import com.alamkanak.weekview.sample.apiclient.Event; import com.alamkanak.weekview.sample.apiclient.MyJsonService; @@ -26,7 +27,7 @@ public class AsynchronousActivity extends BaseActivity implements Callback onMonthChange(int newYear, int newMonth) { + public List onMonthChange(int newYear, int newMonth) { // Download events from network if it hasn't been done already. To understand how events are // downloaded using retrofit, visit http://square.github.io/retrofit @@ -40,7 +41,7 @@ public List onMonthChange(int newYear, int newMonth) { } // Return only the events that matches newYear and newMonth. - List matchedEvents = new ArrayList(); + List matchedEvents = new ArrayList<>(); for (WeekViewEvent event : events) { if (eventMatches(event, newYear, newMonth)) { matchedEvents.add(event); 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 e739ce4cc..dba8946ad 100644 --- a/sample/src/main/java/com/alamkanak/weekview/sample/BaseActivity.java +++ b/sample/src/main/java/com/alamkanak/weekview/sample/BaseActivity.java @@ -23,14 +23,16 @@ * Created by Raquib-ul-Alam Kanak on 1/3/2014. * Website: http://alamkanak.github.io */ -public abstract class BaseActivity extends AppCompatActivity implements WeekView.EventClickListener, MonthLoader.MonthChangeListener, WeekView.EventLongPressListener, WeekView.EmptyViewLongPressListener { +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); @@ -173,4 +175,5 @@ public void onEmptyViewLongPress(Calendar time) { public WeekView getWeekView() { return mWeekView; } + } 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..682b7709b 100644 --- a/sample/src/main/java/com/alamkanak/weekview/sample/BasicActivity.java +++ b/sample/src/main/java/com/alamkanak/weekview/sample/BasicActivity.java @@ -1,5 +1,6 @@ package com.alamkanak.weekview.sample; +import com.alamkanak.weekview.WeekViewDisplayable; import com.alamkanak.weekview.WeekViewEvent; import java.util.ArrayList; @@ -14,9 +15,9 @@ public class BasicActivity extends BaseActivity { @Override - public List onMonthChange(int newYear, int newMonth) { + public List onMonthChange(int newYear, int newMonth) { // Populate the week view with some events. - List events = new ArrayList(); + List events = new ArrayList<>(); Calendar startTime = Calendar.getInstance(); startTime.set(Calendar.HOUR_OF_DAY, 3);