-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuickLook.swift
64 lines (49 loc) · 1.7 KB
/
QuickLook.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//
// File.swift
//
//
// Created by Shunzhe Ma on 8/15/20.
//
#if canImport(UIKit)
import Foundation
import UIKit
import SwiftUI
import QuickLook
@available(iOS 13.0, *)
public struct QuickLookView: UIViewControllerRepresentable {
var urls: [URL]
var onDismiss: () -> Void
public init(urls: [URL], onDismiss: @escaping () -> Void) {
self.urls = urls
self.onDismiss = onDismiss
}
public func makeCoordinator() -> Coordinator {
Coordinator(self)
}
public func updateUIViewController(_ uiViewController: UINavigationController, context: UIViewControllerRepresentableContext<QuickLookView>) {
if let controller = uiViewController.viewControllers.first as? QLPreviewController {
controller.reloadData()
}
}
public func makeUIViewController(context: Context) -> UINavigationController {
let controller = QLPreviewController()
controller.dataSource = context.coordinator
controller.reloadData()
let navController = UINavigationController(rootViewController: controller)
return navController
}
public class Coordinator: NSObject, QLPreviewControllerDataSource {
var parent: QuickLookView
init(_ qlPreviewController: QuickLookView) {
self.parent = qlPreviewController
super.init()
}
public func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
return self.parent.urls.count
}
public func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
return self.parent.urls[index] as QLPreviewItem
}
}
}
#endif