Avoid null type

This commit is contained in:
Benoit Marty 2020-09-02 12:37:06 +02:00
parent 7c33bf2742
commit 93cb6bd26e
2 changed files with 17 additions and 16 deletions

View file

@ -110,13 +110,13 @@ interface SendService {
* Schedule this message to be resent * Schedule this message to be resent
* @param localEcho the unsent local echo * @param localEcho the unsent local echo
*/ */
fun resendTextMessage(localEcho: TimelineEvent): Cancelable? fun resendTextMessage(localEcho: TimelineEvent): Cancelable
/** /**
* Schedule this message to be resent * Schedule this message to be resent
* @param localEcho the unsent local echo * @param localEcho the unsent local echo
*/ */
fun resendMediaMessage(localEcho: TimelineEvent): Cancelable? fun resendMediaMessage(localEcho: TimelineEvent): Cancelable
/** /**
* Remove this failed message from the timeline * Remove this failed message from the timeline

View file

@ -45,6 +45,7 @@ import org.matrix.android.sdk.api.session.room.timeline.TimelineEvent
import org.matrix.android.sdk.api.util.Cancelable import org.matrix.android.sdk.api.util.Cancelable
import org.matrix.android.sdk.api.util.CancelableBag import org.matrix.android.sdk.api.util.CancelableBag
import org.matrix.android.sdk.api.util.JsonDict import org.matrix.android.sdk.api.util.JsonDict
import org.matrix.android.sdk.api.util.NoOpCancellable
import org.matrix.android.sdk.internal.di.SessionId import org.matrix.android.sdk.internal.di.SessionId
import org.matrix.android.sdk.internal.di.WorkManagerProvider import org.matrix.android.sdk.internal.di.WorkManagerProvider
import org.matrix.android.sdk.internal.session.content.UploadContentWorker import org.matrix.android.sdk.internal.session.content.UploadContentWorker
@ -137,28 +138,28 @@ internal class DefaultSendService @AssistedInject constructor(
.let { timelineSendEventWorkCommon.postWork(roomId, it) } .let { timelineSendEventWorkCommon.postWork(roomId, it) }
} }
override fun resendTextMessage(localEcho: TimelineEvent): Cancelable? { override fun resendTextMessage(localEcho: TimelineEvent): Cancelable {
if (localEcho.root.isTextMessage() && localEcho.root.sendState.hasFailed()) { if (localEcho.root.isTextMessage() && localEcho.root.sendState.hasFailed()) {
localEchoRepository.updateSendState(localEcho.eventId, SendState.UNSENT) localEchoRepository.updateSendState(localEcho.eventId, SendState.UNSENT)
return sendEvent(localEcho.root) return sendEvent(localEcho.root)
} }
return null return NoOpCancellable
} }
override fun resendMediaMessage(localEcho: TimelineEvent): Cancelable? { override fun resendMediaMessage(localEcho: TimelineEvent): Cancelable {
if (localEcho.root.sendState.hasFailed()) { if (localEcho.root.sendState.hasFailed()) {
val clearContent = localEcho.root.getClearContent() val clearContent = localEcho.root.getClearContent()
val messageContent = clearContent?.toModel<MessageContent>() as? MessageWithAttachmentContent ?: return null val messageContent = clearContent?.toModel<MessageContent>() as? MessageWithAttachmentContent ?: return NoOpCancellable
val url = messageContent.getFileUrl() ?: return null val url = messageContent.getFileUrl() ?: return NoOpCancellable
if (url.startsWith("mxc://")) { if (url.startsWith("mxc://")) {
// We need to resend only the message as the attachment is ok // We need to resend only the message as the attachment is ok
localEchoRepository.updateSendState(localEcho.eventId, SendState.UNSENT) localEchoRepository.updateSendState(localEcho.eventId, SendState.UNSENT)
return sendEvent(localEcho.root) return sendEvent(localEcho.root)
} }
// we need to resend the media
when (messageContent) { // we need to resend the media
return when (messageContent) {
is MessageImageContent -> { is MessageImageContent -> {
// The image has not yet been sent // The image has not yet been sent
val attachmentData = ContentAttachmentData( val attachmentData = ContentAttachmentData(
@ -171,7 +172,7 @@ internal class DefaultSendService @AssistedInject constructor(
type = ContentAttachmentData.Type.IMAGE type = ContentAttachmentData.Type.IMAGE
) )
localEchoRepository.updateSendState(localEcho.eventId, SendState.UNSENT) localEchoRepository.updateSendState(localEcho.eventId, SendState.UNSENT)
return internalSendMedia(listOf(localEcho.root), attachmentData, true) internalSendMedia(listOf(localEcho.root), attachmentData, true)
} }
is MessageVideoContent -> { is MessageVideoContent -> {
val attachmentData = ContentAttachmentData( val attachmentData = ContentAttachmentData(
@ -185,9 +186,9 @@ internal class DefaultSendService @AssistedInject constructor(
type = ContentAttachmentData.Type.VIDEO type = ContentAttachmentData.Type.VIDEO
) )
localEchoRepository.updateSendState(localEcho.eventId, SendState.UNSENT) localEchoRepository.updateSendState(localEcho.eventId, SendState.UNSENT)
return internalSendMedia(listOf(localEcho.root), attachmentData, true) internalSendMedia(listOf(localEcho.root), attachmentData, true)
} }
is MessageFileContent -> { is MessageFileContent -> {
val attachmentData = ContentAttachmentData( val attachmentData = ContentAttachmentData(
size = messageContent.info!!.size, size = messageContent.info!!.size,
mimeType = messageContent.info.mimeType!!, mimeType = messageContent.info.mimeType!!,
@ -196,7 +197,7 @@ internal class DefaultSendService @AssistedInject constructor(
type = ContentAttachmentData.Type.FILE type = ContentAttachmentData.Type.FILE
) )
localEchoRepository.updateSendState(localEcho.eventId, SendState.UNSENT) localEchoRepository.updateSendState(localEcho.eventId, SendState.UNSENT)
return internalSendMedia(listOf(localEcho.root), attachmentData, true) internalSendMedia(listOf(localEcho.root), attachmentData, true)
} }
is MessageAudioContent -> { is MessageAudioContent -> {
val attachmentData = ContentAttachmentData( val attachmentData = ContentAttachmentData(
@ -208,12 +209,12 @@ internal class DefaultSendService @AssistedInject constructor(
type = ContentAttachmentData.Type.AUDIO type = ContentAttachmentData.Type.AUDIO
) )
localEchoRepository.updateSendState(localEcho.eventId, SendState.UNSENT) localEchoRepository.updateSendState(localEcho.eventId, SendState.UNSENT)
return internalSendMedia(listOf(localEcho.root), attachmentData, true) internalSendMedia(listOf(localEcho.root), attachmentData, true)
} }
else -> NoOpCancellable
} }
return null
} }
return null return NoOpCancellable
} }
override fun deleteFailedEcho(localEcho: TimelineEvent) { override fun deleteFailedEcho(localEcho: TimelineEvent) {