Skip to content

Commit

Permalink
Fix - Remove Date From MCV (prolificinteractive#804)
Browse files Browse the repository at this point in the history
  • Loading branch information
quentin41500 authored Jul 17, 2018
1 parent 0cfa25d commit ae92c76
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,13 @@ public static CalendarDay from(@Nullable Calendar calendar) {
/**
* Get a new instance set to the specified day
*
* @param date {@linkplain Date} to pull date information from. Passing null will return null.
* @param date long to pull date information from.
* @return CalendarDay set to the specified date
*/
public static CalendarDay from(@Nullable Date date) {
if (date == null) {
return null;
}
return from(CalendarUtils.getInstance(date));
public static CalendarDay from(long date) {
final Calendar instance = CalendarUtils.getInstance();
instance.setTimeInMillis(date);
return from(instance);
}

private final int year;
Expand Down Expand Up @@ -116,15 +115,6 @@ public CalendarDay(int year, int month, int day) {
this.day = day;
}

/**
* @param date source to pull date information from for this instance
* @see CalendarDay#from(Date)
*/
@Deprecated
public CalendarDay(Date date) {
this(CalendarUtils.getInstance(date));
}

/**
* Get the year
*
Expand Down Expand Up @@ -180,7 +170,6 @@ public Calendar getCalendar() {
}

void copyToMonthOnly(@NonNull Calendar calendar) {
calendar.clear();
calendar.set(year, month, 1);
}

Expand All @@ -190,7 +179,6 @@ void copyToMonthOnly(@NonNull Calendar calendar) {
* @param calendar calendar to set date information to
*/
public void copyTo(@NonNull Calendar calendar) {
calendar.clear();
calendar.set(year, month, day);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.prolificinteractive.materialcalendarview;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import static java.util.Calendar.DATE;
import static java.util.Calendar.DAY_OF_WEEK;
Expand All @@ -16,68 +15,28 @@
*/
public class CalendarUtils {

/**
* @param date {@linkplain Date} to pull date information from
* @return a new Calendar instance with the date set to the provided date. Time set to zero.
*/
public static Calendar getInstance(@Nullable Date date) {
Calendar calendar = Calendar.getInstance();
if (date != null) {
calendar.setTime(date);
}
copyDateTo(calendar, calendar);
return calendar;
}

/**
* @return a new Calendar instance with the date set to today. Time set to zero.
* @return a new Calendar instance with the date set to today.
*/
@NonNull
public static Calendar getInstance() {
Calendar calendar = Calendar.getInstance();
copyDateTo(calendar, calendar);
return calendar;
}

/**
* Set the provided calendar to the first day of the month. Also clears all time information.
*
* @param calendar {@linkplain Calendar} to modify to be at the first fay of the month
*/
public static void setToFirstDay(Calendar calendar) {
int year = getYear(calendar);
int month = getMonth(calendar);
calendar.clear();
calendar.set(year, month, 1);
}

/**
* Copy <i>only</i> date information to a new calendar.
*
* @param from calendar to copy from
* @param to calendar to copy to
*/
public static void copyDateTo(Calendar from, Calendar to) {
int year = getYear(from);
int month = getMonth(from);
int day = getDay(from);
to.clear();
to.set(year, month, day);
return Calendar.getInstance(Locale.getDefault());
}

public static int getYear(Calendar calendar) {
public static int getYear(final Calendar calendar) {
return calendar.get(YEAR);
}

public static int getMonth(Calendar calendar) {
public static int getMonth(final Calendar calendar) {
return calendar.get(MONTH);
}

public static int getDay(Calendar calendar) {
public static int getDay(final Calendar calendar) {
return calendar.get(DATE);
}

public static int getDayOfWeek(Calendar calendar) {
public static int getDayOfWeek(final Calendar calendar) {
return calendar.get(DAY_OF_WEEK);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import android.support.v4.view.ViewPager;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;
import android.util.Log;
import android.util.SparseArray;
import android.util.TypedValue;
import android.view.Gravity;
Expand All @@ -41,7 +40,6 @@
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
Expand Down Expand Up @@ -297,8 +295,7 @@ public void transformPage(View page, float position) {
VERTICAL));

if (firstDayOfWeek < 0) {
//Allowing use of Calendar.getInstance() here as a performance optimization
firstDayOfWeek = Calendar.getInstance().getFirstDayOfWeek();
firstDayOfWeek = CalendarUtils.getInstance().getFirstDayOfWeek();
}

showWeekDays = a.getBoolean(R.styleable.MaterialCalendarView_mcv_showWeekDays, true);
Expand Down Expand Up @@ -822,7 +819,7 @@ public void setSelectedDate(@Nullable Calendar calendar) {
/**
* @param date a Date to set as selected. Null to clear selection
*/
public void setSelectedDate(@Nullable Date date) {
public void setSelectedDate(long date) {
setSelectedDate(CalendarDay.from(date));
}

Expand All @@ -848,7 +845,7 @@ public void setDateSelected(@Nullable Calendar calendar, boolean selected) {
* @param date a Date to change. Passing null does nothing
* @param selected true if day should be selected, false to deselect
*/
public void setDateSelected(@Nullable Date date, boolean selected) {
public void setDateSelected(long date, boolean selected) {
setDateSelected(CalendarDay.from(date), selected);
}

Expand All @@ -873,7 +870,7 @@ public void setCurrentDate(@Nullable Calendar calendar) {
/**
* @param date a Date to focus the calendar on. Null will do nothing
*/
public void setCurrentDate(@Nullable Date date) {
public void setCurrentDate(long date) {
setCurrentDate(CalendarDay.from(date));
}

Expand Down Expand Up @@ -1908,7 +1905,7 @@ public StateBuilder edit() {

public class StateBuilder {
private CalendarMode calendarMode = CalendarMode.MONTHS;
private int firstDayOfWeek = Calendar.getInstance().getFirstDayOfWeek();
private int firstDayOfWeek = CalendarUtils.getInstance().getFirstDayOfWeek();
private boolean cacheCurrentPosition = false;
private CalendarDay minDate = null;
private CalendarDay maxDate = null;
Expand Down Expand Up @@ -1963,7 +1960,7 @@ public StateBuilder setMinimumDate(@Nullable Calendar calendar) {
/**
* @param date set the minimum selectable date, null for no minimum
*/
public StateBuilder setMinimumDate(@Nullable Date date) {
public StateBuilder setMinimumDate(long date) {
setMinimumDate(CalendarDay.from(date));
return this;
}
Expand All @@ -1987,7 +1984,7 @@ public StateBuilder setMaximumDate(@Nullable Calendar calendar) {
/**
* @param date set the maximum selectable date, null for no maximum
*/
public StateBuilder setMaximumDate(@Nullable Date date) {
public StateBuilder setMaximumDate(long date) {
setMaximumDate(CalendarDay.from(date));
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public CalendarDay getItem(int position) {
position * DAYS_IN_WEEK,
TimeUnit.DAYS);
long positionMillis = minMillis + millisOffset;
return CalendarDay.from(new Date(positionMillis));
return CalendarDay.from(positionMillis);
}

private int weekNumberDifference(@NonNull CalendarDay min, @NonNull CalendarDay max) {
Expand All @@ -77,6 +77,7 @@ private int weekNumberDifference(@NonNull CalendarDay min, @NonNull CalendarDay

/*
* Necessary because of how Calendar handles getting the first day of week internally.
* TODO: WTF IS THIS
*/
private CalendarDay getFirstDayOfWeek(@NonNull CalendarDay min, int wantedFirstDayOfWeek) {
Calendar calendar = Calendar.getInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected void onCreate(Bundle savedInstanceState) {
widget.setShowOtherDates(MaterialCalendarView.SHOW_ALL);

Calendar instance = Calendar.getInstance();
widget.setSelectedDate(instance.getTime());
widget.setSelectedDate(instance);

Calendar instance1 = Calendar.getInstance();
instance1.set(instance1.get(Calendar.YEAR), Calendar.JANUARY, 1);
Expand All @@ -51,8 +51,8 @@ protected void onCreate(Bundle savedInstanceState) {
instance2.set(instance2.get(Calendar.YEAR), Calendar.DECEMBER, 31);

widget.state().edit()
.setMinimumDate(instance1.getTime())
.setMaximumDate(instance2.getTime())
.setMinimumDate(instance1)
.setMaximumDate(instance2)
.commit();

widget.addDecorators(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected void onCreate(Bundle savedInstanceState) {
widget.addDecorator(new EnableOneToTenDecorator());

Calendar calendar = Calendar.getInstance();
widget.setSelectedDate(calendar.getTime());
widget.setSelectedDate(calendar);

Calendar instance1 = Calendar.getInstance();
instance1.set(instance1.get(Calendar.YEAR), Calendar.JANUARY, 1);
Expand All @@ -43,8 +43,8 @@ protected void onCreate(Bundle savedInstanceState) {
instance2.set(instance2.get(Calendar.YEAR) + 2, Calendar.OCTOBER, 31);

widget.state().edit()
.setMinimumDate(instance1.getTime())
.setMaximumDate(instance2.getTime())
.setMinimumDate(instance1)
.setMaximumDate(instance2)
.commit();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
* https://github.com/prolificinteractive/material-calendarview/issues/8#issuecomment-241205704
* , test activity with multiple MaterialCalendarViews
*/
public class MultipleViewActivity extends AppCompatActivity{
public class MultipleViewActivity extends AppCompatActivity {
//number of MaterialCalendarViews to display in list
static final int NUM_ENTRIES = 3;

@BindView(R.id.calendar_list) RecyclerView calendarList;
@BindView(R.id.calendar_list)
RecyclerView calendarList;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -63,7 +64,7 @@ public int getItemCount() {
public void onBindViewHolder(EntryViewHolder holder, int position) {
//set selected date to today
final Calendar instance = Calendar.getInstance();
holder.calendarView.setSelectedDate(instance.getTime());
holder.calendarView.setSelectedDate(instance);
}

/**
Expand All @@ -74,7 +75,7 @@ class EntryViewHolder extends RecyclerView.ViewHolder {

EntryViewHolder(final View itemView) {
super(itemView);
calendarView = (MaterialCalendarView) itemView.findViewById(R.id.list_entry);
calendarView = itemView.findViewById(R.id.list_entry);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protected void onCreate(Bundle savedInstanceState) {


Calendar instance = Calendar.getInstance();
widget.setSelectedDate(instance.getTime());
widget.setSelectedDate(instance);

Calendar instance1 = Calendar.getInstance();
instance1.set(instance1.get(Calendar.YEAR), Calendar.JANUARY, 1);
Expand All @@ -48,8 +48,8 @@ protected void onCreate(Bundle savedInstanceState) {
instance2.set(instance2.get(Calendar.YEAR), Calendar.DECEMBER, 31);

widget.state().edit()
.setMinimumDate(instance1.getTime())
.setMaximumDate(instance2.getTime())
.setMinimumDate(instance1)
.setMaximumDate(instance2)
.commit();

widget.addDecorators(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
import com.prolificinteractive.materialcalendarview.DayViewFacade;

import java.util.Calendar;
import java.util.Locale;

/**
* Highlight Saturdays and Sundays with a background
*/
public class HighlightWeekendsDecorator implements DayViewDecorator {

private final Calendar calendar = Calendar.getInstance();
private final Calendar calendar = Calendar.getInstance(Locale.getDefault());
private final Drawable highlightDrawable;
private static final int color = Color.parseColor("#228BC34A");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import com.prolificinteractive.materialcalendarview.DayViewFacade;
import com.prolificinteractive.materialcalendarview.MaterialCalendarView;

import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
* Decorate a day by making the text big and bold
Expand Down Expand Up @@ -37,6 +39,6 @@ public void decorate(DayViewFacade view) {
* We're changing the internals, so make sure to call {@linkplain MaterialCalendarView#invalidateDecorators()}
*/
public void setDate(Date date) {
this.date = CalendarDay.from(date);
this.date = CalendarDay.from(date.getTime());
}
}

0 comments on commit ae92c76

Please sign in to comment.