completely overhauled the scan item view

This commit is contained in:
breadone 2024-07-03 20:21:12 +12:00
parent 3b6a91388d
commit 64a066fb2a
No known key found for this signature in database
4 changed files with 127 additions and 12 deletions

View File

@ -13,6 +13,8 @@
466989D62C34DCE4009884D1 /* VNDocumentCameraViewControllerR.swift in Sources */ = {isa = PBXBuildFile; fileRef = 466989D52C34DCE4009884D1 /* VNDocumentCameraViewControllerR.swift */; }; 466989D62C34DCE4009884D1 /* VNDocumentCameraViewControllerR.swift in Sources */ = {isa = PBXBuildFile; fileRef = 466989D52C34DCE4009884D1 /* VNDocumentCameraViewControllerR.swift */; };
466989D92C35206E009884D1 /* Scan.swift in Sources */ = {isa = PBXBuildFile; fileRef = 466989D82C35206E009884D1 /* Scan.swift */; }; 466989D92C35206E009884D1 /* Scan.swift in Sources */ = {isa = PBXBuildFile; fileRef = 466989D82C35206E009884D1 /* Scan.swift */; };
466989DB2C352776009884D1 /* MainView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 466989C82C34DC7A009884D1 /* MainView.swift */; }; 466989DB2C352776009884D1 /* MainView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 466989C82C34DC7A009884D1 /* MainView.swift */; };
466989DD2C35299D009884D1 /* ScanItemView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 466989DC2C35299D009884D1 /* ScanItemView.swift */; };
466989DF2C353FEC009884D1 /* SettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 466989DE2C353FEC009884D1 /* SettingsView.swift */; };
/* End PBXBuildFile section */ /* End PBXBuildFile section */
/* Begin PBXFileReference section */ /* Begin PBXFileReference section */
@ -23,6 +25,8 @@
466989CD2C34DC7B009884D1 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = "<group>"; }; 466989CD2C34DC7B009884D1 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = "<group>"; };
466989D52C34DCE4009884D1 /* VNDocumentCameraViewControllerR.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VNDocumentCameraViewControllerR.swift; sourceTree = "<group>"; }; 466989D52C34DCE4009884D1 /* VNDocumentCameraViewControllerR.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VNDocumentCameraViewControllerR.swift; sourceTree = "<group>"; };
466989D82C35206E009884D1 /* Scan.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Scan.swift; sourceTree = "<group>"; }; 466989D82C35206E009884D1 /* Scan.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Scan.swift; sourceTree = "<group>"; };
466989DC2C35299D009884D1 /* ScanItemView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScanItemView.swift; sourceTree = "<group>"; };
466989DE2C353FEC009884D1 /* SettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsView.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */ /* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */ /* Begin PBXFrameworksBuildPhase section */
@ -93,6 +97,8 @@
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
466989C82C34DC7A009884D1 /* MainView.swift */, 466989C82C34DC7A009884D1 /* MainView.swift */,
466989DC2C35299D009884D1 /* ScanItemView.swift */,
466989DE2C353FEC009884D1 /* SettingsView.swift */,
); );
path = View; path = View;
sourceTree = "<group>"; sourceTree = "<group>";
@ -170,7 +176,9 @@
466989C72C34DC7A009884D1 /* JustScanItApp.swift in Sources */, 466989C72C34DC7A009884D1 /* JustScanItApp.swift in Sources */,
466989DB2C352776009884D1 /* MainView.swift in Sources */, 466989DB2C352776009884D1 /* MainView.swift in Sources */,
466989D92C35206E009884D1 /* Scan.swift in Sources */, 466989D92C35206E009884D1 /* Scan.swift in Sources */,
466989DF2C353FEC009884D1 /* SettingsView.swift in Sources */,
466989D62C34DCE4009884D1 /* VNDocumentCameraViewControllerR.swift in Sources */, 466989D62C34DCE4009884D1 /* VNDocumentCameraViewControllerR.swift in Sources */,
466989DD2C35299D009884D1 /* ScanItemView.swift in Sources */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
}; };

View File

@ -8,6 +8,7 @@
import Foundation import Foundation
import SwiftData import SwiftData
import UIKit import UIKit
import SwiftUI
@Model @Model
final class Scan { final class Scan {
@ -17,6 +18,10 @@ final class Scan {
var uiImages: [UIImage] { var uiImages: [UIImage] {
self.images.map { UIImage(data: $0) ?? UIImage(systemName: "exclamationmark.questionmark")! } self.images.map { UIImage(data: $0) ?? UIImage(systemName: "exclamationmark.questionmark")! }
} }
var suiImages: [Image] {
self.images.map { Image(uiImage: UIImage(data: $0) ?? UIImage(systemName: "exclamationmark.questionmark")!) }
}
init() { init() {
self.timestamp = Date() self.timestamp = Date()

View File

@ -40,16 +40,7 @@ struct MainView: View {
NavigationView { NavigationView {
ScrollView { ScrollView {
ForEach(scans, id: \.self) { scan in ForEach(scans, id: \.self) { scan in
HStack { ScanItemView(scan)
ForEach(scan.uiImages, id: \.self) { image in
Image(uiImage: image)
.resizable()
.scaledToFit()
.frame(width: 100, height: 100)
.padding()
}
}
} }
} }
.navigationTitle("Just Scan It!") .navigationTitle("Just Scan It!")
@ -57,6 +48,9 @@ struct MainView: View {
VNDocumentCameraViewControllerRepresentable(scanResult: $scannedImages) VNDocumentCameraViewControllerRepresentable(scanResult: $scannedImages)
} }
.toolbar { .toolbar {
ToolbarItem(placement: .topBarLeading) {
Button("clear") { try? ctx.delete(model: Scan.self) }
}
ToolbarItem { ToolbarItem {
Button(action: showVNDocumentCameraView) { Button(action: showVNDocumentCameraView) {
Image(systemName: "scanner") Image(systemName: "scanner")
@ -73,10 +67,14 @@ struct MainView: View {
} }
private func saveImages() { private func saveImages() {
guard !scannedImages.isEmpty else { return }
let newScan = Scan(images: scannedImages) let newScan = Scan(images: scannedImages)
ctx.insert(newScan) withAnimation(.easeIn) {
try? ctx.save() ctx.insert(newScan)
try? ctx.save()
}
scannedImages = [] scannedImages = []
} }

View File

@ -0,0 +1,104 @@
//
// 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)
}
}
}
}