From 6bd07d265151475f8748d9de80e04db66c177087 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Pich?= Date: Mon, 12 Aug 2019 12:17:39 +0200 Subject: [PATCH] Fix day/week navigation on holiday (#459) --- .../modules/attendance/AttendancePresenter.kt | 37 +++- .../ui/modules/exam/ExamPresenter.kt | 33 +++- .../ui/modules/homework/HomeworkPresenter.kt | 29 ++- .../modules/timetable/TimetablePresenter.kt | 30 ++- .../completed/CompletedLessonsPresenter.kt | 28 ++- .../github/wulkanowy/utils/TimeExtension.kt | 85 +++++---- .../wulkanowy/utils/TimeExtensionTest.kt | 176 ++++++++++-------- 7 files changed, 284 insertions(+), 134 deletions(-) diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/AttendancePresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/AttendancePresenter.kt index 5e7333e1..df311f78 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/AttendancePresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/AttendancePresenter.kt @@ -8,7 +8,13 @@ import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper -import io.github.wulkanowy.utils.* +import io.github.wulkanowy.utils.SchedulersProvider +import io.github.wulkanowy.utils.getLastSchoolDayIfHoliday +import io.github.wulkanowy.utils.isHolidays +import io.github.wulkanowy.utils.nextSchoolDay +import io.github.wulkanowy.utils.previousOrSameSchoolDay +import io.github.wulkanowy.utils.previousSchoolDay +import io.github.wulkanowy.utils.toFormattedString import org.threeten.bp.LocalDate import org.threeten.bp.LocalDate.now import org.threeten.bp.LocalDate.ofEpochDay @@ -26,6 +32,8 @@ class AttendancePresenter @Inject constructor( private val analytics: FirebaseAnalyticsHelper ) : BasePresenter(errorHandler, studentRepository, schedulers) { + private var baseDate: LocalDate = now().previousOrSameSchoolDay + lateinit var currentDate: LocalDate private set @@ -33,7 +41,8 @@ class AttendancePresenter @Inject constructor( super.onAttachView(view) view.initView() Timber.i("Attendance view was initialized") - loadData(ofEpochDay(date ?: now().previousOrSameSchoolDay.toEpochDay())) + loadData(ofEpochDay(date ?: baseDate.toEpochDay())) + if (currentDate.isHolidays) setBaseDateOnHolidays() reloadView() } @@ -56,7 +65,7 @@ class AttendancePresenter @Inject constructor( Timber.i("Attendance view is reselected") view?.also { view -> if (view.currentStackSize == 1) { - now().previousOrSameSchoolDay.also { + baseDate.also { if (currentDate != it) { loadData(it) reloadView() @@ -78,6 +87,20 @@ class AttendancePresenter @Inject constructor( return true } + private fun setBaseDateOnHolidays() { + disposable.add(studentRepository.getCurrentStudent() + .flatMap { semesterRepository.getCurrentSemester(it) } + .subscribeOn(schedulers.backgroundThread) + .observeOn(schedulers.mainThread) + .subscribe({ + baseDate = baseDate.getLastSchoolDayIfHoliday(it.schoolYear) + currentDate = baseDate + reloadNavigation() + }) { + Timber.i("Loading semester result: An exception occurred") + }) + } + private fun loadData(date: LocalDate, forceRefresh: Boolean = false) { Timber.i("Loading attendance data started") currentDate = date @@ -127,8 +150,14 @@ class AttendancePresenter @Inject constructor( showContent(false) showEmpty(false) clearData() - showNextButton(!currentDate.plusDays(1).isHolidays) + reloadNavigation() + } + } + + private fun reloadNavigation() { + view?.apply { showPreButton(!currentDate.minusDays(1).isHolidays) + showNextButton(!currentDate.plusDays(1).isHolidays) updateNavigationDay(currentDate.toFormattedString("EEEE\ndd.MM.YYYY").capitalize()) } } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/exam/ExamPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/exam/ExamPresenter.kt index 4045a956..93109922 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/exam/ExamPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/exam/ExamPresenter.kt @@ -10,6 +10,7 @@ import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.friday +import io.github.wulkanowy.utils.getLastSchoolDayIfHoliday import io.github.wulkanowy.utils.isHolidays import io.github.wulkanowy.utils.monday import io.github.wulkanowy.utils.nextOrSameSchoolDay @@ -30,6 +31,8 @@ class ExamPresenter @Inject constructor( private val analytics: FirebaseAnalyticsHelper ) : BasePresenter(errorHandler, studentRepository, schedulers) { + private var baseDate: LocalDate = now().nextOrSameSchoolDay + lateinit var currentDate: LocalDate private set @@ -37,7 +40,8 @@ class ExamPresenter @Inject constructor( super.onAttachView(view) view.initView() Timber.i("Exam view was initialized") - loadData(ofEpochDay(date ?: now().nextOrSameSchoolDay.toEpochDay())) + loadData(ofEpochDay(date ?: baseDate.toEpochDay())) + if (currentDate.isHolidays) setBaseDateOnHolidays() reloadView() } @@ -65,7 +69,7 @@ class ExamPresenter @Inject constructor( fun onViewReselected() { Timber.i("Exam view is reselected") - now().nextOrSameSchoolDay.also { + baseDate.also { if (currentDate != it) { loadData(it) reloadView() @@ -73,6 +77,20 @@ class ExamPresenter @Inject constructor( } } + private fun setBaseDateOnHolidays() { + disposable.add(studentRepository.getCurrentStudent() + .flatMap { semesterRepository.getCurrentSemester(it) } + .subscribeOn(schedulers.backgroundThread) + .observeOn(schedulers.mainThread) + .subscribe({ + baseDate = baseDate.getLastSchoolDayIfHoliday(it.schoolYear) + currentDate = baseDate + reloadNavigation() + }) { + Timber.i("Loading semester result: An exception occurred") + }) + } + private fun loadData(date: LocalDate, forceRefresh: Boolean = false) { Timber.i("Loading exam data started") currentDate = date @@ -81,9 +99,8 @@ class ExamPresenter @Inject constructor( add(studentRepository.getCurrentStudent() .delay(200, MILLISECONDS) .flatMap { semesterRepository.getCurrentSemester(it) } - .flatMap { - examRepository.getExams(it, currentDate.monday, currentDate.friday, forceRefresh) - }.map { it.groupBy { exam -> exam.date }.toSortedMap() } + .flatMap { examRepository.getExams(it, currentDate.monday, currentDate.friday, forceRefresh) } + .map { it.groupBy { exam -> exam.date }.toSortedMap() } .map { createExamItems(it) } .subscribeOn(schedulers.backgroundThread) .observeOn(schedulers.mainThread) @@ -126,6 +143,12 @@ class ExamPresenter @Inject constructor( showContent(false) showEmpty(false) clearData() + reloadNavigation() + } + } + + private fun reloadNavigation() { + view?.apply { showPreButton(!currentDate.minusDays(7).isHolidays) showNextButton(!currentDate.plusDays(7).isHolidays) updateNavigationWeek("${currentDate.monday.toFormattedString("dd.MM")} - " + diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/homework/HomeworkPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/homework/HomeworkPresenter.kt index 682e87d4..6829031c 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/homework/HomeworkPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/homework/HomeworkPresenter.kt @@ -10,11 +10,13 @@ import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.friday +import io.github.wulkanowy.utils.getLastSchoolDayIfHoliday import io.github.wulkanowy.utils.isHolidays import io.github.wulkanowy.utils.monday import io.github.wulkanowy.utils.nextOrSameSchoolDay import io.github.wulkanowy.utils.toFormattedString import org.threeten.bp.LocalDate +import org.threeten.bp.LocalDate.ofEpochDay import timber.log.Timber import java.util.concurrent.TimeUnit import javax.inject.Inject @@ -28,6 +30,8 @@ class HomeworkPresenter @Inject constructor( private val analytics: FirebaseAnalyticsHelper ) : BasePresenter(errorHandler, studentRepository, schedulers) { + private var baseDate: LocalDate = LocalDate.now().nextOrSameSchoolDay + lateinit var currentDate: LocalDate private set @@ -35,7 +39,8 @@ class HomeworkPresenter @Inject constructor( super.onAttachView(view) view.initView() Timber.i("Homework view was initialized") - loadData(LocalDate.ofEpochDay(date ?: LocalDate.now().nextOrSameSchoolDay.toEpochDay())) + loadData(ofEpochDay(date ?: baseDate.toEpochDay())) + if (currentDate.isHolidays) setBaseDateOnHolidays() reloadView() } @@ -61,6 +66,20 @@ class HomeworkPresenter @Inject constructor( } } + private fun setBaseDateOnHolidays() { + disposable.add(studentRepository.getCurrentStudent() + .flatMap { semesterRepository.getCurrentSemester(it) } + .subscribeOn(schedulers.backgroundThread) + .observeOn(schedulers.mainThread) + .subscribe({ + baseDate = baseDate.getLastSchoolDayIfHoliday(it.schoolYear) + currentDate = baseDate + reloadNavigation() + }) { + Timber.i("Loading semester result: An exception occurred") + }) + } + private fun loadData(date: LocalDate, forceRefresh: Boolean = false) { Timber.i("Loading homework data started") currentDate = date @@ -113,8 +132,14 @@ class HomeworkPresenter @Inject constructor( showContent(false) showEmpty(false) clearData() - showNextButton(!currentDate.plusDays(7).isHolidays) + reloadNavigation() + } + } + + private fun reloadNavigation() { + view?.apply { showPreButton(!currentDate.minusDays(7).isHolidays) + showNextButton(!currentDate.plusDays(7).isHolidays) updateNavigationWeek("${currentDate.monday.toFormattedString("dd.MM")} - " + currentDate.friday.toFormattedString("dd.MM")) } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/TimetablePresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/TimetablePresenter.kt index bead4228..baf347ae 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/TimetablePresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/TimetablePresenter.kt @@ -8,6 +8,7 @@ import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.SchedulersProvider +import io.github.wulkanowy.utils.getLastSchoolDayIfHoliday import io.github.wulkanowy.utils.isHolidays import io.github.wulkanowy.utils.nextOrSameSchoolDay import io.github.wulkanowy.utils.nextSchoolDay @@ -29,6 +30,8 @@ class TimetablePresenter @Inject constructor( private val analytics: FirebaseAnalyticsHelper ) : BasePresenter(errorHandler, studentRepository, schedulers) { + private var baseDate: LocalDate = now().nextOrSameSchoolDay + lateinit var currentDate: LocalDate private set @@ -36,7 +39,8 @@ class TimetablePresenter @Inject constructor( super.onAttachView(view) view.initView() Timber.i("Timetable was initialized") - loadData(ofEpochDay(date ?: now().nextOrSameSchoolDay.toEpochDay())) + loadData(ofEpochDay(date ?: baseDate.toEpochDay())) + if (currentDate.isHolidays) setBaseDateOnHolidays() reloadView() } @@ -59,7 +63,7 @@ class TimetablePresenter @Inject constructor( Timber.i("Timetable view is reselected") view?.also { view -> if (view.currentStackSize == 1) { - now().nextOrSameSchoolDay.also { + baseDate.also { if (currentDate != it) { loadData(it) reloadView() @@ -81,6 +85,20 @@ class TimetablePresenter @Inject constructor( return true } + private fun setBaseDateOnHolidays() { + disposable.add(studentRepository.getCurrentStudent() + .flatMap { semesterRepository.getCurrentSemester(it) } + .subscribeOn(schedulers.backgroundThread) + .observeOn(schedulers.mainThread) + .subscribe({ + baseDate = baseDate.getLastSchoolDayIfHoliday(it.schoolYear) + currentDate = baseDate + reloadNavigation() + }) { + Timber.i("Loading semester result: An exception occurred") + }) + } + private fun loadData(date: LocalDate, forceRefresh: Boolean = false) { Timber.i("Loading timetable data started") currentDate = date @@ -125,8 +143,14 @@ class TimetablePresenter @Inject constructor( showContent(false) showEmpty(false) clearData() - showNextButton(!currentDate.plusDays(1).isHolidays) + reloadNavigation() + } + } + + private fun reloadNavigation() { + view?.apply { showPreButton(!currentDate.minusDays(1).isHolidays) + showNextButton(!currentDate.plusDays(1).isHolidays) updateNavigationDay(currentDate.toFormattedString("EEEE\ndd.MM.YYYY").capitalize()) } } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/completed/CompletedLessonsPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/completed/CompletedLessonsPresenter.kt index 0db5210f..b9e1fe2d 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/completed/CompletedLessonsPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/completed/CompletedLessonsPresenter.kt @@ -7,6 +7,7 @@ import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.SchedulersProvider +import io.github.wulkanowy.utils.getLastSchoolDayIfHoliday import io.github.wulkanowy.utils.isHolidays import io.github.wulkanowy.utils.nextOrSameSchoolDay import io.github.wulkanowy.utils.nextSchoolDay @@ -28,6 +29,8 @@ class CompletedLessonsPresenter @Inject constructor( private val analytics: FirebaseAnalyticsHelper ) : BasePresenter(completedLessonsErrorHandler, studentRepository, schedulers) { + private var baseDate: LocalDate = now().nextOrSameSchoolDay + lateinit var currentDate: LocalDate private set @@ -35,7 +38,8 @@ class CompletedLessonsPresenter @Inject constructor( super.onAttachView(view) Timber.i("Completed lessons is attached") view.initView() - loadData(ofEpochDay(date ?: now().nextOrSameSchoolDay.toEpochDay())) + loadData(ofEpochDay(date ?: baseDate.toEpochDay())) + if (currentDate.isHolidays) setBaseDateOnHolidays() reloadView() completedLessonsErrorHandler.onFeatureDisabled = { this.view?.showFeatureDisabled() @@ -65,6 +69,20 @@ class CompletedLessonsPresenter @Inject constructor( } } + private fun setBaseDateOnHolidays() { + disposable.add(studentRepository.getCurrentStudent() + .flatMap { semesterRepository.getCurrentSemester(it) } + .subscribeOn(schedulers.backgroundThread) + .observeOn(schedulers.mainThread) + .subscribe({ + baseDate = baseDate.getLastSchoolDayIfHoliday(it.schoolYear) + currentDate = baseDate + reloadNavigation() + }) { + Timber.i("Loading semester result: An exception occurred") + }) + } + private fun loadData(date: LocalDate, forceRefresh: Boolean = false) { Timber.i("Loading completed lessons data started") currentDate = date @@ -109,8 +127,14 @@ class CompletedLessonsPresenter @Inject constructor( showContent(false) showEmpty(false) clearData() - showNextButton(!currentDate.plusDays(1).isHolidays) + reloadNavigation() + } + } + + private fun reloadNavigation() { + view?.apply { showPreButton(!currentDate.minusDays(1).isHolidays) + showNextButton(!currentDate.plusDays(1).isHolidays) updateNavigationDay(currentDate.toFormattedString("EEEE\ndd.MM.YYYY").capitalize()) } } diff --git a/app/src/main/java/io/github/wulkanowy/utils/TimeExtension.kt b/app/src/main/java/io/github/wulkanowy/utils/TimeExtension.kt index 426816f4..a2672f86 100644 --- a/app/src/main/java/io/github/wulkanowy/utils/TimeExtension.kt +++ b/app/src/main/java/io/github/wulkanowy/utils/TimeExtension.kt @@ -6,11 +6,11 @@ import org.threeten.bp.DayOfWeek.MONDAY import org.threeten.bp.DayOfWeek.SATURDAY import org.threeten.bp.DayOfWeek.SUNDAY import org.threeten.bp.Instant +import org.threeten.bp.Instant.ofEpochMilli import org.threeten.bp.LocalDate import org.threeten.bp.LocalDateTime import org.threeten.bp.Month import org.threeten.bp.ZoneId -import org.threeten.bp.format.DateTimeFormatter import org.threeten.bp.format.DateTimeFormatter.ofPattern import org.threeten.bp.format.TextStyle.FULL_STANDALONE import org.threeten.bp.temporal.TemporalAdjusters.firstInMonth @@ -21,21 +21,15 @@ import java.util.Locale private const val DATE_PATTERN = "dd.MM.yyyy" -fun Date.toLocalDate(): LocalDate { - return Instant.ofEpochMilli(this.time).atZone(ZoneId.systemDefault()).toLocalDate() -} +fun Date.toLocalDate(): LocalDate = Instant.ofEpochMilli(time).atZone(ZoneId.systemDefault()).toLocalDate() -fun Date.toLocalDateTime(): LocalDateTime { - return Instant.ofEpochMilli(this.time).atZone(ZoneId.systemDefault()).toLocalDateTime() -} +fun Date.toLocalDateTime(): LocalDateTime = ofEpochMilli(time).atZone(ZoneId.systemDefault()).toLocalDateTime() -fun String.toLocalDate(format: String = DATE_PATTERN): LocalDate { - return LocalDate.parse(this, DateTimeFormatter.ofPattern(format)) -} +fun String.toLocalDate(format: String = DATE_PATTERN): LocalDate = LocalDate.parse(this, ofPattern(format)) -fun LocalDate.toFormattedString(format: String = DATE_PATTERN): String = this.format(ofPattern(format)) +fun LocalDate.toFormattedString(format: String = DATE_PATTERN): String = format(ofPattern(format)) -fun LocalDateTime.toFormattedString(format: String = DATE_PATTERN): String = this.format(ofPattern(format)) +fun LocalDateTime.toFormattedString(format: String = DATE_PATTERN): String = format(ofPattern(format)) fun LocalDateTime.toDate(): Date = DateTimeUtils.toDate(atZone(ZoneId.systemDefault()).toInstant()) @@ -66,62 +60,81 @@ fun Month.getFormattedName(): String { inline val LocalDate.nextSchoolDay: LocalDate get() { - return when (this.dayOfWeek) { - FRIDAY, SATURDAY, SUNDAY -> this.with(next(MONDAY)) - else -> this.plusDays(1) + return when (dayOfWeek) { + FRIDAY, SATURDAY, SUNDAY -> with(next(MONDAY)) + else -> plusDays(1) } } inline val LocalDate.previousSchoolDay: LocalDate get() { - return when (this.dayOfWeek) { - SATURDAY, SUNDAY, MONDAY -> this.with(previous(FRIDAY)) - else -> this.minusDays(1) + return when (dayOfWeek) { + SATURDAY, SUNDAY, MONDAY -> with(previous(FRIDAY)) + else -> minusDays(1) } } inline val LocalDate.nextOrSameSchoolDay: LocalDate get() { - return when (this.dayOfWeek) { - SATURDAY, SUNDAY -> this.with(next(MONDAY)) + return when (dayOfWeek) { + SATURDAY, SUNDAY -> with(next(MONDAY)) else -> this } } inline val LocalDate.previousOrSameSchoolDay: LocalDate get() { - return when (this.dayOfWeek) { - SATURDAY, SUNDAY -> this.with(previous(FRIDAY)) + return when (dayOfWeek) { + SATURDAY, SUNDAY -> with(previous(FRIDAY)) else -> this } } inline val LocalDate.weekDayName: String - get() = this.format(ofPattern("EEEE", Locale.getDefault())) + get() = format(ofPattern("EEEE", Locale.getDefault())) inline val LocalDate.shortcutWeekDayName: String - get() = this.format(ofPattern("EEE", Locale.getDefault())) + get() = format(ofPattern("EEE", Locale.getDefault())) inline val LocalDate.monday: LocalDate - get() = this.with(MONDAY) + get() = with(MONDAY) inline val LocalDate.friday: LocalDate - get() = this.with(FRIDAY) + get() = with(FRIDAY) /** * [Dz.U. 2016 poz. 1335](http://prawo.sejm.gov.pl/isap.nsf/DocDetails.xsp?id=WDU20160001335) */ inline val LocalDate.isHolidays: Boolean - get() { - return LocalDate.of(this.year, 9, 1).run { - when (dayOfWeek) { - FRIDAY, SATURDAY, SUNDAY -> with(firstInMonth(MONDAY)) - else -> this - } - }.let { firstSchoolDay -> - LocalDate.of(this.year, 6, 20) - .with(next(FRIDAY)) - .let { lastSchoolDay -> this.isBefore(firstSchoolDay) && this.isAfter(lastSchoolDay) } + get() = isBefore(firstSchoolDay) && isAfter(lastSchoolDay) + +inline val LocalDate.firstSchoolDay: LocalDate + get() = LocalDate.of(year, 9, 1).run { + when (dayOfWeek) { + FRIDAY, SATURDAY, SUNDAY -> with(firstInMonth(MONDAY)) + else -> this } } + +inline val LocalDate.lastSchoolDay: LocalDate + get() = LocalDate.of(year, 6, 20) + .with(next(FRIDAY)) + + +private fun Int.getSchoolYearByMonth(monthValue: Int): Int { + return when (monthValue) { + in 9..12 -> this + else -> this + 1 + } +} + +fun LocalDate.getLastSchoolDayIfHoliday(schoolYear: Int): LocalDate { + val date = LocalDate.of(schoolYear.getSchoolYearByMonth(monthValue), monthValue, dayOfMonth) + + if (date.isHolidays) { + return date.lastSchoolDay + } + + return date +} diff --git a/app/src/test/java/io/github/wulkanowy/utils/TimeExtensionTest.kt b/app/src/test/java/io/github/wulkanowy/utils/TimeExtensionTest.kt index 442356dc..024b4727 100644 --- a/app/src/test/java/io/github/wulkanowy/utils/TimeExtensionTest.kt +++ b/app/src/test/java/io/github/wulkanowy/utils/TimeExtensionTest.kt @@ -4,7 +4,7 @@ import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse import org.junit.Assert.assertTrue import org.junit.Test -import org.threeten.bp.LocalDate +import org.threeten.bp.LocalDate.of import org.threeten.bp.LocalDateTime import org.threeten.bp.Month.JANUARY import java.util.Locale @@ -13,13 +13,13 @@ class TimeExtensionTest { @Test fun toLocalDateTest() { - assertEquals(LocalDate.of(1970, 1, 1), "1970-01-01".toLocalDate("yyyy-MM-dd")) + assertEquals(of(1970, 1, 1), "1970-01-01".toLocalDate("yyyy-MM-dd")) } @Test fun toFormattedStringLocalDateTest() { - assertEquals("01.10.2018", LocalDate.of(2018, 10, 1).toFormattedString()) - assertEquals("2018-10.01", LocalDate.of(2018, 10, 1).toFormattedString("yyyy-MM.dd")) + assertEquals("01.10.2018", of(2018, 10, 1).toFormattedString()) + assertEquals("2018-10.01", of(2018, 10, 1).toFormattedString("yyyy-MM.dd")) } @Test @@ -30,20 +30,20 @@ class TimeExtensionTest { @Test fun mondayTest() { - assertEquals(LocalDate.of(2018, 10, 1), LocalDate.of(2018, 10, 2).monday) - assertEquals(LocalDate.of(2018, 10, 1), LocalDate.of(2018, 10, 5).monday) - assertEquals(LocalDate.of(2018, 10, 1), LocalDate.of(2018, 10, 6).monday) - assertEquals(LocalDate.of(2018, 10, 1), LocalDate.of(2018, 10, 7).monday) - assertEquals(LocalDate.of(2018, 10, 8), LocalDate.of(2018, 10, 8).monday) + assertEquals(of(2018, 10, 1), of(2018, 10, 2).monday) + assertEquals(of(2018, 10, 1), of(2018, 10, 5).monday) + assertEquals(of(2018, 10, 1), of(2018, 10, 6).monday) + assertEquals(of(2018, 10, 1), of(2018, 10, 7).monday) + assertEquals(of(2018, 10, 8), of(2018, 10, 8).monday) } @Test fun fridayTest() { - assertEquals(LocalDate.of(2018, 10, 5), LocalDate.of(2018, 10, 2).friday) - assertEquals(LocalDate.of(2018, 10, 5), LocalDate.of(2018, 10, 5).friday) - assertEquals(LocalDate.of(2018, 10, 5), LocalDate.of(2018, 10, 6).friday) - assertEquals(LocalDate.of(2018, 10, 5), LocalDate.of(2018, 10, 7).friday) - assertEquals(LocalDate.of(2018, 10, 12), LocalDate.of(2018, 10, 8).friday) + assertEquals(of(2018, 10, 5), of(2018, 10, 2).friday) + assertEquals(of(2018, 10, 5), of(2018, 10, 5).friday) + assertEquals(of(2018, 10, 5), of(2018, 10, 6).friday) + assertEquals(of(2018, 10, 5), of(2018, 10, 7).friday) + assertEquals(of(2018, 10, 12), of(2018, 10, 8).friday) } @Test @@ -57,106 +57,118 @@ class TimeExtensionTest { @Test fun weekDayNameTest() { Locale.setDefault(Locale.forLanguageTag("PL")) - assertEquals("poniedziaƂek", LocalDate.of(2018, 10, 1).weekDayName) + assertEquals("poniedziaƂek", of(2018, 10, 1).weekDayName) Locale.setDefault(Locale.forLanguageTag("US")) - assertEquals("Monday", LocalDate.of(2018, 10, 1).weekDayName) + assertEquals("Monday", of(2018, 10, 1).weekDayName) } @Test fun nextSchoolDayTest() { - assertEquals(LocalDate.of(2018, 10, 2), LocalDate.of(2018, 10, 1).nextSchoolDay) - assertEquals(LocalDate.of(2018, 10, 3), LocalDate.of(2018, 10, 2).nextSchoolDay) - assertEquals(LocalDate.of(2018, 10, 4), LocalDate.of(2018, 10, 3).nextSchoolDay) - assertEquals(LocalDate.of(2018, 10, 5), LocalDate.of(2018, 10, 4).nextSchoolDay) - assertEquals(LocalDate.of(2018, 10, 8), LocalDate.of(2018, 10, 5).nextSchoolDay) - assertEquals(LocalDate.of(2018, 10, 8), LocalDate.of(2018, 10, 6).nextSchoolDay) - assertEquals(LocalDate.of(2018, 10, 8), LocalDate.of(2018, 10, 7).nextSchoolDay) - assertEquals(LocalDate.of(2018, 10, 9), LocalDate.of(2018, 10, 8).nextSchoolDay) + assertEquals(of(2018, 10, 2), of(2018, 10, 1).nextSchoolDay) + assertEquals(of(2018, 10, 3), of(2018, 10, 2).nextSchoolDay) + assertEquals(of(2018, 10, 4), of(2018, 10, 3).nextSchoolDay) + assertEquals(of(2018, 10, 5), of(2018, 10, 4).nextSchoolDay) + assertEquals(of(2018, 10, 8), of(2018, 10, 5).nextSchoolDay) + assertEquals(of(2018, 10, 8), of(2018, 10, 6).nextSchoolDay) + assertEquals(of(2018, 10, 8), of(2018, 10, 7).nextSchoolDay) + assertEquals(of(2018, 10, 9), of(2018, 10, 8).nextSchoolDay) } @Test fun previousSchoolDayTest() { - assertEquals(LocalDate.of(2018, 10, 9), LocalDate.of(2018, 10, 10).previousSchoolDay) - assertEquals(LocalDate.of(2018, 10, 8), LocalDate.of(2018, 10, 9).previousSchoolDay) - assertEquals(LocalDate.of(2018, 10, 5), LocalDate.of(2018, 10, 8).previousSchoolDay) - assertEquals(LocalDate.of(2018, 10, 5), LocalDate.of(2018, 10, 7).previousSchoolDay) - assertEquals(LocalDate.of(2018, 10, 5), LocalDate.of(2018, 10, 6).previousSchoolDay) - assertEquals(LocalDate.of(2018, 10, 4), LocalDate.of(2018, 10, 5).previousSchoolDay) - assertEquals(LocalDate.of(2018, 10, 3), LocalDate.of(2018, 10, 4).previousSchoolDay) + assertEquals(of(2018, 10, 9), of(2018, 10, 10).previousSchoolDay) + assertEquals(of(2018, 10, 8), of(2018, 10, 9).previousSchoolDay) + assertEquals(of(2018, 10, 5), of(2018, 10, 8).previousSchoolDay) + assertEquals(of(2018, 10, 5), of(2018, 10, 7).previousSchoolDay) + assertEquals(of(2018, 10, 5), of(2018, 10, 6).previousSchoolDay) + assertEquals(of(2018, 10, 4), of(2018, 10, 5).previousSchoolDay) + assertEquals(of(2018, 10, 3), of(2018, 10, 4).previousSchoolDay) } @Test fun nextOrSameSchoolDayTest() { - assertEquals(LocalDate.of(2018, 9, 28), LocalDate.of(2018, 9, 28).nextOrSameSchoolDay) - assertEquals(LocalDate.of(2018, 10, 1), LocalDate.of(2018, 9, 29).nextOrSameSchoolDay) - assertEquals(LocalDate.of(2018, 10, 1), LocalDate.of(2018, 9, 30).nextOrSameSchoolDay) - assertEquals(LocalDate.of(2018, 10, 1), LocalDate.of(2018, 10, 1).nextOrSameSchoolDay) - assertEquals(LocalDate.of(2018, 10, 2), LocalDate.of(2018, 10, 2).nextOrSameSchoolDay) + assertEquals(of(2018, 9, 28), of(2018, 9, 28).nextOrSameSchoolDay) + assertEquals(of(2018, 10, 1), of(2018, 9, 29).nextOrSameSchoolDay) + assertEquals(of(2018, 10, 1), of(2018, 9, 30).nextOrSameSchoolDay) + assertEquals(of(2018, 10, 1), of(2018, 10, 1).nextOrSameSchoolDay) + assertEquals(of(2018, 10, 2), of(2018, 10, 2).nextOrSameSchoolDay) } @Test fun previousOrSameSchoolDayTest() { - assertEquals(LocalDate.of(2018, 9, 28), LocalDate.of(2018, 9, 28).previousOrSameSchoolDay) - assertEquals(LocalDate.of(2018, 9, 28), LocalDate.of(2018, 9, 29).previousOrSameSchoolDay) - assertEquals(LocalDate.of(2018, 9, 28), LocalDate.of(2018, 9, 30).previousOrSameSchoolDay) - assertEquals(LocalDate.of(2018, 10, 1), LocalDate.of(2018, 10, 1).previousOrSameSchoolDay) - assertEquals(LocalDate.of(2018, 10, 2), LocalDate.of(2018, 10, 2).previousOrSameSchoolDay) + assertEquals(of(2018, 9, 28), of(2018, 9, 28).previousOrSameSchoolDay) + assertEquals(of(2018, 9, 28), of(2018, 9, 29).previousOrSameSchoolDay) + assertEquals(of(2018, 9, 28), of(2018, 9, 30).previousOrSameSchoolDay) + assertEquals(of(2018, 10, 1), of(2018, 10, 1).previousOrSameSchoolDay) + assertEquals(of(2018, 10, 2), of(2018, 10, 2).previousOrSameSchoolDay) } @Test fun isHolidays_schoolEndTest() { - assertFalse(LocalDate.of(2017, 6, 23).isHolidays) - assertFalse(LocalDate.of(2018, 6, 22).isHolidays) - assertFalse(LocalDate.of(2019, 6, 21).isHolidays) - assertFalse(LocalDate.of(2020, 6, 26).isHolidays) - assertFalse(LocalDate.of(2021, 6, 25).isHolidays) - assertFalse(LocalDate.of(2022, 6, 24).isHolidays) - assertFalse(LocalDate.of(2023, 6, 23).isHolidays) - assertFalse(LocalDate.of(2024, 6, 21).isHolidays) - assertFalse(LocalDate.of(2025, 6, 27).isHolidays) + assertFalse(of(2017, 6, 23).isHolidays) + assertFalse(of(2018, 6, 22).isHolidays) + assertFalse(of(2019, 6, 21).isHolidays) + assertFalse(of(2020, 6, 26).isHolidays) + assertFalse(of(2021, 6, 25).isHolidays) + assertFalse(of(2022, 6, 24).isHolidays) + assertFalse(of(2023, 6, 23).isHolidays) + assertFalse(of(2024, 6, 21).isHolidays) + assertFalse(of(2025, 6, 27).isHolidays) } @Test fun isHolidays_holidaysStartTest() { - assertTrue(LocalDate.of(2017, 6, 24).isHolidays) - assertTrue(LocalDate.of(2018, 6, 23).isHolidays) - assertTrue(LocalDate.of(2019, 6, 22).isHolidays) - assertTrue(LocalDate.of(2020, 6, 27).isHolidays) - assertTrue(LocalDate.of(2021, 6, 26).isHolidays) - assertTrue(LocalDate.of(2022, 6, 25).isHolidays) - assertTrue(LocalDate.of(2023, 6, 24).isHolidays) - assertTrue(LocalDate.of(2024, 6, 22).isHolidays) - assertTrue(LocalDate.of(2025, 6, 28).isHolidays) + assertTrue(of(2017, 6, 24).isHolidays) + assertTrue(of(2018, 6, 23).isHolidays) + assertTrue(of(2019, 6, 22).isHolidays) + assertTrue(of(2020, 6, 27).isHolidays) + assertTrue(of(2021, 6, 26).isHolidays) + assertTrue(of(2022, 6, 25).isHolidays) + assertTrue(of(2023, 6, 24).isHolidays) + assertTrue(of(2024, 6, 22).isHolidays) + assertTrue(of(2025, 6, 28).isHolidays) } @Test fun isHolidays_holidaysEndTest() { - assertTrue(LocalDate.of(2017, 9, 1).isHolidays) // friday - assertTrue(LocalDate.of(2017, 9, 2).isHolidays) // saturday - assertTrue(LocalDate.of(2017, 9, 3).isHolidays) // sunday - assertTrue(LocalDate.of(2018, 9, 1).isHolidays) // saturday - assertTrue(LocalDate.of(2018, 9, 2).isHolidays) // sunday - assertTrue(LocalDate.of(2019, 9, 1).isHolidays) // sunday - assertTrue(LocalDate.of(2020, 8, 31).isHolidays) // monday - assertTrue(LocalDate.of(2021, 8, 31).isHolidays) // tuesday - assertTrue(LocalDate.of(2022, 8, 31).isHolidays) // wednesday - assertTrue(LocalDate.of(2023, 9, 1).isHolidays) // friday - assertTrue(LocalDate.of(2023, 9, 2).isHolidays) // saturday - assertTrue(LocalDate.of(2023, 9, 3).isHolidays) // sunday - assertTrue(LocalDate.of(2024, 9, 1).isHolidays) // sunday - assertTrue(LocalDate.of(2025, 8, 31).isHolidays) // sunday + assertTrue(of(2017, 9, 1).isHolidays) // friday + assertTrue(of(2017, 9, 2).isHolidays) // saturday + assertTrue(of(2017, 9, 3).isHolidays) // sunday + assertTrue(of(2018, 9, 1).isHolidays) // saturday + assertTrue(of(2018, 9, 2).isHolidays) // sunday + assertTrue(of(2019, 9, 1).isHolidays) // sunday + assertTrue(of(2020, 8, 31).isHolidays) // monday + assertTrue(of(2021, 8, 31).isHolidays) // tuesday + assertTrue(of(2022, 8, 31).isHolidays) // wednesday + assertTrue(of(2023, 9, 1).isHolidays) // friday + assertTrue(of(2023, 9, 2).isHolidays) // saturday + assertTrue(of(2023, 9, 3).isHolidays) // sunday + assertTrue(of(2024, 9, 1).isHolidays) // sunday + assertTrue(of(2025, 8, 31).isHolidays) // sunday } @Test fun isHolidays_schoolStartTest() { - assertFalse(LocalDate.of(2017, 9, 4).isHolidays) // monday - assertFalse(LocalDate.of(2018, 9, 3).isHolidays) // monday - assertFalse(LocalDate.of(2019, 9, 2).isHolidays) // monday - assertFalse(LocalDate.of(2020, 9, 1).isHolidays) // tuesday - assertFalse(LocalDate.of(2021, 9, 1).isHolidays) // wednesday - assertFalse(LocalDate.of(2022, 9, 1).isHolidays) // thursday - assertFalse(LocalDate.of(2023, 9, 4).isHolidays) // monday - assertFalse(LocalDate.of(2024, 9, 2).isHolidays) // monday - assertFalse(LocalDate.of(2025, 9, 1).isHolidays) // monday + assertFalse(of(2017, 9, 4).isHolidays) // monday + assertFalse(of(2018, 9, 3).isHolidays) // monday + assertFalse(of(2019, 9, 2).isHolidays) // monday + assertFalse(of(2020, 9, 1).isHolidays) // tuesday + assertFalse(of(2021, 9, 1).isHolidays) // wednesday + assertFalse(of(2022, 9, 1).isHolidays) // thursday + assertFalse(of(2023, 9, 4).isHolidays) // monday + assertFalse(of(2024, 9, 2).isHolidays) // monday + assertFalse(of(2025, 9, 1).isHolidays) // monday + } + + @Test + fun getCorrectedDate_holidays() { + assertEquals(of(2019, 6, 21), of(2019, 8, 9).getLastSchoolDayIfHoliday(2018)) + assertEquals(of(2018, 6, 22), of(2019, 8, 9).getLastSchoolDayIfHoliday(2017)) + } + + @Test + fun getCorrectedDate_schoolYear() { + assertEquals(of(2019, 5, 1), of(2019, 5, 1).getLastSchoolDayIfHoliday(2018)) + assertEquals(of(2018, 5, 1), of(2019, 5, 1).getLastSchoolDayIfHoliday(2017)) } }