Impressia/Vernissage/Models/PhotoAttachment.swift

53 lines
1.3 KiB
Swift
Raw Normal View History

2023-02-17 14:47:59 +01:00
//
// https://mczachurski.dev
// Copyright © 2023 Marcin Czachurski and the repository contributors.
2023-03-28 10:35:38 +02:00
// Licensed under the Apache License 2.0.
2023-02-17 14:47:59 +01:00
//
import Foundation
import PhotosUI
import SwiftUI
2023-02-19 10:32:38 +01:00
import PixelfedKit
2023-02-17 14:47:59 +01:00
public class PhotoAttachment: ObservableObject, Identifiable, Equatable, Hashable {
public let id: String
public let photosPickerItem: PhotosPickerItem
2023-02-22 20:49:08 +01:00
@Published public var photoData: Data?
2023-02-17 14:47:59 +01:00
@Published public var uploadedAttachment: UploadedAttachment?
@Published public var error: Error?
2023-02-22 20:49:08 +01:00
init(photosPickerItem: PhotosPickerItem) {
2023-02-17 14:47:59 +01:00
self.id = UUID().uuidString
self.photosPickerItem = photosPickerItem
}
2023-02-17 14:47:59 +01:00
public static func == (lhs: PhotoAttachment, rhs: PhotoAttachment) -> Bool {
lhs.id == rhs.id
}
2023-02-17 14:47:59 +01:00
public func hash(into hasher: inout Hasher) {
return hasher.combine(self.id)
}
}
extension [PhotoAttachment] {
public func hasUploadedPhotos() -> Bool {
return self.contains { photoAttachment in
photoAttachment.uploadedAttachment != nil
}
}
2023-02-17 14:47:59 +01:00
public func getUploadedPhotoIds() -> [String] {
var ids: [String] = []
2023-02-17 14:47:59 +01:00
for item in self {
if let uploadedAttachment = item.uploadedAttachment {
ids.append(uploadedAttachment.id)
}
}
2023-02-17 14:47:59 +01:00
return ids
}
}