Expand exam sync date range to next month (#960)

This commit is contained in:
Mikołaj Pich 2020-09-13 18:37:34 +02:00 committed by GitHub
parent 47150364d8
commit 5d8fb376ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 50 additions and 9 deletions

View File

@ -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)

View File

@ -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()
}
}

View File

@ -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) {

View File

@ -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)
}
}
}