diff --git a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/timetable/TestTimetableEntityCreator.kt b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/timetable/TestTimetableEntityCreator.kt index eea5463d1..c38ffd99f 100644 --- a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/timetable/TestTimetableEntityCreator.kt +++ b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/timetable/TestTimetableEntityCreator.kt @@ -7,7 +7,7 @@ import org.threeten.bp.LocalDateTime.now import io.github.wulkanowy.api.timetable.Timetable as TimetableRemote import io.github.wulkanowy.data.db.entities.Timetable as TimetableLocal -fun createTimetableLocal(number: Int, start: LocalDateTime, room: String = "", subject: String = "", teacher: String = ""): TimetableLocal { +fun createTimetableLocal(number: Int, start: LocalDateTime, room: String = "", subject: String = "", teacher: String = "", changes: Boolean = false): TimetableLocal { return TimetableLocal( studentId = 1, diaryId = 2, @@ -23,12 +23,12 @@ fun createTimetableLocal(number: Int, start: LocalDateTime, room: String = "", s teacher = teacher, teacherOld = "", info = "", - changes = false, + changes = changes, canceled = false ) } -fun createTimetableRemote(number: Int, start: LocalDateTime, room: String, subject: String = "", teacher: String = ""): TimetableRemote { +fun createTimetableRemote(number: Int, start: LocalDateTime, room: String, subject: String = "", teacher: String = "", changes: Boolean = false): TimetableRemote { return TimetableRemote( number = number, start = start.toDate(), @@ -39,7 +39,7 @@ fun createTimetableRemote(number: Int, start: LocalDateTime, room: String, subje room = room, teacher = teacher, info = "", - changes = false, + changes = changes, canceled = false ) } diff --git a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/timetable/TimetableRepositoryTest.kt b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/timetable/TimetableRepositoryTest.kt index 79216d552..02ab605ed 100644 --- a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/timetable/TimetableRepositoryTest.kt +++ b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/timetable/TimetableRepositoryTest.kt @@ -60,7 +60,7 @@ class TimetableRepositoryTest { } @Test - fun copyDetailsToCompletedFromPrevious() { + fun copyRoomToCompletedFromPrevious() { timetableLocal.saveTimetable(listOf( createTimetableLocal(1, of(2019, 3, 5, 8, 0), "123", "Przyroda"), createTimetableLocal(2, of(2019, 3, 5, 8, 50), "321", "Religia"), @@ -83,7 +83,32 @@ class TimetableRepositoryTest { assertEquals("123", lessons[0].room) assertEquals("321", lessons[1].room) assertEquals("213", lessons[2].room) + } - assertEquals("", lessons[3].teacher) + @Test + fun copyTeacherToCompletedFromPrevious() { + timetableLocal.saveTimetable(listOf( + createTimetableLocal(1, of(2019, 3, 5, 8, 0), "123", "Przyroda", "Jan Garnkiewicz", false), + createTimetableLocal(2, of(2019, 3, 5, 8, 50), "321", "Religia", "Paweł Jumper", false), + createTimetableLocal(3, of(2019, 3, 5, 9, 40), "213", "W-F", "", true), + createTimetableLocal(4, of(2019, 3, 5, 10, 30), "213", "W-F", "", false) + )) + + every { mockApi.getTimetable(any(), any()) } returns Single.just(listOf( + createTimetableRemote(1, of(2019, 3, 5, 8, 0), "", "Przyroda", "", true), // should override local + createTimetableRemote(2, of(2019, 3, 5, 8, 50), "", "Religia", "", false), + createTimetableRemote(3, of(2019, 3, 5, 9, 40), "", "W-F", "Jan Garnkiewicz", false), + createTimetableRemote(4, of(2019, 3, 5, 10, 30), "", "W-F", "Paweł Jumper", false) + )) + + val lessons = TimetableRepository(settings, timetableLocal, timetableRemote) + .getTimetable(semesterMock, LocalDate.of(2019, 3, 5), LocalDate.of(2019, 3, 5), true) + .blockingGet() + + assertEquals(4, lessons.size) + assertEquals("", lessons[0].teacher) + assertEquals("Paweł Jumper", lessons[1].teacher) + assertEquals("Jan Garnkiewicz", lessons[2].teacher) + assertEquals("Paweł Jumper", lessons[3].teacher) } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/timetable/TimetableRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/timetable/TimetableRepository.kt index 0e6331f91..e10e958ec 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/timetable/TimetableRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/timetable/TimetableRepository.kt @@ -32,10 +32,11 @@ class TimetableRepository @Inject constructor( .doOnSuccess { old -> local.deleteTimetable(old.uniqueSubtract(new)) local.saveTimetable(new.uniqueSubtract(old).map { item -> - item.apply { - old.singleOrNull { this.start == it.start }?.let { - return@map copy( - room = if (room.isEmpty()) it.room else room + item.also { new -> + old.singleOrNull { new.start == it.start }?.let { old -> + return@map new.copy( + room = if (new.room.isEmpty()) old.room else new.room, + teacher = if (new.teacher.isEmpty() && !new.changes) old.teacher else new.teacher ) } }