Skip to content

Commit 5206c16

Browse files
code cleanup. Update archiving logid
1 parent f8e3d61 commit 5206c16

File tree

3 files changed

+59
-38
lines changed

3 files changed

+59
-38
lines changed

Sources/PowerSync/attachments/Attachment.swift

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public struct Attachment {
4848

4949
/// Specifies if the attachment has been synced locally before.
5050
/// This is particularly useful for restoring archived attachments in edge cases.
51-
public let hasSynced: Int?
51+
public let hasSynced: Bool?
5252

5353
/// Extra attachment metadata
5454
public let metaData: String?
@@ -59,7 +59,7 @@ public struct Attachment {
5959
filename: String,
6060
state: AttachmentState,
6161
timestamp: Int = 0,
62-
hasSynced: Int? = 0,
62+
hasSynced: Bool? = false,
6363
localUri: String? = nil,
6464
mediaType: String? = nil,
6565
size: Int64? = nil,
@@ -89,27 +89,40 @@ public struct Attachment {
8989
/// - metaData: Optional new metadata.
9090
/// - Returns: A new `Attachment` with updated values.
9191
func with(
92-
filename _: String? = nil,
92+
filename: String? = nil,
9393
state: AttachmentState? = nil,
94-
timestamp _: Int = 0,
95-
hasSynced: Int?? = 0,
94+
timestamp : Int = 0,
95+
hasSynced: Bool? = nil,
9696
localUri: String?? = .none,
9797
mediaType: String?? = .none,
9898
size: Int64?? = .none,
9999
metaData: String?? = .none
100100
) -> Attachment {
101101
return Attachment(
102102
id: id,
103-
filename: filename ?? filename,
104-
state: state.map { $0 } ?? self.state,
105-
timestamp: timestamp > 0 ? timestamp : timestamp,
106-
hasSynced: hasSynced.map { $0 } ?? self.hasSynced,
107-
localUri: localUri.map { $0 } ?? self.localUri,
108-
mediaType: mediaType.map { $0 } ?? self.mediaType,
109-
size: size.map { $0 } ?? self.size,
110-
metaData: metaData.map { $0 } ?? self.metaData
103+
filename: filename ?? self.filename,
104+
state: state ?? self.state,
105+
timestamp: timestamp > 0 ? timestamp : self.timestamp,
106+
hasSynced: hasSynced ?? self.hasSynced,
107+
localUri: resolveOverride(localUri, current: self.localUri),
108+
mediaType: resolveOverride(mediaType, current: self.mediaType),
109+
size: resolveOverride(size, current: self.size),
110+
metaData: resolveOverride(metaData, current: self.metaData)
111111
)
112112
}
113+
114+
/// Resolves double optionals
115+
/// if a non nil value is provided: the override will be used
116+
/// if .some(nil) is provided: The value will be set to nil
117+
/// // if nil is provided: the current value will be preserved
118+
private func resolveOverride<T>(_ override: T??, current: T?) -> T? {
119+
if let value = override {
120+
return value // could be nil (explicit clear) or a value
121+
} else {
122+
return current // not provided, use current
123+
}
124+
}
125+
113126

114127
/// Constructs an `Attachment` from a `SqlCursor`.
115128
///
@@ -122,7 +135,7 @@ public struct Attachment {
122135
filename: cursor.getString(name: "filename"),
123136
state: AttachmentState.from(cursor.getLong(name: "state")),
124137
timestamp: cursor.getLong(name: "timestamp"),
125-
hasSynced: cursor.getLongOptional(name: "has_synced"),
138+
hasSynced: cursor.getLong(name: "has_synced") > 0,
126139
localUri: cursor.getStringOptional(name: "local_uri"),
127140
mediaType: cursor.getStringOptional(name: "media_type"),
128141
size: cursor.getLongOptional(name: "size")?.int64Value,

Sources/PowerSync/attachments/AttachmentQueue.swift

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -229,16 +229,14 @@ public class AttachmentQueue {
229229
var attachmentUpdates = [Attachment]()
230230

231231
for item in items {
232-
let existingQueueItem = currentAttachments.first { $0.id == item.id }
233-
234-
if existingQueueItem == nil {
232+
guard let existingQueueItem = currentAttachments.first(where: { $0.id == item.id }) else {
233+
// Item is not present in the queue
234+
235235
if !self.downloadAttachments {
236236
continue
237237
}
238+
238239
// This item should be added to the queue
239-
// This item is assumed to be coming from an upstream sync
240-
// Locally created new items should be persisted using saveFile before
241-
// this point.
242240
let filename = self.resolveNewAttachmentFilename(
243241
attachmentId: item.id,
244242
fileExtension: item.fileExtension
@@ -248,43 +246,53 @@ public class AttachmentQueue {
248246
Attachment(
249247
id: item.id,
250248
filename: filename,
251-
state: AttachmentState.queuedDownload
249+
state: .queuedDownload,
250+
hasSynced: false
252251
)
253252
)
254-
} else if existingQueueItem!.state == AttachmentState.archived {
253+
continue
254+
}
255+
256+
if existingQueueItem.state == AttachmentState.archived {
255257
// The attachment is present again. Need to queue it for sync.
256258
// We might be able to optimize this in future
257-
if existingQueueItem!.hasSynced == 1 {
259+
if existingQueueItem.hasSynced == true {
258260
// No remote action required, we can restore the record (avoids deletion)
259261
attachmentUpdates.append(
260-
existingQueueItem!.with(state: AttachmentState.synced)
262+
existingQueueItem.with(state: AttachmentState.synced)
261263
)
262264
} else {
263265
// The localURI should be set if the record was meant to be downloaded
264266
// and has been synced. If it's missing and hasSynced is false then
265267
// it must be an upload operation
266-
let newState = existingQueueItem!.localUri == nil ?
268+
let newState = existingQueueItem.localUri == nil ?
267269
AttachmentState.queuedDownload :
268270
AttachmentState.queuedUpload
269271

270272
attachmentUpdates.append(
271-
existingQueueItem!.with(state: newState)
273+
existingQueueItem.with(state: newState)
272274
)
273275
}
274276
}
275277
}
276278

277-
/**
278-
* Archive any items not specified in the watched items except for items pending delete.
279-
*/
280279
for attachment in currentAttachments {
281-
if attachment.state != AttachmentState.queuedDelete &&
282-
attachment.state != AttachmentState.queuedUpload,
283-
items.first(where: { $0.id == attachment.id }) == nil
284-
{
285-
attachmentUpdates.append(
286-
attachment.with(state: AttachmentState.archived)
287-
)
280+
let notInWatchedItems = items.first(where: { $0.id == attachment.id }) == nil
281+
if notInWatchedItems {
282+
switch attachment.state {
283+
case .queuedDelete, .queuedUpload:
284+
// Only archive if it has synced
285+
if attachment.hasSynced == true {
286+
attachmentUpdates.append(
287+
attachment.with(state: .archived)
288+
)
289+
}
290+
default:
291+
// Archive other states such as QUEUED_DOWNLOAD
292+
attachmentUpdates.append(
293+
attachment.with(state: .archived)
294+
)
295+
}
288296
}
289297
}
290298

Sources/PowerSync/attachments/SyncingService.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ public class SyncingService {
231231
let fileData = try await localStorage.readFile(filePath: localUri)
232232
try await remoteStorage.uploadFile(fileData: fileData, attachment: attachment)
233233

234-
return attachment.with(state: AttachmentState.synced, hasSynced: 1)
234+
return attachment.with(state: AttachmentState.synced, hasSynced: true)
235235
} catch {
236236
if let errorHandler = errorHandler {
237237
let shouldRetry = await errorHandler.onUploadError(attachment: attachment, error: error)
@@ -256,7 +256,7 @@ public class SyncingService {
256256

257257
return attachment.with(
258258
state: AttachmentState.synced,
259-
hasSynced: 1,
259+
hasSynced: true,
260260
localUri: attachmentPath
261261
)
262262
} catch {

0 commit comments

Comments
 (0)