Refactored code to avoid passing lifecycleCoroutineScope as a parameter

This commit is contained in:
Morgan Pretty 2023-01-11 12:42:09 +11:00
parent afdf730eaa
commit f9ff3feb29
5 changed files with 19 additions and 32 deletions

View File

@ -40,6 +40,8 @@ import network.loki.messenger.databinding.ViewVisibleMessageBinding
import nl.komponents.kovenant.ui.successUi import nl.komponents.kovenant.ui.successUi
import org.session.libsession.messaging.MessagingModuleConfiguration import org.session.libsession.messaging.MessagingModuleConfiguration
import org.session.libsession.messaging.contacts.Contact import org.session.libsession.messaging.contacts.Contact
import org.session.libsession.messaging.jobs.AttachmentDownloadJob
import org.session.libsession.messaging.jobs.JobQueue
import org.session.libsession.messaging.mentions.Mention import org.session.libsession.messaging.mentions.Mention
import org.session.libsession.messaging.mentions.MentionsManager import org.session.libsession.messaging.mentions.MentionsManager
import org.session.libsession.messaging.messages.control.DataExtractionNotification import org.session.libsession.messaging.messages.control.DataExtractionNotification
@ -250,6 +252,12 @@ class ConversationActivityV2 : PassphraseRequiredActionBarActivity(), InputBarDe
onDeselect(message, position, it) onDeselect(message, position, it)
} }
}, },
onAttachmentNeedsDownload = { attachmentId, mmsId ->
// Start download (on IO thread)
lifecycleScope.launch(Dispatchers.IO) {
JobQueue.shared.add(AttachmentDownloadJob(attachmentId, mmsId))
}
},
glide = glide, glide = glide,
lifecycleCoroutineScope = lifecycleScope lifecycleCoroutineScope = lifecycleScope
) )

View File

