generated from StanfordSpezi/SpeziTemplateApplication
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from CS342/feature/documentGallery
Add Document Gallery + Sharing
- Loading branch information
Showing
16 changed files
with
373 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// | ||
// This source file is part of the Stanford CardinalKit Template Application project | ||
// | ||
// SPDX-FileCopyrightText: 2023 Stanford University | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
|
||
import PDFKit | ||
import SwiftUI | ||
|
||
|
||
struct DocumentGalleryView: View { | ||
@EnvironmentObject var documentManager: DocumentManager | ||
@State var documentScanner = false | ||
|
||
|
||
var body: some View { | ||
Group { | ||
if documentManager.documents.isEmpty { | ||
Text("No Documents") | ||
} else { | ||
List(documentManager.documents, id: \.documentURL) { document in | ||
PDFListRow(document: document) | ||
} | ||
} | ||
} | ||
.toolbar { | ||
ToolbarItem { | ||
Button( | ||
action: { | ||
documentScanner = true | ||
}, | ||
label: { | ||
Image(systemName: "plus") | ||
} | ||
) | ||
} | ||
} | ||
.fullScreenCover(isPresented: $documentScanner) { | ||
DocumentScanner() | ||
.background { | ||
Color.black.ignoresSafeArea() | ||
} | ||
} | ||
.navigationTitle("Documents") | ||
} | ||
} | ||
|
||
struct DocumentGalleryView_Previews: PreviewProvider { | ||
static var previews: some View { | ||
DocumentGalleryView() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// | ||
// This source file is part of the Stanford CardinalKit Template Application project | ||
// | ||
// SPDX-FileCopyrightText: 2023 Stanford University | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
|
||
import PDFKit | ||
|
||
|
||
class DocumentManager: ObservableObject { | ||
var documents: [PDFDocument] = [] { | ||
willSet { | ||
self.objectWillChange.send() | ||
} | ||
} | ||
|
||
|
||
init() { | ||
guard let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { | ||
return | ||
} | ||
|
||
let ownYourDataDirectory = documentDirectory.appending(path: "OwnYourData") | ||
|
||
|
||
var documents: [PDFDocument] = [] | ||
do { | ||
let items = try FileManager.default.contentsOfDirectory(at: ownYourDataDirectory, includingPropertiesForKeys: nil) | ||
for item in items { | ||
if let document = PDFDocument(url: item) { | ||
documents.append(document) | ||
} | ||
} | ||
self.documents = documents | ||
} catch { | ||
return | ||
} | ||
} | ||
|
||
|
||
func store(document: PDFDocument, title: String) { | ||
guard let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { | ||
return | ||
} | ||
|
||
try? FileManager.default.createDirectory(at: documentDirectory.appending(path: "OwnYourData"), withIntermediateDirectories: true) | ||
|
||
let documentBaseName: String | ||
if title.isEmpty { | ||
documentBaseName = "Document" | ||
} else { | ||
documentBaseName = title | ||
} | ||
|
||
var documentName = documentBaseName | ||
|
||
for index in 1...Int.max { | ||
if !FileManager.default.fileExists(atPath: documentDirectory.appending(path: "OwnYourData/\(documentName).pdf").path()) { | ||
break | ||
} | ||
documentName = documentBaseName + "\(index)" | ||
} | ||
|
||
let documentPath = documentDirectory.appending(path: "OwnYourData/\(documentName).pdf") | ||
|
||
let documentSaved = document.write(to: documentPath) | ||
|
||
print("Saved the document \(documentSaved) to \(documentPath)") | ||
|
||
if let loadedFromURLDocument = PDFDocument(url: documentPath) { | ||
self.documents.append(loadedFromURLDocument) | ||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { | ||
self.objectWillChange.send() | ||
} | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
TemplateApplication/Documents/PDFDocument+Transferable.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// | ||
// This source file is part of the Stanford CardinalKit Template Application project | ||
// | ||
// SPDX-FileCopyrightText: 2023 Stanford University | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
|
||
import PDFKit | ||
import SwiftUI | ||
|
||
|
||
extension PDFDocument: Transferable { | ||
public static var transferRepresentation: some TransferRepresentation { | ||
DataRepresentation( | ||
contentType: .pdf, | ||
exporting: { pdf in | ||
if let data = pdf.dataRepresentation() { | ||
return data | ||
} else { | ||
return Data() | ||
} | ||
}, | ||
importing: { data in | ||
if let pdf = PDFDocument(data: data) { | ||
return pdf | ||
} else { | ||
return PDFDocument() | ||
} | ||
} | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// | ||
// This source file is part of the Stanford CardinalKit Template Application project | ||
// | ||
// SPDX-FileCopyrightText: 2023 Stanford University | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
|
||
import PDFKit | ||
import SwiftUI | ||
|
||
|
||
struct PDFListDetailView: View { | ||
let document: PDFDocument | ||
|
||
var body: some View { | ||
PDFView(document) | ||
.toolbar { | ||
ToolbarItem { | ||
ShareLink(item: document, preview: SharePreview("PDF")) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.