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 org.session.libsession.messaging.MessagingModuleConfiguration
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.MentionsManager
import org.session.libsession.messaging.messages.control.DataExtractionNotification
@ -250,6 +252,12 @@ class ConversationActivityV2 : PassphraseRequiredActionBarActivity(), InputBarDe
onDeselect(message, position, it)
}
},
onAttachmentNeedsDownload = { attachmentId, mmsId ->
// Start download (on IO thread)
lifecycleScope.launch(Dispatchers.IO) {
JobQueue.shared.add(AttachmentDownloadJob(attachmentId, mmsId))
}
},
glide = glide,
lifecycleCoroutineScope = lifecycleScope
)

View File

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

View File

@ -11,13 +11,8 @@ import android.widget.FrameLayout
import android.widget.TextView
import androidx.core.view.children
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.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.DatabaseAttachment
import org.session.libsession.utilities.recipients.Recipient
@ -66,7 +61,7 @@ class AlbumThumbnailView : FrameLayout {
// 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 rawYInt = event.rawY.toInt()
val eventRect = Rect(rawXInt, rawYInt, rawXInt, rawYInt)
@ -81,12 +76,7 @@ class AlbumThumbnailView : FrameLayout {
if (slide.transferState == AttachmentTransferProgress.TRANSFER_PROGRESS_FAILED) {
// Restart download here (on IO thread)
(slide.asAttachment() as? DatabaseAttachment)?.let { attachment ->
val attachmentId = attachment.attachmentId.rowId
// Start download (on IO thread)
lifecycleCoroutineScope.launch(Dispatchers.IO) {
JobQueue.shared.add(AttachmentDownloadJob(attachmentId, mms.getId()))
}
onAttachmentNeedsDownload(attachment.attachmentId.rowId, mms.getId())
}
}
if (slide.isInProgress) return

View File

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

View File

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