another.im-ios/ConversationsClassic/View/Main/Conversation/Attachments/MediaPickerComponents/GalleryView.swift
fmodf b3b3b3aef7 mv-experiment (#1)
Reviewed-on: narayana/conversations-classic-ios#1
Co-authored-by: fmodf <fmodf.ios@gmail.com>
Co-committed-by: fmodf <fmodf.ios@gmail.com>
2024-09-03 15:13:58 +00:00

114 lines
3.9 KiB
Swift

import SwiftUI
struct GalleryView: View {
@EnvironmentObject var attachments: AttachmentsStore
@Binding var selectedItems: [String]
var body: some View {
Group {
if attachments.galleryAccessGranted {
ForEach(attachments.galleryItems) { item in
GridViewItem(item: item, selected: $selectedItems)
}
} else {
Button {
openAppSettings()
} label: {
ZStack {
Rectangle()
.fill(Color.Material.Background.light)
.overlay {
VStack {
Image(systemName: "photo")
.foregroundColor(.Material.Elements.active)
.font(.system(size: 30))
Text("Allow gallery access")
.foregroundColor(.Material.Text.main)
.font(.body3)
}
}
.frame(height: 100)
}
}
}
}
.task {
await attachments.checkGalleryAuthorization()
}
}
}
private struct GridViewItem: View {
@State var item: GalleryItem
@Binding var selected: [String]
var body: some View {
if let img = item.thumbnail {
ZStack {
img
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: Const.galleryGridSize, height: Const.galleryGridSize)
.clipped()
if let duration = item.duration {
VStack {
Spacer()
HStack {
Spacer()
Text(duration)
.foregroundColor(.Material.Text.white)
.font(.sub1)
.shadow(color: .black, radius: 2)
.padding(4)
}
}
}
if isSelected {
VStack {
HStack {
Spacer()
Circle()
.frame(width: 30, height: 30)
.shadow(color: .black, radius: 2)
.foregroundColor(.Material.Shape.white)
.overlay {
Image(systemName: "checkmark")
.foregroundColor(.Material.Elements.active)
.font(.body3)
}
.padding(4)
}
Spacer()
}
}
}
.onTapGesture {
if isSelected {
selected.removeAll { $0 == item.id }
} else {
selected.append(item.id)
}
}
} else {
ZStack {
Rectangle()
.fill(Color.Material.Background.light)
.overlay {
ProgressView()
.foregroundColor(.Material.Elements.active)
}
.frame(width: Const.galleryGridSize, height: Const.galleryGridSize)
}
.task {
if item.thumbnail == nil {
try? await item.fetchThumbnail()
}
}
}
}
private var isSelected: Bool {
selected.contains(item.id)
}
}