From 63404b8576689f6c580a313464814d071a7b2149 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Pich?= Date: Tue, 30 Apr 2019 19:04:05 +0200 Subject: [PATCH] Fix entity list comparing (#335) --- .../repositories/grade/GradeRepositoryTest.kt | 70 +++++++++++++++++++ .../attendance/AttendanceRepository.kt | 5 +- .../AttendanceSummaryRepository.kt | 5 +- .../CompletedLessonsRepository.kt | 5 +- .../data/repositories/exam/ExamRepository.kt | 9 +-- .../repositories/grade/GradeRepository.kt | 12 ++-- .../gradessummary/GradeSummaryRepository.kt | 9 +-- .../GradeStatisticsRepository.kt | 9 +-- .../homework/HomeworkRepository.kt | 9 +-- .../repositories/message/MessageRepository.kt | 5 +- .../data/repositories/note/NoteRepository.kt | 5 +- .../recipient/RecipientRepository.kt | 5 +- .../reportingunit/ReportingUnitRepository.kt | 5 +- .../semester/SemesterRepository.kt | 5 +- .../repositories/subject/SubjectRepository.kt | 5 +- .../timetable/TimetableRepository.kt | 11 +-- .../ui/modules/splash/SplashActivity.kt | 1 + .../github/wulkanowy/utils/ListExtension.kt | 9 +++ 18 files changed, 139 insertions(+), 45 deletions(-) create mode 100644 app/src/main/java/io/github/wulkanowy/utils/ListExtension.kt diff --git a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/grade/GradeRepositoryTest.kt b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/grade/GradeRepositoryTest.kt index 17e788fc0..a0acb5a76 100644 --- a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/grade/GradeRepositoryTest.kt +++ b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/grade/GradeRepositoryTest.kt @@ -22,6 +22,7 @@ import org.junit.Test import org.junit.runner.RunWith import org.threeten.bp.LocalDate.of import org.threeten.bp.LocalDateTime +import kotlin.test.assertEquals import kotlin.test.assertFalse import kotlin.test.assertTrue import io.github.wulkanowy.api.grades.Grade as GradeApi @@ -109,4 +110,73 @@ class GradeRepositoryTest { assertTrue { grades[2].isRead } assertTrue { grades[3].isRead } } + + @Test + fun subtractLocaleDuplicateGrades() { + gradeLocal.saveGrades(listOf( + createGradeLocal(5, 3.0, of(2019, 2, 25), "Taka sama ocena"), + createGradeLocal(5, 3.0, of(2019, 2, 25), "Taka sama ocena"), + createGradeLocal(3, 5.0, of(2019, 2, 26), "Jakaś inna ocena") + )) + + every { mockApi.getGrades(1) } returns Single.just(listOf( + createGradeApi(5, 3.0, of(2019, 2, 25), "Taka sama ocena"), + createGradeApi(3, 5.0, of(2019, 2, 26), "Jakaś inna ocena") + )) + + val grades = GradeRepository(settings, gradeLocal, gradeRemote) + .getGrades(studentMock, semesterMock, true).blockingGet() + + assertEquals(2, grades.size) + } + + @Test + fun subtractRemoteDuplicateGrades() { + gradeLocal.saveGrades(listOf( + createGradeLocal(5, 3.0, of(2019, 2, 25), "Taka sama ocena"), + createGradeLocal(3, 5.0, of(2019, 2, 26), "Jakaś inna ocena") + )) + + every { mockApi.getGrades(1) } returns Single.just(listOf( + createGradeApi(5, 3.0, of(2019, 2, 25), "Taka sama ocena"), + createGradeApi(5, 3.0, of(2019, 2, 25), "Taka sama ocena"), + createGradeApi(3, 5.0, of(2019, 2, 26), "Jakaś inna ocena") + )) + + val grades = GradeRepository(settings, gradeLocal, gradeRemote) + .getGrades(studentMock, semesterMock, true).blockingGet() + + assertEquals(3, grades.size) + } + + @Test + fun emptyLocal() { + gradeLocal.saveGrades(listOf()) + + every { mockApi.getGrades(1) } returns Single.just(listOf( + createGradeApi(5, 3.0, of(2019, 2, 25), "Taka sama ocena"), + createGradeApi(5, 3.0, of(2019, 2, 25), "Taka sama ocena"), + createGradeApi(3, 5.0, of(2019, 2, 26), "Jakaś inna ocena") + )) + + val grades = GradeRepository(settings, gradeLocal, gradeRemote) + .getGrades(studentMock, semesterMock, true).blockingGet() + + assertEquals(3, grades.size) + } + + @Test + fun emptyRemote() { + gradeLocal.saveGrades(listOf( + createGradeLocal(5, 3.0, of(2019, 2, 25), "Taka sama ocena"), + createGradeLocal(3, 5.0, of(2019, 2, 26), "Jakaś inna ocena") + )) + + every { mockApi.getGrades(1) } returns Single.just(listOf()) + + val grades = GradeRepository(settings, gradeLocal, gradeRemote) + .getGrades(studentMock, semesterMock, true).blockingGet() + + assertEquals(0, grades.size) + } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/attendance/AttendanceRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/attendance/AttendanceRepository.kt index f6eb07dae..85102b3c5 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/attendance/AttendanceRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/attendance/AttendanceRepository.kt @@ -6,6 +6,7 @@ import io.github.wulkanowy.data.db.entities.Attendance import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.utils.friday import io.github.wulkanowy.utils.monday +import io.github.wulkanowy.utils.uniqueSubtract import io.reactivex.Single import org.threeten.bp.LocalDate import java.net.UnknownHostException @@ -31,8 +32,8 @@ class AttendanceRepository @Inject constructor( local.getAttendance(semester, dates.first, dates.second) .toSingle(emptyList()) .doOnSuccess { oldAttendance -> - local.deleteAttendance(oldAttendance - newAttendance) - local.saveAttendance(newAttendance - oldAttendance) + local.deleteAttendance(oldAttendance.uniqueSubtract(newAttendance)) + local.saveAttendance(newAttendance.uniqueSubtract(oldAttendance)) } }.flatMap { local.getAttendance(semester, dates.first, dates.second) diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/attendancesummary/AttendanceSummaryRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/attendancesummary/AttendanceSummaryRepository.kt index 90d39aab2..c65870508 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/attendancesummary/AttendanceSummaryRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/attendancesummary/AttendanceSummaryRepository.kt @@ -4,6 +4,7 @@ import com.github.pwittchen.reactivenetwork.library.rx2.ReactiveNetwork import com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.InternetObservingSettings import io.github.wulkanowy.data.db.entities.AttendanceSummary import io.github.wulkanowy.data.db.entities.Semester +import io.github.wulkanowy.utils.uniqueSubtract import io.reactivex.Single import java.net.UnknownHostException import javax.inject.Inject @@ -25,8 +26,8 @@ class AttendanceSummaryRepository @Inject constructor( }.flatMap { new -> local.getAttendanceSummary(semester, subjectId).toSingle(emptyList()) .doOnSuccess { old -> - local.deleteAttendanceSummary(old - new) - local.saveAttendanceSummary(new - old) + local.deleteAttendanceSummary(old.uniqueSubtract(new)) + local.saveAttendanceSummary(new.uniqueSubtract(old)) } }.flatMap { local.getAttendanceSummary(semester, subjectId).toSingle(emptyList()) }) } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsRepository.kt index 5b5941076..c22fabc39 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsRepository.kt @@ -6,6 +6,7 @@ import io.github.wulkanowy.data.db.entities.CompletedLesson import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.utils.friday import io.github.wulkanowy.utils.monday +import io.github.wulkanowy.utils.uniqueSubtract import io.reactivex.Single import org.threeten.bp.LocalDate import java.net.UnknownHostException @@ -31,8 +32,8 @@ class CompletedLessonsRepository @Inject constructor( local.getCompletedLessons(semester, dates.first, dates.second) .toSingle(emptyList()) .doOnSuccess { old -> - local.deleteCompleteLessons(old - new) - local.saveCompletedLessons(new - old) + local.deleteCompleteLessons(old.uniqueSubtract(new)) + local.saveCompletedLessons(new.uniqueSubtract(old)) } }.flatMap { local.getCompletedLessons(semester, dates.first, dates.second) 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 a0cb5ba11..be60eaecd 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 @@ -6,6 +6,7 @@ import io.github.wulkanowy.data.db.entities.Exam import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.utils.friday import io.github.wulkanowy.utils.monday +import io.github.wulkanowy.utils.uniqueSubtract import io.reactivex.Single import org.threeten.bp.LocalDate import java.net.UnknownHostException @@ -27,12 +28,12 @@ class ExamRepository @Inject constructor( .flatMap { if (it) remote.getExams(semester, dates.first, dates.second) else Single.error(UnknownHostException()) - }.flatMap { newExams -> + }.flatMap { new -> local.getExams(semester, dates.first, dates.second) .toSingle(emptyList()) - .doOnSuccess { oldExams -> - local.deleteExams(oldExams - newExams) - local.saveExams(newExams - oldExams) + .doOnSuccess { old -> + local.deleteExams(old.uniqueSubtract(new)) + local.saveExams(new.uniqueSubtract(old)) } }.flatMap { local.getExams(semester, dates.first, dates.second) diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/grade/GradeRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/grade/GradeRepository.kt index 3dd456bff..7f8e92a2e 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/grade/GradeRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/grade/GradeRepository.kt @@ -5,6 +5,7 @@ import com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.Inter import io.github.wulkanowy.data.db.entities.Grade import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Student +import io.github.wulkanowy.utils.uniqueSubtract import io.reactivex.Completable import io.reactivex.Single import java.net.UnknownHostException @@ -24,13 +25,12 @@ class GradeRepository @Inject constructor( .flatMap { if (it) remote.getGrades(semester) else Single.error(UnknownHostException()) - }.flatMap { newGrades -> + }.flatMap { new -> local.getGrades(semester).toSingle(emptyList()) - .doOnSuccess { oldGrades -> - val notifyBreakDate = oldGrades.maxBy { it.date }?.date - ?: student.registrationDate.toLocalDate() - local.deleteGrades(oldGrades - newGrades) - local.saveGrades((newGrades - oldGrades) + .doOnSuccess { old -> + val notifyBreakDate = old.maxBy { it.date }?.date ?: student.registrationDate.toLocalDate() + local.deleteGrades(old.uniqueSubtract(new)) + local.saveGrades(new.uniqueSubtract(old) .onEach { if (it.date >= notifyBreakDate) it.apply { isRead = false diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/gradessummary/GradeSummaryRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/gradessummary/GradeSummaryRepository.kt index b19e07f00..660a032c2 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/gradessummary/GradeSummaryRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/gradessummary/GradeSummaryRepository.kt @@ -4,6 +4,7 @@ import com.github.pwittchen.reactivenetwork.library.rx2.ReactiveNetwork import com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.InternetObservingSettings import io.github.wulkanowy.data.db.entities.GradeSummary import io.github.wulkanowy.data.db.entities.Semester +import io.github.wulkanowy.utils.uniqueSubtract import io.reactivex.Single import java.net.UnknownHostException import javax.inject.Inject @@ -22,11 +23,11 @@ class GradeSummaryRepository @Inject constructor( .flatMap { if (it) remote.getGradeSummary(semester) else Single.error(UnknownHostException()) - }.flatMap { newGradesSummary -> + }.flatMap { new -> local.getGradesSummary(semester).toSingle(emptyList()) - .doOnSuccess { oldGradesSummary -> - local.deleteGradesSummary(oldGradesSummary - newGradesSummary) - local.saveGradesSummary(newGradesSummary - oldGradesSummary) + .doOnSuccess { old -> + local.deleteGradesSummary(old.uniqueSubtract(new)) + local.saveGradesSummary(new.uniqueSubtract(old)) } }.flatMap { local.getGradesSummary(semester).toSingle(emptyList()) }) } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/gradestatistics/GradeStatisticsRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/gradestatistics/GradeStatisticsRepository.kt index 870bd1e93..9617cdd64 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/gradestatistics/GradeStatisticsRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/gradestatistics/GradeStatisticsRepository.kt @@ -4,6 +4,7 @@ import com.github.pwittchen.reactivenetwork.library.rx2.ReactiveNetwork import com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.InternetObservingSettings import io.github.wulkanowy.data.db.entities.GradeStatistics import io.github.wulkanowy.data.db.entities.Semester +import io.github.wulkanowy.utils.uniqueSubtract import io.reactivex.Single import java.net.UnknownHostException import javax.inject.Inject @@ -22,11 +23,11 @@ class GradeStatisticsRepository @Inject constructor( .flatMap { if (it) remote.getGradeStatistics(semester, isSemester) else Single.error(UnknownHostException()) - }.flatMap { newGradesStats -> + }.flatMap { new -> local.getGradesStatistics(semester, isSemester).toSingle(emptyList()) - .doOnSuccess { oldGradesStats -> - local.deleteGradesStatistics(oldGradesStats - newGradesStats) - local.saveGradesStatistics(newGradesStats - oldGradesStats) + .doOnSuccess { old -> + local.deleteGradesStatistics(old.uniqueSubtract(new)) + local.saveGradesStatistics(new.uniqueSubtract(old)) } }.flatMap { local.getGradesStatistics(semester, isSemester, subjectName).toSingle(emptyList()) }) } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/homework/HomeworkRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/homework/HomeworkRepository.kt index 0447c86fe..3f924944d 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/homework/HomeworkRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/homework/HomeworkRepository.kt @@ -6,6 +6,7 @@ import io.github.wulkanowy.data.db.entities.Homework import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.utils.friday import io.github.wulkanowy.utils.monday +import io.github.wulkanowy.utils.uniqueSubtract import io.reactivex.Single import org.threeten.bp.LocalDate import java.net.UnknownHostException @@ -26,11 +27,11 @@ class HomeworkRepository @Inject constructor( .flatMap { if (it) remote.getHomework(semester, monday, friday) else Single.error(UnknownHostException()) - }.flatMap { newGrades -> + }.flatMap { new -> local.getHomework(semester, monday, friday).toSingle(emptyList()) - .doOnSuccess { oldGrades -> - local.deleteHomework(oldGrades - newGrades) - local.saveHomework(newGrades - oldGrades) + .doOnSuccess { old -> + local.deleteHomework(old.uniqueSubtract(new)) + local.saveHomework(new.uniqueSubtract(old)) } }.flatMap { local.getHomework(semester, monday, friday).toSingle(emptyList()) }) } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/message/MessageRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/message/MessageRepository.kt index fe4cfbf34..b68f7f719 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/message/MessageRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/message/MessageRepository.kt @@ -8,6 +8,7 @@ import io.github.wulkanowy.data.db.entities.Message import io.github.wulkanowy.data.db.entities.Recipient import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.repositories.message.MessageFolder.RECEIVED +import io.github.wulkanowy.utils.uniqueSubtract import io.reactivex.Completable import io.reactivex.Maybe import io.reactivex.Single @@ -34,8 +35,8 @@ class MessageRepository @Inject constructor( }.flatMap { new -> local.getMessages(student, folder).toSingle(emptyList()) .doOnSuccess { old -> - local.deleteMessages(old - new) - local.saveMessages((new - old) + local.deleteMessages(old.uniqueSubtract(new)) + local.saveMessages(new.uniqueSubtract(old) .onEach { it.isNotified = !notify }) diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/note/NoteRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/note/NoteRepository.kt index d74bc7eaf..52cb2d0f5 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/note/NoteRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/note/NoteRepository.kt @@ -5,6 +5,7 @@ import com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.Inter import io.github.wulkanowy.data.db.entities.Note import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Student +import io.github.wulkanowy.utils.uniqueSubtract import io.reactivex.Completable import io.reactivex.Single import java.net.UnknownHostException @@ -27,8 +28,8 @@ class NoteRepository @Inject constructor( }.flatMap { new -> local.getNotes(student).toSingle(emptyList()) .doOnSuccess { old -> - local.deleteNotes(old - new) - local.saveNotes((new - old) + local.deleteNotes(old.uniqueSubtract(new)) + local.saveNotes(new.uniqueSubtract(old) .onEach { if (it.date >= student.registrationDate.toLocalDate()) it.apply { isRead = false diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/recipient/RecipientRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/recipient/RecipientRepository.kt index 0b02721f1..cde75ea8b 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/recipient/RecipientRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/recipient/RecipientRepository.kt @@ -7,6 +7,7 @@ import io.github.wulkanowy.data.db.entities.Message import io.github.wulkanowy.data.db.entities.Recipient import io.github.wulkanowy.data.db.entities.ReportingUnit import io.github.wulkanowy.data.db.entities.Student +import io.github.wulkanowy.utils.uniqueSubtract import io.reactivex.Single import java.net.UnknownHostException import javax.inject.Inject @@ -31,8 +32,8 @@ class RecipientRepository @Inject constructor( }.flatMap { new -> local.getRecipients(student, role, unit).toSingle(emptyList()) .doOnSuccess { old -> - local.deleteRecipients(old - new) - local.saveRecipients(new - old) + local.deleteRecipients(old.uniqueSubtract(new)) + local.saveRecipients(new.uniqueSubtract(old)) } }.flatMap { local.getRecipients(student, role, unit).toSingle(emptyList()) diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/reportingunit/ReportingUnitRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/reportingunit/ReportingUnitRepository.kt index 9184b4bb1..6758898e2 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/reportingunit/ReportingUnitRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/reportingunit/ReportingUnitRepository.kt @@ -5,6 +5,7 @@ import com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.Inter import io.github.wulkanowy.data.ApiHelper import io.github.wulkanowy.data.db.entities.ReportingUnit import io.github.wulkanowy.data.db.entities.Student +import io.github.wulkanowy.utils.uniqueSubtract import io.reactivex.Maybe import io.reactivex.Single import java.net.UnknownHostException @@ -30,8 +31,8 @@ class ReportingUnitRepository @Inject constructor( }.flatMap { new -> local.getReportingUnits(student).toSingle(emptyList()) .doOnSuccess { old -> - local.deleteReportingUnits(old - new) - local.saveReportingUnits(new - old) + local.deleteReportingUnits(old.uniqueSubtract(new)) + local.saveReportingUnits(new.uniqueSubtract(old)) } }.flatMap { local.getReportingUnits(student).toSingle(emptyList()) } ) diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/semester/SemesterRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/semester/SemesterRepository.kt index 9735f0290..593014032 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/semester/SemesterRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/semester/SemesterRepository.kt @@ -5,6 +5,7 @@ import com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.Inter import io.github.wulkanowy.data.ApiHelper import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Student +import io.github.wulkanowy.utils.uniqueSubtract import io.reactivex.Maybe import io.reactivex.Single import timber.log.Timber @@ -31,8 +32,8 @@ class SemesterRepository @Inject constructor( if (currentSemesters.size == 1) { local.getSemesters(student).toSingle(emptyList()) .doOnSuccess { old -> - local.deleteSemesters(old - new) - local.saveSemesters(new - old) + local.deleteSemesters(old.uniqueSubtract(new)) + local.saveSemesters(new.uniqueSubtract(old)) } } else { Timber.i("Current semesters list:\n${currentSemesters.joinToString(separator = "\n")}") diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/subject/SubjectRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/subject/SubjectRepository.kt index 6167251b9..0c5f386b6 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/subject/SubjectRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/subject/SubjectRepository.kt @@ -4,6 +4,7 @@ import com.github.pwittchen.reactivenetwork.library.rx2.ReactiveNetwork import com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.InternetObservingSettings import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Subject +import io.github.wulkanowy.utils.uniqueSubtract import io.reactivex.Single import java.net.UnknownHostException import javax.inject.Inject @@ -26,8 +27,8 @@ class SubjectRepository @Inject constructor( local.getSubjects(semester) .toSingle(emptyList()) .doOnSuccess { old -> - local.deleteSubjects(old - new) - local.saveSubjects(new - old) + local.deleteSubjects(old.uniqueSubtract(new)) + local.saveSubjects(new.uniqueSubtract(old)) } }.flatMap { local.getSubjects(semester).toSingle(emptyList()) 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 460d55fd7..c3f23811a 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 @@ -6,6 +6,7 @@ import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Timetable import io.github.wulkanowy.utils.friday import io.github.wulkanowy.utils.monday +import io.github.wulkanowy.utils.uniqueSubtract import io.reactivex.Single import org.threeten.bp.LocalDate import java.net.UnknownHostException @@ -25,14 +26,14 @@ class TimetableRepository @Inject constructor( .switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings).flatMap { if (it) remote.getTimetable(semester, monday, friday) else Single.error(UnknownHostException()) - }.flatMap { newTimetable -> + }.flatMap { new -> local.getTimetable(semester, monday, friday) .toSingle(emptyList()) - .doOnSuccess { oldTimetable -> - local.deleteTimetable(oldTimetable - newTimetable) - local.saveTimetable((newTimetable - oldTimetable).map { item -> + .doOnSuccess { old -> + local.deleteTimetable(old.uniqueSubtract(new)) + local.saveTimetable(new.uniqueSubtract(old).map { item -> item.apply { - oldTimetable.singleOrNull { this.start == it.start }?.let { + old.singleOrNull { this.start == it.start }?.let { return@map copy( room = if (room.isEmpty()) it.room else room, teacher = if (teacher.isEmpty()) it.teacher else teacher diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/splash/SplashActivity.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/splash/SplashActivity.kt index 851b69172..2bd625332 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/splash/SplashActivity.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/splash/SplashActivity.kt @@ -25,6 +25,7 @@ class SplashActivity : BaseActivity(), SplashView { override fun openMainView() { startActivity(MainActivity.getStartIntent(this)) + finish() } override fun showError(text: String, error: Throwable) { diff --git a/app/src/main/java/io/github/wulkanowy/utils/ListExtension.kt b/app/src/main/java/io/github/wulkanowy/utils/ListExtension.kt new file mode 100644 index 000000000..4374aeb41 --- /dev/null +++ b/app/src/main/java/io/github/wulkanowy/utils/ListExtension.kt @@ -0,0 +1,9 @@ +package io.github.wulkanowy.utils + +infix fun List.uniqueSubtract(other: List): List { + val list = toMutableList() + other.forEach { + list.remove(it) + } + return list.toList() +}