import Combine import Foundation import Photos import SwiftUI @MainActor final class AttachmentsStore: ObservableObject { @Published var cameraAccessGranted = false @Published var galleryAccessGranted = false @Published var galleryItems: [GalleryItem] = [] private let client: Client private let roster: Roster init(roster: Roster, client: Client) { self.client = client self.roster = roster } } extension AttachmentsStore { func checkCameraAuthorization() async { let status = AVCaptureDevice.authorizationStatus(for: .video) var isAuthorized = status == .authorized if status == .notDetermined { isAuthorized = await AVCaptureDevice.requestAccess(for: .video) } cameraAccessGranted = isAuthorized } func checkGalleryAuthorization() async { let status = PHPhotoLibrary.authorizationStatus() var isAuthorized = status == .authorized if status == .notDetermined { let req = await PHPhotoLibrary.requestAuthorization(for: .readWrite) isAuthorized = (req == .authorized) || (req == .limited) } galleryAccessGranted = isAuthorized } func fetchGalleryItems() async { guard galleryAccessGranted else { return } galleryItems = await GalleryItem.fetchAll() } }