1
0
mirror of https://github.com/wulkanowy/wulkanowy.git synced 2024-09-20 03:29:08 -05:00

Fix day/week navigation on holiday (#459)

This commit is contained in:
Mikołaj Pich 2019-08-12 12:17:39 +02:00 committed by Rafał Borcz
parent 070fba734c
commit 6bd07d2651
7 changed files with 284 additions and 134 deletions

View File

@ -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.BasePresenter
import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.ui.base.ErrorHandler
import io.github.wulkanowy.utils.FirebaseAnalyticsHelper 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
import org.threeten.bp.LocalDate.now import org.threeten.bp.LocalDate.now
import org.threeten.bp.LocalDate.ofEpochDay import org.threeten.bp.LocalDate.ofEpochDay
@ -26,6 +32,8 @@ class AttendancePresenter @Inject constructor(
private val analytics: FirebaseAnalyticsHelper private val analytics: FirebaseAnalyticsHelper
) : BasePresenter<AttendanceView>(errorHandler, studentRepository, schedulers) { ) : BasePresenter<AttendanceView>(errorHandler, studentRepository, schedulers) {
private var baseDate: LocalDate = now().previousOrSameSchoolDay
lateinit var currentDate: LocalDate lateinit var currentDate: LocalDate
private set private set
@ -33,7 +41,8 @@ class AttendancePresenter @Inject constructor(
super.onAttachView(view) super.onAttachView(view)
view.initView() view.initView()
Timber.i("Attendance view was initialized") Timber.i("Attendance view was initialized")
loadData(ofEpochDay(date ?: now().previousOrSameSchoolDay.toEpochDay())) loadData(ofEpochDay(date ?: baseDate.toEpochDay()))
if (currentDate.isHolidays) setBaseDateOnHolidays()
reloadView() reloadView()
} }
@ -56,7 +65,7 @@ class AttendancePresenter @Inject constructor(
Timber.i("Attendance view is reselected") Timber.i("Attendance view is reselected")
view?.also { view -> view?.also { view ->
if (view.currentStackSize == 1) { if (view.currentStackSize == 1) {
now().previousOrSameSchoolDay.also { baseDate.also {
if (currentDate != it) { if (currentDate != it) {
loadData(it) loadData(it)
reloadView() reloadView()
@ -78,6 +87,20 @@ class AttendancePresenter @Inject constructor(
return true 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) { private fun loadData(date: LocalDate, forceRefresh: Boolean = false) {
Timber.i("Loading attendance data started") Timber.i("Loading attendance data started")
currentDate = date currentDate = date
@ -127,8 +150,14 @@ class AttendancePresenter @Inject constructor(
showContent(false) showContent(false)
showEmpty(false) showEmpty(false)
clearData() clearData()
showNextButton(!currentDate.plusDays(1).isHolidays) reloadNavigation()
}
}
private fun reloadNavigation() {
view?.apply {
showPreButton(!currentDate.minusDays(1).isHolidays) showPreButton(!currentDate.minusDays(1).isHolidays)
showNextButton(!currentDate.plusDays(1).isHolidays)
updateNavigationDay(currentDate.toFormattedString("EEEE\ndd.MM.YYYY").capitalize()) updateNavigationDay(currentDate.toFormattedString("EEEE\ndd.MM.YYYY").capitalize())
} }
} }

View File

@ -10,6 +10,7 @@ import io.github.wulkanowy.ui.base.ErrorHandler
import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.FirebaseAnalyticsHelper
import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.SchedulersProvider
import io.github.wulkanowy.utils.friday import io.github.wulkanowy.utils.friday
import io.github.wulkanowy.utils.getLastSchoolDayIfHoliday
import io.github.wulkanowy.utils.isHolidays import io.github.wulkanowy.utils.isHolidays
import io.github.wulkanowy.utils.monday import io.github.wulkanowy.utils.monday
import io.github.wulkanowy.utils.nextOrSameSchoolDay import io.github.wulkanowy.utils.nextOrSameSchoolDay
@ -30,6 +31,8 @@ class ExamPresenter @Inject constructor(
private val analytics: FirebaseAnalyticsHelper private val analytics: FirebaseAnalyticsHelper
) : BasePresenter<ExamView>(errorHandler, studentRepository, schedulers) { ) : BasePresenter<ExamView>(errorHandler, studentRepository, schedulers) {
private var baseDate: LocalDate = now().nextOrSameSchoolDay
lateinit var currentDate: LocalDate lateinit var currentDate: LocalDate
private set private set
@ -37,7 +40,8 @@ class ExamPresenter @Inject constructor(
super.onAttachView(view) super.onAttachView(view)
view.initView() view.initView()
Timber.i("Exam view was initialized") Timber.i("Exam view was initialized")
loadData(ofEpochDay(date ?: now().nextOrSameSchoolDay.toEpochDay())) loadData(ofEpochDay(date ?: baseDate.toEpochDay()))
if (currentDate.isHolidays) setBaseDateOnHolidays()
reloadView() reloadView()
} }
@ -65,7 +69,7 @@ class ExamPresenter @Inject constructor(
fun onViewReselected() { fun onViewReselected() {
Timber.i("Exam view is reselected") Timber.i("Exam view is reselected")
now().nextOrSameSchoolDay.also { baseDate.also {
if (currentDate != it) { if (currentDate != it) {
loadData(it) loadData(it)
reloadView() 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) { private fun loadData(date: LocalDate, forceRefresh: Boolean = false) {
Timber.i("Loading exam data started") Timber.i("Loading exam data started")
currentDate = date currentDate = date
@ -81,9 +99,8 @@ class ExamPresenter @Inject constructor(
add(studentRepository.getCurrentStudent() add(studentRepository.getCurrentStudent()
.delay(200, MILLISECONDS) .delay(200, MILLISECONDS)
.flatMap { semesterRepository.getCurrentSemester(it) } .flatMap { semesterRepository.getCurrentSemester(it) }
.flatMap { .flatMap { examRepository.getExams(it, currentDate.monday, currentDate.friday, forceRefresh) }
examRepository.getExams(it, currentDate.monday, currentDate.friday, forceRefresh) .map { it.groupBy { exam -> exam.date }.toSortedMap() }
}.map { it.groupBy { exam -> exam.date }.toSortedMap() }
.map { createExamItems(it) } .map { createExamItems(it) }
.subscribeOn(schedulers.backgroundThread) .subscribeOn(schedulers.backgroundThread)
.observeOn(schedulers.mainThread) .observeOn(schedulers.mainThread)
@ -126,6 +143,12 @@ class ExamPresenter @Inject constructor(
showContent(false) showContent(false)
showEmpty(false) showEmpty(false)
clearData() clearData()
reloadNavigation()
}
}
private fun reloadNavigation() {
view?.apply {
showPreButton(!currentDate.minusDays(7).isHolidays) showPreButton(!currentDate.minusDays(7).isHolidays)
showNextButton(!currentDate.plusDays(7).isHolidays) showNextButton(!currentDate.plusDays(7).isHolidays)
updateNavigationWeek("${currentDate.monday.toFormattedString("dd.MM")} - " + updateNavigationWeek("${currentDate.monday.toFormattedString("dd.MM")} - " +

View File

@ -10,11 +10,13 @@ import io.github.wulkanowy.ui.base.ErrorHandler
import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.FirebaseAnalyticsHelper
import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.SchedulersProvider
import io.github.wulkanowy.utils.friday import io.github.wulkanowy.utils.friday
import io.github.wulkanowy.utils.getLastSchoolDayIfHoliday
import io.github.wulkanowy.utils.isHolidays import io.github.wulkanowy.utils.isHolidays
import io.github.wulkanowy.utils.monday import io.github.wulkanowy.utils.monday
import io.github.wulkanowy.utils.nextOrSameSchoolDay import io.github.wulkanowy.utils.nextOrSameSchoolDay
import io.github.wulkanowy.utils.toFormattedString import io.github.wulkanowy.utils.toFormattedString
import org.threeten.bp.LocalDate import org.threeten.bp.LocalDate
import org.threeten.bp.LocalDate.ofEpochDay
import timber.log.Timber import timber.log.Timber
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
import javax.inject.Inject import javax.inject.Inject
@ -28,6 +30,8 @@ class HomeworkPresenter @Inject constructor(
private val analytics: FirebaseAnalyticsHelper private val analytics: FirebaseAnalyticsHelper
) : BasePresenter<HomeworkView>(errorHandler, studentRepository, schedulers) { ) : BasePresenter<HomeworkView>(errorHandler, studentRepository, schedulers) {
private var baseDate: LocalDate = LocalDate.now().nextOrSameSchoolDay
lateinit var currentDate: LocalDate lateinit var currentDate: LocalDate
private set private set
@ -35,7 +39,8 @@ class HomeworkPresenter @Inject constructor(
super.onAttachView(view) super.onAttachView(view)
view.initView() view.initView()
Timber.i("Homework view was initialized") Timber.i("Homework view was initialized")
loadData(LocalDate.ofEpochDay(date ?: LocalDate.now().nextOrSameSchoolDay.toEpochDay())) loadData(ofEpochDay(date ?: baseDate.toEpochDay()))
if (currentDate.isHolidays) setBaseDateOnHolidays()
reloadView() 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) { private fun loadData(date: LocalDate, forceRefresh: Boolean = false) {
Timber.i("Loading homework data started") Timber.i("Loading homework data started")
currentDate = date currentDate = date
@ -113,8 +132,14 @@ class HomeworkPresenter @Inject constructor(
showContent(false) showContent(false)
showEmpty(false) showEmpty(false)
clearData() clearData()
showNextButton(!currentDate.plusDays(7).isHolidays) reloadNavigation()
}
}
private fun reloadNavigation() {
view?.apply {
showPreButton(!currentDate.minusDays(7).isHolidays) showPreButton(!currentDate.minusDays(7).isHolidays)
showNextButton(!currentDate.plusDays(7).isHolidays)
updateNavigationWeek("${currentDate.monday.toFormattedString("dd.MM")} - " + updateNavigationWeek("${currentDate.monday.toFormattedString("dd.MM")} - " +
currentDate.friday.toFormattedString("dd.MM")) currentDate.friday.toFormattedString("dd.MM"))
} }

View File

@ -8,6 +8,7 @@ import io.github.wulkanowy.ui.base.BasePresenter
import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.ui.base.ErrorHandler
import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.FirebaseAnalyticsHelper
import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.SchedulersProvider
import io.github.wulkanowy.utils.getLastSchoolDayIfHoliday
import io.github.wulkanowy.utils.isHolidays import io.github.wulkanowy.utils.isHolidays
import io.github.wulkanowy.utils.nextOrSameSchoolDay import io.github.wulkanowy.utils.nextOrSameSchoolDay
import io.github.wulkanowy.utils.nextSchoolDay import io.github.wulkanowy.utils.nextSchoolDay
@ -29,6 +30,8 @@ class TimetablePresenter @Inject constructor(
private val analytics: FirebaseAnalyticsHelper private val analytics: FirebaseAnalyticsHelper
) : BasePresenter<TimetableView>(errorHandler, studentRepository, schedulers) { ) : BasePresenter<TimetableView>(errorHandler, studentRepository, schedulers) {
private var baseDate: LocalDate = now().nextOrSameSchoolDay
lateinit var currentDate: LocalDate lateinit var currentDate: LocalDate
private set private set
@ -36,7 +39,8 @@ class TimetablePresenter @Inject constructor(
super.onAttachView(view) super.onAttachView(view)
view.initView() view.initView()
Timber.i("Timetable was initialized") Timber.i("Timetable was initialized")
loadData(ofEpochDay(date ?: now().nextOrSameSchoolDay.toEpochDay())) loadData(ofEpochDay(date ?: baseDate.toEpochDay()))
if (currentDate.isHolidays) setBaseDateOnHolidays()
reloadView() reloadView()
} }
@ -59,7 +63,7 @@ class TimetablePresenter @Inject constructor(
Timber.i("Timetable view is reselected") Timber.i("Timetable view is reselected")
view?.also { view -> view?.also { view ->
if (view.currentStackSize == 1) { if (view.currentStackSize == 1) {
now().nextOrSameSchoolDay.also { baseDate.also {
if (currentDate != it) { if (currentDate != it) {
loadData(it) loadData(it)
reloadView() reloadView()
@ -81,6 +85,20 @@ class TimetablePresenter @Inject constructor(
return true 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) { private fun loadData(date: LocalDate, forceRefresh: Boolean = false) {
Timber.i("Loading timetable data started") Timber.i("Loading timetable data started")
currentDate = date currentDate = date
@ -125,8 +143,14 @@ class TimetablePresenter @Inject constructor(
showContent(false) showContent(false)
showEmpty(false) showEmpty(false)
clearData() clearData()
showNextButton(!currentDate.plusDays(1).isHolidays) reloadNavigation()
}
}
private fun reloadNavigation() {
view?.apply {
showPreButton(!currentDate.minusDays(1).isHolidays) showPreButton(!currentDate.minusDays(1).isHolidays)
showNextButton(!currentDate.plusDays(1).isHolidays)
updateNavigationDay(currentDate.toFormattedString("EEEE\ndd.MM.YYYY").capitalize()) updateNavigationDay(currentDate.toFormattedString("EEEE\ndd.MM.YYYY").capitalize())
} }
} }

View File

@ -7,6 +7,7 @@ import io.github.wulkanowy.data.repositories.student.StudentRepository
import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.BasePresenter
import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.FirebaseAnalyticsHelper
import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.SchedulersProvider
import io.github.wulkanowy.utils.getLastSchoolDayIfHoliday
import io.github.wulkanowy.utils.isHolidays import io.github.wulkanowy.utils.isHolidays
import io.github.wulkanowy.utils.nextOrSameSchoolDay import io.github.wulkanowy.utils.nextOrSameSchoolDay
import io.github.wulkanowy.utils.nextSchoolDay import io.github.wulkanowy.utils.nextSchoolDay
@ -28,6 +29,8 @@ class CompletedLessonsPresenter @Inject constructor(
private val analytics: FirebaseAnalyticsHelper private val analytics: FirebaseAnalyticsHelper
) : BasePresenter<CompletedLessonsView>(completedLessonsErrorHandler, studentRepository, schedulers) { ) : BasePresenter<CompletedLessonsView>(completedLessonsErrorHandler, studentRepository, schedulers) {
private var baseDate: LocalDate = now().nextOrSameSchoolDay
lateinit var currentDate: LocalDate lateinit var currentDate: LocalDate
private set private set
@ -35,7 +38,8 @@ class CompletedLessonsPresenter @Inject constructor(
super.onAttachView(view) super.onAttachView(view)
Timber.i("Completed lessons is attached") Timber.i("Completed lessons is attached")
view.initView() view.initView()
loadData(ofEpochDay(date ?: now().nextOrSameSchoolDay.toEpochDay())) loadData(ofEpochDay(date ?: baseDate.toEpochDay()))
if (currentDate.isHolidays) setBaseDateOnHolidays()
reloadView() reloadView()
completedLessonsErrorHandler.onFeatureDisabled = { completedLessonsErrorHandler.onFeatureDisabled = {
this.view?.showFeatureDisabled() 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) { private fun loadData(date: LocalDate, forceRefresh: Boolean = false) {
Timber.i("Loading completed lessons data started") Timber.i("Loading completed lessons data started")
currentDate = date currentDate = date
@ -109,8 +127,14 @@ class CompletedLessonsPresenter @Inject constructor(
showContent(false) showContent(false)
showEmpty(false) showEmpty(false)
clearData() clearData()
showNextButton(!currentDate.plusDays(1).isHolidays) reloadNavigation()
}
}
private fun reloadNavigation() {
view?.apply {
showPreButton(!currentDate.minusDays(1).isHolidays) showPreButton(!currentDate.minusDays(1).isHolidays)
showNextButton(!currentDate.plusDays(1).isHolidays)
updateNavigationDay(currentDate.toFormattedString("EEEE\ndd.MM.YYYY").capitalize()) updateNavigationDay(currentDate.toFormattedString("EEEE\ndd.MM.YYYY").capitalize())
} }
} }

View File

@ -6,11 +6,11 @@ import org.threeten.bp.DayOfWeek.MONDAY
import org.threeten.bp.DayOfWeek.SATURDAY import org.threeten.bp.DayOfWeek.SATURDAY
import org.threeten.bp.DayOfWeek.SUNDAY import org.threeten.bp.DayOfWeek.SUNDAY
import org.threeten.bp.Instant import org.threeten.bp.Instant
import org.threeten.bp.Instant.ofEpochMilli
import org.threeten.bp.LocalDate import org.threeten.bp.LocalDate
import org.threeten.bp.LocalDateTime import org.threeten.bp.LocalDateTime
import org.threeten.bp.Month import org.threeten.bp.Month
import org.threeten.bp.ZoneId import org.threeten.bp.ZoneId
import org.threeten.bp.format.DateTimeFormatter
import org.threeten.bp.format.DateTimeFormatter.ofPattern import org.threeten.bp.format.DateTimeFormatter.ofPattern
import org.threeten.bp.format.TextStyle.FULL_STANDALONE import org.threeten.bp.format.TextStyle.FULL_STANDALONE
import org.threeten.bp.temporal.TemporalAdjusters.firstInMonth import org.threeten.bp.temporal.TemporalAdjusters.firstInMonth
@ -21,21 +21,15 @@ import java.util.Locale
private const val DATE_PATTERN = "dd.MM.yyyy" private const val DATE_PATTERN = "dd.MM.yyyy"
fun Date.toLocalDate(): LocalDate { fun Date.toLocalDate(): LocalDate = Instant.ofEpochMilli(time).atZone(ZoneId.systemDefault()).toLocalDate()
return Instant.ofEpochMilli(this.time).atZone(ZoneId.systemDefault()).toLocalDate()
}
fun Date.toLocalDateTime(): LocalDateTime { fun Date.toLocalDateTime(): LocalDateTime = ofEpochMilli(time).atZone(ZoneId.systemDefault()).toLocalDateTime()
return Instant.ofEpochMilli(this.time).atZone(ZoneId.systemDefault()).toLocalDateTime()
}
fun String.toLocalDate(format: String = DATE_PATTERN): LocalDate { fun String.toLocalDate(format: String = DATE_PATTERN): LocalDate = LocalDate.parse(this, ofPattern(format))
return LocalDate.parse(this, DateTimeFormatter.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()) fun LocalDateTime.toDate(): Date = DateTimeUtils.toDate(atZone(ZoneId.systemDefault()).toInstant())
@ -66,62 +60,81 @@ fun Month.getFormattedName(): String {
inline val LocalDate.nextSchoolDay: LocalDate inline val LocalDate.nextSchoolDay: LocalDate
get() { get() {
return when (this.dayOfWeek) { return when (dayOfWeek) {
FRIDAY, SATURDAY, SUNDAY -> this.with(next(MONDAY)) FRIDAY, SATURDAY, SUNDAY -> with(next(MONDAY))
else -> this.plusDays(1) else -> plusDays(1)
} }
} }
inline val LocalDate.previousSchoolDay: LocalDate inline val LocalDate.previousSchoolDay: LocalDate
get() { get() {
return when (this.dayOfWeek) { return when (dayOfWeek) {
SATURDAY, SUNDAY, MONDAY -> this.with(previous(FRIDAY)) SATURDAY, SUNDAY, MONDAY -> with(previous(FRIDAY))
else -> this.minusDays(1) else -> minusDays(1)
} }
} }
inline val LocalDate.nextOrSameSchoolDay: LocalDate inline val LocalDate.nextOrSameSchoolDay: LocalDate
get() { get() {
return when (this.dayOfWeek) { return when (dayOfWeek) {
SATURDAY, SUNDAY -> this.with(next(MONDAY)) SATURDAY, SUNDAY -> with(next(MONDAY))
else -> this else -> this
} }
} }
inline val LocalDate.previousOrSameSchoolDay: LocalDate inline val LocalDate.previousOrSameSchoolDay: LocalDate
get() { get() {
return when (this.dayOfWeek) { return when (dayOfWeek) {
SATURDAY, SUNDAY -> this.with(previous(FRIDAY)) SATURDAY, SUNDAY -> with(previous(FRIDAY))
else -> this else -> this
} }
} }
inline val LocalDate.weekDayName: String inline val LocalDate.weekDayName: String
get() = this.format(ofPattern("EEEE", Locale.getDefault())) get() = format(ofPattern("EEEE", Locale.getDefault()))
inline val LocalDate.shortcutWeekDayName: String inline val LocalDate.shortcutWeekDayName: String
get() = this.format(ofPattern("EEE", Locale.getDefault())) get() = format(ofPattern("EEE", Locale.getDefault()))
inline val LocalDate.monday: LocalDate inline val LocalDate.monday: LocalDate
get() = this.with(MONDAY) get() = with(MONDAY)
inline val LocalDate.friday: LocalDate 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) * [Dz.U. 2016 poz. 1335](http://prawo.sejm.gov.pl/isap.nsf/DocDetails.xsp?id=WDU20160001335)
*/ */
inline val LocalDate.isHolidays: Boolean inline val LocalDate.isHolidays: Boolean
get() { get() = isBefore(firstSchoolDay) && isAfter(lastSchoolDay)
return LocalDate.of(this.year, 9, 1).run {
inline val LocalDate.firstSchoolDay: LocalDate
get() = LocalDate.of(year, 9, 1).run {
when (dayOfWeek) { when (dayOfWeek) {
FRIDAY, SATURDAY, SUNDAY -> with(firstInMonth(MONDAY)) FRIDAY, SATURDAY, SUNDAY -> with(firstInMonth(MONDAY))
else -> this else -> this
} }
}.let { firstSchoolDay -> }
LocalDate.of(this.year, 6, 20)
inline val LocalDate.lastSchoolDay: LocalDate
get() = LocalDate.of(year, 6, 20)
.with(next(FRIDAY)) .with(next(FRIDAY))
.let { lastSchoolDay -> this.isBefore(firstSchoolDay) && this.isAfter(lastSchoolDay) }
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
}

View File

@ -4,7 +4,7 @@ import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue import org.junit.Assert.assertTrue
import org.junit.Test import org.junit.Test
import org.threeten.bp.LocalDate import org.threeten.bp.LocalDate.of
import org.threeten.bp.LocalDateTime import org.threeten.bp.LocalDateTime
import org.threeten.bp.Month.JANUARY import org.threeten.bp.Month.JANUARY
import java.util.Locale import java.util.Locale
@ -13,13 +13,13 @@ class TimeExtensionTest {
@Test @Test
fun toLocalDateTest() { 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 @Test
fun toFormattedStringLocalDateTest() { fun toFormattedStringLocalDateTest() {
assertEquals("01.10.2018", LocalDate.of(2018, 10, 1).toFormattedString()) assertEquals("01.10.2018", of(2018, 10, 1).toFormattedString())
assertEquals("2018-10.01", LocalDate.of(2018, 10, 1).toFormattedString("yyyy-MM.dd")) assertEquals("2018-10.01", of(2018, 10, 1).toFormattedString("yyyy-MM.dd"))
} }
@Test @Test
@ -30,20 +30,20 @@ class TimeExtensionTest {
@Test @Test
fun mondayTest() { fun mondayTest() {
assertEquals(LocalDate.of(2018, 10, 1), LocalDate.of(2018, 10, 2).monday) assertEquals(of(2018, 10, 1), of(2018, 10, 2).monday)
assertEquals(LocalDate.of(2018, 10, 1), LocalDate.of(2018, 10, 5).monday) assertEquals(of(2018, 10, 1), of(2018, 10, 5).monday)
assertEquals(LocalDate.of(2018, 10, 1), LocalDate.of(2018, 10, 6).monday) assertEquals(of(2018, 10, 1), of(2018, 10, 6).monday)
assertEquals(LocalDate.of(2018, 10, 1), LocalDate.of(2018, 10, 7).monday) assertEquals(of(2018, 10, 1), of(2018, 10, 7).monday)
assertEquals(LocalDate.of(2018, 10, 8), LocalDate.of(2018, 10, 8).monday) assertEquals(of(2018, 10, 8), of(2018, 10, 8).monday)
} }
@Test @Test
fun fridayTest() { fun fridayTest() {
assertEquals(LocalDate.of(2018, 10, 5), LocalDate.of(2018, 10, 2).friday) assertEquals(of(2018, 10, 5), of(2018, 10, 2).friday)
assertEquals(LocalDate.of(2018, 10, 5), LocalDate.of(2018, 10, 5).friday) assertEquals(of(2018, 10, 5), of(2018, 10, 5).friday)
assertEquals(LocalDate.of(2018, 10, 5), LocalDate.of(2018, 10, 6).friday) assertEquals(of(2018, 10, 5), of(2018, 10, 6).friday)
assertEquals(LocalDate.of(2018, 10, 5), LocalDate.of(2018, 10, 7).friday) assertEquals(of(2018, 10, 5), of(2018, 10, 7).friday)
assertEquals(LocalDate.of(2018, 10, 12), LocalDate.of(2018, 10, 8).friday) assertEquals(of(2018, 10, 12), of(2018, 10, 8).friday)
} }
@Test @Test
@ -57,106 +57,118 @@ class TimeExtensionTest {
@Test @Test
fun weekDayNameTest() { fun weekDayNameTest() {
Locale.setDefault(Locale.forLanguageTag("PL")) 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")) Locale.setDefault(Locale.forLanguageTag("US"))
assertEquals("Monday", LocalDate.of(2018, 10, 1).weekDayName) assertEquals("Monday", of(2018, 10, 1).weekDayName)
} }
@Test @Test
fun nextSchoolDayTest() { fun nextSchoolDayTest() {
assertEquals(LocalDate.of(2018, 10, 2), LocalDate.of(2018, 10, 1).nextSchoolDay) assertEquals(of(2018, 10, 2), of(2018, 10, 1).nextSchoolDay)
assertEquals(LocalDate.of(2018, 10, 3), LocalDate.of(2018, 10, 2).nextSchoolDay) assertEquals(of(2018, 10, 3), of(2018, 10, 2).nextSchoolDay)
assertEquals(LocalDate.of(2018, 10, 4), LocalDate.of(2018, 10, 3).nextSchoolDay) assertEquals(of(2018, 10, 4), of(2018, 10, 3).nextSchoolDay)
assertEquals(LocalDate.of(2018, 10, 5), LocalDate.of(2018, 10, 4).nextSchoolDay) assertEquals(of(2018, 10, 5), of(2018, 10, 4).nextSchoolDay)
assertEquals(LocalDate.of(2018, 10, 8), LocalDate.of(2018, 10, 5).nextSchoolDay) assertEquals(of(2018, 10, 8), of(2018, 10, 5).nextSchoolDay)
assertEquals(LocalDate.of(2018, 10, 8), LocalDate.of(2018, 10, 6).nextSchoolDay) assertEquals(of(2018, 10, 8), of(2018, 10, 6).nextSchoolDay)
assertEquals(LocalDate.of(2018, 10, 8), LocalDate.of(2018, 10, 7).nextSchoolDay) assertEquals(of(2018, 10, 8), of(2018, 10, 7).nextSchoolDay)
assertEquals(LocalDate.of(2018, 10, 9), LocalDate.of(2018, 10, 8).nextSchoolDay) assertEquals(of(2018, 10, 9), of(2018, 10, 8).nextSchoolDay)
} }
@Test @Test
fun previousSchoolDayTest() { fun previousSchoolDayTest() {
assertEquals(LocalDate.of(2018, 10, 9), LocalDate.of(2018, 10, 10).previousSchoolDay) assertEquals(of(2018, 10, 9), of(2018, 10, 10).previousSchoolDay)
assertEquals(LocalDate.of(2018, 10, 8), LocalDate.of(2018, 10, 9).previousSchoolDay) assertEquals(of(2018, 10, 8), of(2018, 10, 9).previousSchoolDay)
assertEquals(LocalDate.of(2018, 10, 5), LocalDate.of(2018, 10, 8).previousSchoolDay) assertEquals(of(2018, 10, 5), of(2018, 10, 8).previousSchoolDay)
assertEquals(LocalDate.of(2018, 10, 5), LocalDate.of(2018, 10, 7).previousSchoolDay) assertEquals(of(2018, 10, 5), of(2018, 10, 7).previousSchoolDay)
assertEquals(LocalDate.of(2018, 10, 5), LocalDate.of(2018, 10, 6).previousSchoolDay) assertEquals(of(2018, 10, 5), of(2018, 10, 6).previousSchoolDay)
assertEquals(LocalDate.of(2018, 10, 4), LocalDate.of(2018, 10, 5).previousSchoolDay) assertEquals(of(2018, 10, 4), of(2018, 10, 5).previousSchoolDay)
assertEquals(LocalDate.of(2018, 10, 3), LocalDate.of(2018, 10, 4).previousSchoolDay) assertEquals(of(2018, 10, 3), of(2018, 10, 4).previousSchoolDay)
} }
@Test @Test
fun nextOrSameSchoolDayTest() { fun nextOrSameSchoolDayTest() {
assertEquals(LocalDate.of(2018, 9, 28), LocalDate.of(2018, 9, 28).nextOrSameSchoolDay) assertEquals(of(2018, 9, 28), of(2018, 9, 28).nextOrSameSchoolDay)
assertEquals(LocalDate.of(2018, 10, 1), LocalDate.of(2018, 9, 29).nextOrSameSchoolDay) assertEquals(of(2018, 10, 1), of(2018, 9, 29).nextOrSameSchoolDay)
assertEquals(LocalDate.of(2018, 10, 1), LocalDate.of(2018, 9, 30).nextOrSameSchoolDay) assertEquals(of(2018, 10, 1), of(2018, 9, 30).nextOrSameSchoolDay)
assertEquals(LocalDate.of(2018, 10, 1), LocalDate.of(2018, 10, 1).nextOrSameSchoolDay) assertEquals(of(2018, 10, 1), of(2018, 10, 1).nextOrSameSchoolDay)
assertEquals(LocalDate.of(2018, 10, 2), LocalDate.of(2018, 10, 2).nextOrSameSchoolDay) assertEquals(of(2018, 10, 2), of(2018, 10, 2).nextOrSameSchoolDay)
} }
@Test @Test
fun previousOrSameSchoolDayTest() { fun previousOrSameSchoolDayTest() {
assertEquals(LocalDate.of(2018, 9, 28), LocalDate.of(2018, 9, 28).previousOrSameSchoolDay) assertEquals(of(2018, 9, 28), of(2018, 9, 28).previousOrSameSchoolDay)
assertEquals(LocalDate.of(2018, 9, 28), LocalDate.of(2018, 9, 29).previousOrSameSchoolDay) assertEquals(of(2018, 9, 28), of(2018, 9, 29).previousOrSameSchoolDay)
assertEquals(LocalDate.of(2018, 9, 28), LocalDate.of(2018, 9, 30).previousOrSameSchoolDay) assertEquals(of(2018, 9, 28), of(2018, 9, 30).previousOrSameSchoolDay)
assertEquals(LocalDate.of(2018, 10, 1), LocalDate.of(2018, 10, 1).previousOrSameSchoolDay) assertEquals(of(2018, 10, 1), of(2018, 10, 1).previousOrSameSchoolDay)
assertEquals(LocalDate.of(2018, 10, 2), LocalDate.of(2018, 10, 2).previousOrSameSchoolDay) assertEquals(of(2018, 10, 2), of(2018, 10, 2).previousOrSameSchoolDay)
} }
@Test @Test
fun isHolidays_schoolEndTest() { fun isHolidays_schoolEndTest() {
assertFalse(LocalDate.of(2017, 6, 23).isHolidays) assertFalse(of(2017, 6, 23).isHolidays)
assertFalse(LocalDate.of(2018, 6, 22).isHolidays) assertFalse(of(2018, 6, 22).isHolidays)
assertFalse(LocalDate.of(2019, 6, 21).isHolidays) assertFalse(of(2019, 6, 21).isHolidays)
assertFalse(LocalDate.of(2020, 6, 26).isHolidays) assertFalse(of(2020, 6, 26).isHolidays)
assertFalse(LocalDate.of(2021, 6, 25).isHolidays) assertFalse(of(2021, 6, 25).isHolidays)
assertFalse(LocalDate.of(2022, 6, 24).isHolidays) assertFalse(of(2022, 6, 24).isHolidays)
assertFalse(LocalDate.of(2023, 6, 23).isHolidays) assertFalse(of(2023, 6, 23).isHolidays)
assertFalse(LocalDate.of(2024, 6, 21).isHolidays) assertFalse(of(2024, 6, 21).isHolidays)
assertFalse(LocalDate.of(2025, 6, 27).isHolidays) assertFalse(of(2025, 6, 27).isHolidays)
} }
@Test @Test
fun isHolidays_holidaysStartTest() { fun isHolidays_holidaysStartTest() {
assertTrue(LocalDate.of(2017, 6, 24).isHolidays) assertTrue(of(2017, 6, 24).isHolidays)
assertTrue(LocalDate.of(2018, 6, 23).isHolidays) assertTrue(of(2018, 6, 23).isHolidays)
assertTrue(LocalDate.of(2019, 6, 22).isHolidays) assertTrue(of(2019, 6, 22).isHolidays)
assertTrue(LocalDate.of(2020, 6, 27).isHolidays) assertTrue(of(2020, 6, 27).isHolidays)
assertTrue(LocalDate.of(2021, 6, 26).isHolidays) assertTrue(of(2021, 6, 26).isHolidays)
assertTrue(LocalDate.of(2022, 6, 25).isHolidays) assertTrue(of(2022, 6, 25).isHolidays)
assertTrue(LocalDate.of(2023, 6, 24).isHolidays) assertTrue(of(2023, 6, 24).isHolidays)
assertTrue(LocalDate.of(2024, 6, 22).isHolidays) assertTrue(of(2024, 6, 22).isHolidays)
assertTrue(LocalDate.of(2025, 6, 28).isHolidays) assertTrue(of(2025, 6, 28).isHolidays)
} }
@Test @Test
fun isHolidays_holidaysEndTest() { fun isHolidays_holidaysEndTest() {
assertTrue(LocalDate.of(2017, 9, 1).isHolidays) // friday assertTrue(of(2017, 9, 1).isHolidays) // friday
assertTrue(LocalDate.of(2017, 9, 2).isHolidays) // saturday assertTrue(of(2017, 9, 2).isHolidays) // saturday
assertTrue(LocalDate.of(2017, 9, 3).isHolidays) // sunday assertTrue(of(2017, 9, 3).isHolidays) // sunday
assertTrue(LocalDate.of(2018, 9, 1).isHolidays) // saturday assertTrue(of(2018, 9, 1).isHolidays) // saturday
assertTrue(LocalDate.of(2018, 9, 2).isHolidays) // sunday assertTrue(of(2018, 9, 2).isHolidays) // sunday
assertTrue(LocalDate.of(2019, 9, 1).isHolidays) // sunday assertTrue(of(2019, 9, 1).isHolidays) // sunday
assertTrue(LocalDate.of(2020, 8, 31).isHolidays) // monday assertTrue(of(2020, 8, 31).isHolidays) // monday
assertTrue(LocalDate.of(2021, 8, 31).isHolidays) // tuesday assertTrue(of(2021, 8, 31).isHolidays) // tuesday
assertTrue(LocalDate.of(2022, 8, 31).isHolidays) // wednesday assertTrue(of(2022, 8, 31).isHolidays) // wednesday
assertTrue(LocalDate.of(2023, 9, 1).isHolidays) // friday assertTrue(of(2023, 9, 1).isHolidays) // friday
assertTrue(LocalDate.of(2023, 9, 2).isHolidays) // saturday assertTrue(of(2023, 9, 2).isHolidays) // saturday
assertTrue(LocalDate.of(2023, 9, 3).isHolidays) // sunday assertTrue(of(2023, 9, 3).isHolidays) // sunday
assertTrue(LocalDate.of(2024, 9, 1).isHolidays) // sunday assertTrue(of(2024, 9, 1).isHolidays) // sunday
assertTrue(LocalDate.of(2025, 8, 31).isHolidays) // sunday assertTrue(of(2025, 8, 31).isHolidays) // sunday
} }
@Test @Test
fun isHolidays_schoolStartTest() { fun isHolidays_schoolStartTest() {
assertFalse(LocalDate.of(2017, 9, 4).isHolidays) // monday assertFalse(of(2017, 9, 4).isHolidays) // monday
assertFalse(LocalDate.of(2018, 9, 3).isHolidays) // monday assertFalse(of(2018, 9, 3).isHolidays) // monday
assertFalse(LocalDate.of(2019, 9, 2).isHolidays) // monday assertFalse(of(2019, 9, 2).isHolidays) // monday
assertFalse(LocalDate.of(2020, 9, 1).isHolidays) // tuesday assertFalse(of(2020, 9, 1).isHolidays) // tuesday
assertFalse(LocalDate.of(2021, 9, 1).isHolidays) // wednesday assertFalse(of(2021, 9, 1).isHolidays) // wednesday
assertFalse(LocalDate.of(2022, 9, 1).isHolidays) // thursday assertFalse(of(2022, 9, 1).isHolidays) // thursday
assertFalse(LocalDate.of(2023, 9, 4).isHolidays) // monday assertFalse(of(2023, 9, 4).isHolidays) // monday
assertFalse(LocalDate.of(2024, 9, 2).isHolidays) // monday assertFalse(of(2024, 9, 2).isHolidays) // monday
assertFalse(LocalDate.of(2025, 9, 1).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))
} }
} }