forked from github/wulkanowy-mirror
Stop synchronization on holidays (#142)
This commit is contained in:

committed by
Rafał Borcz

parent
8725640168
commit
5a4b8b22f3
@ -30,6 +30,8 @@ import io.github.wulkanowy.ui.main.MainActivity;
|
||||
import io.github.wulkanowy.utils.FabricUtils;
|
||||
import timber.log.Timber;
|
||||
|
||||
import static io.github.wulkanowy.utils.TimeUtilsKt.isHolidays;
|
||||
|
||||
public class SyncJob extends SimpleJobService {
|
||||
|
||||
public static final String JOB_TAG = "SyncJob";
|
||||
@ -66,6 +68,12 @@ public class SyncJob extends SimpleJobService {
|
||||
|
||||
@Override
|
||||
public int onRunJob(JobParameters job) {
|
||||
if (isHolidays()) {
|
||||
stop(getApplicationContext());
|
||||
|
||||
return JobService.RESULT_FAIL_NORETRY;
|
||||
}
|
||||
|
||||
try {
|
||||
repository.getSyncRepo().initLastUser();
|
||||
repository.getSyncRepo().syncAll();
|
||||
|
@ -1,6 +1,5 @@
|
||||
package io.github.wulkanowy.ui.main;
|
||||
|
||||
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import javax.inject.Inject;
|
||||
@ -8,6 +7,8 @@ import javax.inject.Inject;
|
||||
import io.github.wulkanowy.data.RepositoryContract;
|
||||
import io.github.wulkanowy.ui.base.BasePresenter;
|
||||
|
||||
import static io.github.wulkanowy.utils.TimeUtilsKt.isHolidays;
|
||||
|
||||
public class MainPresenter extends BasePresenter<MainContract.View>
|
||||
implements MainContract.Presenter {
|
||||
|
||||
@ -35,7 +36,7 @@ public class MainPresenter extends BasePresenter<MainContract.View>
|
||||
getView().initiationBottomNav(tabPosition);
|
||||
getView().initiationViewPager(tabPosition);
|
||||
|
||||
if (getRepository().getSharedRepo().isServicesEnable()) {
|
||||
if (getRepository().getSharedRepo().isServicesEnable() && !isHolidays()) {
|
||||
getView().startSyncService(getRepository().getSharedRepo().getServicesInterval(),
|
||||
getRepository().getSharedRepo().isMobileDisable());
|
||||
}
|
||||
|
@ -10,10 +10,12 @@ import javax.inject.Inject;
|
||||
import io.github.wulkanowy.data.RepositoryContract;
|
||||
import io.github.wulkanowy.ui.base.BasePresenter;
|
||||
import io.github.wulkanowy.ui.main.OnFragmentIsReadyListener;
|
||||
import io.github.wulkanowy.utils.TimeUtils;
|
||||
import io.github.wulkanowy.utils.async.AbstractTask;
|
||||
import io.github.wulkanowy.utils.async.AsyncListeners;
|
||||
|
||||
import static io.github.wulkanowy.utils.TimeUtilsKt.getFirstDayOfCurrentWeek;
|
||||
import static io.github.wulkanowy.utils.TimeUtilsKt.getMondaysFromCurrentSchoolYear;
|
||||
|
||||
public class AttendancePresenter extends BasePresenter<AttendanceContract.View>
|
||||
implements AttendanceContract.Presenter, AsyncListeners.OnFirstLoadingListener {
|
||||
|
||||
@ -42,11 +44,11 @@ public class AttendancePresenter extends BasePresenter<AttendanceContract.View>
|
||||
}
|
||||
|
||||
if (dates.isEmpty()) {
|
||||
dates = TimeUtils.getMondaysFromCurrentSchoolYear();
|
||||
dates = getMondaysFromCurrentSchoolYear();
|
||||
}
|
||||
|
||||
if (positionToScroll == 0) {
|
||||
positionToScroll = dates.indexOf(TimeUtils.getDateOfCurrentMonday(true));
|
||||
positionToScroll = dates.indexOf(getFirstDayOfCurrentWeek());
|
||||
}
|
||||
|
||||
if (!isFirstSight) {
|
||||
|
@ -10,10 +10,12 @@ import javax.inject.Inject;
|
||||
import io.github.wulkanowy.data.RepositoryContract;
|
||||
import io.github.wulkanowy.ui.base.BasePresenter;
|
||||
import io.github.wulkanowy.ui.main.OnFragmentIsReadyListener;
|
||||
import io.github.wulkanowy.utils.TimeUtils;
|
||||
import io.github.wulkanowy.utils.async.AbstractTask;
|
||||
import io.github.wulkanowy.utils.async.AsyncListeners;
|
||||
|
||||
import static io.github.wulkanowy.utils.TimeUtilsKt.getFirstDayOfCurrentWeek;
|
||||
import static io.github.wulkanowy.utils.TimeUtilsKt.getMondaysFromCurrentSchoolYear;
|
||||
|
||||
public class ExamsPresenter extends BasePresenter<ExamsContract.View>
|
||||
implements ExamsContract.Presenter, AsyncListeners.OnFirstLoadingListener {
|
||||
|
||||
@ -42,11 +44,11 @@ public class ExamsPresenter extends BasePresenter<ExamsContract.View>
|
||||
}
|
||||
|
||||
if (dates.isEmpty()) {
|
||||
dates = TimeUtils.getMondaysFromCurrentSchoolYear();
|
||||
dates = getMondaysFromCurrentSchoolYear();
|
||||
}
|
||||
|
||||
if (positionToScroll == 0) {
|
||||
positionToScroll = dates.indexOf(TimeUtils.getDateOfCurrentMonday(true));
|
||||
positionToScroll = dates.indexOf(getFirstDayOfCurrentWeek());
|
||||
}
|
||||
|
||||
if (!isFirstSight) {
|
||||
|
@ -18,6 +18,8 @@ import io.github.wulkanowy.services.jobs.SyncJob;
|
||||
import io.github.wulkanowy.ui.main.MainActivity;
|
||||
import io.github.wulkanowy.utils.AppConstant;
|
||||
|
||||
import static io.github.wulkanowy.utils.TimeUtilsKt.isHolidays;
|
||||
|
||||
public class SettingsFragment extends PreferenceFragmentCompat
|
||||
implements SharedPreferences.OnSharedPreferenceChangeListener {
|
||||
|
||||
@ -86,7 +88,13 @@ public class SettingsFragment extends PreferenceFragmentCompat
|
||||
findPreference(SHARED_KEY_ABOUT_VERSION).setOnPreferenceClickListener(onProgrammerListener);
|
||||
findPreference(SHARED_KEY_ABOUT_REPO).setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(AppConstant.REPO_URL)));
|
||||
findPreference(SHARED_KEY_ABOUT_LICENSES).setIntent(new Intent(getActivity(), OssLicensesMenuActivity.class)
|
||||
.putExtra("title", getString(R.string.pref_about_osl)));
|
||||
.putExtra("title", R.string.pref_about_osl));
|
||||
|
||||
if (isHolidays()) {
|
||||
Preference services = findPreference(SHARED_KEY_SERVICES_ENABLE);
|
||||
services.setSummary(R.string.pref_services_suspended_on_holidays);
|
||||
services.setEnabled(false);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -10,10 +10,12 @@ import javax.inject.Inject;
|
||||
import io.github.wulkanowy.data.RepositoryContract;
|
||||
import io.github.wulkanowy.ui.base.BasePresenter;
|
||||
import io.github.wulkanowy.ui.main.OnFragmentIsReadyListener;
|
||||
import io.github.wulkanowy.utils.TimeUtils;
|
||||
import io.github.wulkanowy.utils.async.AbstractTask;
|
||||
import io.github.wulkanowy.utils.async.AsyncListeners;
|
||||
|
||||
import static io.github.wulkanowy.utils.TimeUtilsKt.getFirstDayOfCurrentWeek;
|
||||
import static io.github.wulkanowy.utils.TimeUtilsKt.getMondaysFromCurrentSchoolYear;
|
||||
|
||||
public class TimetablePresenter extends BasePresenter<TimetableContract.View>
|
||||
implements TimetableContract.Presenter, AsyncListeners.OnFirstLoadingListener {
|
||||
|
||||
@ -42,11 +44,11 @@ public class TimetablePresenter extends BasePresenter<TimetableContract.View>
|
||||
}
|
||||
|
||||
if (dates.isEmpty()) {
|
||||
dates = TimeUtils.getMondaysFromCurrentSchoolYear();
|
||||
dates = getMondaysFromCurrentSchoolYear();
|
||||
}
|
||||
|
||||
if (positionToScroll == 0) {
|
||||
positionToScroll = dates.indexOf(TimeUtils.getDateOfCurrentMonday(true));
|
||||
positionToScroll = dates.indexOf(getFirstDayOfCurrentWeek());
|
||||
}
|
||||
|
||||
if (!isFirstSight) {
|
||||
|
@ -16,10 +16,12 @@ import io.github.wulkanowy.data.db.dao.entities.Week;
|
||||
import io.github.wulkanowy.ui.base.BasePresenter;
|
||||
import io.github.wulkanowy.utils.AppConstant;
|
||||
import io.github.wulkanowy.utils.FabricUtils;
|
||||
import io.github.wulkanowy.utils.TimeUtils;
|
||||
import io.github.wulkanowy.utils.async.AbstractTask;
|
||||
import io.github.wulkanowy.utils.async.AsyncListeners;
|
||||
|
||||
import static io.github.wulkanowy.utils.TimeUtilsKt.getParsedDate;
|
||||
import static io.github.wulkanowy.utils.TimeUtilsKt.isDateInWeek;
|
||||
|
||||
public class TimetableTabPresenter extends BasePresenter<TimetableTabContract.View>
|
||||
implements TimetableTabContract.Presenter, AsyncListeners.OnRefreshListener,
|
||||
AsyncListeners.OnFirstLoadingListener {
|
||||
@ -162,9 +164,9 @@ public class TimetableTabPresenter extends BasePresenter<TimetableTabContract.Vi
|
||||
}
|
||||
|
||||
private void expandCurrentDayHeader() {
|
||||
LocalDate monday = TimeUtils.getParsedDate(date, AppConstant.DATE_PATTERN);
|
||||
LocalDate monday = getParsedDate(date, AppConstant.DATE_PATTERN);
|
||||
|
||||
if (TimeUtils.isDateInWeek(monday, LocalDate.now()) && !isFirstSight) {
|
||||
if (isDateInWeek(monday, LocalDate.now()) && !isFirstSight) {
|
||||
getView().expandItem(LocalDate.now().getDayOfWeek().getValue() - 1);
|
||||
}
|
||||
}
|
||||
|
@ -17,7 +17,9 @@ import io.github.wulkanowy.R;
|
||||
import io.github.wulkanowy.data.RepositoryContract;
|
||||
import io.github.wulkanowy.data.db.dao.entities.TimetableLesson;
|
||||
import io.github.wulkanowy.data.db.dao.entities.Week;
|
||||
import io.github.wulkanowy.utils.TimeUtils;
|
||||
|
||||
import static io.github.wulkanowy.utils.TimeUtilsKt.getFirstDayOfCurrentWeek;
|
||||
import static io.github.wulkanowy.utils.TimeUtilsKt.getTodayOrNextDayOrder;
|
||||
|
||||
public class TimetableWidgetFactory implements RemoteViewsService.RemoteViewsFactory {
|
||||
|
||||
@ -44,8 +46,8 @@ public class TimetableWidgetFactory implements RemoteViewsService.RemoteViewsFac
|
||||
|
||||
if (repository.getSharedRepo().isUserLoggedIn()) {
|
||||
|
||||
Week week = repository.getDbRepo().getWeek(TimeUtils.getDateOfCurrentMonday(true));
|
||||
int valueOfDay = TimeUtils.getTodayOrNextDayValue(repository.getSharedRepo().getTimetableWidgetState());
|
||||
Week week = repository.getDbRepo().getWeek(getFirstDayOfCurrentWeek());
|
||||
int valueOfDay = getTodayOrNextDayOrder(repository.getSharedRepo().getTimetableWidgetState());
|
||||
|
||||
if (valueOfDay != 5 && valueOfDay != 6 && week != null) {
|
||||
week.resetDayList();
|
||||
|
@ -16,7 +16,8 @@ import io.github.wulkanowy.R;
|
||||
import io.github.wulkanowy.data.RepositoryContract;
|
||||
import io.github.wulkanowy.services.widgets.TimetableWidgetServices;
|
||||
import io.github.wulkanowy.ui.main.MainActivity;
|
||||
import io.github.wulkanowy.utils.TimeUtils;
|
||||
|
||||
import static io.github.wulkanowy.utils.TimeUtilsKt.getTodayOrNextDay;
|
||||
|
||||
public class TimetableWidgetProvider extends AppWidgetProvider {
|
||||
|
||||
@ -95,7 +96,7 @@ public class TimetableWidgetProvider extends AppWidgetProvider {
|
||||
: R.string.widget_timetable_today);
|
||||
|
||||
views.setTextViewText(R.id.timetable_widget_toggle, toggleText);
|
||||
views.setTextViewText(R.id.timetable_widget_date, TimeUtils.getTodayOrNextDay(nextDay));
|
||||
views.setTextViewText(R.id.timetable_widget_date, getTodayOrNextDay(nextDay));
|
||||
}
|
||||
|
||||
private void updateWidget(RemoteViews views, AppWidgetManager appWidgetManager, int appWidgetId) {
|
||||
|
@ -1,81 +0,0 @@
|
||||
package io.github.wulkanowy.utils;
|
||||
|
||||
import org.threeten.bp.DayOfWeek;
|
||||
import org.threeten.bp.LocalDate;
|
||||
import org.threeten.bp.format.DateTimeFormatter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public final class TimeUtils {
|
||||
|
||||
private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern(AppConstant.DATE_PATTERN);
|
||||
|
||||
private TimeUtils() {
|
||||
throw new IllegalStateException("Utility class");
|
||||
}
|
||||
|
||||
public static LocalDate getParsedDate(String dateString, String dateFormat) {
|
||||
return LocalDate.parse(dateString, DateTimeFormatter.ofPattern(dateFormat));
|
||||
}
|
||||
|
||||
public static List<String> getMondaysFromCurrentSchoolYear() {
|
||||
LocalDate startDate = LocalDate.of(getCurrentSchoolYear(), 9, 1);
|
||||
LocalDate endDate = LocalDate.of(getCurrentSchoolYear() + 1, 8, 31);
|
||||
|
||||
List<String> dateList = new ArrayList<>();
|
||||
|
||||
LocalDate thisMonday = startDate.with(DayOfWeek.MONDAY);
|
||||
|
||||
if (startDate.isAfter(thisMonday)) {
|
||||
startDate = thisMonday.plusWeeks(1);
|
||||
} else {
|
||||
startDate = thisMonday;
|
||||
}
|
||||
|
||||
while (startDate.isBefore(endDate)) {
|
||||
dateList.add(startDate.format(formatter));
|
||||
startDate = startDate.plusWeeks(1);
|
||||
}
|
||||
return dateList;
|
||||
}
|
||||
|
||||
public static int getCurrentSchoolYear() {
|
||||
LocalDate localDate = LocalDate.now();
|
||||
return localDate.getMonthValue() <= 8 ? localDate.getYear() - 1 : localDate.getYear();
|
||||
}
|
||||
|
||||
public static String getDateOfCurrentMonday(boolean normalize) {
|
||||
LocalDate currentDate = LocalDate.now();
|
||||
|
||||
if (currentDate.getDayOfWeek() == DayOfWeek.SATURDAY && normalize) {
|
||||
currentDate = currentDate.plusDays(2);
|
||||
} else if (currentDate.getDayOfWeek() == DayOfWeek.SUNDAY && normalize) {
|
||||
currentDate = currentDate.plusDays(1);
|
||||
} else {
|
||||
currentDate = currentDate.with(DayOfWeek.MONDAY);
|
||||
}
|
||||
return currentDate.format(formatter);
|
||||
}
|
||||
|
||||
public static int getTodayOrNextDayValue(boolean nextDay) {
|
||||
DayOfWeek day = LocalDate.now().getDayOfWeek();
|
||||
if (nextDay) {
|
||||
if (day == DayOfWeek.SUNDAY) {
|
||||
return 0;
|
||||
}
|
||||
return day.getValue();
|
||||
}
|
||||
return day.getValue() - 1;
|
||||
}
|
||||
|
||||
public static String getTodayOrNextDay(boolean nextDay) {
|
||||
LocalDate current = LocalDate.now();
|
||||
return nextDay ? current.plusDays(1).format(formatter) : current.format(formatter);
|
||||
}
|
||||
|
||||
public static boolean isDateInWeek(LocalDate firstWeekDay, LocalDate date) {
|
||||
return date.isAfter(firstWeekDay.minusDays(1)) && date.isBefore(firstWeekDay.plusDays(5));
|
||||
|
||||
}
|
||||
}
|
97
app/src/main/java/io/github/wulkanowy/utils/TimeUtils.kt
Normal file
97
app/src/main/java/io/github/wulkanowy/utils/TimeUtils.kt
Normal file
@ -0,0 +1,97 @@
|
||||
package io.github.wulkanowy.utils
|
||||
|
||||
import org.threeten.bp.DayOfWeek.*
|
||||
import org.threeten.bp.LocalDate
|
||||
import org.threeten.bp.Year
|
||||
import org.threeten.bp.format.DateTimeFormatter
|
||||
import org.threeten.bp.temporal.TemporalAdjusters
|
||||
import java.util.*
|
||||
|
||||
private val formatter = DateTimeFormatter.ofPattern(AppConstant.DATE_PATTERN)
|
||||
|
||||
fun getParsedDate(dateString: String, dateFormat: String): LocalDate {
|
||||
return LocalDate.parse(dateString, DateTimeFormatter.ofPattern(dateFormat))
|
||||
}
|
||||
|
||||
fun getMondaysFromCurrentSchoolYear() = getMondaysFromCurrentSchoolYear(LocalDate.now())
|
||||
|
||||
fun getMondaysFromCurrentSchoolYear(date: LocalDate): List<String> {
|
||||
val startDate = getFirstSchoolDay(getSchoolYearForDate(date))
|
||||
?.with(TemporalAdjusters.previousOrSame(MONDAY))
|
||||
val endDate = getFirstSchoolDay(getSchoolYearForDate(date) + 1)
|
||||
?.with(TemporalAdjusters.previousOrSame(MONDAY))
|
||||
|
||||
val dateList = ArrayList<String>()
|
||||
var monday = startDate as LocalDate
|
||||
while (monday.isBefore(endDate)) {
|
||||
dateList.add(monday.format(formatter))
|
||||
monday = monday.plusWeeks(1)
|
||||
}
|
||||
|
||||
return dateList
|
||||
}
|
||||
|
||||
fun getSchoolYearForDate(date: LocalDate): Int {
|
||||
return if (date.monthValue <= 8) date.year - 1 else date.year
|
||||
}
|
||||
|
||||
fun getFirstDayOfCurrentWeek(): String = getFirstDayOfCurrentWeek(LocalDate.now())
|
||||
|
||||
fun getFirstDayOfCurrentWeek(date: LocalDate): String {
|
||||
return when (date.dayOfWeek) {
|
||||
SATURDAY -> date.plusDays(2)
|
||||
SUNDAY -> date.plusDays(1)
|
||||
else -> date.with(MONDAY)
|
||||
}.format(formatter)
|
||||
}
|
||||
|
||||
fun getTodayOrNextDayOrder(next: Boolean): Int = getTodayOrNextDayOrder(next, LocalDate.now())
|
||||
|
||||
fun getTodayOrNextDayOrder(next: Boolean, date: LocalDate): Int {
|
||||
val day = date.dayOfWeek
|
||||
return if (next) {
|
||||
if (day == SUNDAY) {
|
||||
0
|
||||
} else day.value
|
||||
} else day.value - 1
|
||||
}
|
||||
|
||||
fun getTodayOrNextDay(next: Boolean): String? = getTodayOrNextDay(next, LocalDate.now())
|
||||
|
||||
fun getTodayOrNextDay(next: Boolean, date: LocalDate): String? {
|
||||
return (if (next) {
|
||||
date.plusDays(1)
|
||||
} else date).format(formatter)
|
||||
}
|
||||
|
||||
fun isDateInWeek(firstWeekDay: LocalDate, date: LocalDate): Boolean {
|
||||
return date.isAfter(firstWeekDay.minusDays(1)) && date.isBefore(firstWeekDay.plusDays(5))
|
||||
}
|
||||
|
||||
/**
|
||||
* [Dz.U. 2016 poz. 1335](http://prawo.sejm.gov.pl/isap.nsf/DocDetails.xsp?id=WDU20160001335)
|
||||
*/
|
||||
fun isHolidays(): Boolean = isHolidays(LocalDate.now(), Year.now().value)
|
||||
|
||||
fun isHolidays(day: LocalDate, year: Int): Boolean {
|
||||
return day.isAfter(getLastSchoolDay(year)) && day.isBefore(getFirstSchoolDay(year))
|
||||
}
|
||||
|
||||
fun getFirstSchoolDay(year: Int): LocalDate? {
|
||||
val firstSeptember = LocalDate.of(year, 9, 1)
|
||||
|
||||
return when (firstSeptember.dayOfWeek) {
|
||||
FRIDAY,
|
||||
SATURDAY,
|
||||
SUNDAY -> firstSeptember.with(TemporalAdjusters.firstInMonth(MONDAY))
|
||||
else -> {
|
||||
firstSeptember
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun getLastSchoolDay(year: Int): LocalDate? {
|
||||
return LocalDate
|
||||
.of(year, 6, 20)
|
||||
.with(TemporalAdjusters.next(FRIDAY))
|
||||
}
|
@ -136,6 +136,7 @@
|
||||
|
||||
<string name="pref_services_header">Usługi</string>
|
||||
<string name="pref_services_switch">Włącz odświeżanie danych w tle</string>
|
||||
<string name="pref_services_suspended_on_holidays">Zawieszone na wakacjach</string>
|
||||
<string name="pref_services_interval">Interwał między odświeżaniem danych</string>
|
||||
<string name="pref_services_mobile_data">Synchronizacja tylko przez WiFi</string>
|
||||
|
||||
|
@ -132,6 +132,7 @@
|
||||
|
||||
<string name="pref_services_header">Services</string>
|
||||
<string name="pref_services_switch">Enable background data refreshing</string>
|
||||
<string name="pref_services_suspended_on_holidays">Suspended on holidays</string>
|
||||
<string name="pref_services_interval">Interval between data refreshing</string>
|
||||
<string name="pref_services_mobile_data">Synchronization via WiFi only</string>
|
||||
|
||||
|
@ -1,48 +0,0 @@
|
||||
package io.github.wulkanowy.utils;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.threeten.bp.LocalDate;
|
||||
|
||||
public class TimeUtilsTest {
|
||||
|
||||
@Test
|
||||
public void getParsedDateTest() {
|
||||
Assert.assertEquals(LocalDate.of(1970, 1, 1),
|
||||
TimeUtils.getParsedDate("1970-01-01", "yyyy-MM-dd"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isDateInWeekInsideTest() {
|
||||
Assert.assertTrue(TimeUtils.isDateInWeek(
|
||||
LocalDate.of(2018, 5, 28),
|
||||
LocalDate.of(2018, 5, 31)
|
||||
));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isDateInWeekExtremesTest() {
|
||||
Assert.assertTrue(TimeUtils.isDateInWeek(
|
||||
LocalDate.of(2018, 5, 28),
|
||||
LocalDate.of(2018, 5, 28)
|
||||
));
|
||||
|
||||
Assert.assertTrue(TimeUtils.isDateInWeek(
|
||||
LocalDate.of(2018, 5, 28),
|
||||
LocalDate.of(2018, 6, 1)
|
||||
));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isDateInWeekOutOfTest() {
|
||||
Assert.assertFalse(TimeUtils.isDateInWeek(
|
||||
LocalDate.of(2018, 5, 28),
|
||||
LocalDate.of(2018, 6, 2)
|
||||
));
|
||||
|
||||
Assert.assertFalse(TimeUtils.isDateInWeek(
|
||||
LocalDate.of(2018, 5, 28),
|
||||
LocalDate.of(2018, 5, 27)
|
||||
));
|
||||
}
|
||||
}
|
140
app/src/test/java/io/github/wulkanowy/utils/TimeUtilsTest.kt
Normal file
140
app/src/test/java/io/github/wulkanowy/utils/TimeUtilsTest.kt
Normal file
@ -0,0 +1,140 @@
|
||||
package io.github.wulkanowy.utils
|
||||
|
||||
import org.junit.Test
|
||||
import org.threeten.bp.LocalDate
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
|
||||
class TimeUtilsTest {
|
||||
|
||||
@Test fun getParsedDateTest() {
|
||||
assertEquals(LocalDate.of(1970, 1, 1),
|
||||
getParsedDate("1970-01-01", "yyyy-MM-dd"))
|
||||
}
|
||||
|
||||
@Test fun getMondaysFromCurrentSchoolYearTest() {
|
||||
val y201718 = getMondaysFromCurrentSchoolYear(LocalDate.of(2018, 1, 1))
|
||||
assertEquals("2017-09-04", y201718.first())
|
||||
assertEquals("2018-08-27", y201718.last())
|
||||
|
||||
val y202122 = getMondaysFromCurrentSchoolYear(LocalDate.of(2022, 1, 1))
|
||||
assertEquals("2021-08-30", y202122.first())
|
||||
assertEquals("2022-08-22", y202122.last())
|
||||
|
||||
val y202223 = getMondaysFromCurrentSchoolYear(LocalDate.of(2023, 1, 1))
|
||||
assertEquals("2022-08-29", y202223.first())
|
||||
assertEquals("2023-08-28", y202223.last())
|
||||
}
|
||||
|
||||
@Test fun getCurrentSchoolYearTest() {
|
||||
assertEquals(2017, getSchoolYearForDate(LocalDate.of(2018, 8, 31)))
|
||||
assertEquals(2018, getSchoolYearForDate(LocalDate.of(2018, 9, 1)))
|
||||
}
|
||||
|
||||
@Test fun getFirstWeekDayTest() {
|
||||
assertEquals("2018-06-18", getFirstDayOfCurrentWeek(LocalDate.of(2018, 6, 21)))
|
||||
assertEquals("2018-06-18", getFirstDayOfCurrentWeek(LocalDate.of(2018, 6, 22)))
|
||||
assertEquals("2018-06-25", getFirstDayOfCurrentWeek(LocalDate.of(2018, 6, 23)))
|
||||
assertEquals("2018-06-25", getFirstDayOfCurrentWeek(LocalDate.of(2018, 6, 24)))
|
||||
assertEquals("2018-06-25", getFirstDayOfCurrentWeek(LocalDate.of(2018, 6, 25)))
|
||||
assertEquals("2018-06-25", getFirstDayOfCurrentWeek(LocalDate.of(2018, 6, 26)))
|
||||
}
|
||||
|
||||
@Test fun getTodayOrNextDayOrderTest() {
|
||||
assertEquals(0, getTodayOrNextDayOrder(true, LocalDate.of(2018, 6, 24))) // sunday
|
||||
assertEquals(6, getTodayOrNextDayOrder(false, LocalDate.of(2018, 6, 24)))
|
||||
assertEquals(1, getTodayOrNextDayOrder(true, LocalDate.of(2018, 6, 25)))
|
||||
assertEquals(0, getTodayOrNextDayOrder(false, LocalDate.of(2018, 6, 25)))
|
||||
}
|
||||
|
||||
@Test fun getTodayOrNextDayTest() {
|
||||
assertEquals("2018-06-26", getTodayOrNextDay(false, LocalDate.of(2018, 6, 26)))
|
||||
assertEquals("2018-06-27", getTodayOrNextDay(true, LocalDate.of(2018, 6, 26)))
|
||||
}
|
||||
|
||||
@Test fun isDateInWeekInsideTest() {
|
||||
assertTrue(isDateInWeek(
|
||||
LocalDate.of(2018, 5, 28),
|
||||
LocalDate.of(2018, 5, 31)
|
||||
))
|
||||
}
|
||||
|
||||
@Test fun isDateInWeekExtremesTest() {
|
||||
assertTrue(isDateInWeek(
|
||||
LocalDate.of(2018, 5, 28),
|
||||
LocalDate.of(2018, 5, 28)
|
||||
))
|
||||
|
||||
assertTrue(isDateInWeek(
|
||||
LocalDate.of(2018, 5, 28),
|
||||
LocalDate.of(2018, 6, 1)
|
||||
))
|
||||
}
|
||||
|
||||
@Test fun isDateInWeekOutOfTest() {
|
||||
assertFalse(isDateInWeek(
|
||||
LocalDate.of(2018, 5, 28),
|
||||
LocalDate.of(2018, 6, 2)
|
||||
))
|
||||
|
||||
assertFalse(isDateInWeek(
|
||||
LocalDate.of(2018, 5, 28),
|
||||
LocalDate.of(2018, 5, 27)
|
||||
))
|
||||
}
|
||||
|
||||
@Test fun isHolidaysInSchoolEndTest() {
|
||||
assertFalse(isHolidays(LocalDate.of(2017, 6, 23), 2017))
|
||||
assertFalse(isHolidays(LocalDate.of(2018, 6, 22), 2018))
|
||||
assertFalse(isHolidays(LocalDate.of(2019, 6, 21), 2019))
|
||||
assertFalse(isHolidays(LocalDate.of(2020, 6, 26), 2020))
|
||||
assertFalse(isHolidays(LocalDate.of(2021, 6, 25), 2021))
|
||||
assertFalse(isHolidays(LocalDate.of(2022, 6, 24), 2022))
|
||||
assertFalse(isHolidays(LocalDate.of(2023, 6, 23), 2023))
|
||||
assertFalse(isHolidays(LocalDate.of(2024, 6, 21), 2024))
|
||||
assertFalse(isHolidays(LocalDate.of(2025, 6, 27), 2025))
|
||||
}
|
||||
|
||||
@Test fun isHolidaysInHolidaysStartTest() {
|
||||
assertTrue(isHolidays(LocalDate.of(2017, 6, 24), 2017))
|
||||
assertTrue(isHolidays(LocalDate.of(2018, 6, 23), 2018))
|
||||
assertTrue(isHolidays(LocalDate.of(2019, 6, 22), 2019))
|
||||
assertTrue(isHolidays(LocalDate.of(2020, 6, 27), 2020))
|
||||
assertTrue(isHolidays(LocalDate.of(2021, 6, 26), 2021))
|
||||
assertTrue(isHolidays(LocalDate.of(2022, 6, 25), 2022))
|
||||
assertTrue(isHolidays(LocalDate.of(2023, 6, 24), 2023))
|
||||
assertTrue(isHolidays(LocalDate.of(2024, 6, 22), 2024))
|
||||
assertTrue(isHolidays(LocalDate.of(2025, 6, 28), 2025))
|
||||
}
|
||||
|
||||
@Test fun isHolidaysInHolidaysEndTest() {
|
||||
assertTrue(isHolidays(LocalDate.of(2017, 9, 1), 2017)) // friday
|
||||
assertTrue(isHolidays(LocalDate.of(2017, 9, 2), 2017)) // saturday
|
||||
assertTrue(isHolidays(LocalDate.of(2017, 9, 3), 2017)) // sunday
|
||||
assertTrue(isHolidays(LocalDate.of(2018, 9, 1), 2018)) // saturday
|
||||
assertTrue(isHolidays(LocalDate.of(2018, 9, 2), 2018)) // sunday
|
||||
assertTrue(isHolidays(LocalDate.of(2019, 9, 1), 2019)) // sunday
|
||||
assertTrue(isHolidays(LocalDate.of(2020, 8, 31), 2020)) // monday
|
||||
assertTrue(isHolidays(LocalDate.of(2021, 8, 31), 2021)) // tuesday
|
||||
assertTrue(isHolidays(LocalDate.of(2022, 8, 31), 2022)) // wednesday
|
||||
assertTrue(isHolidays(LocalDate.of(2023, 9, 1), 2023)) // friday
|
||||
assertTrue(isHolidays(LocalDate.of(2023, 9, 2), 2023)) // saturday
|
||||
assertTrue(isHolidays(LocalDate.of(2023, 9, 3), 2023)) // sunday
|
||||
assertTrue(isHolidays(LocalDate.of(2024, 9, 1), 2024)) // sunday
|
||||
assertTrue(isHolidays(LocalDate.of(2025, 8, 31), 2025)) // sunday
|
||||
}
|
||||
|
||||
@Test fun isHolidaysInSchoolStartTest() {
|
||||
assertFalse(isHolidays(LocalDate.of(2017, 9, 4), 2017)) // monday
|
||||
assertFalse(isHolidays(LocalDate.of(2018, 9, 3), 2018)) // monday
|
||||
assertFalse(isHolidays(LocalDate.of(2019, 9, 2), 2019)) // monday
|
||||
assertFalse(isHolidays(LocalDate.of(2020, 9, 1), 2020)) // tuesday
|
||||
assertFalse(isHolidays(LocalDate.of(2021, 9, 1), 2021)) // wednesday
|
||||
assertFalse(isHolidays(LocalDate.of(2022, 9, 1), 2022)) // thursday
|
||||
assertFalse(isHolidays(LocalDate.of(2023, 9, 4), 2023)) // monday
|
||||
assertFalse(isHolidays(LocalDate.of(2024, 9, 2), 2024)) // monday
|
||||
assertFalse(isHolidays(LocalDate.of(2025, 9, 1), 2025)) // monday
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user