feat: add display size for attachments util

This commit is contained in:
0x330a
2022-11-11 10:39:10 +11:00
parent 00f0efb808
commit 5860c0b961
3 changed files with 30 additions and 8 deletions

View File

@@ -11,17 +11,18 @@ import network.loki.messenger.R
import network.loki.messenger.databinding.DialogDownloadBinding
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.sending_receiving.attachments.DatabaseAttachment
import org.session.libsession.utilities.recipients.Recipient
import org.thoughtcrime.securesms.conversation.v2.utilities.BaseDialog
import org.thoughtcrime.securesms.database.SessionContactDatabase
import org.thoughtcrime.securesms.util.displaySize
import javax.inject.Inject
/** Shown when receiving media from a contact for the first time, to confirm that
* they are to be trusted and files sent by them are to be downloaded. */
@AndroidEntryPoint
class DownloadDialog(private val recipient: Recipient,
private val
private val databaseAttachment: DatabaseAttachment
) : BaseDialog() {
@Inject lateinit var contactDB: SessionContactDatabase
@@ -33,18 +34,20 @@ class DownloadDialog(private val recipient: Recipient,
val name = contact?.displayName(Contact.ContactContext.REGULAR) ?: sessionID
val title = resources.getString(R.string.dialog_download_title, name)
binding.downloadTitleTextView.text = title
val explanation = resources.getString(R.string.dialog_download_explanation, name)
val displaySize = databaseAttachment.displaySize()
val explanation = resources.getString(R.string.dialog_download_explanation, "$name ($displaySize)")
val spannable = SpannableStringBuilder(explanation)
val startIndex = explanation.indexOf(name)
spannable.setSpan(StyleSpan(Typeface.BOLD), startIndex, startIndex + name.count(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
binding.downloadExplanationTextView.text = spannable
binding.cancelButton.setOnClickListener { dismiss() }
binding.downloadButton.setOnClickListener { trust() }
binding.downloadButton.setOnClickListener { download() }
builder.setView(binding.root)
}
private fun trust() {
JobQueue.shared.resumePendingJobs(AttachmentDownloadJob.KEY)
private fun download() {
// TODO: add attachment download job trigger with attachmentID and databaseMessageID
val downloadJob = AttachmentDownloadJob()
dismiss()
}
}

View File

@@ -0,0 +1,18 @@
package org.thoughtcrime.securesms.util
import org.session.libsession.messaging.sending_receiving.attachments.Attachment
private const val ZERO_SIZE = "0.00"
private const val KILO_SIZE = 1024
fun Attachment.displaySize(): String {
val kbSize = size / KILO_SIZE
val needsMb = kbSize > KILO_SIZE
val sizeText = "%.2f".format(if (needsMb) kbSize / KILO_SIZE else kbSize)
return when {
sizeText == ZERO_SIZE -> "0.01"
sizeText.endsWith(".00") -> sizeText.takeWhile { it != '.' }
else -> sizeText
}
}

View File

@@ -100,8 +100,9 @@ class AttachmentDownloadJob(val attachmentID: Long, val databaseMessageID: Long)
return
}
if (!threadRecipient.isGroupRecipient && (!threadRecipient.autoDownloadAttachments && storage.getUserPublicKey() != sender)) {
// if we aren't receiving a group message, a message from ourselves (self-send) and the contact sending is not trusted:
// do not continue, but do not fail
// if we aren't receiving a group message, a message from ourselves (self-send) and the thread does not auto-download:
// do not continue, but do not fail (it will be restarted later)
// TODO: probably remove this logic and just re-trigger based on attachment ID in future
return
}