JustScanIt/JustScanIt/View/ScanItemView.swift
2024-07-03 20:21:12 +12:00

105 lines
3.0 KiB
Swift

//
// ScanItemView.swift
// JustScanIt
//
// Created by Pradyun Setti on 03/07/2024.
//
import SwiftUI
struct ScanItemView: View {
@Environment(\.modelContext) var ctx
let scan: Scan
private let translucentColour = Color(red: 1, green: 1, blue: 1, opacity: 0.4)
init(_ scan: Scan) {
self.scan = scan
}
var body: some View {
HStack {
Image(uiImage: scan.uiImages.first ?? UIImage(systemName: "questionmark")!)
.resizable()
.scaledToFit()
.frame(maxWidth: 70, maxHeight: 70)
.padding(4)
.background(translucentColour)
.clipShape(RoundedRectangle(cornerRadius: 12))
VStack(alignment: .leading) {
Text("\(scan.uiImages.count) \(scan.uiImages.count == 1 ? "Page" : "Pages")")
.font(.title3)
Text("\(scan.timestamp.formatted())")
.font(.footnote)
}
.foregroundStyle(.white)
Spacer()
if scan.images.count == 1 {
Button { copy() } label: {
Image(systemName: "doc.on.doc.fill").tint(.white)
.padding(8)
.background(Color(red: 1, green: 1, blue: 1, opacity: 0.4))
.clipShape(Circle())
}
}
ShareLink(items: scan.suiImages, preview: { _ in SharePreview("Scan", image: scan.suiImages.first!)} ) {
Image(systemName: "square.and.arrow.up").tint(.white)
.padding(8)
.background(Color(red: 1, green: 1, blue: 1, opacity: 0.4))
.offset(y: -2)
.clipShape(Circle())
}
Button { delete() } label: {
Image(systemName: "trash.fill").tint(.red)
.padding(8)
.background(Color(red: 1, green: 1, blue: 1, opacity: 0.4))
.clipShape(Circle())
}
.contextMenu(menuItems: {
Button("Delete All Scans", role: .destructive) {
deleteAll()
}
})
}
.padding()
.background(Color.accentColor)
.clipShape(RoundedRectangle(cornerRadius: 12))
.padding(.horizontal, 12)
}
func copy() {
UIPasteboard.general.images = scan.uiImages
}
func delete() {
withAnimation(.easeOut) {
ctx.delete(scan)
try? ctx.save()
}
}
func deleteAll() {
withAnimation(.easeOut) {
try? ctx.delete(model: Scan.self)
try? ctx.save()
}
}
}
#Preview {
let scan = Scan(images: Array(repeating: UIImage(systemName: "book.fill")!, count: 5))
return NavigationView {
ScrollView {
ForEach(Array(repeating: scan, count: 10)) { s in
ScanItemView(s)
}
}
}
}