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

Made TitleChanger respect modes #904

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public void onClick(View v) {
new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
titleChanger.setPreviousMonth(currentMonth);
titleChanger.setPreviousDate(currentMonth);
currentMonth = adapter.getItem(position);
updateUi();

Expand Down Expand Up @@ -428,7 +428,7 @@ private void setupChildren() {
}

private void updateUi() {
titleChanger.change(currentMonth);
titleChanger.change(currentMonth, calendarMode);
enableView(buttonPast, canGoBack());
enableView(buttonFuture, canGoForward());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
import android.widget.TextView;
import com.prolificinteractive.materialcalendarview.format.TitleFormatter;

import org.threeten.bp.LocalDate;
import org.threeten.bp.temporal.WeekFields;

import java.util.Locale;

class TitleChanger {

public static final int DEFAULT_ANIMATION_DELAY = 400;
Expand All @@ -28,7 +33,7 @@ class TitleChanger {
private int orientation = MaterialCalendarView.VERTICAL;

private long lastAnimTime = 0;
private CalendarDay previousMonth = null;
private CalendarDay previousDate = null;

public TitleChanger(TextView title) {
this.title = title;
Expand All @@ -44,24 +49,39 @@ public TitleChanger(TextView title) {
);
}

public void change(final CalendarDay currentMonth) {
public void change(final CalendarDay currentDate, CalendarMode mode) {
long currentTime = System.currentTimeMillis();

if (currentMonth == null) {
if (currentDate == null) {
return;
}

if (TextUtils.isEmpty(title.getText()) || (currentTime - lastAnimTime) < animDelay) {
doChange(currentTime, currentMonth, false);
doChange(currentTime, currentDate, false);
}

if (currentMonth.equals(previousMonth) ||
(currentMonth.getMonth() == previousMonth.getMonth()
&& currentMonth.getYear() == previousMonth.getYear())) {
return;
if (mode == CalendarMode.MONTHS) {
if (currentDate.equals(previousDate) ||
(currentDate.getMonth() == previousDate.getMonth()
&& currentDate.getYear() == previousDate.getYear())) {
return;
}
}
else if (mode == CalendarMode.WEEKS) {
LocalDate current = currentDate.getDate();
LocalDate previous = previousDate.getDate();
WeekFields weekFields = WeekFields.of(Locale.getDefault());
int currentWeek = current.get(weekFields.weekOfWeekBasedYear());
int previousWeek = previous.get(weekFields.weekOfWeekBasedYear());

if (currentDate.equals(previousDate) ||
(currentWeek == previousWeek
&& currentDate.getYear() == previousDate.getYear())) {
return;
}
}

doChange(currentTime, currentMonth, true);
doChange(currentTime, currentDate, true);
}

private void doChange(final long now, final CalendarDay currentMonth, boolean animate) {
Expand All @@ -77,7 +97,7 @@ private void doChange(final long now, final CalendarDay currentMonth, boolean an
if (!animate) {
title.setText(newTitle);
} else {
final int translation = translate * (previousMonth.isBefore(currentMonth) ? 1 : -1);
final int translation = translate * (previousDate.isBefore(currentMonth) ? 1 : -1);
final ViewPropertyAnimator viewPropertyAnimator = title.animate();

if (orientation == MaterialCalendarView.HORIZONTAL) {
Expand Down Expand Up @@ -120,7 +140,7 @@ public void onAnimationEnd(Animator animator) {
}).start();
}

previousMonth = currentMonth;
previousDate = currentMonth;
}

private void doTranslation(final TextView title, final int translate) {
Expand All @@ -143,7 +163,7 @@ public int getOrientation() {
return orientation;
}

public void setPreviousMonth(CalendarDay previousMonth) {
this.previousMonth = previousMonth;
public void setPreviousDate(CalendarDay previousDate) {
this.previousDate = previousDate;
}
}