-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
146 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// | ||
// DocumentInteractionController.swift | ||
// CoreDex | ||
// | ||
// Created by Adrian Castro on 22.06.24. | ||
// | ||
|
||
import SwiftUI | ||
import UIKit | ||
|
||
struct DocumentInteractionController: UIViewControllerRepresentable { | ||
let url: URL | ||
|
||
func makeUIViewController(context: Context) -> UIViewController { | ||
let viewController = UIViewController() | ||
DispatchQueue.main.async { | ||
let documentInteractionController = UIDocumentInteractionController(url: self.url) | ||
documentInteractionController.delegate = context.coordinator | ||
documentInteractionController.presentPreview(animated: true) | ||
} | ||
return viewController | ||
} | ||
|
||
func updateUIViewController(_ uiViewController: UIViewController, context: Context) { | ||
let documentInteractionController = UIDocumentInteractionController(url: url) | ||
documentInteractionController.delegate = context.coordinator | ||
documentInteractionController.presentPreview(animated: true) | ||
|
||
if let rootVC = uiViewController.presentingViewController { | ||
documentInteractionController.presentOptionsMenu(from: rootVC.view.bounds, in: rootVC.view, animated: true) | ||
} | ||
} | ||
|
||
func makeCoordinator() -> Coordinator { | ||
Coordinator(self) | ||
} | ||
|
||
class Coordinator: NSObject, UIDocumentInteractionControllerDelegate { | ||
var parent: DocumentInteractionController | ||
|
||
init(_ parent: DocumentInteractionController) { | ||
self.parent = parent | ||
} | ||
|
||
func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController { | ||
guard let scene = UIApplication.shared.connectedScenes.first as? UIWindowScene, | ||
let rootViewController = scene.windows.first?.rootViewController else { | ||
fatalError("Unable to find the root view controller") | ||
} | ||
return rootViewController | ||
} | ||
|
||
func documentInteractionControllerDidEndPreview(_ controller: UIDocumentInteractionController) { | ||
do { | ||
try FileManager.default.removeItem(at: parent.url) | ||
} catch { | ||
print("Error removing temporary file: \(error)") | ||
} | ||
} | ||
} | ||
} |
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