import SwiftUI struct AttachmentMediaPickerView: View { @StateObject private var mediaManager = MediaManager() var body: some View { ScrollView { LazyVGrid(columns: Array(repeating: .init(.flexible()), count: 3)) { ForEach(elements) { element in element } } .padding(.horizontal, 8) } .padding(.vertical, 8) } private var elements: [GridElement] { print("Creating elements") var result: [GridElement] = [] // camera if let feed = mediaManager.cameraFeed, mediaManager.cameraAccessLevel == .authorized { result.append(GridElement(id: UUID(), type: .cameraFeed, content: feed) { print("Go to capture???") }) print("Added camera feed") } else if mediaManager.cameraAccessLevel == .restricted { result.append(GridElement(id: UUID(), type: .cameraRestricted, content: nil) { print("Show alert") }) print("Added camera restricted") } else { result.append(GridElement(id: UUID(), type: .cameraAskButton, content: nil) { mediaManager.openAppSettings() }) print("Added camera ask button") } // photos // if mediaManager.galleryAccessLevel == .restricted { // result.append(GridElement(id: UUID(), type: .photoRestricted, content: nil)) // } else { // for photo in mediaManager.photos { // result.append(GridElement(id: UUID(), type: .photo, content: photo)) // } // if mediaManager.galleryAccessLevel != .authorized { // result.append(GridElement(id: UUID(), type: .photoAskButton, content: nil)) // } // } return result } } private enum GridElementType { case cameraFeed case cameraAskButton case cameraRestricted case photo case photoAskButton case photoRestricted } private struct GridElement: View, Identifiable { let id: UUID let type: GridElementType let content: UIImage? let action: () -> Void var body: some View { switch type { case .cameraFeed: image .resizable() .aspectRatio(contentMode: .fill) .frame(width: 100, height: 100) .clipped() case .cameraAskButton: Button { action() } label: { RoundedRectangle(cornerRadius: 5) .stroke(Color.Main.backgroundDark, lineWidth: 2) .overlay { Image(systemName: "camera") .foregroundColor(.Material.tortoiseLight300) .font(.system(size: 40)) } .frame(height: 100) // .resizable() // .aspectRatio(contentMode: .fill) // .frame(width: 100, height: 100) // .clipped() } case .photo: image .resizable() .aspectRatio(contentMode: .fill) .frame(width: 100, height: 100) .clipped() case .photoAskButton: Button { action() } label: { Image(systemName: "photo.badge.plus") .resizable() .aspectRatio(contentMode: .fill) .frame(width: 100, height: 100) .clipped() } case .photoRestricted, .cameraRestricted: Button { action() } label: { Image(systemName: "cross") .resizable() .aspectRatio(contentMode: .fill) .frame(width: 100, height: 100) .clipped() } } } private var image: Image { guard let content = content else { return Image(systemName: "questionmark.square.dashed") } return Image(uiImage: content) } } struct AttachmentMediaPickerView_Previews: PreviewProvider { static var previews: some View { AttachmentMediaPickerView() } }