Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of Two Week display mode #903

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ public enum CalendarMode {
/**
* Week mode that shows the calendar week by week.
*/
WEEKS(1);
WEEKS(1),
/**
* Two Week mode that shows the calendar in two weeks.
*/
TWO_WEEKS(2);

/**
* Number of visible weeks per calendar mode.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1946,6 +1946,17 @@ private void commit(State state) {
} else {
calendarDayToShow = lastVisibleCalendarDay;
}
} else if (calendarMode == CalendarMode.TWO_WEEKS) {
LocalDate lastVisibleCalendar = calendarDayToShow.getDate();
CalendarDay lastVisibleCalendarDay = CalendarDay.from(lastVisibleCalendar.plusDays(13));
if (currentlySelectedDate != null &&
(currentlySelectedDate.equals(calendarDayToShow) || currentlySelectedDate.equals(lastVisibleCalendarDay) ||
(currentlySelectedDate.isAfter(calendarDayToShow) && currentlySelectedDate.isBefore(lastVisibleCalendarDay)))) {
// Currently selected date is within view, so center on that
calendarDayToShow = currentlySelectedDate;
} else {
calendarDayToShow = lastVisibleCalendarDay;
}
}
}
}
Expand All @@ -1967,6 +1978,9 @@ private void commit(State state) {
case WEEKS:
newAdapter = new WeekPagerAdapter(this);
break;
case TWO_WEEKS:
newAdapter = new TwoWeekPagerAdapter(this);
break;
default:
throw new IllegalArgumentException("Provided display mode which is not yet implemented");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.prolificinteractive.materialcalendarview;

import android.support.annotation.NonNull;

import org.threeten.bp.DayOfWeek;
import org.threeten.bp.LocalDate;
import org.threeten.bp.jdk8.Jdk8Methods;
import org.threeten.bp.temporal.ChronoUnit;
import org.threeten.bp.temporal.WeekFields;

public class TwoWeekPagerAdapter extends CalendarPagerAdapter<TwoWeekView> {

public TwoWeekPagerAdapter(MaterialCalendarView mcv) {
super(mcv);
}

@Override
protected TwoWeekView createView(int position) {
return new TwoWeekView(mcv, getItem(position), mcv.getFirstDayOfWeek(), showWeekDays);
}

@Override
protected int indexOf(TwoWeekView view) {
CalendarDay week = view.getFirstViewDay();
return getRangeIndex().indexOf(week);
}

@Override
protected boolean isInstanceOfView(Object object) {
return object instanceof TwoWeekView;
}

@Override
protected DateRangeIndex createRangeIndex(CalendarDay min, CalendarDay max) {
return new BiWeekly(min, max, mcv.getFirstDayOfWeek());
}

public static class BiWeekly implements DateRangeIndex {

private static final int DAYS_IN_TWO_WEEK = 14;
private final CalendarDay min;
private final int count;
/**
* First day of the week to base the weeks on.
*/
private final DayOfWeek firstDayOfWeek;

public BiWeekly(@NonNull CalendarDay min, @NonNull CalendarDay max, DayOfWeek firstDayOfWeek) {
this.firstDayOfWeek = firstDayOfWeek;
this.min = getFirstDayOfWeek(min);
this.count = indexOf(max) + 1;
}

@Override
public int getCount() {
return count;
}

@Override
public int indexOf(CalendarDay day) {
final WeekFields weekFields = WeekFields.of(firstDayOfWeek, 1);
final LocalDate temp = day.getDate().with(weekFields.dayOfWeek(), 1L);
return (int) ChronoUnit.DAYS.between(min.getDate(), temp)/DAYS_IN_TWO_WEEK;
}

@Override
public CalendarDay getItem(int position) {
return CalendarDay.from(min.getDate().plusDays(Jdk8Methods.safeMultiply(position, DAYS_IN_TWO_WEEK)));
}

/**
* Getting the first day of a week for a specific date based on a specific week day as first
* day.
*/
private CalendarDay getFirstDayOfWeek(@NonNull final CalendarDay day) {
final LocalDate temp = day.getDate().with(WeekFields.of(firstDayOfWeek, 1).dayOfWeek(), 1L);
return CalendarDay.from(temp);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.prolificinteractive.materialcalendarview;

import android.annotation.SuppressLint;
import android.support.annotation.NonNull;

import org.threeten.bp.DayOfWeek;
import org.threeten.bp.LocalDate;

import java.util.Collection;

/**
* Display a two week of {@linkplain DayView}s and
* fourteen {@linkplain WeekDayView}s.
*/
@SuppressLint("ViewConstructor")
public class TwoWeekView extends CalendarPagerView {

public TwoWeekView(@NonNull MaterialCalendarView view, CalendarDay firstViewDay,
final DayOfWeek firstDayOfWeek, boolean showWeekDays) {
super(view, firstViewDay, firstDayOfWeek, showWeekDays);
}

@Override
protected void buildDayViews(Collection<DayView> dayViews, final LocalDate calendar) {
LocalDate temp = calendar;
for (int i = 0; i < (DEFAULT_DAYS_IN_WEEK * 2); i++) {
addDayView(dayViews, temp);
temp = temp.plusDays(1);
}
}

@Override
protected boolean isDayEnabled(CalendarDay day) {
return true;
}

@Override
protected int getRows() {
return showWeekDays ? DAY_NAMES_ROW + 2 : 2;
}
}
1 change: 1 addition & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
<attr name="mcv_calendarMode" format="enum">
<enum name="month" value="0"/>
<enum name="week" value="1"/>
<enum name="two_week" value="2" />
</attr>

<attr name="mcv_selectionMode" format="enum">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.prolificinteractive.materialcalendarview;

import org.junit.Test;
import org.threeten.bp.DayOfWeek;
import org.threeten.bp.Month;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;

public class BiWeeklyRangeIndexTest {
private static final int _2018 = 2018;

@Test
public void test2week() {
final CalendarDay startAugust2018 = CalendarDay.from(_2018, Month.AUGUST.getValue(), 14);
final CalendarDay endAugust2018 = CalendarDay.from(_2018, Month.AUGUST.getValue(), 23);

final DateRangeIndex biWeekly =
new TwoWeekPagerAdapter.BiWeekly(startAugust2018, endAugust2018, DayOfWeek.SUNDAY);

assertThat(biWeekly.getCount(), equalTo(1));

assertThat(biWeekly.getItem(0), equalTo(CalendarDay.from(_2018, Month.AUGUST.getValue(), 12)));

assertThat(biWeekly.indexOf(startAugust2018), equalTo(0));
assertThat(biWeekly.indexOf(endAugust2018), equalTo(0));
}

@Test
public void test4weeks() {
final CalendarDay startAugust2018 = CalendarDay.from(_2018, Month.AUGUST.getValue(), 2);
final CalendarDay endAugust2018 = CalendarDay.from(_2018, Month.AUGUST.getValue(), 23);

final DateRangeIndex biWeekly =
new TwoWeekPagerAdapter.BiWeekly(startAugust2018, endAugust2018, DayOfWeek.SUNDAY);

assertThat(biWeekly.getCount(), equalTo(2));

assertThat(biWeekly.getItem(0), equalTo(CalendarDay.from(_2018, Month.JULY.getValue(), 29)));
assertThat(biWeekly.getItem(1), equalTo(CalendarDay.from(_2018, Month.AUGUST.getValue(), 12)));

assertThat(biWeekly.indexOf(startAugust2018), equalTo(0));
assertThat(biWeekly.indexOf(endAugust2018), equalTo(1));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,14 @@ public void onSetWeekMode() {
widget.state().edit().setCalendarDisplayMode(CalendarMode.WEEKS).commit();
}

@OnClick(R.id.button_two_weeks)
public void onSetTwoWeekMode() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && animateModeTransition.isChecked()) {
TransitionManager.beginDelayedTransition(parent);
}
widget.state().edit().setCalendarDisplayMode(CalendarMode.TWO_WEEKS).commit();
}

@OnClick(R.id.button_months)
public void onSetMonthMode() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && animateModeTransition.isChecked()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ public void onSetWeekMode() {
.commit();
}

@OnClick(R.id.button_two_weeks)
public void onSetTwoWeekMode() {
widget.state().edit()
.setCalendarDisplayMode(CalendarMode.TWO_WEEKS)
.commit();
}

@OnClick(R.id.button_months)
public void onSetMonthMode() {
widget.state().edit()
Expand Down
41 changes: 24 additions & 17 deletions sample/src/main/res/layout/activity_basic_modes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,34 @@
/>

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>

<Button
android:id="@+id/button_months"
android:text="Month Mode"
android:layout_width="0dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
>

<Button
android:id="@+id/button_weeks"
android:text="Week Mode"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>

<Button
android:id="@+id/button_two_weeks"
android:text="Two Week mode"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>

</LinearLayout>

<Button
android:id="@+id/button_weeks"
android:text="Week mode"
android:layout_width="0dp"
android:id="@+id/button_months"
android:text="Month mode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
/>

</LinearLayout>

</LinearLayout>
46 changes: 27 additions & 19 deletions sample/src/main/res/layout/activity_dynamic_setters.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,33 @@
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:id="@+id/button_months"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Month Mode"
/>
<Button
android:id="@+id/button_weeks"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Week mode"
/>
</LinearLayout>
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<Button
android:id="@+id/button_weeks"
android:text="Week mode"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<Button
android:id="@+id/button_two_weeks"
android:text="Two Week mode"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
</LinearLayout>

<Button
android:id="@+id/button_months"
android:text="Month Mode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
/>

<LinearLayout
android:layout_width="match_parent"
Expand Down