2024-08-17 12:11:11 +00:00
|
|
|
import Combine
|
|
|
|
import Foundation
|
|
|
|
import Photos
|
2024-08-17 13:54:54 +00:00
|
|
|
import SwiftUI
|
2024-08-17 12:11:11 +00:00
|
|
|
|
|
|
|
@MainActor
|
2024-08-17 16:15:05 +00:00
|
|
|
final class AttachmentsStore: ObservableObject {
|
2024-08-17 12:11:11 +00:00
|
|
|
@Published var cameraAccessGranted = false
|
|
|
|
@Published var galleryAccessGranted = false
|
2024-08-17 13:54:54 +00:00
|
|
|
@Published var galleryItems: [GalleryItem] = []
|
2024-08-17 12:11:11 +00:00
|
|
|
|
2024-08-18 09:05:43 +00:00
|
|
|
private let client: Client
|
|
|
|
private let roster: Roster
|
|
|
|
|
|
|
|
init(roster: Roster, client: Client) {
|
|
|
|
self.client = client
|
|
|
|
self.roster = roster
|
|
|
|
}
|
2024-08-17 12:11:11 +00:00
|
|
|
}
|
|
|
|
|
2024-08-17 16:15:05 +00:00
|
|
|
extension AttachmentsStore {
|
2024-08-17 12:11:11 +00:00
|
|
|
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
|
|
|
|
}
|
2024-08-17 13:54:54 +00:00
|
|
|
|
|
|
|
func fetchGalleryItems() async {
|
|
|
|
guard galleryAccessGranted else { return }
|
|
|
|
galleryItems = await GalleryItem.fetchAll()
|
|
|
|
}
|
2024-08-17 12:11:11 +00:00
|
|
|
}
|