Skip to content
This repository has been archived by the owner on Jan 11, 2021. It is now read-only.

Invalid Date Birthday crash #71

Merged
merged 5 commits into from
Oct 31, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.alexstyl.specialdates.addevent.ui;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.transition.TransitionManager;
import android.support.transition.TransitionManager;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckedTextView;
Expand All @@ -12,9 +10,9 @@

import com.alexstyl.specialdates.R;
import com.alexstyl.specialdates.date.Date;
import com.alexstyl.specialdates.date.DateConstants;
import com.alexstyl.specialdates.date.MonthInt;
import com.alexstyl.specialdates.upcoming.MonthLabels;
import com.alexstyl.specialdates.util.Utils;
import com.novoda.notils.caster.Views;

import java.util.Locale;
Expand Down Expand Up @@ -61,19 +59,13 @@ public void onClick(View v) {
}
}

@TargetApi(Build.VERSION_CODES.KITKAT)
private void hideYearPicker() {
if (Utils.hasKitKat()) {
TransitionManager.beginDelayedTransition(BirthdayDatePicker.this);
}
TransitionManager.beginDelayedTransition(BirthdayDatePicker.this);
yearPicker.setVisibility(GONE);
}

@TargetApi(Build.VERSION_CODES.KITKAT)
private void showYearPicker() {
if (Utils.hasKitKat()) {
TransitionManager.beginDelayedTransition(BirthdayDatePicker.this);
}
TransitionManager.beginDelayedTransition(BirthdayDatePicker.this);
yearPicker.setVisibility(VISIBLE);
}
});
Expand All @@ -94,13 +86,15 @@ private void setupMonthPicker() {
monthPicker.setDisplayedValues(labels.getMonthsOfYear());

monthPicker.setValue(today.getMonth());
monthPicker.setOnValueChangedListener(dateValidator);
}

private void setupYearPicker() {
yearPicker.setMinValue(1900);
yearPicker.setMaxValue(todaysYear());

yearPicker.setValue(todaysYear());
yearPicker.setOnValueChangedListener(dateValidator);
}

private Integer todaysYear() {
Expand Down Expand Up @@ -143,6 +137,7 @@ private int getDayOfMonth() {
}

@MonthInt
@SuppressWarnings("WrongConstant")
private int getMonth() {
return monthPicker.getValue();
}
Expand All @@ -151,4 +146,29 @@ private int getYear() {
return yearPicker.getValue();
}

private final NumberPicker.OnValueChangeListener dateValidator = new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
if (getMonth() == DateConstants.FEBRUARY && isDisplayingYear()) {

if (isValidDate(29, DateConstants.FEBRUARY, getYear())) {
dayPicker.setMaxValue(29);
} else {
dayPicker.setMaxValue(28);
}
} else {
dayPicker.setMaxValue(31);
}
}

private boolean isValidDate(int dayOfMonth, int month, int year) {
try {
Date.on(dayOfMonth, month, year);
return true;
} catch (IllegalArgumentException ex) {
return false;
}
}
};

}
11 changes: 9 additions & 2 deletions mobile/src/main/java/com/alexstyl/specialdates/date/Date.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import com.alexstyl.specialdates.Optional;
import com.alexstyl.specialdates.contact.ShortDate;

import java.util.Locale;

import org.joda.time.IllegalFieldValueException;
import org.joda.time.LocalDate;

import static com.alexstyl.specialdates.date.DateConstants.DECEMBER;
Expand Down Expand Up @@ -38,8 +41,12 @@ public static Date on(int dayOfMonth, @MonthInt int month, int year) {
throw new IllegalArgumentException(
"Do not call DayDate.on() if no year is present. Call the respective method without the year argument instead");
}
LocalDate localDate = new LocalDate(year, month, dayOfMonth);
return new Date(localDate, new Optional<>(year));
try {
LocalDate localDate = new LocalDate(year, month, dayOfMonth);
return new Date(localDate, new Optional<>(year));
} catch (IllegalFieldValueException a) {
throw new IllegalArgumentException(String.format(Locale.US, "%d/%d/%d is invalid", dayOfMonth, month, year));
}
}

private Date(LocalDate localDate, Optional<Integer> year) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,22 @@ public void comparePastDate() {

@Test
public void whenComparingToSameDate_thenTheyAreEqual() {
Date firstDate = Date.on(16, 4, 1991);
Date secondDate = Date.on(16, 4, 1991);
Date firstDate = Date.on(16, APRIL, 1991);
Date secondDate = Date.on(16, APRIL, 1991);

assertThat(firstDate.equals(secondDate)).isTrue();
}

@Test
public void whenComparingToDateWithDifferentYear_thenTheyAreNotEqual() {
Date firstDate = Date.on(16, 4, 1991);
Date secondDate = Date.on(16, 4, 1987);
Date firstDate = Date.on(16, APRIL, 1991);
Date secondDate = Date.on(16, APRIL, 1987);

assertThat(firstDate.equals(secondDate)).isFalse();
}

@Test(expected = IllegalArgumentException.class)
public void throwsException_whenInvalidDateIsCreated() {
Date.on(31, FEBRUARY, 1991);
}
}