44import android .graphics .Color ;
55
66import com .facebook .react .bridge .JSApplicationIllegalArgumentException ;
7+ import com .facebook .react .bridge .ReadableArray ;
78import com .facebook .react .uimanager .SimpleViewManager ;
89import com .facebook .react .uimanager .ThemedReactContext ;
910import com .facebook .react .uimanager .annotations .ReactProp ;
2425public 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 () {
0 commit comments