Skip to content

Commit

Permalink
Merge pull request #6294 from grzesiek2010/COLLECT-5866_1
Browse files Browse the repository at this point in the history
Added Buddhist calendar
  • Loading branch information
seadowg authored Aug 20, 2024
2 parents 490f750 + ce7d8ce commit dad9240
Show file tree
Hide file tree
Showing 39 changed files with 613 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.odk.collect.android.application.Collect;
import org.odk.collect.android.logic.DatePickerDetails;
import org.odk.collect.android.widgets.datetime.DatePickerDetails;
import org.odk.collect.android.widgets.utilities.DateTimeWidgetUtils;
import org.odk.collect.testshared.TimeZoneSetter;

Expand All @@ -46,6 +46,7 @@ public class DateTimeUtilsTest {
private DatePickerDetails bikramSambatDatePickerDetails;
private DatePickerDetails myanmarDatePickerDetails;
private DatePickerDetails persianDatePickerDetails;
private DatePickerDetails buddhistDatePickerDetails;

private Context context;
private Locale defaultLocale;
Expand All @@ -60,6 +61,7 @@ public void setUp() {
bikramSambatDatePickerDetails = new DatePickerDetails(DatePickerDetails.DatePickerType.BIKRAM_SAMBAT, DatePickerDetails.DatePickerMode.SPINNERS);
myanmarDatePickerDetails = new DatePickerDetails(DatePickerDetails.DatePickerType.MYANMAR, DatePickerDetails.DatePickerMode.SPINNERS);
persianDatePickerDetails = new DatePickerDetails(DatePickerDetails.DatePickerType.PERSIAN, DatePickerDetails.DatePickerMode.SPINNERS);
buddhistDatePickerDetails = new DatePickerDetails(DatePickerDetails.DatePickerType.BUDDHIST, DatePickerDetails.DatePickerMode.SPINNERS);

context = Collect.getInstance();
defaultLocale = Locale.getDefault();
Expand Down Expand Up @@ -96,6 +98,9 @@ public void getDateTimeLabelTest() {

assertEquals("28 Mehr 1370 (Oct 20, 1991)", DateTimeWidgetUtils.getDateTimeLabel(date, persianDatePickerDetails, false, context));
assertEquals("28 Mehr 1370, 14:00 (Oct 20, 1991, 14:00)", DateTimeWidgetUtils.getDateTimeLabel(date, persianDatePickerDetails, true, context));

assertEquals("20 ตุลาคม 2534 (Oct 20, 1991)", DateTimeWidgetUtils.getDateTimeLabel(date, buddhistDatePickerDetails, false, context));
assertEquals("20 ตุลาคม 2534, 14:00 (Oct 20, 1991, 14:00)", DateTimeWidgetUtils.getDateTimeLabel(date, buddhistDatePickerDetails, true, context));
}

@After
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@
import org.odk.collect.android.formhierarchy.FormHierarchyActivity;
import org.odk.collect.android.formhierarchy.ViewOnlyFormHierarchyActivity;
import org.odk.collect.android.fragments.MediaLoadingFragment;
import org.odk.collect.android.fragments.dialogs.CustomDatePickerDialog;
import org.odk.collect.android.fragments.dialogs.CustomTimePickerDialog;
import org.odk.collect.android.widgets.datetime.pickers.CustomDatePickerDialog;
import org.odk.collect.android.widgets.datetime.pickers.CustomTimePickerDialog;
import org.odk.collect.android.fragments.dialogs.LocationProvidersDisabledDialog;
import org.odk.collect.android.fragments.dialogs.NumberPickerDialog;
import org.odk.collect.android.fragments.dialogs.RankingWidgetDialog;
Expand Down Expand Up @@ -161,7 +161,7 @@
import org.odk.collect.android.utilities.SavepointsRepositoryProvider;
import org.odk.collect.android.utilities.ScreenContext;
import org.odk.collect.android.utilities.SoftKeyboardController;
import org.odk.collect.android.widgets.DateTimeWidget;
import org.odk.collect.android.widgets.datetime.DateTimeWidget;
import org.odk.collect.android.widgets.QuestionWidget;
import org.odk.collect.android.widgets.interfaces.WidgetDataReceiver;
import org.odk.collect.android.widgets.items.SelectOneFromMapDialogFragment;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ object Appearances {
const val BIKRAM_SAMBAT = "bikram-sambat"
const val MYANMAR = "myanmar"
const val PERSIAN = "persian"
const val BUDDHIST = "buddhist"
const val NO_CALENDAR = "no-calendar"
const val MONTH_YEAR = "month-year"
const val YEAR = "year"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
import org.odk.collect.android.storage.StoragePathProvider;
import org.odk.collect.android.utilities.Appearances;
import org.odk.collect.android.utilities.QuestionMediaManager;
import org.odk.collect.android.widgets.datetime.DateTimeWidget;
import org.odk.collect.android.widgets.datetime.DateWidget;
import org.odk.collect.android.widgets.datetime.TimeWidget;
import org.odk.collect.android.widgets.items.LabelWidget;
import org.odk.collect.android.widgets.items.LikertWidget;
import org.odk.collect.android.widgets.items.ListMultiWidget;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
* limitations under the License.
*/

package org.odk.collect.android.logic;
package org.odk.collect.android.widgets.datetime;

import java.io.Serializable;

public class DatePickerDetails implements Serializable {
public enum DatePickerType {
GREGORIAN, ETHIOPIAN, COPTIC, ISLAMIC, BIKRAM_SAMBAT, MYANMAR, PERSIAN
GREGORIAN, ETHIOPIAN, COPTIC, ISLAMIC, BIKRAM_SAMBAT, MYANMAR, PERSIAN, BUDDHIST
}

public enum DatePickerMode {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.odk.collect.android.utilities;
package org.odk.collect.android.widgets.datetime;

import org.javarosa.core.model.data.TimeData;
import org.joda.time.DateTime;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* the License.
*/

package org.odk.collect.android.widgets;
package org.odk.collect.android.widgets.datetime;

import android.annotation.SuppressLint;
import android.app.Activity;
Expand All @@ -28,8 +28,7 @@
import org.joda.time.LocalDateTime;
import org.odk.collect.android.databinding.DateTimeWidgetAnswerBinding;
import org.odk.collect.android.formentry.questions.QuestionDetails;
import org.odk.collect.android.logic.DatePickerDetails;
import org.odk.collect.android.utilities.DateTimeUtils;
import org.odk.collect.android.widgets.QuestionWidget;
import org.odk.collect.android.widgets.interfaces.WidgetDataReceiver;
import org.odk.collect.android.widgets.utilities.DateTimeWidgetUtils;
import org.odk.collect.android.widgets.utilities.WaitingForDataRegistry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* the License.
*/

package org.odk.collect.android.widgets;
package org.odk.collect.android.widgets.datetime;

import android.annotation.SuppressLint;
import android.app.Activity;
Expand All @@ -26,8 +26,7 @@
import org.joda.time.LocalDateTime;
import org.odk.collect.android.databinding.DateWidgetAnswerBinding;
import org.odk.collect.android.formentry.questions.QuestionDetails;
import org.odk.collect.android.logic.DatePickerDetails;
import org.odk.collect.android.utilities.DateTimeUtils;
import org.odk.collect.android.widgets.QuestionWidget;
import org.odk.collect.android.widgets.interfaces.WidgetDataReceiver;
import org.odk.collect.android.widgets.utilities.DateTimeWidgetUtils;
import org.odk.collect.android.widgets.utilities.WaitingForDataRegistry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* the License.
*/

package org.odk.collect.android.widgets;
package org.odk.collect.android.widgets.datetime;

import android.annotation.SuppressLint;
import android.app.Activity;
Expand All @@ -28,7 +28,7 @@
import org.joda.time.LocalDateTime;
import org.odk.collect.android.databinding.TimeWidgetAnswerBinding;
import org.odk.collect.android.formentry.questions.QuestionDetails;
import org.odk.collect.android.utilities.DateTimeUtils;
import org.odk.collect.android.widgets.QuestionWidget;
import org.odk.collect.android.widgets.interfaces.WidgetDataReceiver;
import org.odk.collect.android.widgets.utilities.DateTimeWidgetUtils;
import org.odk.collect.android.widgets.utilities.WaitingForDataRegistry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package org.odk.collect.android.fragments.dialogs;
package org.odk.collect.android.widgets.datetime.pickers;

import org.joda.time.LocalDateTime;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package org.odk.collect.android.widgets.datetime.pickers

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import org.joda.time.LocalDateTime
import org.joda.time.chrono.BuddhistChronology
import org.odk.collect.android.R
import org.odk.collect.android.widgets.datetime.DateTimeUtils

class BuddhistDatePickerDialog : CustomDatePickerDialog() {
private lateinit var monthsArray: Array<String>

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
monthsArray = resources.getStringArray(R.array.buddhist_months)
return super.onCreateView(inflater, container, savedInstanceState)
}

override fun onResume() {
super.onResume()
setUpValues()
}

override fun updateDays() {
val localDateTime = getCurrentBuddhistDate()
setUpDayPicker(localDateTime.dayOfMonth, localDateTime.dayOfMonth().maximumValue)
}

override fun getOriginalDate(): LocalDateTime {
return getCurrentBuddhistDate()
}

private fun setUpDatePicker() {
val buddhistDate = DateTimeUtils
.skipDaylightSavingGapIfExists(date)
.toDateTime()
.withChronology(BuddhistChronology.getInstance())
.toLocalDateTime()

setUpDayPicker(buddhistDate.dayOfMonth, buddhistDate.dayOfMonth().maximumValue)
setUpMonthPicker(buddhistDate.monthOfYear, monthsArray)
setUpYearPicker(buddhistDate.year, MIN_SUPPORTED_YEAR, MAX_SUPPORTED_YEAR)
}

private fun setUpValues() {
setUpDatePicker()
updateGregorianDateLabel()
}

private fun getCurrentBuddhistDate(): LocalDateTime {
var buddhistDay = day
val buddhistMonth = monthsArray.indexOf(month)
val buddhistYear = year

val buddhistDate = LocalDateTime(
buddhistYear,
buddhistMonth + 1,
1,
0,
0,
0,
0,
BuddhistChronology.getInstance()
)
if (buddhistDay > buddhistDate.dayOfMonth().maximumValue) {
buddhistDay = buddhistDate.dayOfMonth().maximumValue
}

return LocalDateTime(
buddhistYear,
buddhistMonth + 1,
buddhistDay,
0,
0,
0,
0,
BuddhistChronology.getInstance()
)
}

companion object {
const val MIN_SUPPORTED_YEAR = 2443 // 1900 in Gregorian calendar
const val MAX_SUPPORTED_YEAR = 2643 // 2100 in Gregorian calendar
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
* limitations under the License.
*/

package org.odk.collect.android.fragments.dialogs;
package org.odk.collect.android.widgets.datetime.pickers;

import org.joda.time.LocalDateTime;
import org.joda.time.chrono.CopticChronology;
import org.odk.collect.android.R;
import org.odk.collect.android.utilities.DateTimeUtils;
import org.odk.collect.android.widgets.datetime.DateTimeUtils;

import java.util.Arrays;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package org.odk.collect.android.fragments.dialogs;
package org.odk.collect.android.widgets.datetime.pickers;

import android.app.Dialog;
import android.content.Context;
Expand All @@ -30,8 +30,8 @@

import org.joda.time.LocalDateTime;
import org.odk.collect.android.R;
import org.odk.collect.android.logic.DatePickerDetails;
import org.odk.collect.android.utilities.DateTimeUtils;
import org.odk.collect.android.widgets.datetime.DatePickerDetails;
import org.odk.collect.android.widgets.datetime.DateTimeUtils;
import org.odk.collect.android.widgets.utilities.DateTimeWidgetUtils;
import org.odk.collect.android.widgets.viewmodels.DateTimeViewModel;

Expand Down Expand Up @@ -67,7 +67,7 @@ public void onAttach(Context context) {
viewModel.setDatePickerDetails((DatePickerDetails) getArguments().getSerializable(DateTimeWidgetUtils.DATE_PICKER_DETAILS));

viewModel.getSelectedDate().observe(this, localDateTime -> {
if (localDateTime != null) {
if (localDateTime != null && dateChangeListener != null) {
dateChangeListener.onDateChanged(localDateTime);
}
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.odk.collect.android.fragments.dialogs;
package org.odk.collect.android.widgets.datetime.pickers;

import android.app.Dialog;
import android.app.TimePickerDialog;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
* limitations under the License.
*/

package org.odk.collect.android.fragments.dialogs;
package org.odk.collect.android.widgets.datetime.pickers;

import org.joda.time.LocalDateTime;
import org.joda.time.chrono.EthiopicChronology;
import org.odk.collect.android.R;
import org.odk.collect.android.utilities.DateTimeUtils;
import org.odk.collect.android.widgets.datetime.DateTimeUtils;

import java.util.Arrays;

Expand Down Expand Up @@ -82,4 +82,4 @@ private LocalDateTime getCurrentEthiopianDate() {

return new LocalDateTime(ethiopianYear, ethiopianMonth + 1, ethiopianDay, 0, 0, 0, 0, EthiopicChronology.getInstance());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.odk.collect.android.fragments.dialogs;
package org.odk.collect.android.widgets.datetime.pickers;

import android.app.DatePickerDialog;
import android.app.Dialog;
Expand All @@ -19,7 +19,7 @@
import androidx.lifecycle.ViewModelProvider;

import org.joda.time.LocalDateTime;
import org.odk.collect.android.logic.DatePickerDetails;
import org.odk.collect.android.widgets.datetime.DatePickerDetails;
import org.odk.collect.android.utilities.ThemeUtils;
import org.odk.collect.android.widgets.utilities.DateTimeWidgetUtils;
import org.odk.collect.android.widgets.viewmodels.DateTimeViewModel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
* limitations under the License.
*/

package org.odk.collect.android.fragments.dialogs;
package org.odk.collect.android.widgets.datetime.pickers;

import org.joda.time.LocalDateTime;
import org.joda.time.chrono.IslamicChronology;
import org.odk.collect.android.R;
import org.odk.collect.android.utilities.DateTimeUtils;
import org.odk.collect.android.widgets.datetime.DateTimeUtils;

import java.util.Arrays;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
* limitations under the License.
*/

package org.odk.collect.android.fragments.dialogs;
package org.odk.collect.android.widgets.datetime.pickers;

import org.joda.time.LocalDateTime;
import org.odk.collect.android.utilities.DateTimeUtils;
import org.odk.collect.android.widgets.datetime.DateTimeUtils;
import org.odk.collect.android.utilities.MyanmarDateUtils;

import java.util.List;
Expand Down Expand Up @@ -70,4 +70,4 @@ private MyanmarDate getCurrentMyanmarDate() {

return MyanmarDateUtils.createMyanmarDate(getYear(), monthIndex, dayOfMonth);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
* limitations under the License.
*/

package org.odk.collect.android.fragments.dialogs;
package org.odk.collect.android.widgets.datetime.pickers;

import org.joda.time.LocalDateTime;
import org.joda.time.chrono.PersianChronologyKhayyamBorkowski;
import org.odk.collect.android.R;
import org.odk.collect.android.utilities.DateTimeUtils;
import org.odk.collect.android.widgets.datetime.DateTimeUtils;

import java.util.Arrays;

Expand Down Expand Up @@ -78,4 +78,4 @@ private LocalDateTime getCurrentPersianDate() {

return new LocalDateTime(persianYear, persianMonth + 1, persianDay, 0, 0, 0, 0, PersianChronologyKhayyamBorkowski.getInstance());
}
}
}
Loading

0 comments on commit dad9240

Please sign in to comment.