From d6b14402175bb3307f51874628c2b89dc7783b58 Mon Sep 17 00:00:00 2001 From: andrew Date: Tue, 4 Jul 2023 15:27:00 +0930 Subject: [PATCH] Refactor MessageDetailsViewModel --- .../conversation/v2/MessageDetailActivity.kt | 92 ------------------- .../v2/MessageDetailsViewModel.kt | 87 ++++++++++++++++++ 2 files changed, 87 insertions(+), 92 deletions(-) create mode 100644 app/src/main/java/org/thoughtcrime/securesms/conversation/v2/MessageDetailsViewModel.kt diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/MessageDetailActivity.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/MessageDetailActivity.kt index b8abb7152a..7a0c24e15f 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/MessageDetailActivity.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/MessageDetailActivity.kt @@ -39,23 +39,15 @@ import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import androidx.compose.ui.viewinterop.AndroidView -import androidx.lifecycle.LiveData -import androidx.lifecycle.MutableLiveData -import androidx.lifecycle.ViewModel import com.bumptech.glide.integration.compose.ExperimentalGlideComposeApi import com.bumptech.glide.integration.compose.GlideImage import dagger.hilt.android.AndroidEntryPoint import network.loki.messenger.R -import org.session.libsession.messaging.sending_receiving.attachments.DatabaseAttachment -import org.session.libsession.utilities.Util import org.session.libsession.utilities.recipients.Recipient import org.thoughtcrime.securesms.MediaPreviewActivity import org.thoughtcrime.securesms.PassphraseRequiredActionBarActivity import org.thoughtcrime.securesms.components.ProfilePictureView -import org.thoughtcrime.securesms.database.AttachmentDatabase import org.thoughtcrime.securesms.database.Storage -import org.thoughtcrime.securesms.database.model.MessageRecord -import org.thoughtcrime.securesms.database.model.MmsMessageRecord import org.thoughtcrime.securesms.dependencies.DatabaseComponent import org.thoughtcrime.securesms.mms.Slide import org.thoughtcrime.securesms.ui.AppTheme @@ -70,8 +62,6 @@ import org.thoughtcrime.securesms.ui.ItemButton import org.thoughtcrime.securesms.ui.blackAlpha40 import org.thoughtcrime.securesms.ui.colorDestructive import org.thoughtcrime.securesms.ui.destructiveButtonColors -import java.util.* -import java.util.concurrent.TimeUnit import javax.inject.Inject @@ -94,70 +84,6 @@ class MessageDetailActivity : PassphraseRequiredActionBarActivity() { val viewModel = MessageDetailsViewModel() - class MessageDetailsViewModel : ViewModel() { - @Inject - lateinit var attachmentDb: AttachmentDatabase - - fun setMessageRecord(value: MessageRecord?, error: String?) { - val mmsRecord = value as? MmsMessageRecord - - val slides: List = mmsRecord?.slideDeck?.thumbnailSlides?.toList() ?: emptyList() - - _details.value = value?.run { - MessageDetails( - mmsRecord = mmsRecord, - attachments = slides.map { slide -> - val duration = slide.takeIf { it.hasAudio() } - ?.let { it.asAttachment() as? DatabaseAttachment } - ?.let { attachment -> - attachmentDb.getAttachmentAudioExtras(attachment.attachmentId) - ?.let { audioExtras -> - audioExtras.durationMs.takeIf { it > 0 }?.let { - String.format( - "%01d:%02d", - TimeUnit.MILLISECONDS.toMinutes(it), - TimeUnit.MILLISECONDS.toSeconds(it) % 60 - ) - } - } - } - - val details = slide.run { - listOfNotNull( - fileName.orNull()?.let { TitledText("File Id:", it) }, - TitledText("File Type:", asAttachment().contentType), - TitledText("File Size:", Util.getPrettyFileSize(fileSize)), - if (slide.hasImage()) { - TitledText( - "Resolution:", - slide.asAttachment().run { "${width}x$height" }) - } else null, - duration?.let { TitledText("Duration:", it) }, - ) - } - Attachment(slide, details) - }, - sent = dateSent.let(::Date).toString().let { TitledText("Sent:", it) }, - received = dateReceived.let(::Date).toString() - .let { TitledText("Received:", it) }, - error = error?.let { TitledText("Error:", it) }, - senderInfo = individualRecipient.run { - name?.let { - TitledText( - it, - address.serialize() - ) - } - }, - sender = individualRecipient - ) - } - } - - private var _details = MutableLiveData(MessageDetails()) - val details: LiveData = _details - } - override fun onCreate(savedInstanceState: Bundle?, ready: Boolean) { super.onCreate(savedInstanceState, ready) @@ -204,24 +130,6 @@ class MessageDetailActivity : PassphraseRequiredActionBarActivity() { finish() } - data class TitledText(val title: String, val value: String) - - data class MessageDetails( - val attachments: List = emptyList(), - val mmsRecord: MmsMessageRecord? = null, - - val sent: TitledText? = null, - val received: TitledText? = null, - val error: TitledText? = null, - val senderInfo: TitledText? = null, - val sender: Recipient? = null - ) - - data class Attachment( - val slide: Slide, - val fileDetails: List - ) - @Preview @Composable fun PreviewMessageDetails() { diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/MessageDetailsViewModel.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/MessageDetailsViewModel.kt new file mode 100644 index 0000000000..80693a167c --- /dev/null +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/MessageDetailsViewModel.kt @@ -0,0 +1,87 @@ +package org.thoughtcrime.securesms.conversation.v2 + +import androidx.lifecycle.LiveData +import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.ViewModel +import org.session.libsession.messaging.sending_receiving.attachments.DatabaseAttachment +import org.session.libsession.utilities.Util +import org.session.libsession.utilities.recipients.Recipient +import org.thoughtcrime.securesms.database.AttachmentDatabase +import org.thoughtcrime.securesms.database.model.MessageRecord +import org.thoughtcrime.securesms.database.model.MmsMessageRecord +import org.thoughtcrime.securesms.mms.Slide +import java.util.* +import java.util.concurrent.TimeUnit +import javax.inject.Inject + +data class TitledText(val title: String, val value: String) + +data class MessageDetails( + val attachments: List = emptyList(), + val mmsRecord: MmsMessageRecord? = null, + val sent: TitledText? = null, + val received: TitledText? = null, + val error: TitledText? = null, + val senderInfo: TitledText? = null, + val sender: Recipient? = null +) + +data class Attachment( + val slide: Slide, + val fileDetails: List +) + +class MessageDetailsViewModel : ViewModel() { + @Inject + lateinit var attachmentDb: AttachmentDatabase + + fun setMessageRecord(value: MessageRecord?, error: String?) { + val mmsRecord = value as? MmsMessageRecord + + val slides: List = mmsRecord?.slideDeck?.thumbnailSlides?.toList() ?: emptyList() + + _details.value = value?.run { + MessageDetails( + mmsRecord = mmsRecord, + attachments = slides.map { Attachment(it, it.details) }, + sent = dateSent.let(::Date).toString().let { TitledText("Sent:", it) }, + received = dateReceived.let(::Date).toString().let { TitledText("Received:", it) }, + error = error?.let { TitledText("Error:", it) }, + senderInfo = individualRecipient.run { + name?.let { TitledText(it, address.serialize()) } + }, + sender = individualRecipient + ) + } + } + + private var _details = MutableLiveData(MessageDetails()) + val details: LiveData = _details + + private val Slide.details: List + get() = listOfNotNull( + fileName.orNull()?.let { TitledText("File Id:", it) }, + TitledText("File Type:", asAttachment().contentType), + TitledText("File Size:", Util.getPrettyFileSize(fileSize)), + takeIf { it.hasImage() } + .run { asAttachment().run { "${width}x$height" } } + .let { TitledText("Resolution:", it) }, + attachmentDb.duration(this)?.let { TitledText("Duration:", it) }, + ) + + private fun AttachmentDatabase.duration(slide: Slide): String? = + slide.takeIf { it.hasAudio() } + ?.run { asAttachment() as? DatabaseAttachment } + ?.run { + getAttachmentAudioExtras(attachmentId) + ?.let { audioExtras -> + audioExtras.durationMs.takeIf { it > 0 }?.let { + String.format( + "%01d:%02d", + TimeUnit.MILLISECONDS.toMinutes(it), + TimeUnit.MILLISECONDS.toSeconds(it) % 60 + ) + } + } + } +} \ No newline at end of file