completely overhauled the scan item view
This commit is contained in:
parent
3b6a91388d
commit
64a066fb2a
@ -13,6 +13,8 @@
|
||||
466989D62C34DCE4009884D1 /* VNDocumentCameraViewControllerR.swift in Sources */ = {isa = PBXBuildFile; fileRef = 466989D52C34DCE4009884D1 /* VNDocumentCameraViewControllerR.swift */; };
|
||||
466989D92C35206E009884D1 /* Scan.swift in Sources */ = {isa = PBXBuildFile; fileRef = 466989D82C35206E009884D1 /* Scan.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 */
|
||||
|
||||
/* Begin PBXFileReference section */
|
||||
@ -23,6 +25,8 @@
|
||||
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>"; };
|
||||
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 */
|
||||
|
||||
/* Begin PBXFrameworksBuildPhase section */
|
||||
@ -93,6 +97,8 @@
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
466989C82C34DC7A009884D1 /* MainView.swift */,
|
||||
466989DC2C35299D009884D1 /* ScanItemView.swift */,
|
||||
466989DE2C353FEC009884D1 /* SettingsView.swift */,
|
||||
);
|
||||
path = View;
|
||||
sourceTree = "<group>";
|
||||
@ -170,7 +176,9 @@
|
||||
466989C72C34DC7A009884D1 /* JustScanItApp.swift in Sources */,
|
||||
466989DB2C352776009884D1 /* MainView.swift in Sources */,
|
||||
466989D92C35206E009884D1 /* Scan.swift in Sources */,
|
||||
466989DF2C353FEC009884D1 /* SettingsView.swift in Sources */,
|
||||
466989D62C34DCE4009884D1 /* VNDocumentCameraViewControllerR.swift in Sources */,
|
||||
466989DD2C35299D009884D1 /* ScanItemView.swift in Sources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
|
@ -8,6 +8,7 @@
|
||||
import Foundation
|
||||
import SwiftData
|
||||
import UIKit
|
||||
import SwiftUI
|
||||
|
||||
@Model
|
||||
final class Scan {
|
||||
@ -18,6 +19,10 @@ final class Scan {
|
||||
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() {
|
||||
self.timestamp = Date()
|
||||
self.images = []
|
||||
|
@ -40,16 +40,7 @@ struct MainView: View {
|
||||
NavigationView {
|
||||
ScrollView {
|
||||
ForEach(scans, id: \.self) { scan in
|
||||
HStack {
|
||||
ForEach(scan.uiImages, id: \.self) { image in
|
||||
Image(uiImage: image)
|
||||
.resizable()
|
||||
.scaledToFit()
|
||||
.frame(width: 100, height: 100)
|
||||
.padding()
|
||||
|
||||
}
|
||||
}
|
||||
ScanItemView(scan)
|
||||
}
|
||||
}
|
||||
.navigationTitle("Just Scan It!")
|
||||
@ -57,6 +48,9 @@ struct MainView: View {
|
||||
VNDocumentCameraViewControllerRepresentable(scanResult: $scannedImages)
|
||||
}
|
||||
.toolbar {
|
||||
ToolbarItem(placement: .topBarLeading) {
|
||||
Button("clear") { try? ctx.delete(model: Scan.self) }
|
||||
}
|
||||
ToolbarItem {
|
||||
Button(action: showVNDocumentCameraView) {
|
||||
Image(systemName: "scanner")
|
||||
@ -73,10 +67,14 @@ struct MainView: View {
|
||||
}
|
||||
|
||||
private func saveImages() {
|
||||
guard !scannedImages.isEmpty else { return }
|
||||
|
||||
let newScan = Scan(images: scannedImages)
|
||||
|
||||
ctx.insert(newScan)
|
||||
try? ctx.save()
|
||||
withAnimation(.easeIn) {
|
||||
ctx.insert(newScan)
|
||||
try? ctx.save()
|
||||
}
|
||||
|
||||
scannedImages = []
|
||||
}
|
||||
|
104
JustScanIt/View/ScanItemView.swift
Normal file
104
JustScanIt/View/ScanItemView.swift
Normal 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user