Skip to content

Commit 156b31f

Browse files
cleanup
1 parent a64555a commit 156b31f

File tree

3 files changed

+64
-43
lines changed

3 files changed

+64
-43
lines changed

Demo/PowerSyncExample/PowerSync/SupabaseConnector.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,12 @@ class SupabaseConnector: PowerSyncBackendConnector {
6868
return id.uuidString.lowercased()
6969
}
7070

71-
func getStorageBucket() -> StorageFileApi {
72-
return client.storage.from(Secrets.supabaseStorageBucket)
71+
func getStorageBucket() -> StorageFileApi? {
72+
guard let bucket = Secrets.supabaseStorageBucket else {
73+
return nil
74+
}
75+
76+
return client.storage.from(bucket)
7377
}
7478

7579
override func fetchCredentials() async throws -> PowerSyncCredentials? {

Demo/PowerSyncExample/PowerSync/SystemManager.swift

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,40 +16,54 @@ class SystemManager {
1616
let connector = SupabaseConnector()
1717
let schema = AppSchema
1818
let db: PowerSyncDatabaseProtocol
19-
let attachments: AttachmentQueue?
20-
19+
20+
var attachments: AttachmentQueue?
21+
2122
init() {
2223
db = PowerSyncDatabase(
2324
schema: schema,
2425
dbFilename: "powersync-swift.sqlite"
2526
)
26-
// Try and configure attachments
27-
do {
28-
let attachmentsDir = try getAttachmentsDirectoryPath()
29-
let watchedAttachments = try db.watch(
30-
options: WatchOptions(
31-
sql: "SELECT photo_id FROM \(TODOS_TABLE) WHERE photo_id IS NOT NULL",
32-
parameters: [],
33-
mapper: { cursor in
34-
try WatchedAttachmentItem(
35-
id: cursor.getString(name: "photo_id"),
36-
fileExtension: "jpg"
37-
)
38-
}
27+
attachments = Self.createAttachments(
28+
db: db,
29+
connector: connector
30+
)
31+
}
32+
33+
private static func createAttachments(
34+
db: PowerSyncDatabaseProtocol,
35+
connector: SupabaseConnector
36+
) -> AttachmentQueue? {
37+
guard let bucket = connector.getStorageBucket() else {
38+
return nil
39+
}
40+
41+
do {
42+
let attachmentsDir = try getAttachmentsDirectoryPath()
43+
let watchedAttachments = try db.watch(
44+
options: WatchOptions(
45+
sql: "SELECT photo_id FROM \(TODOS_TABLE) WHERE photo_id IS NOT NULL",
46+
parameters: [],
47+
mapper: { cursor in
48+
try WatchedAttachmentItem(
49+
id: cursor.getString(name: "photo_id"),
50+
fileExtension: "jpg"
51+
)
52+
}
53+
)
3954
)
40-
)
4155

42-
attachments = AttachmentQueue(
43-
db: db,
44-
remoteStorage: SupabaseRemoteStorage(storage: connector.getStorageBucket()),
45-
attachmentsDirectory: attachmentsDir,
46-
watchedAttachments: watchedAttachments
47-
)
48-
} catch {
49-
print("Failed to initialize attachments queue: \(error)")
50-
attachments = nil
56+
return AttachmentQueue(
57+
db: db,
58+
remoteStorage: SupabaseRemoteStorage(storage: bucket),
59+
attachmentsDirectory: attachmentsDir,
60+
watchedAttachments: watchedAttachments
61+
)
62+
} catch {
63+
print("Failed to initialize attachments queue: \(error)")
64+
return nil
65+
}
5166
}
52-
}
5367

5468
func connect() async {
5569
do {

Sources/PowerSync/attachments/AttachmentContext.swift

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ public class AttachmentContext {
2828

2929
/// Deletes the attachment from the attachment queue.
3030
public func deleteAttachment(id: String) async throws {
31-
_ = try await db.execute(sql: "DELETE FROM \(table) WHERE id = ?", parameters: [id])
31+
_ = try await db.execute(
32+
sql: "DELETE FROM \(table) WHERE id = ?",
33+
parameters: [id]
34+
)
3235
}
3336

3437
/// Sets the state of the attachment to ignored (archived).
@@ -41,9 +44,12 @@ public class AttachmentContext {
4144

4245
/// Gets the attachment from the attachment queue using an ID.
4346
public func getAttachment(id: String) async throws -> Attachment? {
44-
return try await db.getOptional(sql: "SELECT * FROM \(table) WHERE id = ?", parameters: [id], mapper: { cursor in
47+
return try await db.getOptional(
48+
sql: "SELECT * FROM \(table) WHERE id = ?",
49+
parameters: [id]
50+
) { cursor in
4551
try Attachment.fromCursor(cursor)
46-
})
52+
}
4753
}
4854

4955
/// Saves the attachment to the attachment queue.
@@ -70,11 +76,10 @@ public class AttachmentContext {
7076
public func getAttachmentIds() async throws -> [String] {
7177
return try await db.getAll(
7278
sql: "SELECT id FROM \(table) WHERE id IS NOT NULL",
73-
parameters: [],
74-
mapper: { cursor in
75-
try cursor.getString(name: "id")
76-
}
77-
)
79+
parameters: []
80+
) { cursor in
81+
try cursor.getString(name: "id")
82+
}
7883
}
7984

8085
/// Gets all attachments in the attachment queue.
@@ -90,11 +95,10 @@ public class AttachmentContext {
9095
ORDER BY
9196
timestamp ASC
9297
""",
93-
parameters: [],
94-
mapper: { cursor in
95-
try Attachment.fromCursor(cursor)
96-
}
97-
)
98+
parameters: []
99+
) { cursor in
100+
try Attachment.fromCursor(cursor)
101+
}
98102
}
99103

100104
/// Gets all active attachments that require an operation to be performed.
@@ -206,11 +210,10 @@ public class AttachmentContext {
206210
updatedRecord.mediaType ?? NSNull(),
207211
updatedRecord.size ?? NSNull(),
208212
updatedRecord.state.rawValue,
209-
updatedRecord.hasSynced ?? 0
213+
updatedRecord.hasSynced ?? 0,
210214
]
211215
)
212216

213217
return attachment
214218
}
215219
}
216-

0 commit comments

Comments
 (0)