Skip to content

Commit

Permalink
Add WeekViewDisplayable interface
Browse files Browse the repository at this point in the history
Instead of having to convert one's own data classes to WeekViewEvent, the WeekViewDisplayable interface includes a toWeekViewEvent() method. The conversion can be handled in the data class and does not add boilerplate in your controlling classes.

As a result, onMonthChanged() can return a list of objects that implement WeekViewDisplayable and does not require a list of WeekViewEvents.
  • Loading branch information
thellmund committed Sep 8, 2018
1 parent 3054049 commit de5b8b3
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* Created by Raquib on 1/6/2015.
*/
public interface DateTimeInterpreter {

String interpretDate(Calendar date);
String interpretTime(int hour);

}
35 changes: 24 additions & 11 deletions library/src/main/java/com/alamkanak/weekview/MonthLoader.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -22,7 +29,16 @@ public double toWeekViewPeriodIndex(Calendar instance){
public List<? extends WeekViewEvent> onLoad(int periodIndex) {
int newYear = periodIndex / 12;
int newMonth = periodIndex % 12 + 1;
return mOnMonthChangeListener.onMonthChange(newYear, newMonth);

List<WeekViewDisplayable> displayableItems =
mOnMonthChangeListener.onMonthChange(newYear, newMonth);

List<WeekViewEvent> events = new ArrayList<>();
for (WeekViewDisplayable displayableItem : displayableItems) {
events.add(displayableItem.toWeekViewEvent());
}

return events;
}

public MonthChangeListener getOnMonthChangeListener() {
Expand All @@ -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.<br/>
* <strong>That's why you can have three times the same event at the same place if you mess
* up with the configuration</strong>
* @param newYear : year of the events required by the view.
* @param newMonth : month of the events required by the view <br/><strong>1 based (not
* like JAVA API) --> January = 1 and December = 12</strong>.
* @return a list of the events happening <strong>during the specified month</strong>.
* 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<? extends WeekViewEvent> onMonthChange(int newYear, int newMonth);
List<WeekViewDisplayable> onMonthChange(int newYear, int newMonth);

}
}
8 changes: 5 additions & 3 deletions library/src/main/java/com/alamkanak/weekview/WeekView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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();

}
10 changes: 7 additions & 3 deletions library/src/main/java/com/alamkanak/weekview/WeekViewEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* Created by Raquib-ul-Alam Kanak on 7/21/2014.
* Website: http://april-shower.com
*/
public class WeekViewEvent implements Comparable<WeekViewEvent> {
public class WeekViewEvent implements WeekViewDisplayable, Comparable<WeekViewEvent> {

private long mId;
private Calendar mStartTime;
Expand Down Expand Up @@ -193,9 +193,9 @@ public int hashCode() {
return (int) (mId ^ (mId >>> 32));
}

public List<WeekViewEvent> splitWeekViewEvents(){
public List<WeekViewEvent> splitWeekViewEvents() {
//This function splits the WeekViewEvent in WeekViewEvents by day
List<WeekViewEvent> events = new ArrayList<WeekViewEvent>();
List<WeekViewEvent> 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();
Expand Down Expand Up @@ -245,4 +245,8 @@ public List<WeekViewEvent> splitWeekViewEvents(){
return events;
}

@Override
public WeekViewEvent toWeekViewEvent() {
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
15 changes: 7 additions & 8 deletions sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.alamkanak.weekview.sample" >
package="com.alamkanak.weekview.sample">

<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".BasicActivity"
android:label="@string/title_activity_basic" >
</activity>
android:label="@string/title_activity_basic" />
<activity
android:name=".AsynchronousActivity"
android:label="@string/title_activity_asynchronous" >
</activity>
android:label="@string/title_activity_asynchronous" />
</application>

</manifest>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,7 +27,7 @@ public class AsynchronousActivity extends BaseActivity implements Callback<List<
boolean calledNetwork = false;

@Override
public List<? extends WeekViewEvent> onMonthChange(int newYear, int newMonth) {
public List<WeekViewDisplayable> 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
Expand All @@ -40,7 +41,7 @@ public List<? extends WeekViewEvent> onMonthChange(int newYear, int newMonth) {
}

// Return only the events that matches newYear and newMonth.
List<WeekViewEvent> matchedEvents = new ArrayList<WeekViewEvent>();
List<WeekViewDisplayable> matchedEvents = new ArrayList<>();
for (WeekViewEvent event : events) {
if (eventMatches(event, newYear, newMonth)) {
matchedEvents.add(event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -173,4 +175,5 @@ public void onEmptyViewLongPress(Calendar time) {
public WeekView getWeekView() {
return mWeekView;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.alamkanak.weekview.sample;

import com.alamkanak.weekview.WeekViewDisplayable;
import com.alamkanak.weekview.WeekViewEvent;

import java.util.ArrayList;
Expand All @@ -14,9 +15,9 @@
public class BasicActivity extends BaseActivity {

@Override
public List<? extends WeekViewEvent> onMonthChange(int newYear, int newMonth) {
public List<WeekViewDisplayable> onMonthChange(int newYear, int newMonth) {
// Populate the week view with some events.
List<WeekViewEvent> events = new ArrayList<WeekViewEvent>();
List<WeekViewDisplayable> events = new ArrayList<>();

Calendar startTime = Calendar.getInstance();
startTime.set(Calendar.HOUR_OF_DAY, 3);
Expand Down

0 comments on commit de5b8b3

Please sign in to comment.