Caps-iOS/Caps/Views/CapImagesView.swift

76 lines
2.2 KiB
Swift
Raw Normal View History

2023-02-19 00:38:52 +01:00
import SwiftUI
struct CapImagesView: View {
private let imageSize: CGFloat = 70
@Binding
var cap: Cap?
@Binding
var isPresented: Bool
init(cap: Binding<Cap?>, database: Database, isPresented: Binding<Bool>) {
self.database = database
self._cap = cap
self._isPresented = isPresented
}
let database: Database
var images: [CapImage] {
guard let cap else {
return []
}
return (0..<cap.imageCount).map {
cap.image(version: $0)
}
}
var title: String {
guard let cap else {
return "Images"
}
return "Cap \(cap.id)"
}
var body: some View {
GeometryReader { geo in
VStack {
HStack {
Text(title).font(.title2).bold()
Spacer()
Button(action: { isPresented = false }) {
Image(systemSymbol: .xmarkCircleFill)
.foregroundColor(.gray)
.font(.system(size: 26))
}
}
let gridItem = GridItem(.flexible(), spacing: 10, alignment: .leading)
ScrollView(.vertical) {
LazyVGrid(columns: [gridItem, gridItem, gridItem, gridItem]) {
ForEach(images) { item in
CachedCapImage(
item,
check: { database.images.cachedImage(item) },
fetch: { await database.images.image(item) },
content: { $0.resizable() },
placeholder: { ProgressView() })
.frame(width: imageSize,
height: imageSize)
.clipShape(Circle())
}
}
}
}
.padding(.horizontal)
}
}
}
struct CapImagesView_Previews: PreviewProvider {
static var previews: some View {
CapImagesView(cap: .constant(.init(id: 123, name: "Some")), database: .mock, isPresented: .constant(true))
}
}