mirror of
https://github.com/wulkanowy/wulkanowy.git
synced 2025-01-19 00:06:46 -06:00
Fix crash in flowWithResourceIn() (#967)
This commit is contained in:
parent
0cb65a29ba
commit
73be416807
@ -126,7 +126,7 @@ configurations.all {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
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'
|
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.0.10'
|
||||||
|
|
||||||
|
@ -12,7 +12,7 @@ interface MessagesDao : BaseDao<Message> {
|
|||||||
|
|
||||||
@Transaction
|
@Transaction
|
||||||
@Query("SELECT * FROM Messages WHERE student_id = :studentId AND message_id = :messageId")
|
@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")
|
@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>>
|
fun loadAll(studentId: Int, folder: Int): Flow<List<Message>>
|
||||||
|
@ -36,12 +36,6 @@ data class Message(
|
|||||||
|
|
||||||
var unread: Boolean,
|
var unread: Boolean,
|
||||||
|
|
||||||
@ColumnInfo(name = "unread_by")
|
|
||||||
val unreadBy: Int,
|
|
||||||
|
|
||||||
@ColumnInfo(name = "read_by")
|
|
||||||
val readBy: Int,
|
|
||||||
|
|
||||||
val removed: Boolean,
|
val removed: Boolean,
|
||||||
|
|
||||||
@ColumnInfo(name = "has_attachments")
|
@ColumnInfo(name = "has_attachments")
|
||||||
@ -54,5 +48,11 @@ data class Message(
|
|||||||
@ColumnInfo(name = "is_notified")
|
@ColumnInfo(name = "is_notified")
|
||||||
var isNotified: Boolean = true
|
var isNotified: Boolean = true
|
||||||
|
|
||||||
|
@ColumnInfo(name = "unread_by")
|
||||||
|
var unreadBy: Int = 0
|
||||||
|
|
||||||
|
@ColumnInfo(name = "read_by")
|
||||||
|
var readBy: Int = 0
|
||||||
|
|
||||||
var content: String = ""
|
var content: String = ""
|
||||||
}
|
}
|
||||||
|
@ -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.MessageAttachment
|
||||||
import io.github.wulkanowy.data.db.entities.MessageWithAttachment
|
import io.github.wulkanowy.data.db.entities.MessageWithAttachment
|
||||||
import io.github.wulkanowy.data.db.entities.Student
|
import io.github.wulkanowy.data.db.entities.Student
|
||||||
import io.github.wulkanowy.data.repositories.message.MessageFolder.TRASHED
|
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import javax.inject.Singleton
|
import javax.inject.Singleton
|
||||||
@ -29,7 +28,7 @@ class MessageLocal @Inject constructor(
|
|||||||
messagesDb.deleteAll(messages)
|
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)
|
return messagesDb.loadMessageWithAttachment(student.id.toInt(), message.messageId)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,12 +30,12 @@ class MessageRemote @Inject constructor(private val sdk: Sdk) {
|
|||||||
date = it.date ?: now(),
|
date = it.date ?: now(),
|
||||||
folderId = it.folderId,
|
folderId = it.folderId,
|
||||||
unread = it.unread ?: false,
|
unread = it.unread ?: false,
|
||||||
unreadBy = it.unreadBy ?: 0,
|
|
||||||
readBy = it.readBy ?: 0,
|
|
||||||
removed = it.removed,
|
removed = it.removed,
|
||||||
hasAttachments = it.hasAttachments
|
hasAttachments = it.hasAttachments
|
||||||
).apply {
|
).apply {
|
||||||
content = it.content.orEmpty()
|
content = it.content.orEmpty()
|
||||||
|
unreadBy = it.unreadBy ?: 0
|
||||||
|
readBy = it.readBy ?: 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -34,12 +34,14 @@ class MessageRepository @Inject constructor(
|
|||||||
|
|
||||||
fun getMessage(student: Student, message: Message, markAsRead: Boolean = false) = networkBoundResource(
|
fun getMessage(student: Student, message: Message, markAsRead: Boolean = false) = networkBoundResource(
|
||||||
shouldFetch = {
|
shouldFetch = {
|
||||||
|
checkNotNull(it, { "This message no longer exist!" })
|
||||||
Timber.d("Message content in db empty: ${it.message.content.isEmpty()}")
|
Timber.d("Message content in db empty: ${it.message.content.isEmpty()}")
|
||||||
it.message.unread || it.message.content.isEmpty()
|
it.message.unread || it.message.content.isEmpty()
|
||||||
},
|
},
|
||||||
query = { local.getMessageWithAttachment(student, message) },
|
query = { local.getMessageWithAttachment(student, message) },
|
||||||
fetch = { remote.getMessagesContentDetails(student, it.message, markAsRead) },
|
fetch = { remote.getMessagesContentDetails(student, it!!.message, markAsRead) },
|
||||||
saveFetchResult = { old, (downloadedMessage, attachments) ->
|
saveFetchResult = { old, (downloadedMessage, attachments) ->
|
||||||
|
checkNotNull(old, { "Fetched message no longer exist!" })
|
||||||
local.updateMessages(listOf(old.message.copy(unread = !markAsRead).apply {
|
local.updateMessages(listOf(old.message.copy(unread = !markAsRead).apply {
|
||||||
id = old.message.id
|
id = old.message.id
|
||||||
content = content.ifBlank { downloadedMessage }
|
content = content.ifBlank { downloadedMessage }
|
||||||
|
@ -64,7 +64,8 @@ class MessagePreviewPresenter @Inject constructor(
|
|||||||
when (it.status) {
|
when (it.status) {
|
||||||
Status.LOADING -> Timber.i("Loading message ${message.messageId} preview started")
|
Status.LOADING -> Timber.i("Loading message ${message.messageId} preview started")
|
||||||
Status.SUCCESS -> {
|
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.message = it.data.message
|
||||||
this@MessagePreviewPresenter.attachments = it.data.attachments
|
this@MessagePreviewPresenter.attachments = it.data.attachments
|
||||||
view?.apply {
|
view?.apply {
|
||||||
@ -194,6 +195,7 @@ class MessagePreviewPresenter @Inject constructor(
|
|||||||
view?.run {
|
view?.run {
|
||||||
lastError = error
|
lastError = error
|
||||||
setErrorDetails(message)
|
setErrorDetails(message)
|
||||||
|
showContent(false)
|
||||||
showErrorView(true)
|
showErrorView(true)
|
||||||
setErrorRetryCallback { retryCallback() }
|
setErrorRetryCallback { retryCallback() }
|
||||||
}
|
}
|
||||||
|
@ -80,17 +80,13 @@ fun <T> flowWithResource(block: suspend () -> T) = flow {
|
|||||||
fun <T> flowWithResourceIn(block: suspend () -> Flow<Resource<T>>) = flow {
|
fun <T> flowWithResourceIn(block: suspend () -> Flow<Resource<T>>) = flow {
|
||||||
emit(Resource.loading())
|
emit(Resource.loading())
|
||||||
|
|
||||||
try {
|
block()
|
||||||
block()
|
.catch { emit(Resource.error(it)) }
|
||||||
.catch { emit(Resource.error(it)) }
|
.collect {
|
||||||
.collect {
|
if (it.status != Status.LOADING) { // LOADING is already emitted
|
||||||
if (it.status != Status.LOADING) { // LOADING is already emitted
|
emit(it)
|
||||||
emit(it)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch (e: Throwable) {
|
}
|
||||||
emit(Resource.error<T>(e))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun <T> Flow<Resource<T>>.afterLoading(callback: () -> Unit) = onEach {
|
fun <T> Flow<Resource<T>>.afterLoading(callback: () -> Unit) = onEach {
|
||||||
|
@ -90,10 +90,10 @@ fun getMessageEntity(
|
|||||||
date = now(),
|
date = now(),
|
||||||
folderId = 1,
|
folderId = 1,
|
||||||
unread = unread,
|
unread = unread,
|
||||||
unreadBy = 1,
|
|
||||||
readBy = 1,
|
|
||||||
removed = false,
|
removed = false,
|
||||||
hasAttachments = false
|
hasAttachments = false
|
||||||
).apply {
|
).apply {
|
||||||
this.content = content
|
this.content = content
|
||||||
|
unreadBy = 1
|
||||||
|
readBy = 1
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user