diff --git a/app/src/main/java/io/github/wulkanowy/data/db/migrations/Migration13.kt b/app/src/main/java/io/github/wulkanowy/data/db/migrations/Migration13.kt index 83ab8609..0cf8cd9b 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/migrations/Migration13.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/migrations/Migration13.kt @@ -9,6 +9,7 @@ class Migration13 : Migration(12, 13) { addClassNameToStudents(database, getStudentsIds(database)) updateSemestersTable(database) markAtLeastAndOnlyOneSemesterAtCurrent(database, getStudentsAndClassIds(database)) + clearMessagesTable(database) } private fun addClassNameToStudents(database: SupportSQLiteDatabase, students: List>) { @@ -56,4 +57,8 @@ class Migration13 : Migration(12, 13) { database.execSQL("UPDATE Semesters SET is_current = 1 WHERE id = (SELECT id FROM Semesters WHERE student_id = '$studentId' AND class_id = '$classId' ORDER BY semester_id DESC)") } } + + private fun clearMessagesTable(database: SupportSQLiteDatabase) { + database.execSQL("DELETE FROM Messages") + } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/message/MessageLocal.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/message/MessageLocal.kt index e9ab7297..411cca39 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/message/MessageLocal.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/message/MessageLocal.kt @@ -24,13 +24,13 @@ class MessageLocal @Inject constructor(private val messagesDb: MessagesDao) { } fun getMessage(student: Student, id: Int): Maybe { - return messagesDb.load(student.studentId, id) + return messagesDb.load(student.id.toInt(), id) } fun getMessages(student: Student, folder: MessageFolder): Maybe> { return when (folder) { - TRASHED -> messagesDb.loadDeleted(student.studentId) - else -> messagesDb.loadAll(student.studentId, folder.id) + TRASHED -> messagesDb.loadDeleted(student.id.toInt()) + else -> messagesDb.loadAll(student.id.toInt(), folder.id) }.filter { it.isNotEmpty() } } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/message/MessageRemote.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/message/MessageRemote.kt index 3fb01d30..d50c40f3 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/message/MessageRemote.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/message/MessageRemote.kt @@ -5,6 +5,7 @@ import io.github.wulkanowy.api.messages.Folder import io.github.wulkanowy.api.messages.SentMessage 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.utils.toLocalDateTime import io.reactivex.Single import org.threeten.bp.LocalDateTime.now @@ -16,11 +17,11 @@ import io.github.wulkanowy.api.messages.Recipient as ApiRecipient @Singleton class MessageRemote @Inject constructor(private val api: Api) { - fun getMessages(studentId: Int, folder: MessageFolder): Single> { + fun getMessages(student: Student, folder: MessageFolder): Single> { return api.getMessages(Folder.valueOf(folder.name)).map { messages -> messages.map { Message( - studentId = studentId, + studentId = student.id.toInt(), realId = it.id ?: 0, messageId = it.messageId ?: 0, sender = it.sender.orEmpty(), 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 d319689e..3b4f9582 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 @@ -28,7 +28,7 @@ class MessageRepository @Inject constructor( local.getMessages(student, folder).filter { !forceRefresh } .switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings) .flatMap { - if (it) remote.getMessages(student.studentId, folder) + if (it) remote.getMessages(student, folder) else Single.error(UnknownHostException()) }.flatMap { new -> local.getMessages(student, folder).toSingle(emptyList())