diff --git a/android/build.gradle b/android/build.gradle index ea7cbfa..ed283eb 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -17,6 +17,6 @@ android { dependencies { compile 'com.android.support:appcompat-v7:23.0.0' - compile 'com.facebook.react:react-native:0.19.+' - compile 'com.prolificinteractive:material-calendarview:1.1.0' + compile 'com.facebook.react:react-native:0.20.+' + compile 'com.prolificinteractive:material-calendarview:1.4.0' } diff --git a/android/src/main/java/com/chymtt/reactnativecalendar/CalendarManager.java b/android/src/main/java/com/chymtt/reactnativecalendar/CalendarManager.java index b1e2087..6f90e8e 100644 --- a/android/src/main/java/com/chymtt/reactnativecalendar/CalendarManager.java +++ b/android/src/main/java/com/chymtt/reactnativecalendar/CalendarManager.java @@ -94,7 +94,9 @@ public void setArrowColor(Calendar view, String color) { @ReactProp(name = "firstDayOfWeek") public void setFirstDayOfWeek(Calendar view, String firstDayOfWeek) { if (firstDayOfWeek != null) { - view.setFirstDayOfWeek(getFirstDayOfWeekFromString(firstDayOfWeek)); + view.state().edit() + .setFirstDayOfWeek(getFirstDayOfWeekFromString(firstDayOfWeek)) + .commit(); } } @@ -130,6 +132,62 @@ public void setCurrentDate(Calendar view, ReadableArray data) { } } + @ReactProp(name = "minimumDate") + public void setMinimumDate(Calendar view, ReadableArray data) { + String type = data.getType(0).name(); + if ("String".equals(type)) { + try { + Date date = dateFormat.parse(data.getString(0)); + if (shouldUpdateMinMaxDate(view.getMinimumDate(), date)) { + view.state().edit() + .setMinimumDate(date) + .commit(); + } + } catch (ParseException e) { + throw new JSApplicationIllegalArgumentException("Invalid date format: " + data.getString(0)); + } + } else if ("Number".equals(type)) { + Double value = data.getDouble(0); + Date date = new Date(value.longValue()); + if (shouldUpdateMinMaxDate(view.getMinimumDate(), date)) { + view.state().edit() + .setMinimumDate(date) + .commit(); + } + } else { + throw new JSApplicationIllegalArgumentException("Invalid date format: " + data.getString(0)); + } + } + + @ReactProp(name = "maximumDate") + public void setMaximumDate(Calendar view, ReadableArray data) { + String type = data.getType(0).name(); + + if ("String".equals(type)) { + try { + Date date = dateFormat.parse(data.getString(0)); + if (shouldUpdateMinMaxDate(view.getMaximumDate(), date)) { + view.state().edit() + .setMaximumDate(date) + .commit(); + } + } catch (ParseException e) { + throw new JSApplicationIllegalArgumentException("Invalid date format: " + data.getString(0)); + } + } else if ("Number".equals(type)) { + Double value = data.getDouble(0); + Date date = new Date(value.longValue()); + + if (shouldUpdateMinMaxDate(view.getMaximumDate(), date)) { + view.state().edit() + .setMaximumDate(date) + .commit(); + } + } else { + throw new JSApplicationIllegalArgumentException("Invalid date format: " + data.getString(0)); + } + } + @ReactProp(name = "selectionMode") public void setSelectionMode(Calendar view, String mode) { if (mode != null) { @@ -200,4 +258,26 @@ private int getFirstDayOfWeekFromString(String firstDayOfWeek) { throw new JSApplicationIllegalArgumentException("Unknown firstDayOfWeek property: " + firstDayOfWeek); } } + + /** + * Should update new value of minimum or maximum date + * + * Check if the new min or max date is different from the previous one, if yes we update otherwise we don't. + * + * @param minMaxDate + * @param newDate + * @return boolean + */ + private boolean shouldUpdateMinMaxDate(CalendarDay minMaxDate, Date newDate) { + if (minMaxDate == null) { + return true; + } + + java.util.Calendar newDateCalendar = java.util.Calendar.getInstance(); + newDateCalendar.setTimeInMillis(newDate.getTime()); + + return (minMaxDate.getYear() != newDateCalendar.get(java.util.Calendar.YEAR) && + minMaxDate.getMonth() != newDateCalendar.get(java.util.Calendar.MONTH) && + minMaxDate.getDay() != newDateCalendar.get(java.util.Calendar.DAY_OF_MONTH)); + } }