@@ -363,7 +363,7 @@ public open class AttachmentQueue(
363
363
(existingQueueItem.state == AttachmentState .ARCHIVED ) {
364
364
// The attachment is present again. Need to queue it for sync.
365
365
// We might be able to optimize this in future.
366
- if (existingQueueItem.hasSynced == 1 ) {
366
+ if (existingQueueItem.hasSynced) {
367
367
// No remote action required, we can restore the record (avoids deletion).
368
368
attachmentUpdates.add(
369
369
existingQueueItem.copy(state = AttachmentState .SYNCED ),
@@ -389,13 +389,25 @@ public open class AttachmentQueue(
389
389
}
390
390
391
391
/* *
392
- * Archive any items not specified in the watched items except for items pending delete.
392
+ * Archive any items not specified in the watched items.
393
+ * For QUEUED_DELETE or QUEUED_UPLOAD states, archive only if hasSynced is true.
394
+ * For other states, archive if the record is not found in the items.
393
395
*/
394
396
currentAttachments
395
- .filter {
396
- it.state != AttachmentState .QUEUED_DELETE &&
397
- it.state != AttachmentState .QUEUED_UPLOAD &&
398
- null == items.find { update -> update.id == it.id }
397
+ .filter { attachment ->
398
+ val notInWatchedItems =
399
+ items.find { update -> update.id == attachment.id } == null
400
+ if (notInWatchedItems) {
401
+ when (attachment.state) {
402
+ // Archive these record if they have synced
403
+ AttachmentState .QUEUED_DELETE , AttachmentState .QUEUED_UPLOAD -> attachment.hasSynced
404
+ // Other states, such as QUEUED_DOWNLOAD can be archived if they are not present in watched items
405
+ else -> true
406
+ }
407
+ } else {
408
+ // The record is present in watched items, no need to archive it
409
+ false
410
+ }
399
411
}.forEach {
400
412
attachmentUpdates.add(it.copy(state = AttachmentState .ARCHIVED ))
401
413
}
@@ -484,7 +496,10 @@ public open class AttachmentQueue(
484
496
db.writeTransaction { tx ->
485
497
updateHook.invoke(tx, attachment)
486
498
return @writeTransaction attachmentContext.upsertAttachment(
487
- attachment.copy(state = AttachmentState .QUEUED_DELETE ),
499
+ attachment.copy(
500
+ state = AttachmentState .QUEUED_DELETE ,
501
+ hasSynced = false ,
502
+ ),
488
503
tx,
489
504
)
490
505
}
0 commit comments