Skip to content

Commit e0d4772

Browse files
committed
Add decorators
1 parent 8a0318b commit e0d4772

13 files changed

Lines changed: 121 additions & 53 deletions

File tree

app/src/main/java/com/nucllear/rn_materialcalendarview/ReactMaterialCalendarView.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
public class ReactMaterialCalendarView extends MaterialCalendarView implements OnDateSelectedListener, OnMonthChangedListener {
2525
private WeekEndsDecorator weekEnds;
2626
private EventDecorator events;
27+
private TodayDecorator today;
28+
2729
public ReactMaterialCalendarView(Context context) {
2830
super(context);
2931
setLayoutParams(new ViewGroup.LayoutParams(
@@ -35,9 +37,14 @@ public ReactMaterialCalendarView(Context context) {
3537
this.setOnMonthChangedListener(this);
3638

3739
weekEnds = new WeekEndsDecorator();
38-
events = new EventDecorator("dot");
40+
events = new EventDecorator();
41+
today = new TodayDecorator(context, this.getSelectionColor());
42+
43+
this.addDecorators(today, weekEnds, events);
44+
}
3945

40-
this.addDecorators(new TodayDecorator(), weekEnds, events);
46+
public EventDecorator getEvents() {
47+
return events;
4148
}
4249

4350
private final Runnable mLayoutRunnable = new Runnable() {
@@ -89,4 +96,12 @@ public void onMonthChanged(MaterialCalendarView widget, CalendarDay date) {
8996
public void setWeekEndsColor(String color) {
9097
weekEnds.setColor(color);
9198
}
99+
100+
public void setEventsColor(String color) {
101+
events.setColor(color);
102+
}
103+
104+
public void setTodayColor(String color) {
105+
today.setColor(color);
106+
}
92107
}

app/src/main/java/com/nucllear/rn_materialcalendarview/ReactMaterialCalendarViewManager.java

Lines changed: 74 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.graphics.Color;
55

66
import com.facebook.react.bridge.JSApplicationIllegalArgumentException;
7+
import com.facebook.react.bridge.ReadableArray;
78
import com.facebook.react.uimanager.SimpleViewManager;
89
import com.facebook.react.uimanager.ThemedReactContext;
910
import com.facebook.react.uimanager.annotations.ReactProp;
@@ -24,7 +25,12 @@
2425
public class ReactMaterialCalendarViewManager extends SimpleViewManager<ReactMaterialCalendarView> {
2526

2627
private static final String REACT_CLASS = "RCTMaterialCalendarView";
28+
private static final String DATE_FORMAT = "yyyy/MM/dd";
2729
private static final String COLOR_REGEX = "^#([0-9A-Fa-f]{6}|[0-9A-Fa-f]{8})$";
30+
private static final String DATE_REGEX = "^(19|20)\\d\\d[/](0[1-9]|1[012])[/](0[1-9]|[12][0-9]|3[01])";
31+
32+
@SuppressLint("SimpleDateFormat")
33+
private static final DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
2834

2935
@Override
3036
public String getName() {
@@ -91,30 +97,29 @@ public void setFirstDayOfWeek(ReactMaterialCalendarView view, String firstDayOfW
9197
}
9298

9399
@ReactProp(name = "minimumDate")
94-
public void setMinimumDate(ReactMaterialCalendarView view, String minimumDate) {
100+
public void setMinimumDate(ReactMaterialCalendarView view, String minimumDate) throws ParseException {
95101
if (minimumDate != null) {
96-
try {
97-
@SuppressLint("SimpleDateFormat") DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
98-
Date minDate = df.parse(minimumDate);
102+
if (minimumDate.matches(DATE_REGEX)) {
103+
Date minDate = dateFormat.parse(minimumDate);
99104
view.state().edit()
100105
.setMinimumDate(CalendarDay.from(minDate))
101106
.commit();
102-
} catch (ParseException e) {
107+
} else {
103108
throw new JSApplicationIllegalArgumentException("Invalid date format: " + minimumDate);
104109
}
105110
}
106111
}
107112

108113
@ReactProp(name = "maximumDate")
109-
public void setMaximumDate(ReactMaterialCalendarView view, String maximumDate) {
114+
public void setMaximumDate(ReactMaterialCalendarView view, String maximumDate) throws ParseException {
110115
if (maximumDate != null) {
111-
try {
112-
@SuppressLint("SimpleDateFormat") DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
113-
Date maxDate = df.parse(maximumDate);
116+
if (maximumDate.matches(DATE_REGEX)) {
117+
Date maxDate = dateFormat.parse(maximumDate);
114118
view.state().edit()
115119
.setMaximumDate(CalendarDay.from(maxDate))
116120
.commit();
117-
} catch (ParseException e) {
121+
122+
} else {
118123
throw new JSApplicationIllegalArgumentException("Invalid date format: " + maximumDate);
119124
}
120125
}
@@ -143,7 +148,6 @@ public void setDatesSelection(ReactMaterialCalendarView view, String sMode) {
143148
}
144149
}
145150

146-
// todo doesn't work properly
147151
@ReactProp(name = "showOtherDates")
148152
public void setShowOtherDates(ReactMaterialCalendarView view, String showDate) {
149153
if (showDate != null) {
@@ -169,25 +173,61 @@ public void setShowOtherDates(ReactMaterialCalendarView view, String showDate) {
169173
// Set date
170174

171175
@ReactProp(name = "currentDate")
172-
public void setCurrentDate(ReactMaterialCalendarView view, String currentDate) {
176+
public void setCurrentDate(ReactMaterialCalendarView view, String currentDate) throws ParseException {
173177
if (currentDate != null) {
174-
try {
175-
@SuppressLint("SimpleDateFormat") DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
176-
Date curDate = df.parse(currentDate);
178+
if (currentDate.matches(DATE_REGEX)) {
179+
Date curDate = dateFormat.parse(currentDate);
177180
view.setCurrentDate(curDate);
178-
} catch (ParseException e) {
181+
} else {
179182
throw new JSApplicationIllegalArgumentException("Invalid date format: " + currentDate);
180183
}
181184
}
182185
}
183186

187+
@ReactProp(name = "selectedDates")
188+
public void setSelectedDates(ReactMaterialCalendarView view, ReadableArray dates) throws ParseException {
189+
ArrayList<Date> selectedDates = new ArrayList<Date>();
190+
for (int i = 0; i < dates.size(); i++) {
191+
String type = dates.getType(i).name();
192+
if("String".equals(type) && dates.getString(i).matches(DATE_REGEX)){
193+
Date date = dateFormat.parse(dates.getString(i));
194+
selectedDates.add(date);
195+
} else {
196+
throw new JSApplicationIllegalArgumentException("Invalid date format: " + dates.getString(i));
197+
}
198+
}
199+
for (Date date : selectedDates) {
200+
view.setDateSelected(date, true);
201+
}
202+
}
203+
204+
@ReactProp(name = "eventsDates")
205+
public void setEventsDates(ReactMaterialCalendarView view, ReadableArray dates) throws ParseException {
206+
ArrayList<CalendarDay> decorated = new ArrayList<CalendarDay>();
207+
for (int i = 0; i < dates.size(); i++) {
208+
String type = dates.getType(i).name();
209+
if("String".equals(type) && dates.getString(i).matches(DATE_REGEX)){
210+
Date date = dateFormat.parse(dates.getString(i));
211+
decorated.add(CalendarDay.from(date));
212+
} else {
213+
throw new JSApplicationIllegalArgumentException("Invalid date format: " + dates.getString(i));
214+
}
215+
}
216+
if (decorated.size() > 0) {
217+
view.getEvents().setDates(decorated);
218+
}
219+
}
220+
221+
222+
184223
// Color customizations
185224

186225
@ReactProp(name = "selectionColor")
187226
public void setSelectionColor(ReactMaterialCalendarView view, String color) {
188227
if (color != null) {
189228
if (color.matches(COLOR_REGEX)) {
190229
view.setSelectionColor(Color.parseColor(color));
230+
view.setTodayColor(color);
191231
} else {
192232
throw new JSApplicationIllegalArgumentException("Invalid selectionColor property: " + color);
193233
}
@@ -205,21 +245,32 @@ public void setWeekendsColor(ReactMaterialCalendarView view, String color) {
205245
}
206246
}
207247

248+
@ReactProp(name = "eventsColor")
249+
public void setEventsColor(ReactMaterialCalendarView view, String color) {
250+
if (color != null) {
251+
if (color.matches(COLOR_REGEX)) {
252+
view.setEventsColor(color);
253+
} else {
254+
throw new JSApplicationIllegalArgumentException("Invalid weekendsColor property: " + color);
255+
}
256+
}
257+
}
258+
208259
// Events
209260

210261
/**
211262
* Returns a map of config data passed to JS that defines eligible events that can be placed on
212263
* native views. This should return bubbling directly-dispatched event types and specify what
213264
* names should be used to subscribe to either form (bubbling/capturing).
214-
*
265+
* <p>
215266
* Returned map should be of the form:
216267
* {
217-
* "onTwirl": {
218-
* "phasedRegistrationNames": {
219-
* "bubbled": "onTwirl",
220-
* "captured": "onTwirlCaptured"
221-
* }
222-
* }
268+
* "onTwirl": {
269+
* "phasedRegistrationNames": {
270+
* "bubbled": "onTwirl",
271+
* "captured": "onTwirlCaptured"
272+
* }
273+
* }
223274
* }
224275
*/
225276
public Map<String, Object> getExportedCustomBubblingEventTypeConstants() {

app/src/main/java/com/nucllear/rn_materialcalendarview/decorators/EventDecorator.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,21 @@
1212

1313
public class EventDecorator implements DayViewDecorator {
1414

15-
private String name;
1615
private int color;
1716
private HashSet<CalendarDay> dates;
1817

19-
public EventDecorator(String name) {
20-
this.name = name;
18+
public EventDecorator() {
2119
}
2220

23-
public EventDecorator(String name, int color) {
24-
this.name = name;
21+
public EventDecorator(int color) {
2522
this.color = color;
2623
}
2724

28-
public EventDecorator(String name, Collection<CalendarDay> dates) {
25+
public EventDecorator(Collection<CalendarDay> dates) {
2926
this.dates = new HashSet<>(dates);
3027
}
3128

32-
public EventDecorator(String name, int color, Collection<CalendarDay> dates) {
29+
public EventDecorator(int color, Collection<CalendarDay> dates) {
3330
this.color = color;
3431
this.dates = new HashSet<>(dates);
3532
}
@@ -44,7 +41,7 @@ public void setColor(String color) {
4441

4542
@Override
4643
public boolean shouldDecorate(CalendarDay day) {
47-
return dates.contains(day);
44+
return dates != null && dates.contains(day);
4845
}
4946

5047
@Override
Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,35 @@
11
package com.nucllear.rn_materialcalendarview.decorators;
22

33
import android.content.Context;
4-
import android.graphics.Typeface;
5-
import android.text.style.RelativeSizeSpan;
6-
import android.text.style.StyleSpan;
4+
import android.graphics.Color;
5+
import android.graphics.drawable.Drawable;
6+
import android.os.Build;
77

8+
import com.nucllear.rn_materialcalendarview.R;
89
import com.prolificinteractive.materialcalendarview.CalendarDay;
910
import com.prolificinteractive.materialcalendarview.DayViewDecorator;
1011
import com.prolificinteractive.materialcalendarview.DayViewFacade;
1112

1213
public class TodayDecorator implements DayViewDecorator {
1314

15+
private final Drawable drawable;
1416
private CalendarDay date;
17+
private String color;
1518

16-
public TodayDecorator() {
19+
public TodayDecorator(Context context) {
1720
date = CalendarDay.today();
21+
drawable = (Drawable) context.getResources().getDrawable(R.drawable.shape);
22+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && color != null) {
23+
drawable.setTint(Color.parseColor(color));
24+
}
25+
}
26+
27+
public TodayDecorator(Context context, int color) {
28+
date = CalendarDay.today();
29+
drawable = (Drawable) context.getResources().getDrawable(R.drawable.shape);
30+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
31+
drawable.setTint(color);
32+
}
1833
}
1934

2035
@Override
@@ -24,7 +39,10 @@ public boolean shouldDecorate(CalendarDay day) {
2439

2540
@Override
2641
public void decorate(DayViewFacade view) {
27-
view.addSpan(new StyleSpan(Typeface.BOLD));
28-
view.addSpan(new RelativeSizeSpan(1.4f));
42+
view.setBackgroundDrawable(drawable);
43+
}
44+
45+
public void setColor(String color) {
46+
this.color = color;
2947
}
3048
}

app/src/main/java/com/nucllear/rn_materialcalendarview/decorators/WeekEndsDecorator.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,12 @@
33
import android.graphics.Color;
44
import android.text.style.ForegroundColorSpan;
55

6-
import com.nucllear.rn_materialcalendarview.ReactMaterialCalendarView;
76
import com.prolificinteractive.materialcalendarview.CalendarDay;
87
import com.prolificinteractive.materialcalendarview.DayViewDecorator;
98
import com.prolificinteractive.materialcalendarview.DayViewFacade;
109

1110
import java.util.Calendar;
1211

13-
/**
14-
* Highlight Saturdays and Sundays with a foreground red
15-
*/
1612
public class WeekEndsDecorator implements DayViewDecorator {
1713

1814
private final Calendar calendar = Calendar.getInstance();
562 Bytes
Loading
355 Bytes
Loading
711 Bytes
Loading
1.09 KB
Loading
1.67 KB
Loading

0 commit comments

Comments
 (0)