/
CalendarAdapter.java
160 lines (129 loc) · 4.69 KB
/
CalendarAdapter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package com.shrikanthravi.collapsiblecalendarview.data;
import android.content.Context;
import android.graphics.PorterDuff;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.shrikanthravi.collapsiblecalendarview.R;
import com.shrikanthravi.collapsiblecalendarview.widget.UICalendar;
import org.threeten.bp.DayOfWeek;
import org.threeten.bp.LocalDate;
import java.util.ArrayList;
import java.util.List;
import static org.threeten.bp.Month.DECEMBER;
import static org.threeten.bp.Month.JANUARY;
/**
* Created by shrikanthravi on 06/03/18.
*/
public class CalendarAdapter {
private DayOfWeek mFirstDayOfWeek = DayOfWeek.MONDAY;
private LocalDate mCal;
private LayoutInflater mInflater;
private int mEventDotSize = UICalendar.EVENT_DOT_BIG;
private List<LocalDate> mItemList = new ArrayList<>();
private List<View> mViewList = new ArrayList<>();
private List<Event> mEventList = new ArrayList<>();
public CalendarAdapter(Context context) {
this.mCal = LocalDate.now().withDayOfMonth(1);
mInflater = LayoutInflater.from(context);
refresh();
}
// public methods
public int getCount() {
return mItemList.size();
}
public LocalDate getItem(int position) {
return mItemList.get(position);
}
public View getView(final int position) {
return mViewList.get(position);
}
public void nextMonth() {
mCal = mCal.plusMonths(1);
}
public void previousMonth() {
mCal = mCal.minusMonths(1);
}
public void setDate(LocalDate date) {
mCal = date;
}
public void setFirstDayOfWeek(DayOfWeek firstDayOfWeek) {
mFirstDayOfWeek = firstDayOfWeek;
}
public void setEventDotSize(int eventDotSize) {
mEventDotSize = eventDotSize;
}
public LocalDate getCalendar() {
return mCal;
}
public void addEvent(Event event) {
mEventList.add(event);
}
public void refresh() {
// clear data
mItemList.clear();
mViewList.clear();
// set calendar
int year = mCal.getYear();
int month = mCal.getMonthValue();
mCal = LocalDate.of(year, month, 1);
int lastDayOfMonth = mCal.lengthOfMonth();
DayOfWeek firstDayOfWeek = mCal.getDayOfWeek();
// generate day list
int offset = 0 - (firstDayOfWeek.getValue() - mFirstDayOfWeek.getValue());
if (offset > 0) offset += -7;
int length = (int) Math.ceil((float) (lastDayOfMonth - offset) / 7) * 7;
for (int i = offset; i < length + offset; i++) {
int numYear;
int numMonth;
int numDay;
if (i <= 0) { // prev month
if (month == JANUARY.getValue()) {
numYear = year - 1;
numMonth = DECEMBER.getValue();
} else {
numYear = year;
numMonth = month - 1;
}
LocalDate tempCal = LocalDate.of(numYear, numMonth, 1);
numDay = tempCal.lengthOfMonth() + i;
} else if (i > lastDayOfMonth) { // next month
if (month == DECEMBER.getValue()) {
numYear = year + 1;
numMonth = JANUARY.getValue();
} else {
numYear = year;
numMonth = month;
}
numDay = i - lastDayOfMonth;
} else {
numYear = year;
numMonth = month;
numDay = i;
}
LocalDate day = LocalDate.of(numYear, numMonth, numDay);
View view;
if (mEventDotSize == UICalendar.EVENT_DOT_SMALL) {
view = mInflater.inflate(R.layout.day_layout_small, null);
} else {
view = mInflater.inflate(R.layout.day_layout, null);
}
TextView txtDay = view.findViewById(R.id.txt_day);
ImageView imgEventTag = view.findViewById(R.id.img_event_tag);
txtDay.setText(String.valueOf(day.getDayOfMonth()));
if (day.getMonth() != mCal.getMonth()) {
txtDay.setAlpha(0.3f);
}
for (int j = 0; j < mEventList.size(); j++) {
Event event = mEventList.get(j);
if (day.equals(event.getDate())) {
imgEventTag.setVisibility(View.VISIBLE);
imgEventTag.setColorFilter(event.getColor(), PorterDuff.Mode.SRC_ATOP);
}
}
mItemList.add(day);
mViewList.add(view);
}
}
}