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

Adding StartingDay option #124

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
26 changes: 22 additions & 4 deletions library/src/com/roomorama/caldroid/CaldroidFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ public class CaldroidFragment extends DialogFragment {
public final static String YEAR = "year";
public final static String SHOW_NAVIGATION_ARROWS = "showNavigationArrows";
public final static String DISABLE_DATES = "disableDates";
public final static String STARTING_DATE = "startingDate";
public final static String SELECTED_DATES = "selectedDates";
public final static String MIN_DATE = "minDate";
public final static String MAX_DATE = "maxDate";
Expand All @@ -161,8 +162,10 @@ public class CaldroidFragment extends DialogFragment {
protected int year = -1;
protected ArrayList<DateTime> disableDates = new ArrayList<DateTime>();
protected ArrayList<DateTime> selectedDates = new ArrayList<DateTime>();

protected DateTime minDateTime;
protected DateTime maxDateTime;
protected int startingDate = 1;
protected ArrayList<DateTime> dateInMonthsList;

/**
Expand Down Expand Up @@ -297,6 +300,7 @@ public HashMap<String, Object> getCaldroidData() {
caldroidData.put(SELECTED_DATES, selectedDates);
caldroidData.put(_MIN_DATE_TIME, minDateTime);
caldroidData.put(_MAX_DATE_TIME, maxDateTime);
caldroidData.put(STARTING_DATE, startingDate);
caldroidData.put(START_DAY_OF_WEEK, Integer.valueOf(startDayOfWeek));
caldroidData.put(SIX_WEEKS_IN_CALENDAR,
Boolean.valueOf(sixWeeksInCalendar));
Expand Down Expand Up @@ -427,6 +431,7 @@ public Bundle getSavedStates() {

bundle.putBoolean(SHOW_NAVIGATION_ARROWS, showNavigationArrows);
bundle.putBoolean(ENABLE_SWIPE, enableSwipe);
bundle.putInt(STARTING_DATE, startingDate);
bundle.putInt(START_DAY_OF_WEEK, startDayOfWeek);
bundle.putBoolean(SIX_WEEKS_IN_CALENDAR, sixWeeksInCalendar);

Expand Down Expand Up @@ -499,8 +504,8 @@ public void moveToDate(Date date) {
*/
public void moveToDateTime(DateTime dateTime) {

DateTime firstOfMonth = new DateTime(year, month, 1, 0, 0, 0, 0);
DateTime lastOfMonth = firstOfMonth.getEndOfMonth();
DateTime firstOfMonth = new DateTime(year, month, startingDate, 0, 0, 0, 0);
DateTime lastOfMonth = firstOfMonth.getEndOfMonth().plusDays(startingDate-1);

// To create a swipe effect
// Do nothing if the dateTime is in current month
Expand Down Expand Up @@ -891,13 +896,25 @@ protected void refreshMonthTitleTextView() {
firstMonthTime.month = month - 1;
firstMonthTime.monthDay = 1;
long millis = firstMonthTime.toMillis(true);
Time nextMonthTime = firstMonthTime;
nextMonthTime.month = month % 12;
long nextMillis = nextMonthTime.toMillis(true);

// This is the method used by the platform Calendar app to get a
// correctly localized month name for display on a wall calendar
monthYearStringBuilder.setLength(0);
String monthTitle = DateUtils.formatDateRange(getActivity(),
monthYearFormatter, millis, millis, MONTH_YEAR_FLAG).toString();

if(startingDate != 1) {
StringBuilder nextStringBuilder = new StringBuilder(50);
Formatter nextFormatter = new Formatter(
nextStringBuilder, Locale.getDefault());
monthTitle += " - " + DateUtils.formatDateRange(getActivity(),
nextFormatter, nextMillis, nextMillis,
DateUtils.FORMAT_NO_MONTH_DAY | DateUtils.FORMAT_SHOW_DATE).toString();
}

monthTitleTextView.setText(monthTitle);
}

Expand Down Expand Up @@ -956,6 +973,7 @@ protected void retrieveInitialArgs() {
if (startDayOfWeek > 7) {
startDayOfWeek = startDayOfWeek % 7;
}
startingDate = args.getInt(STARTING_DATE, 1);

// Should show arrow
showNavigationArrows = args
Expand Down Expand Up @@ -1131,7 +1149,7 @@ public void onClick(View v) {
*/
private void setupDateGridPages(View view) {
// Get current date time
DateTime currentDateTime = new DateTime(year, month, 1, 0, 0, 0, 0);
DateTime currentDateTime = new DateTime(year, month, startingDate, 0, 0, 0, 0);

// Set to pageChangeListener
pageChangeListener = new DatePageChangeListener();
Expand Down Expand Up @@ -1184,7 +1202,7 @@ private void setupDateGridPages(View view) {

// Set if viewpager wrap around particular month or all months (6 rows)
dateViewPager.setSixWeeksInCalendar(sixWeeksInCalendar);

// Set the numberOfDaysInMonth to dateViewPager so it can calculate the
// height correctly
dateViewPager.setDatesInMonth(dateInMonthsList);
Expand Down
16 changes: 12 additions & 4 deletions library/src/com/roomorama/caldroid/CaldroidGridAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class CaldroidGridAdapter extends BaseAdapter {
protected DateTime maxDateTime;
protected DateTime today;
protected int startDayOfWeek;
protected int startingDate = 1;
protected boolean sixWeeksInCalendar;
protected Resources resources;

Expand All @@ -55,7 +56,7 @@ public class CaldroidGridAdapter extends BaseAdapter {
public void setAdapterDateTime(DateTime dateTime) {
this.month = dateTime.getMonth();
this.year = dateTime.getYear();
this.datetimeList = CalendarHelper.getFullWeeks(this.month, this.year,
this.datetimeList = CalendarHelper.getFullWeeks(this.month, this.year, startingDate,
startDayOfWeek, sixWeeksInCalendar);
}

Expand Down Expand Up @@ -168,10 +169,12 @@ private void populateFromCaldroidData() {
.get(CaldroidFragment._MAX_DATE_TIME);
startDayOfWeek = (Integer) caldroidData
.get(CaldroidFragment.START_DAY_OF_WEEK);
startingDate = (Integer) caldroidData
.get(CaldroidFragment.STARTING_DATE);
sixWeeksInCalendar = (Boolean) caldroidData
.get(CaldroidFragment.SIX_WEEKS_IN_CALENDAR);

this.datetimeList = CalendarHelper.getFullWeeks(this.month, this.year,
this.datetimeList = CalendarHelper.getFullWeeks(this.month, this.year, startingDate,
startDayOfWeek, sixWeeksInCalendar);
}

Expand Down Expand Up @@ -228,9 +231,11 @@ protected void customizeTextView(int position, TextView cellView) {

// Get dateTime of this cell
DateTime dateTime = this.datetimeList.get(position);
DateTime startDate = new DateTime(year, month, startingDate, 0, 0, 0, 0);
DateTime endDate = startDate.getEndOfMonth().plusDays(startingDate - 1);

// Set color of the dates in previous / next month
if (dateTime.getMonth() != month) {
if (dateTime.lt(startDate) || dateTime.gt(endDate)) {
cellView.setTextColor(resources
.getColor(R.color.caldroid_darker_gray));
}
Expand Down Expand Up @@ -281,7 +286,10 @@ protected void customizeTextView(int position, TextView cellView) {
}
}

cellView.setText("" + dateTime.getDay());
if(startingDate != 1 && dateTime.getDay() == 1)
cellView.setText(dateTime.getMonth() + "." + dateTime.getDay());
else
cellView.setText("" + dateTime.getDay());

// Set custom color if required
setCustomResources(dateTime, cellView, cellView);
Expand Down
13 changes: 7 additions & 6 deletions library/src/com/roomorama/caldroid/CalendarHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,12 @@ public class CalendarHelper {
* : calendar can start from customized date instead of Sunday
* @return
*/
public static ArrayList<DateTime> getFullWeeks(int month, int year,
public static ArrayList<DateTime> getFullWeeks(int month, int year, int day,
int startDayOfWeek, boolean sixWeeksInCalendar) {
ArrayList<DateTime> datetimeList = new ArrayList<DateTime>();

DateTime firstDateOfMonth = new DateTime(year, month, 1, 0, 0, 0, 0);
DateTime lastDateOfMonth = firstDateOfMonth.plusDays(firstDateOfMonth
.getNumDaysInMonth() - 1);
DateTime firstDateOfMonth = new DateTime(year, month, day, 0, 0, 0, 0);
DateTime lastDateOfMonth = firstDateOfMonth.getEndOfMonth().plusDays(day - 1);

// Add dates of first week from previous month
int weekdayOfFirstDate = firstDateOfMonth.getWeekDay();
Expand All @@ -63,8 +62,10 @@ public static ArrayList<DateTime> getFullWeeks(int month, int year,
}

// Add dates of current month
for (int i = 0; i < lastDateOfMonth.getDay(); i++) {
datetimeList.add(firstDateOfMonth.plusDays(i));
DateTime index = firstDateOfMonth;
while(index.lt(lastDateOfMonth)) {
datetimeList.add(index);
index = index.plusDays(1);
}

// Add dates of last week from next month
Expand Down