another.im-ios/ConversationsClassic/View/Screens/Attachments/AttachmentMediaPickerView.swift

142 lines
4.3 KiB
Swift
Raw Normal View History

2024-07-02 09:56:27 +00:00
import SwiftUI
2024-07-02 10:23:27 +00:00
2024-07-02 09:56:27 +00:00
struct AttachmentMediaPickerView: View {
2024-07-03 10:47:59 +00:00
@StateObject private var mediaManager = MediaManager()
2024-07-02 09:56:27 +00:00
var body: some View {
2024-07-02 10:23:27 +00:00
ScrollView {
2024-07-03 08:53:39 +00:00
LazyVGrid(columns: Array(repeating: .init(.flexible()), count: 3)) {
2024-07-03 10:47:59 +00:00
ForEach(elements) { element in
element
2024-07-02 10:23:27 +00:00
}
2024-07-02 09:56:27 +00:00
}
2024-07-03 10:47:59 +00:00
.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")
2024-07-02 09:56:27 +00:00
}
2024-07-03 10:47:59 +00:00
return Image(uiImage: content)
2024-07-02 09:56:27 +00:00
}
}
2024-07-03 08:53:39 +00:00
struct AttachmentMediaPickerView_Previews: PreviewProvider {
static var previews: some View {
AttachmentMediaPickerView()
2024-07-02 09:56:27 +00:00
}
}