Skip to content

Commit 9fe51ca

Browse files
author
Shrikanth Ravi
authored
Merge pull request #36 from tobiasschuerg/removed_calendar
Replaced all usages of Calendar by LocalDate
2 parents 664e9b3 + df52f68 commit 9fe51ca

File tree

8 files changed

+148
-270
lines changed

8 files changed

+148
-270
lines changed

app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ dependencies {
3030
implementation project(':collapsiblecalendarview2')
3131
implementation 'androidx.appcompat:appcompat:1.0.2'
3232
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha2'
33+
implementation 'com.jakewharton.threetenabp:threetenabp:1.1.1'
3334

3435
testImplementation 'junit:junit:4.12'
3536

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
import android.graphics.Color;
44
import androidx.appcompat.app.AppCompatActivity;
55
import android.os.Bundle;
6+
import android.util.Log;
67
import android.view.View;
78

89
import com.shrikanthravi.collapsiblecalendarview.widget.CollapsibleCalendar;
910

10-
import java.util.Calendar;
11+
import org.threeten.bp.LocalDate;
12+
1113
import java.util.GregorianCalendar;
1214

1315

@@ -21,12 +23,12 @@ protected void onCreate(Bundle savedInstanceState) {
2123
getWindow().setStatusBarColor(getResources().getColor(R.color.google_red));
2224

2325
CollapsibleCalendar collapsibleCalendar = findViewById(R.id.collapsibleCalendarView);
24-
Calendar today=new GregorianCalendar();
25-
collapsibleCalendar.addEventTag(today.get(Calendar.YEAR),today.get(Calendar.MONTH),today.get(Calendar.DAY_OF_MONTH));
26-
today.add(Calendar.DATE,1);
27-
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);
2830

29-
System.out.println("Testing date "+collapsibleCalendar.getSelectedDay().getDay()+"/"+collapsibleCalendar.getSelectedDay().getMonth()+"/"+collapsibleCalendar.getSelectedDay().getYear());
31+
Log.d("Testing date ", collapsibleCalendar.getSelectedDay().toString());
3032
collapsibleCalendar.setCalendarListener(new CollapsibleCalendar.CalendarListener() {
3133
@Override
3234
public void onDaySelect() {

collapsiblecalendarview2/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ android {
3434
dependencies {
3535
implementation 'androidx.appcompat:appcompat:1.0.2'
3636
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
37+
implementation 'com.jakewharton.threetenabp:threetenabp:1.1.1'
3738

3839
testImplementation 'junit:junit:4.12'
3940

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

Lines changed: 56 additions & 42 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,27 +10,31 @@
1110
import com.shrikanthravi.collapsiblecalendarview.R;
1211
import com.shrikanthravi.collapsiblecalendarview.widget.UICalendar;
1312

13+
import org.threeten.bp.DayOfWeek;
14+
import org.threeten.bp.LocalDate;
15+
1416
import java.util.ArrayList;
15-
import java.util.Calendar;
1617
import java.util.List;
1718

19+
import static org.threeten.bp.Month.DECEMBER;
20+
import static org.threeten.bp.Month.JANUARY;
21+
1822
/**
1923
* Created by shrikanthravi on 06/03/18.
2024
*/
2125

2226
public class CalendarAdapter {
23-
private int mFirstDayOfWeek = 0;
24-
private Calendar mCal;
27+
private DayOfWeek mFirstDayOfWeek = DayOfWeek.MONDAY;
28+
private LocalDate mCal;
2529
private LayoutInflater mInflater;
26-
private int mEventDotSize= UICalendar.EVENT_DOT_BIG;
30+
private int mEventDotSize = UICalendar.EVENT_DOT_BIG;
2731

28-
List<Day> mItemList = new ArrayList<>();
29-
List<View> mViewList = new ArrayList<>();
30-
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<>();
3135

32-
public CalendarAdapter(Context context, Calendar cal) {
33-
this.mCal = (Calendar) cal.clone();
34-
this.mCal.set(Calendar.DAY_OF_MONTH, 1);
36+
public CalendarAdapter(Context context) {
37+
this.mCal = LocalDate.now().withDayOfMonth(1);
3538
mInflater = LayoutInflater.from(context);
3639

3740
refresh();
@@ -42,22 +45,35 @@ public int getCount() {
4245
return mItemList.size();
4346
}
4447

45-
public Day getItem(int position) {
48+
public LocalDate getItem(int position) {
4649
return mItemList.get(position);
4750
}
4851

4952
public View getView(final int position) {
5053
return mViewList.get(position);
5154
}
5255

53-
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) {
5469
mFirstDayOfWeek = firstDayOfWeek;
5570
}
71+
5672
public void setEventDotSize(int eventDotSize) {
5773
mEventDotSize = eventDotSize;
5874
}
5975

60-
public Calendar getCalendar() {
76+
public LocalDate getCalendar() {
6177
return mCal;
6278
}
6379

@@ -71,71 +87,69 @@ public void refresh() {
7187
mViewList.clear();
7288

7389
// set calendar
74-
int year = mCal.get(Calendar.YEAR);
75-
int month = mCal.get(Calendar.MONTH);
90+
int year = mCal.getYear();
91+
int month = mCal.getMonthValue();
7692

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

79-
int lastDayOfMonth = mCal.getActualMaximum(Calendar.DAY_OF_MONTH);
80-
int firstDayOfWeek = mCal.get(Calendar.DAY_OF_WEEK) - 1;
95+
int lastDayOfMonth = mCal.lengthOfMonth();
96+
DayOfWeek firstDayOfWeek = mCal.getDayOfWeek();
8197

8298
// generate day list
83-
int offset = 0 - (firstDayOfWeek - mFirstDayOfWeek) + 1;
84-
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;
85102
for (int i = offset; i < length + offset; i++) {
86103
int numYear;
87104
int numMonth;
88105
int numDay;
89106

90-
Calendar tempCal = Calendar.getInstance();
91107
if (i <= 0) { // prev month
92-
if (month == 0) {
108+
if (month == JANUARY.getValue()) {
93109
numYear = year - 1;
94-
numMonth = 11;
110+
numMonth = DECEMBER.getValue();
95111
} else {
96112
numYear = year;
97113
numMonth = month - 1;
98114
}
99-
tempCal.set(numYear, numMonth, 1);
100-
numDay = tempCal.getActualMaximum(Calendar.DAY_OF_MONTH) + i;
115+
LocalDate tempCal = LocalDate.of(numYear, numMonth, 1);
116+
numDay = tempCal.lengthOfMonth() + i;
101117
} else if (i > lastDayOfMonth) { // next month
102-
if (month == 11) {
118+
if (month == DECEMBER.getValue()) {
103119
numYear = year + 1;
104-
numMonth = 0;
120+
numMonth = JANUARY.getValue();
105121
} else {
106122
numYear = year;
107-
numMonth = month + 1;
123+
numMonth = month;
108124
}
109-
tempCal.set(numYear, numMonth, 1);
110125
numDay = i - lastDayOfMonth;
111126
} else {
112127
numYear = year;
113128
numMonth = month;
114129
numDay = i;
115130
}
116131

117-
Day day = new Day(numYear, numMonth, numDay);
132+
LocalDate day = LocalDate.of(numYear, numMonth, numDay);
118133
View view;
119-
if(mEventDotSize==UICalendar.EVENT_DOT_SMALL)
120-
view = mInflater.inflate(R.layout.day_layout_small, null);
121-
else
134+
if (mEventDotSize == UICalendar.EVENT_DOT_SMALL) {
135+
view = mInflater.inflate(R.layout.day_layout_small, null);
136+
} else {
122137
view = mInflater.inflate(R.layout.day_layout, null);
138+
}
123139

124-
TextView txtDay = (TextView) view.findViewById(R.id.txt_day);
125-
ImageView imgEventTag = (ImageView) view.findViewById(R.id.img_event_tag);
140+
TextView txtDay = view.findViewById(R.id.txt_day);
141+
ImageView imgEventTag = view.findViewById(R.id.img_event_tag);
126142

127-
txtDay.setText(String.valueOf(day.getDay()));
128-
if (day.getMonth() != mCal.get(Calendar.MONTH)) {
143+
txtDay.setText(String.valueOf(day.getDayOfMonth()));
144+
if (day.getMonth() != mCal.getMonth()) {
129145
txtDay.setAlpha(0.3f);
130146
}
131147

132148
for (int j = 0; j < mEventList.size(); j++) {
133149
Event event = mEventList.get(j);
134-
if (day.getYear() == event.getYear()
135-
&& day.getMonth() == event.getMonth()
136-
&& day.getDay() == event.getDay()) {
150+
if (day.equals(event.getDate())) {
137151
imgEventTag.setVisibility(View.VISIBLE);
138-
imgEventTag.setColorFilter(event.getColor(),PorterDuff.Mode.SRC_ATOP);
152+
imgEventTag.setColorFilter(event.getColor(), PorterDuff.Mode.SRC_ATOP);
139153
}
140154
}
141155

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

Lines changed: 0 additions & 67 deletions
This file was deleted.

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)