diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/exam/ExamRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/exam/ExamRepository.kt index e7f115ac..3f4591a2 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/exam/ExamRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/exam/ExamRepository.kt @@ -2,9 +2,9 @@ package io.github.wulkanowy.data.repositories.exam import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Student -import io.github.wulkanowy.utils.monday +import io.github.wulkanowy.utils.endExamsDay import io.github.wulkanowy.utils.networkBoundResource -import io.github.wulkanowy.utils.sunday +import io.github.wulkanowy.utils.startExamsDay import io.github.wulkanowy.utils.uniqueSubtract import java.time.LocalDate import javax.inject.Inject @@ -18,8 +18,8 @@ class ExamRepository @Inject constructor( fun getExams(student: Student, semester: Semester, start: LocalDate, end: LocalDate, forceRefresh: Boolean) = networkBoundResource( shouldFetch = { it.isEmpty() || forceRefresh }, - query = { local.getExams(semester, start.monday, end.sunday) }, - fetch = { remote.getExams(student, semester, start.monday, end.sunday) }, + query = { local.getExams(semester, start.startExamsDay, start.endExamsDay) }, + fetch = { remote.getExams(student, semester, start.startExamsDay, start.endExamsDay) }, saveFetchResult = { old, new -> local.deleteExams(old uniqueSubtract new) local.saveExams(new uniqueSubtract old) diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/ExamWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/ExamWork.kt index f7d8db0a..899d45cb 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/ExamWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/ExamWork.kt @@ -3,8 +3,6 @@ package io.github.wulkanowy.services.sync.works import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.repositories.exam.ExamRepository -import io.github.wulkanowy.utils.monday -import io.github.wulkanowy.utils.sunday import io.github.wulkanowy.utils.waitForResult import java.time.LocalDate.now import javax.inject.Inject @@ -12,6 +10,6 @@ import javax.inject.Inject class ExamWork @Inject constructor(private val examRepository: ExamRepository) : Work { override suspend fun doWork(student: Student, semester: Semester) { - examRepository.getExams(student, semester, now().monday, now().sunday, true).waitForResult() + examRepository.getExams(student, semester, now(), now(), true).waitForResult() } } 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 d1aba160..9bd30e87 100644 --- a/app/src/main/java/io/github/wulkanowy/utils/TimeExtension.kt +++ b/app/src/main/java/io/github/wulkanowy/utils/TimeExtension.kt @@ -13,8 +13,7 @@ import java.time.Month import java.time.ZoneId import java.time.ZoneOffset import java.time.format.DateTimeFormatter.ofPattern -import java.time.format.TextStyle.FULL_STANDALONE -import java.time.format.TextStyle.* +import java.time.format.TextStyle.FULL import java.time.temporal.TemporalAdjusters.firstInMonth import java.time.temporal.TemporalAdjusters.next import java.time.temporal.TemporalAdjusters.previous @@ -78,6 +77,12 @@ inline val LocalDate.nextOrSameSchoolDay: LocalDate } } +inline val LocalDate.startExamsDay: LocalDate + get() = nextOrSameSchoolDay.monday + +inline val LocalDate.endExamsDay: LocalDate + get() = nextOrSameSchoolDay.monday.plusWeeks(4).minusDays(1) + inline val LocalDate.previousOrSameSchoolDay: LocalDate get() { return when (dayOfWeek) { 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 9709ded3..d604dfef 100644 --- a/app/src/test/java/io/github/wulkanowy/utils/TimeExtensionTest.kt +++ b/app/src/test/java/io/github/wulkanowy/utils/TimeExtensionTest.kt @@ -171,4 +171,42 @@ class TimeExtensionTest { assertEquals(of(2019, 5, 1), of(2019, 5, 1).getLastSchoolDayIfHoliday(2018)) assertEquals(of(2018, 5, 1), of(2019, 5, 1).getLastSchoolDayIfHoliday(2017)) } + + @Test + fun getExamsCutOffDates() { + with(of(2020, 9, 13)) { + assertEquals(of(2020, 9, 14), startExamsDay) + assertEquals(of(2020, 10, 11), endExamsDay) + } + + with(of(2020, 9, 14)) { + assertEquals(of(2020, 9, 14), startExamsDay) + assertEquals(of(2020, 10, 11), endExamsDay) + } + + with(of(2020, 9, 15)) { + assertEquals(of(2020, 9, 14), startExamsDay) + assertEquals(of(2020, 10, 11), endExamsDay) + } + + with(of(2020, 9, 16)) { + assertEquals(of(2020, 9, 14), startExamsDay) + assertEquals(of(2020, 10, 11), endExamsDay) + } + + with(of(2020, 9, 17)) { + assertEquals(of(2020, 9, 14), startExamsDay) + assertEquals(of(2020, 10, 11), endExamsDay) + } + + with(of(2020, 9, 18)) { + assertEquals(of(2020, 9, 14), startExamsDay) + assertEquals(of(2020, 10, 11), endExamsDay) + } + + with(of(2020, 9, 19)) { + assertEquals(of(2020, 9, 21), startExamsDay) + assertEquals(of(2020, 10, 18), endExamsDay) + } + } }