Skip to content

Commit

Permalink
Fixed bugs in pickers
Browse files Browse the repository at this point in the history
  • Loading branch information
grzesiek2010 committed May 8, 2019
1 parent 59e0f68 commit 6fad725
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.odk.collect.android.logic.DatePickerDetails;
import org.odk.collect.android.utilities.MyanmarDateUtils;

import java.util.List;

import mmcalendar.MyanmarDate;
import mmcalendar.MyanmarDateKernel;

Expand All @@ -45,7 +47,7 @@ public void onResume() {
@Override
protected void updateDays() {
MyanmarDate myanmarDate = getCurrentMyanmarDate();
setUpDayPicker(MyanmarDateUtils.getFirstDayOfCurrentMonth(myanmarDate), myanmarDate.getMonthDay(), myanmarDate.getMonthLength());
setUpDayPicker(MyanmarDateUtils.getFirstMonthDay(myanmarDate), myanmarDate.getMonthDay(), MyanmarDateUtils.getMonthLength(myanmarDate));
}

@Override
Expand All @@ -63,15 +65,17 @@ protected LocalDateTime getOriginalDate() {
private void setUpDatePicker() {
MyanmarDate myanmarDate = MyanmarDateUtils.gregorianDateToMyanmarDate(getDateWithSkippedDaylightSavingGapIfExists());

setUpDayPicker(MyanmarDateUtils.getFirstDayOfCurrentMonth(myanmarDate), myanmarDate.getMonthDay(), myanmarDate.getMonthLength());
setUpDayPicker(MyanmarDateUtils.getFirstMonthDay(myanmarDate), myanmarDate.getMonthDay(), MyanmarDateUtils.getMonthLength(myanmarDate));
setUpMonthPicker(MyanmarDateUtils.getMonthId(myanmarDate) + 1, MyanmarDateUtils.getMyanmarMonthsArray(myanmarDate.getYearInt()));
setUpYearPicker(myanmarDate.getYearInt(), MIN_SUPPORTED_YEAR, MAX_SUPPORTED_YEAR);
}

private MyanmarDate getCurrentMyanmarDate() {
return MyanmarDateUtils.createMyanmarDate(
getYear(),
MyanmarDateKernel.getMyanmarMonth(getYear(), 1).getIndex().get(getMonthId()),
getDay());
List<Integer> monthIndexes = MyanmarDateKernel.getMyanmarMonth(getYear(), 1).getIndex();
int monthIndex = getMonthId() < monthIndexes.size() ? monthIndexes.get(getMonthId()) : monthIndexes.get(monthIndexes.size() - 1);
int monthLength = MyanmarDateUtils.getMonthLength(getYear(), monthIndex);
int dayOfMonth = getDay() > monthLength ? monthLength : getDay();

return MyanmarDateUtils.createMyanmarDate(getYear(), monthIndex, dayOfMonth);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import mmcalendar.MyanmarDate;
import mmcalendar.MyanmarDateConverter;
import mmcalendar.MyanmarDateKernel;
import mmcalendar.Thingyan;
import mmcalendar.ThingyanCalculator;
import mmcalendar.WesternDate;
import mmcalendar.WesternDateConverter;
Expand Down Expand Up @@ -66,17 +65,12 @@ public static String[] getMyanmarMonthsArray(int myanmarYear) {
.toArray(new String[0]);
}

public static int getFirstDayOfCurrentMonth(MyanmarDate myanmarDate) {
int day = 1;
// New year date is based on the solar year, therefore, we need to find a new year day for the first month.
if (isFirstMonthInYear(myanmarDate)) {
Thingyan thingyan = ThingyanCalculator.getThingyan(myanmarDate.getYearInt());
day = MyanmarDateKernel
.j2m(thingyan.getMyanmarNewYearDay())
.getMonthDay();
}

return day;
public static int getFirstMonthDay(MyanmarDate myanmarDate) {
return isFirstYearMonth(myanmarDate) ? getNewYearsDay(myanmarDate.getYearInt()) : 1;
}

private static int getFirstMonthDay(int myanmarYear, int monthIndex) {
return isFirstYearMonth(myanmarYear, monthIndex) ? getNewYearsDay(myanmarYear) : 1;
}

public static int getMonthId(MyanmarDate myanmarDate) {
Expand All @@ -86,7 +80,33 @@ public static int getMonthId(MyanmarDate myanmarDate) {
.indexOf(myanmarDate.getMonthName(new LanguageCatalog(Language.MYANMAR)));
}

private static boolean isFirstMonthInYear(MyanmarDate myanmarDate) {
public static int getMonthLength(MyanmarDate myanmarDate) {
int newYearsDayOfNextYear = getNewYearsDay(myanmarDate.getYearInt() + 1);
return isLastMonthInYear(myanmarDate) && newYearsDayOfNextYear > 1
? newYearsDayOfNextYear - 1
: myanmarDate.getMonthLength();
}

public static int getMonthLength(int myanmarYear, int monthIndex) {
MyanmarDate myanmarDate = MyanmarDateUtils.createMyanmarDate(myanmarYear, monthIndex, MyanmarDateUtils.getFirstMonthDay(myanmarYear, monthIndex));
return getMonthLength(myanmarDate);
}

private static int getNewYearsDay(int myanmarYear) {
return MyanmarDateKernel
.j2m(ThingyanCalculator.getThingyan(myanmarYear).getMyanmarNewYearDay())
.getMonthDay();
}

private static boolean isLastMonthInYear(MyanmarDate myanmarDate) {
return getMonthId(myanmarDate) == getMyanmarMonthsArray(myanmarDate.getYearInt()).length - 1;
}

private static boolean isFirstYearMonth(MyanmarDate myanmarDate) {
return getMonthId(myanmarDate) == 0;
}

private static boolean isFirstYearMonth(int myanmarYear, int monthIndex) {
return monthIndex == MyanmarDateKernel.getMyanmarMonth(myanmarYear, 1).getIndex().get(0);
}
}

0 comments on commit 6fad725

Please sign in to comment.