Skip to content

Commit

Permalink
Merge pull request #12 from fxhereng/update_libs
Browse files Browse the repository at this point in the history
Update libs - Material calendar to 1.4.0, RN to 0.20
  • Loading branch information
chymtt authored Aug 2, 2016
2 parents 9be20d4 + 12d1b1e commit 452ead8
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 3 deletions.
4 changes: 2 additions & 2 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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));
}
}

0 comments on commit 452ead8

Please sign in to comment.