another.im-ios/ConversationsClassic/AppData/Store/AttachmentsStore.swift
2024-08-18 11:05:43 +02:00

46 lines
1.3 KiB
Swift

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()
}
}