Skip to content

Commit

Permalink
fix: Delete Attachment in Realm when we delete it while writing a mail (
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinBoulongne authored Feb 10, 2025
2 parents e19401e + 73c1fb9 commit c8ed034
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -780,13 +780,22 @@ class NewMessageViewModel @Inject constructor(
if (recipient.isDisplayedAsExternal) appContext.trackExternalEvent("deleteRecipient")
}

fun deleteAttachment(position: Int) = runCatching {
val attachments = attachmentsLiveData.valueOrEmpty().toMutableList()
val attachment = attachments[position]
attachment.getUploadLocalFile()?.delete()
LocalStorageUtils.deleteAttachmentUploadDir(appContext, draftLocalUuid!!, attachment.localUuid)
attachments.removeAt(position)
attachmentsLiveData.value = attachments
fun deleteAttachment(position: Int) = viewModelScope.launch(ioCoroutineContext) {
runCatching {
val attachments = attachmentsLiveData.valueOrEmpty().toMutableList()
val attachment = attachments[position]
attachment.getUploadLocalFile()?.delete()
LocalStorageUtils.deleteAttachmentUploadDir(appContext, draftLocalUuid!!, attachment.localUuid)

mailboxContentRealm().write {
draftController.updateDraft(draftLocalUuid!!, realm = this) {
it.attachments.findSpecificAttachment(attachment)?.let(::delete)
}
}

attachments.removeAt(position)
attachmentsLiveData.postValue(attachments)
}
}

fun updateIsSendingAllowed(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,14 +208,7 @@ object AttachmentExtensions {
}
}

fun List<Attachment>.findSpecificAttachment(attachment: Attachment) = this
.filter { it.localUuid == attachment.localUuid }
.also {
// TODO: If this Sentry never triggers, remove it and replace the
// `filter { … }.also { … }.firstOrNull()` with `singleOrNull { … }`
if (it.count() > 1) Sentry.captureMessage("Found several Attachments with the same localUuid")
}
.firstOrNull()
fun List<Attachment>.findSpecificAttachment(attachment: Attachment) = singleOrNull { it.localUuid == attachment.localUuid }

enum class AttachmentIntentType {
OPEN_WITH,
Expand Down

0 comments on commit c8ed034

Please sign in to comment.