Skip to content

Commit c8ed034

Browse files
fix: Delete Attachment in Realm when we delete it while writing a mail (#2149)
2 parents e19401e + 73c1fb9 commit c8ed034

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

Diff for: app/src/main/java/com/infomaniak/mail/ui/newMessage/NewMessageViewModel.kt

+16-7
Original file line numberDiff line numberDiff line change
@@ -780,13 +780,22 @@ class NewMessageViewModel @Inject constructor(
780780
if (recipient.isDisplayedAsExternal) appContext.trackExternalEvent("deleteRecipient")
781781
}
782782

783-
fun deleteAttachment(position: Int) = runCatching {
784-
val attachments = attachmentsLiveData.valueOrEmpty().toMutableList()
785-
val attachment = attachments[position]
786-
attachment.getUploadLocalFile()?.delete()
787-
LocalStorageUtils.deleteAttachmentUploadDir(appContext, draftLocalUuid!!, attachment.localUuid)
788-
attachments.removeAt(position)
789-
attachmentsLiveData.value = attachments
783+
fun deleteAttachment(position: Int) = viewModelScope.launch(ioCoroutineContext) {
784+
runCatching {
785+
val attachments = attachmentsLiveData.valueOrEmpty().toMutableList()
786+
val attachment = attachments[position]
787+
attachment.getUploadLocalFile()?.delete()
788+
LocalStorageUtils.deleteAttachmentUploadDir(appContext, draftLocalUuid!!, attachment.localUuid)
789+
790+
mailboxContentRealm().write {
791+
draftController.updateDraft(draftLocalUuid!!, realm = this) {
792+
it.attachments.findSpecificAttachment(attachment)?.let(::delete)
793+
}
794+
}
795+
796+
attachments.removeAt(position)
797+
attachmentsLiveData.postValue(attachments)
798+
}
790799
}
791800

792801
fun updateIsSendingAllowed(

Diff for: app/src/main/java/com/infomaniak/mail/utils/extensions/AttachmentExtensions.kt

+1-8
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,7 @@ object AttachmentExtensions {
208208
}
209209
}
210210

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

220213
enum class AttachmentIntentType {
221214
OPEN_WITH,

0 commit comments

Comments
 (0)