Skip to content

Commit 2183f69

Browse files
committed
Replaced remaining usages of Calendar.
1 parent 568da1e commit 2183f69

File tree

5 files changed

+114
-163
lines changed

5 files changed

+114
-163
lines changed

app/src/main/java/com/shrikanthravi/collapsiblecalendarview/MainActivity.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
import com.shrikanthravi.collapsiblecalendarview.widget.CollapsibleCalendar;
1010

11-
import java.util.Calendar;
11+
import org.threeten.bp.LocalDate;
12+
1213
import java.util.GregorianCalendar;
1314

1415

@@ -22,10 +23,10 @@ protected void onCreate(Bundle savedInstanceState) {
2223
getWindow().setStatusBarColor(getResources().getColor(R.color.google_red));
2324

2425
CollapsibleCalendar collapsibleCalendar = findViewById(R.id.collapsibleCalendarView);
25-
Calendar today=new GregorianCalendar();
26-
collapsibleCalendar.addEventTag(today.get(Calendar.YEAR),today.get(Calendar.MONTH),today.get(Calendar.DAY_OF_MONTH));
27-
today.add(Calendar.DATE,1);
28-
collapsibleCalendar.addEventTag(today.get(Calendar.YEAR),today.get(Calendar.MONTH),today.get(Calendar.DAY_OF_MONTH),Color.BLUE);
26+
LocalDate today = LocalDate.now();
27+
collapsibleCalendar.addEventTag(today);
28+
LocalDate tomorrow = today.plusDays(1);
29+
collapsibleCalendar.addEventTag(tomorrow, Color.BLUE);
2930

3031
Log.d("Testing date ", collapsibleCalendar.getSelectedDay().toString());
3132
collapsibleCalendar.setCalendarListener(new CollapsibleCalendar.CalendarListener() {

collapsiblecalendarview2/src/main/java/com/shrikanthravi/collapsiblecalendarview/data/CalendarAdapter.java

Lines changed: 50 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.shrikanthravi.collapsiblecalendarview.data;
22

33
import android.content.Context;
4-
import android.graphics.Color;
54
import android.graphics.PorterDuff;
65
import android.view.LayoutInflater;
76
import android.view.View;
@@ -11,31 +10,31 @@
1110
import com.shrikanthravi.collapsiblecalendarview.R;
1211
import com.shrikanthravi.collapsiblecalendarview.widget.UICalendar;
1312

13+
import org.threeten.bp.DayOfWeek;
1414
import org.threeten.bp.LocalDate;
15-
import org.threeten.bp.LocalTime;
16-
import org.threeten.bp.Month;
1715

1816
import java.util.ArrayList;
19-
import java.util.Calendar;
2017
import java.util.List;
2118

19+
import static org.threeten.bp.Month.DECEMBER;
20+
import static org.threeten.bp.Month.JANUARY;
21+
2222
/**
2323
* Created by shrikanthravi on 06/03/18.
2424
*/
2525

2626
public class CalendarAdapter {
27-
private int mFirstDayOfWeek = 0;
28-
private Calendar mCal;
27+
private DayOfWeek mFirstDayOfWeek = DayOfWeek.MONDAY;
28+
private LocalDate mCal;
2929
private LayoutInflater mInflater;
30-
private int mEventDotSize= UICalendar.EVENT_DOT_BIG;
30+
private int mEventDotSize = UICalendar.EVENT_DOT_BIG;
3131

32-
List<LocalDate> mItemList = new ArrayList<>();
33-
List<View> mViewList = new ArrayList<>();
34-
List<Event> mEventList = new ArrayList<>();
32+
private List<LocalDate> mItemList = new ArrayList<>();
33+
private List<View> mViewList = new ArrayList<>();
34+
private List<Event> mEventList = new ArrayList<>();
3535

36-
public CalendarAdapter(Context context, Calendar cal) {
37-
this.mCal = (Calendar) cal.clone();
38-
this.mCal.set(Calendar.DAY_OF_MONTH, 1);
36+
public CalendarAdapter(Context context) {
37+
this.mCal = LocalDate.now().withDayOfMonth(1);
3938
mInflater = LayoutInflater.from(context);
4039

4140
refresh();
@@ -54,14 +53,27 @@ public View getView(final int position) {
5453
return mViewList.get(position);
5554
}
5655

57-
public void setFirstDayOfWeek(int firstDayOfWeek) {
56+
public void nextMonth() {
57+
mCal = mCal.plusMonths(1);
58+
}
59+
60+
public void previousMonth() {
61+
mCal = mCal.minusMonths(1);
62+
}
63+
64+
public void setDate(LocalDate date) {
65+
mCal = date;
66+
}
67+
68+
public void setFirstDayOfWeek(DayOfWeek firstDayOfWeek) {
5869
mFirstDayOfWeek = firstDayOfWeek;
5970
}
71+
6072
public void setEventDotSize(int eventDotSize) {
6173
mEventDotSize = eventDotSize;
6274
}
6375

64-
public Calendar getCalendar() {
76+
public LocalDate getCalendar() {
6577
return mCal;
6678
}
6779

@@ -75,71 +87,69 @@ public void refresh() {
7587
mViewList.clear();
7688

7789
// set calendar
78-
int year = mCal.get(Calendar.YEAR);
79-
int month = mCal.get(Calendar.MONTH);
90+
int year = mCal.getYear();
91+
int month = mCal.getMonthValue();
8092

81-
mCal.set(year, month, 1);
93+
mCal = LocalDate.of(year, month, 1);
8294

83-
int lastDayOfMonth = mCal.getActualMaximum(Calendar.DAY_OF_MONTH);
84-
int firstDayOfWeek = mCal.get(Calendar.DAY_OF_WEEK) - 1;
95+
int lastDayOfMonth = mCal.lengthOfMonth();
96+
DayOfWeek firstDayOfWeek = mCal.getDayOfWeek();
8597

8698
// generate day list
87-
int offset = 0 - (firstDayOfWeek - mFirstDayOfWeek) + 1;
88-
int length = (int) Math.ceil((float) (lastDayOfMonth - offset + 1) / 7) * 7;
99+
int offset = 0 - (firstDayOfWeek.getValue() - mFirstDayOfWeek.getValue());
100+
if (offset > 0) offset += -7;
101+
int length = (int) Math.ceil((float) (lastDayOfMonth - offset) / 7) * 7;
89102
for (int i = offset; i < length + offset; i++) {
90103
int numYear;
91104
int numMonth;
92105
int numDay;
93106

94-
Calendar tempCal = Calendar.getInstance();
95107
if (i <= 0) { // prev month
96-
if (month == 0) {
108+
if (month == JANUARY.getValue()) {
97109
numYear = year - 1;
98-
numMonth = 11;
110+
numMonth = DECEMBER.getValue();
99111
} else {
100112
numYear = year;
101113
numMonth = month - 1;
102114
}
103-
tempCal.set(numYear, numMonth, 1);
104-
numDay = tempCal.getActualMaximum(Calendar.DAY_OF_MONTH) + i;
115+
LocalDate tempCal = LocalDate.of(numYear, numMonth, 1);
116+
numDay = tempCal.lengthOfMonth() + i;
105117
} else if (i > lastDayOfMonth) { // next month
106-
if (month == 11) {
118+
if (month == DECEMBER.getValue()) {
107119
numYear = year + 1;
108-
numMonth = 0;
120+
numMonth = JANUARY.getValue();
109121
} else {
110122
numYear = year;
111-
numMonth = month + 1;
123+
numMonth = month;
112124
}
113-
tempCal.set(numYear, numMonth, 1);
114125
numDay = i - lastDayOfMonth;
115126
} else {
116127
numYear = year;
117128
numMonth = month;
118129
numDay = i;
119130
}
120131

121-
LocalDate day = LocalDate.of(numYear, numMonth + 1, numDay);
132+
LocalDate day = LocalDate.of(numYear, numMonth, numDay);
122133
View view;
123-
if(mEventDotSize==UICalendar.EVENT_DOT_SMALL)
124-
view = mInflater.inflate(R.layout.day_layout_small, null);
125-
else
134+
if (mEventDotSize == UICalendar.EVENT_DOT_SMALL) {
135+
view = mInflater.inflate(R.layout.day_layout_small, null);
136+
} else {
126137
view = mInflater.inflate(R.layout.day_layout, null);
138+
}
127139

128140
TextView txtDay = view.findViewById(R.id.txt_day);
129141
ImageView imgEventTag = view.findViewById(R.id.img_event_tag);
130142

131143
txtDay.setText(String.valueOf(day.getDayOfMonth()));
132-
if (day.getMonthValue() -1 != mCal.get(Calendar.MONTH)) {
144+
if (day.getMonth() != mCal.getMonth()) {
133145
txtDay.setAlpha(0.3f);
134146
}
135147

136148
for (int j = 0; j < mEventList.size(); j++) {
137149
Event event = mEventList.get(j);
138-
if (day.getYear() == event.getYear()
139-
&& day.getMonthValue() == event.getMonth()
140-
&& day.getDayOfMonth() == event.getDay()) {
150+
if (day.equals(event.getDate())) {
141151
imgEventTag.setVisibility(View.VISIBLE);
142-
imgEventTag.setColorFilter(event.getColor(),PorterDuff.Mode.SRC_ATOP);
152+
imgEventTag.setColorFilter(event.getColor(), PorterDuff.Mode.SRC_ATOP);
143153
}
144154
}
145155

collapsiblecalendarview2/src/main/java/com/shrikanthravi/collapsiblecalendarview/data/Event.java

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,27 @@
11
package com.shrikanthravi.collapsiblecalendarview.data;
22

3+
import org.threeten.bp.LocalDate;
4+
35
/**
46
* Created by shrikanthravi on 06/03/18.
57
*/
68

79
public class Event {
8-
private int mYear;
9-
private int mMonth;
10-
private int mDay;
11-
private int mColor;
12-
13-
public Event(int year, int month, int day){
14-
this.mYear = year;
15-
this.mMonth = month;
16-
this.mDay = day;
17-
}
10+
private final LocalDate date;
1811

19-
public Event(int year, int month, int day, int color){
20-
this.mYear = year;
21-
this.mMonth = month;
22-
this.mDay = day;
23-
this.mColor=color;
24-
}
12+
private int mColor;
2513

26-
public int getMonth(){
27-
return mMonth;
14+
public Event(LocalDate date) {
15+
this.date = date;
2816
}
2917

30-
public int getYear(){
31-
return mYear;
18+
public Event(LocalDate date, int color) {
19+
this.date = date;
20+
this.mColor = color;
3221
}
3322

34-
public int getDay(){
35-
return mDay;
23+
public LocalDate getDate() {
24+
return date;
3625
}
3726

3827
public int getColor() {

0 commit comments

Comments
 (0)