@ -39,8 +39,9 @@ class ConversationAdapter(
private val onItemSwipeToReply: (MessageRecord, Int) -> Unit, private val onItemSwipeToReply: (MessageRecord, Int) -> Unit,
private val onItemLongPress: (MessageRecord, Int, VisibleMessageView) -> Unit, private val onItemLongPress: (MessageRecord, Int, VisibleMessageView) -> Unit,
private val onDeselect: (MessageRecord, Int) -> Unit, private val onDeselect: (MessageRecord, Int) -> Unit,
private val onAttachmentNeedsDownload: (Long, Long) -> Unit,
private val glide: GlideRequests, private val glide: GlideRequests,
private val lifecycleCoroutineScope: LifecycleCoroutineScope lifecycleCoroutineScope: LifecycleCoroutineScope
) : CursorRecyclerViewAdapter<ViewHolder>(context, cursor) { ) : CursorRecyclerViewAdapter<ViewHolder>(context, cursor) {
private val messageDB by lazy { DatabaseComponent.get(context).mmsSmsDatabase() } private val messageDB by lazy { DatabaseComponent.get(context).mmsSmsDatabase() }
private val contactDB by lazy { DatabaseComponent.get(context).sessionContactDatabase() } private val contactDB by lazy { DatabaseComponent.get(context).sessionContactDatabase() }
@ -128,7 +129,7 @@ class ConversationAdapter(
contact, contact,
senderId, senderId,
visibleMessageViewDelegate, visibleMessageViewDelegate,
lifecycleCoroutineScope onAttachmentNeedsDownload
) )
if (!message.isDeleted) { if (!message.isDeleted) {

View File

@ -11,13 +11,8 @@ import android.widget.FrameLayout
import android.widget.TextView import android.widget.TextView
import androidx.core.view.children import androidx.core.view.children
import androidx.core.view.isVisible import androidx.core.view.isVisible
import androidx.lifecycle.LifecycleCoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import network.loki.messenger.R import network.loki.messenger.R
import network.loki.messenger.databinding.AlbumThumbnailViewBinding import network.loki.messenger.databinding.AlbumThumbnailViewBinding
import org.session.libsession.messaging.jobs.AttachmentDownloadJob
import org.session.libsession.messaging.jobs.JobQueue
import org.session.libsession.messaging.sending_receiving.attachments.AttachmentTransferProgress import org.session.libsession.messaging.sending_receiving.attachments.AttachmentTransferProgress
import org.session.libsession.messaging.sending_receiving.attachments.DatabaseAttachment import org.session.libsession.messaging.sending_receiving.attachments.DatabaseAttachment
import org.session.libsession.utilities.recipients.Recipient import org.session.libsession.utilities.recipients.Recipient
@ -66,7 +61,7 @@ class AlbumThumbnailView : FrameLayout {
// region Interaction // region Interaction
fun calculateHitObject(event: MotionEvent, mms: MmsMessageRecord, threadRecipient: Recipient, lifecycleCoroutineScope: LifecycleCoroutineScope) { fun calculateHitObject(event: MotionEvent, mms: MmsMessageRecord, threadRecipient: Recipient, onAttachmentNeedsDownload: (Long, Long) -> Unit) {
val rawXInt = event.rawX.toInt() val rawXInt = event.rawX.toInt()
val rawYInt = event.rawY.toInt() val rawYInt = event.rawY.toInt()
val eventRect = Rect(rawXInt, rawYInt, rawXInt, rawYInt) val eventRect = Rect(rawXInt, rawYInt, rawXInt, rawYInt)
@ -81,12 +76,7 @@ class AlbumThumbnailView : FrameLayout {
if (slide.transferState == AttachmentTransferProgress.TRANSFER_PROGRESS_FAILED) { if (slide.transferState == AttachmentTransferProgress.TRANSFER_PROGRESS_FAILED) {
// Restart download here (on IO thread) // Restart download here (on IO thread)
(slide.asAttachment() as? DatabaseAttachment)?.let { attachment -> (slide.asAttachment() as? DatabaseAttachment)?.let { attachment ->
val attachmentId = attachment.attachmentId.rowId onAttachmentNeedsDownload(attachment.attachmentId.rowId, mms.getId())
// Start download (on IO thread)
lifecycleCoroutineScope.launch(Dispatchers.IO) {
JobQueue.shared.add(AttachmentDownloadJob(attachmentId, mms.getId()))
}
} }
} }
if (slide.isInProgress) return if (slide.isInProgress) return

View File

@ -23,15 +23,10 @@ import androidx.core.graphics.BlendModeCompat
import androidx.core.text.getSpans import androidx.core.text.getSpans
import androidx.core.text.toSpannable import androidx.core.text.toSpannable
import androidx.core.view.isVisible import androidx.core.view.isVisible
import androidx.lifecycle.LifecycleCoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import network.loki.messenger.R import network.loki.messenger.R
import network.loki.messenger.databinding.ViewVisibleMessageContentBinding import network.loki.messenger.databinding.ViewVisibleMessageContentBinding
import okhttp3.HttpUrl import okhttp3.HttpUrl
import org.session.libsession.messaging.MessagingModuleConfiguration import org.session.libsession.messaging.MessagingModuleConfiguration
import org.session.libsession.messaging.jobs.AttachmentDownloadJob
import org.session.libsession.messaging.jobs.JobQueue
import org.session.libsession.messaging.sending_receiving.attachments.AttachmentTransferProgress import org.session.libsession.messaging.sending_receiving.attachments.AttachmentTransferProgress
import org.session.libsession.messaging.sending_receiving.attachments.DatabaseAttachment import org.session.libsession.messaging.sending_receiving.attachments.DatabaseAttachment
import org.session.libsession.utilities.getColorFromAttr import org.session.libsession.utilities.getColorFromAttr
@ -76,7 +71,7 @@ class VisibleMessageContentView : LinearLayout {
thread: Recipient, thread: Recipient,
searchQuery: String?, searchQuery: String?,
contactIsTrusted: Boolean, contactIsTrusted: Boolean,
lifecycleCoroutineScope: LifecycleCoroutineScope onAttachmentNeedsDownload: (Long, Long) -> Unit
) { ) {
// Background // Background
val background = getBackground(message.isOutgoing) val background = getBackground(message.isOutgoing)
@ -152,10 +147,7 @@ class VisibleMessageContentView : LinearLayout {
val attachmentId = dbAttachment.attachmentId.rowId val attachmentId = dbAttachment.attachmentId.rowId
if (attach.transferState == AttachmentTransferProgress.TRANSFER_PROGRESS_PENDING if (attach.transferState == AttachmentTransferProgress.TRANSFER_PROGRESS_PENDING
&& MessagingModuleConfiguration.shared.storage.getAttachmentUploadJob(attachmentId) == null) { && MessagingModuleConfiguration.shared.storage.getAttachmentUploadJob(attachmentId) == null) {
// Start download (on IO thread) onAttachmentNeedsDownload(attachmentId, dbAttachment.mmsId)
lifecycleCoroutineScope.launch(Dispatchers.IO) {
JobQueue.shared.add(AttachmentDownloadJob(attachmentId, dbAttachment.mmsId))
}
} }
} }
message.linkPreviews.forEach { preview -> message.linkPreviews.forEach { preview ->
@ -163,10 +155,7 @@ class VisibleMessageContentView : LinearLayout {
val attachmentId = previewThumbnail.attachmentId.rowId val attachmentId = previewThumbnail.attachmentId.rowId
if (previewThumbnail.transferState == AttachmentTransferProgress.TRANSFER_PROGRESS_PENDING if (previewThumbnail.transferState == AttachmentTransferProgress.TRANSFER_PROGRESS_PENDING
&& MessagingModuleConfiguration.shared.storage.getAttachmentUploadJob(attachmentId) == null) { && MessagingModuleConfiguration.shared.storage.getAttachmentUploadJob(attachmentId) == null) {
// Start download (on IO thread) onAttachmentNeedsDownload(attachmentId, previewThumbnail.mmsId)
lifecycleCoroutineScope.launch(Dispatchers.IO) {
JobQueue.shared.add(AttachmentDownloadJob(attachmentId, previewThumbnail.mmsId))
}
} }
} }
} }
@ -221,7 +210,7 @@ class VisibleMessageContentView : LinearLayout {
layoutParams.horizontalBias = if (message.isOutgoing) 1f else 0f layoutParams.horizontalBias = if (message.isOutgoing) 1f else 0f
binding.albumThumbnailView.layoutParams = layoutParams binding.albumThumbnailView.layoutParams = layoutParams
onContentClick.add { event -> onContentClick.add { event ->
binding.albumThumbnailView.calculateHitObject(event, message, thread, lifecycleCoroutineScope) binding.albumThumbnailView.calculateHitObject(event, message, thread, onAttachmentNeedsDownload)
} }
} else { } else {
hideBody = true hideBody = true

View File

@ -20,7 +20,6 @@ import androidx.core.os.bundleOf
import androidx.core.view.isInvisible import androidx.core.view.isInvisible
import androidx.core.view.isVisible import androidx.core.view.isVisible
import androidx.core.view.marginBottom import androidx.core.view.marginBottom
import androidx.lifecycle.LifecycleCoroutineScope
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import network.loki.messenger.R import network.loki.messenger.R
import network.loki.messenger.databinding.ViewVisibleMessageBinding import network.loki.messenger.databinding.ViewVisibleMessageBinding
@ -123,7 +122,7 @@ class VisibleMessageView : LinearLayout {
contact: Contact?, contact: Contact?,
senderSessionID: String, senderSessionID: String,
delegate: VisibleMessageViewDelegate?, delegate: VisibleMessageViewDelegate?,
lifecycleCoroutineScope: LifecycleCoroutineScope onAttachmentNeedsDownload: (Long, Long) -> Unit
) { ) {
val threadID = message.threadId val threadID = message.threadId
val thread = threadDb.getRecipientForThreadId(threadID) ?: return val thread = threadDb.getRecipientForThreadId(threadID) ?: return
@ -238,7 +237,7 @@ class VisibleMessageView : LinearLayout {
thread, thread,
searchQuery, searchQuery,
message.isOutgoing || isGroupThread || (contact?.isTrusted ?: false), message.isOutgoing || isGroupThread || (contact?.isTrusted ?: false),
lifecycleCoroutineScope onAttachmentNeedsDownload
) )
binding.messageContentView.delegate = delegate binding.messageContentView.delegate = delegate
onDoubleTap = { binding.messageContentView.onContentDoubleTap?.invoke() } onDoubleTap = { binding.messageContentView.onContentDoubleTap?.invoke() }