|
| 1 | +/** |
| 2 | + * Enum for the attachment state |
| 3 | + */ |
| 4 | +public enum AttachmentState: Int { |
| 5 | + case queuedDownload |
| 6 | + case queuedUpload |
| 7 | + case queuedDelete |
| 8 | + case synced |
| 9 | + case archived |
| 10 | +} |
| 11 | + |
| 12 | +/** |
| 13 | + * Struct representing an attachment |
| 14 | + */ |
| 15 | +public struct Attachment { |
| 16 | + let id: String |
| 17 | + let timestamp: Int |
| 18 | + let filename: String |
| 19 | + let state: Int |
| 20 | + let localUri: String? |
| 21 | + let mediaType: String? |
| 22 | + 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 | + */ |
| 27 | + let hasSynced: Int? |
| 28 | + |
| 29 | + public init( |
| 30 | + id: String, |
| 31 | + filename: String, |
| 32 | + state: Int, |
| 33 | + timestamp: Int = 0, |
| 34 | + hasSynced: Int? = 0, |
| 35 | + localUri: String? = nil, |
| 36 | + mediaType: String? = nil, |
| 37 | + size: Int64? = nil, |
| 38 | + ) { |
| 39 | + self.id = id |
| 40 | + self.timestamp = timestamp |
| 41 | + self.filename = filename |
| 42 | + self.state = state |
| 43 | + self.localUri = localUri |
| 44 | + self.mediaType = mediaType |
| 45 | + self.size = size |
| 46 | + self.hasSynced = hasSynced |
| 47 | + } |
| 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( |
| 63 | + id: try cursor.getString(name: "id"), |
| 64 | + filename: try cursor.getString(name: "filename"), |
| 65 | + state: try cursor.getLong(name: "state"), |
| 66 | + timestamp: try cursor.getLong(name: "timestamp"), |
| 67 | + hasSynced: try cursor.getLongOptional(name: "has_synced"), |
| 68 | + localUri: try cursor.getStringOptional(name: "local_uri"), |
| 69 | + mediaType: try cursor.getStringOptional(name: "media_type"), |
| 70 | + size: try cursor.getLongOptional(name: "size")?.int64Value, |
| 71 | + ) |
| 72 | + } |
| 73 | +} |
| 74 | + |
0 commit comments