From c0bd08cc8831a14359671cc92d8c6681a9b2ad5a Mon Sep 17 00:00:00 2001 From: Ilia Kurtov Date: Wed, 1 Jun 2016 11:14:04 +0300 Subject: [PATCH] calendar header fix (TUTU-1286) --- .../components/calendar/CalendarUtils.java | 27 ++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/main/java/ru/touchin/roboswag/components/calendar/CalendarUtils.java b/src/main/java/ru/touchin/roboswag/components/calendar/CalendarUtils.java index fd5971f..fed0433 100644 --- a/src/main/java/ru/touchin/roboswag/components/calendar/CalendarUtils.java +++ b/src/main/java/ru/touchin/roboswag/components/calendar/CalendarUtils.java @@ -219,16 +219,37 @@ public final class CalendarUtils { // add range with days before today calendarItems.add(new CalendarDayItem(calendar.getTimeInMillis() / CalendarAdapter.ONE_DAY_LENGTH + 1, 1, shift, shift + firstDate - 1, CalendarDateState.BEFORE_TODAY)); - shift += firstDate - 1; + shift += firstDate; // add today item calendar.setTime(cleanStartDate.getTime()); calendarItems.add(new CalendarDayItem(calendar.getTimeInMillis() / CalendarAdapter.ONE_DAY_LENGTH + 1, - firstDate + 1, shift + 1, shift + 1, CalendarDateState.TODAY)); + firstDate + 1, shift, shift, CalendarDateState.TODAY)); + shift += 1; + + //add empty items and header if current day the last day in the month + if (calendar.get(Calendar.DAY_OF_MONTH) == calendar.getActualMaximum(Calendar.DAY_OF_MONTH)) { + addItemsIfCurrentDayTheLastDayInTheMonth(calendar, calendarItems, shift); + } return calendarItems; } + private static void addItemsIfCurrentDayTheLastDayInTheMonth(@NonNull final Calendar calendar, + @NonNull final List calendarItems, + final int initialShift) { + int shift = initialShift; + final Calendar newMonthCalendar = Calendar.getInstance(); + newMonthCalendar.setTime(calendar.getTime()); + newMonthCalendar.add(Calendar.DAY_OF_MONTH, 1); + final int firstFayInNextMonth = getFirstDateStart(newMonthCalendar); + calendarItems.add(new CalendarEmptyItem(shift, shift + (7 - firstFayInNextMonth) - 1)); + shift += 7 - firstFayInNextMonth; + calendarItems.add(new CalendarHeaderItem(calendar.get(Calendar.MONTH) + 1, shift, shift)); + shift += 1; + calendarItems.add(new CalendarEmptyItem(shift, shift + firstFayInNextMonth - 1)); + } + private static int getFirstDateStart(@NonNull final Calendar calendar) { int firstDateStart = calendar.get(Calendar.DAY_OF_WEEK) - 2; if (firstDateStart == -1) { @@ -249,4 +270,4 @@ public final class CalendarUtils { private CalendarUtils() { } -} +} \ No newline at end of file