Skip to content

Commit 18bcd4c

Browse files
cleanup docs
1 parent 282ed06 commit 18bcd4c

File tree

8 files changed

+362
-385
lines changed

8 files changed

+362
-385
lines changed

Sources/PowerSync/attachments/Attachment.swift

Lines changed: 73 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,48 @@
1-
/**
2-
* Enum for the attachment state
3-
*/
1+
/// Enum representing the state of an attachment
42
public enum AttachmentState: Int {
3+
/// The attachment is queued for download
54
case queuedDownload
5+
/// The attachment is queued for upload
66
case queuedUpload
7+
/// The attachment is queued for deletion
78
case queuedDelete
9+
/// The attachment is fully synced
810
case synced
11+
/// The attachment is archived
912
case archived
1013
}
1114

12-
/**
13-
* Struct representing an attachment
14-
*/
15+
/// Struct representing an attachment
1516
public struct Attachment {
17+
/// Unique identifier for the attachment
1618
let id: String
19+
20+
/// Timestamp for the last record update
1721
let timestamp: Int
22+
23+
/// Attachment filename, e.g. `[id].jpg`
1824
let filename: String
25+
26+
/// Current attachment state, represented by the raw value of `AttachmentState`
1927
let state: Int
28+
29+
/// Local URI pointing to the attachment file
2030
let localUri: String?
31+
32+
/// Attachment media type (usually a MIME type)
2133
let mediaType: String?
34+
35+
/// Attachment byte size
2236
let size: Int64?
23-
/**
24-
* Specifies if the attachment has been synced locally before. This is particularly useful
25-
* for restoring archived attachments in edge cases.
26-
*/
37+
38+
/// Specifies if the attachment has been synced locally before.
39+
/// This is particularly useful for restoring archived attachments in edge cases.
2740
let hasSynced: Int?
28-
41+
42+
/// Extra attachment metadata
43+
let metaData: String?
44+
45+
/// Initializes a new `Attachment` instance
2946
public init(
3047
id: String,
3148
filename: String,
@@ -35,6 +52,7 @@ public struct Attachment {
3552
localUri: String? = nil,
3653
mediaType: String? = nil,
3754
size: Int64? = nil,
55+
metaData: String? = nil
3856
) {
3957
self.id = id
4058
self.timestamp = timestamp
@@ -44,22 +62,50 @@ public struct Attachment {
4462
self.mediaType = mediaType
4563
self.size = size
4664
self.hasSynced = hasSynced
65+
self.metaData = metaData
4766
}
48-
49-
func with(filename: String? = nil, state: Int? = nil, hasSynced: Int? = nil, localUri: String? = nil, mediaType: String? = nil, size: Int64? = nil ) -> Attachment {
50-
return Attachment(
51-
id: self.id,
52-
filename: self.filename,
53-
state: state ?? self.state,
54-
hasSynced: hasSynced ?? self.hasSynced,
55-
localUri: localUri ?? self.localUri,
56-
mediaType: mediaType ?? self.mediaType,
57-
size: size ?? self.size,
58-
)
59-
}
60-
61-
public static func fromCursor(_ cursor: SqlCursor) throws -> Attachment {
62-
return Attachment(
67+
68+
/// Returns a new `Attachment` instance with the option to override specific fields.
69+
///
70+
/// - Parameters:
71+
/// - filename: Optional new filename.
72+
/// - state: Optional new state.
73+
/// - timestamp: Optional new timestamp.
74+
/// - hasSynced: Optional new `hasSynced` flag.
75+
/// - localUri: Optional new local URI.
76+
/// - mediaType: Optional new media type.
77+
/// - size: Optional new size.
78+
/// - metaData: Optional new metadata.
79+
/// - Returns: A new `Attachment` with updated values.
80+
func with(
81+
filename: String? = nil,
82+
state: Int? = nil,
83+
timestamp: Int = 0,
84+
hasSynced: Int? = 0,
85+
localUri: String? = nil,
86+
mediaType: String? = nil,
87+
size: Int64? = nil,
88+
metaData: String? = nil
89+
) -> Attachment {
90+
return Attachment(
91+
id: self.id,
92+
filename: self.filename,
93+
state: state ?? self.state,
94+
hasSynced: hasSynced ?? self.hasSynced,
95+
localUri: localUri ?? self.localUri,
96+
mediaType: mediaType ?? self.mediaType,
97+
size: size ?? self.size,
98+
metaData: metaData ?? self.metaData
99+
)
100+
}
101+
102+
/// Constructs an `Attachment` from a `SqlCursor`.
103+
///
104+
/// - Parameter cursor: The `SqlCursor` containing the attachment data.
105+
/// - Throws: If required fields are missing or of incorrect type.
106+
/// - Returns: A fully constructed `Attachment` instance.
107+
public static func fromCursor(_ cursor: SqlCursor) throws -> Attachment {
108+
return Attachment(
63109
id: try cursor.getString(name: "id"),
64110
filename: try cursor.getString(name: "filename"),
65111
state: try cursor.getLong(name: "state"),
@@ -68,7 +114,7 @@ public struct Attachment {
68114
localUri: try cursor.getStringOptional(name: "local_uri"),
69115
mediaType: try cursor.getStringOptional(name: "media_type"),
70116
size: try cursor.getLongOptional(name: "size")?.int64Value,
117+
metaData: try cursor.getStringOptional(name: "meta_data")
71118
)
72119
}
73120
}
74-

0 commit comments

Comments
 (0)