1
0
mirror of https://github.com/wulkanowy/wulkanowy.git synced 2024-09-19 23:29:08 -05:00

Fix crash in flowWithResourceIn() (#967)

This commit is contained in:
Mikołaj Pich 2020-09-19 00:57:55 +02:00 committed by GitHub
parent 0cb65a29ba
commit 73be416807
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 25 additions and 26 deletions

View File

@ -126,7 +126,7 @@ configurations.all {
}
dependencies {
implementation "io.github.wulkanowy:sdk:0.20.4"
implementation "io.github.wulkanowy:sdk:b652036"
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.0.10'

View File

@ -12,7 +12,7 @@ interface MessagesDao : BaseDao<Message> {
@Transaction
@Query("SELECT * FROM Messages WHERE student_id = :studentId AND message_id = :messageId")
fun loadMessageWithAttachment(studentId: Int, messageId: Int): Flow<MessageWithAttachment>
fun loadMessageWithAttachment(studentId: Int, messageId: Int): Flow<MessageWithAttachment?>
@Query("SELECT * FROM Messages WHERE student_id = :studentId AND folder_id = :folder ORDER BY date DESC")
fun loadAll(studentId: Int, folder: Int): Flow<List<Message>>

View File

@ -36,12 +36,6 @@ data class Message(
var unread: Boolean,
@ColumnInfo(name = "unread_by")
val unreadBy: Int,
@ColumnInfo(name = "read_by")
val readBy: Int,
val removed: Boolean,
@ColumnInfo(name = "has_attachments")
@ -54,5 +48,11 @@ data class Message(
@ColumnInfo(name = "is_notified")
var isNotified: Boolean = true
@ColumnInfo(name = "unread_by")
var unreadBy: Int = 0
@ColumnInfo(name = "read_by")
var readBy: Int = 0
var content: String = ""
}

View File

@ -6,7 +6,6 @@ import io.github.wulkanowy.data.db.entities.Message
import io.github.wulkanowy.data.db.entities.MessageAttachment
import io.github.wulkanowy.data.db.entities.MessageWithAttachment
import io.github.wulkanowy.data.db.entities.Student
import io.github.wulkanowy.data.repositories.message.MessageFolder.TRASHED
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject
import javax.inject.Singleton
@ -29,7 +28,7 @@ class MessageLocal @Inject constructor(
messagesDb.deleteAll(messages)
}
fun getMessageWithAttachment(student: Student, message: Message): Flow<MessageWithAttachment> {
fun getMessageWithAttachment(student: Student, message: Message): Flow<MessageWithAttachment?> {
return messagesDb.loadMessageWithAttachment(student.id.toInt(), message.messageId)
}

View File

@ -30,12 +30,12 @@ class MessageRemote @Inject constructor(private val sdk: Sdk) {
date = it.date ?: now(),
folderId = it.folderId,
unread = it.unread ?: false,
unreadBy = it.unreadBy ?: 0,
readBy = it.readBy ?: 0,
removed = it.removed,
hasAttachments = it.hasAttachments
).apply {
content = it.content.orEmpty()
unreadBy = it.unreadBy ?: 0
readBy = it.readBy ?: 0
}
}
}

View File

@ -34,12 +34,14 @@ class MessageRepository @Inject constructor(
fun getMessage(student: Student, message: Message, markAsRead: Boolean = false) = networkBoundResource(
shouldFetch = {
checkNotNull(it, { "This message no longer exist!" })
Timber.d("Message content in db empty: ${it.message.content.isEmpty()}")
it.message.unread || it.message.content.isEmpty()
},
query = { local.getMessageWithAttachment(student, message) },
fetch = { remote.getMessagesContentDetails(student, it.message, markAsRead) },
fetch = { remote.getMessagesContentDetails(student, it!!.message, markAsRead) },
saveFetchResult = { old, (downloadedMessage, attachments) ->
checkNotNull(old, { "Fetched message no longer exist!" })
local.updateMessages(listOf(old.message.copy(unread = !markAsRead).apply {
id = old.message.id
content = content.ifBlank { downloadedMessage }

View File

@ -64,7 +64,8 @@ class MessagePreviewPresenter @Inject constructor(
when (it.status) {
Status.LOADING -> Timber.i("Loading message ${message.messageId} preview started")
Status.SUCCESS -> {
Timber.i("Loading message ${it.data!!.message.messageId} preview result: Success ")
Timber.i("Loading message ${message.messageId} preview result: Success ")
checkNotNull(it.data, { "Can't find message in local db! Probably no longer exist in this folder" })
this@MessagePreviewPresenter.message = it.data.message
this@MessagePreviewPresenter.attachments = it.data.attachments
view?.apply {
@ -194,6 +195,7 @@ class MessagePreviewPresenter @Inject constructor(
view?.run {
lastError = error
setErrorDetails(message)
showContent(false)
showErrorView(true)
setErrorRetryCallback { retryCallback() }
}

View File

@ -80,17 +80,13 @@ fun <T> flowWithResource(block: suspend () -> T) = flow {
fun <T> flowWithResourceIn(block: suspend () -> Flow<Resource<T>>) = flow {
emit(Resource.loading())
try {
block()
.catch { emit(Resource.error(it)) }
.collect {
if (it.status != Status.LOADING) { // LOADING is already emitted
emit(it)
}
block()
.catch { emit(Resource.error(it)) }
.collect {
if (it.status != Status.LOADING) { // LOADING is already emitted
emit(it)
}
} catch (e: Throwable) {
emit(Resource.error<T>(e))
}
}
}
fun <T> Flow<Resource<T>>.afterLoading(callback: () -> Unit) = onEach {

View File

@ -90,10 +90,10 @@ fun getMessageEntity(
date = now(),
folderId = 1,
unread = unread,
unreadBy = 1,
readBy = 1,
removed = false,
hasAttachments = false
).apply {
this.content = content
unreadBy = 1
readBy = 1
}