added calendar date state
This commit is contained in:
parent
b8c0614506
commit
efe3ccc58c
|
|
@ -123,7 +123,8 @@ public abstract class CalendarAdapter<TDayViewHolder extends RecyclerView.ViewHo
|
|||
protected abstract void bindDayItem(@NonNull final TDayViewHolder viewHolder,
|
||||
@NonNull final String day,
|
||||
@NonNull final Date date,
|
||||
@NonNull final CalendarAdapter.State state);
|
||||
@NonNull final CalendarAdapter.State state,
|
||||
@NonNull final CalendarDateState dateState);
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
|
|
@ -156,25 +157,26 @@ public abstract class CalendarAdapter<TDayViewHolder extends RecyclerView.ViewHo
|
|||
+ position - calendarItem.getStartRange());
|
||||
final Date currentDate = new Date((((CalendarDayItem) calendarItem).getDateOfFirstDay()
|
||||
+ position - calendarItem.getStartRange()) * ONE_DAY_LENGTH);
|
||||
final CalendarDateState dateState = ((CalendarDayItem) calendarItem).getDateState();
|
||||
if (startSelectionPosition != null && position == startSelectionPosition) {
|
||||
if (endSelectionPosition == null || endSelectionPosition.equals(startSelectionPosition)) {
|
||||
bindDayItem(holder, currentDay, currentDate, State.SELECTED_ONE_ONLY);
|
||||
bindDayItem(holder, currentDay, currentDate, State.SELECTED_ONE_ONLY, dateState);
|
||||
return;
|
||||
}
|
||||
bindDayItem(holder, currentDay, currentDate, State.SELECTED_FIRST);
|
||||
bindDayItem(holder, currentDay, currentDate, State.SELECTED_FIRST, dateState);
|
||||
return;
|
||||
}
|
||||
if (endSelectionPosition != null && position == endSelectionPosition) {
|
||||
bindDayItem(holder, currentDay, currentDate, State.SELECTED_LAST);
|
||||
bindDayItem(holder, currentDay, currentDate, State.SELECTED_LAST, dateState);
|
||||
return;
|
||||
}
|
||||
if (startSelectionPosition != null && endSelectionPosition != null
|
||||
&& position >= startSelectionPosition && position <= endSelectionPosition) {
|
||||
bindDayItem(holder, currentDay, currentDate, State.SELECTED_MIDDLE);
|
||||
bindDayItem(holder, currentDay, currentDate, State.SELECTED_MIDDLE, dateState);
|
||||
return;
|
||||
}
|
||||
|
||||
bindDayItem(holder, currentDay, currentDate, State.NOT_SELECTED);
|
||||
bindDayItem(holder, currentDay, currentDate, State.NOT_SELECTED, dateState);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -197,8 +199,4 @@ public abstract class CalendarAdapter<TDayViewHolder extends RecyclerView.ViewHo
|
|||
return calendarItems.isEmpty() ? 0 : calendarItems.get(calendarItems.size() - 1).getEndRange();
|
||||
}
|
||||
|
||||
protected boolean isToday(@NonNull final Date currentDate, @NonNull final Date date) {
|
||||
return currentDate.getTime() / ONE_DAY_LENGTH == date.getTime() / ONE_DAY_LENGTH;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
/*
|
||||
* Copyright (c) 2015 RoboSwag (Gavriil Sitnikov, Vsevolod Ivanov)
|
||||
*
|
||||
* This file is part of RoboSwag library.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package ru.touchin.roboswag.components.calendar;
|
||||
|
||||
/**
|
||||
* Created by Ilia Kurtov on 18.03.2016.
|
||||
*/
|
||||
public enum CalendarDateState {
|
||||
|
||||
BEFORE_TODAY,
|
||||
TODAY,
|
||||
AFTER_TODAY
|
||||
|
||||
}
|
||||
|
|
@ -1,25 +1,34 @@
|
|||
package ru.touchin.roboswag.components.calendar;
|
||||
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
public class CalendarDayItem implements CalendarItem {
|
||||
|
||||
private final long firstDayReal;
|
||||
private final int firstDayInMonth;
|
||||
private final long dateOfFirstDay;
|
||||
private final int positionOfFirstDate;
|
||||
private final int startRange;
|
||||
private final int endRange;
|
||||
@NonNull
|
||||
private final CalendarDateState dateState;
|
||||
|
||||
public CalendarDayItem(final long firstDayReal, final int firstDayInMonth, final int startRange, final int endRange) {
|
||||
this.firstDayReal = firstDayReal;
|
||||
this.firstDayInMonth = firstDayInMonth;
|
||||
public CalendarDayItem(final long dateOfFirstDay,
|
||||
final int positionOfFirstDate,
|
||||
final int startRange,
|
||||
final int endRange,
|
||||
@NonNull final CalendarDateState dateState) {
|
||||
this.dateOfFirstDay = dateOfFirstDay;
|
||||
this.positionOfFirstDate = positionOfFirstDate;
|
||||
this.startRange = startRange;
|
||||
this.endRange = endRange;
|
||||
this.dateState = dateState;
|
||||
}
|
||||
|
||||
public long getDateOfFirstDay() {
|
||||
return firstDayReal;
|
||||
return dateOfFirstDay;
|
||||
}
|
||||
|
||||
public int getPositionOfFirstDay() {
|
||||
return firstDayInMonth;
|
||||
return positionOfFirstDate;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -32,4 +41,9 @@ public class CalendarDayItem implements CalendarItem {
|
|||
return endRange;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public CalendarDateState getDateState() {
|
||||
return dateState;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -124,39 +124,34 @@ public final class CalendarUtils {
|
|||
final Calendar cleanStartDate = getCleanDate(startDate);
|
||||
final Calendar cleanEndDate = getCleanDate(endDate);
|
||||
|
||||
final List<CalendarItem> calendarItems = new ArrayList<>();
|
||||
|
||||
final Calendar calendar = Calendar.getInstance(Locale.getDefault());
|
||||
calendar.setTime(cleanStartDate.getTime());
|
||||
|
||||
calendarItems.add(new CalendarHeaderItem(calendar.get(Calendar.MONTH), 0, 0));
|
||||
int shift = 1;
|
||||
final List<CalendarItem> calendarItems = fillCalendarTillCurrentDate(cleanStartDate, calendar);
|
||||
|
||||
final int totalDaysCount = (int) ((cleanEndDate.getTimeInMillis() - cleanStartDate.getTimeInMillis()) / CalendarAdapter.ONE_DAY_LENGTH + 1);
|
||||
long firstRangeDate = calendar.getTimeInMillis() / CalendarAdapter.ONE_DAY_LENGTH + 1;
|
||||
int firstRange = calendar.get(Calendar.DAY_OF_MONTH) - 1;
|
||||
int daysEnded = 0;
|
||||
calendar.add(Calendar.DAY_OF_MONTH, 1);
|
||||
|
||||
shift += getFirstDateStart(cleanStartDate);
|
||||
if (shift > 1) {
|
||||
calendarItems.add(new CalendarEmptyItem(1, shift - 1));
|
||||
}
|
||||
final int totalDaysCount = (int) ((cleanEndDate.getTimeInMillis() - calendar.getTimeInMillis()) / CalendarAdapter.ONE_DAY_LENGTH + 1);
|
||||
int shift = calendarItems.get(calendarItems.size() - 1).getEndRange();
|
||||
int firstDate = calendar.get(Calendar.DAY_OF_MONTH) - 1;
|
||||
int daysEnded = 1;
|
||||
|
||||
while (true) {
|
||||
final int daysInCurrentMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
|
||||
if ((daysEnded + (daysInCurrentMonth - firstRange)) <= totalDaysCount) {
|
||||
long firstRangeDate = calendar.getTimeInMillis() / CalendarAdapter.ONE_DAY_LENGTH + 1;
|
||||
|
||||
if ((daysEnded + (daysInCurrentMonth - firstDate)) <= totalDaysCount) {
|
||||
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
|
||||
calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
|
||||
calendar.setTime(new Date(calendar.getTimeInMillis() + CalendarAdapter.ONE_DAY_LENGTH));
|
||||
|
||||
calendarItems.add(new CalendarDayItem(firstRangeDate, firstRange + 1,
|
||||
shift + daysEnded, shift + daysEnded + (daysInCurrentMonth - firstRange) - 1));
|
||||
daysEnded += daysInCurrentMonth - firstRange;
|
||||
calendarItems.add(new CalendarDayItem(firstRangeDate, firstDate + 1, shift + daysEnded,
|
||||
shift + daysEnded + (daysInCurrentMonth - firstDate) - 1, CalendarDateState.AFTER_TODAY));
|
||||
daysEnded += daysInCurrentMonth - firstDate;
|
||||
if (daysEnded == totalDaysCount) {
|
||||
break;
|
||||
}
|
||||
firstRangeDate = calendar.getTimeInMillis() / CalendarAdapter.ONE_DAY_LENGTH + 1;
|
||||
firstRange = 0;
|
||||
firstDate = 0;
|
||||
|
||||
final int firstDayInWeek = getFirstDateStart(calendar);
|
||||
|
||||
|
|
@ -174,7 +169,8 @@ public final class CalendarUtils {
|
|||
}
|
||||
|
||||
} else {
|
||||
calendarItems.add(new CalendarDayItem(firstRangeDate, firstRange + 1, shift + daysEnded, shift + totalDaysCount));
|
||||
calendarItems.add(new CalendarDayItem(firstRangeDate, firstDate + 1, shift + daysEnded, shift + totalDaysCount,
|
||||
CalendarDateState.AFTER_TODAY));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
@ -182,6 +178,37 @@ public final class CalendarUtils {
|
|||
return calendarItems;
|
||||
}
|
||||
|
||||
private static List<CalendarItem> fillCalendarTillCurrentDate(final Calendar cleanStartDate, final Calendar calendar) {
|
||||
final List<CalendarItem> calendarItems = new ArrayList<>();
|
||||
int shift = 0;
|
||||
int firstDate = calendar.get(Calendar.DAY_OF_MONTH) - 1;
|
||||
|
||||
// add first month header
|
||||
calendarItems.add(new CalendarHeaderItem(calendar.get(Calendar.MONTH), 0, 0));
|
||||
calendar.set(Calendar.DAY_OF_MONTH, 1);
|
||||
shift += 1;
|
||||
|
||||
final int firstDayInTheWeek = getFirstDateStart(calendar);
|
||||
|
||||
// check if first day is Monday. If not - add empty items. Otherwise do nothing
|
||||
if (firstDayInTheWeek != 0) {
|
||||
calendarItems.add(new CalendarEmptyItem(shift, shift + firstDayInTheWeek - 1));
|
||||
}
|
||||
shift += firstDayInTheWeek;
|
||||
|
||||
// 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;
|
||||
|
||||
// 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));
|
||||
|
||||
return calendarItems;
|
||||
}
|
||||
|
||||
private static int getFirstDateStart(@NonNull final Calendar calendar) {
|
||||
int firstDateStart = calendar.get(Calendar.DAY_OF_WEEK) - 2;
|
||||
if (firstDateStart == -1) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue