2024-02-21 12:21:50 +03:00
|
|
|
//
|
|
|
|
// ShareViewController.swift
|
|
|
|
// FileProviderUIExt
|
|
|
|
//
|
|
|
|
// Created by Claudio Cambra on 21/2/24.
|
|
|
|
//
|
|
|
|
|
|
|
|
import AppKit
|
2024-02-21 12:58:39 +03:00
|
|
|
import FileProvider
|
2024-02-28 16:36:19 +03:00
|
|
|
import NextcloudKit
|
2024-02-21 13:49:58 +03:00
|
|
|
import OSLog
|
|
|
|
import QuickLookThumbnailing
|
2024-02-21 12:21:50 +03:00
|
|
|
|
2024-02-28 16:36:19 +03:00
|
|
|
class ShareViewController: NSViewController, ShareViewDataSourceUIDelegate {
|
2024-02-27 15:29:07 +03:00
|
|
|
let shareDataSource = ShareTableViewDataSource()
|
2024-02-21 12:58:39 +03:00
|
|
|
let itemIdentifiers: [NSFileProviderItemIdentifier]
|
|
|
|
|
2024-02-21 12:56:57 +03:00
|
|
|
@IBOutlet weak var fileNameIcon: NSImageView!
|
|
|
|
@IBOutlet weak var fileNameLabel: NSTextField!
|
|
|
|
@IBOutlet weak var descriptionLabel: NSTextField!
|
|
|
|
@IBOutlet weak var closeButton: NSButton!
|
2024-02-27 15:27:19 +03:00
|
|
|
@IBOutlet weak var tableView: NSTableView!
|
2024-02-28 16:28:07 +03:00
|
|
|
@IBOutlet weak var optionsView: NSView!
|
2024-02-28 16:36:19 +03:00
|
|
|
@IBOutlet weak var splitView: NSSplitView!
|
2024-02-21 12:21:50 +03:00
|
|
|
|
|
|
|
public override var nibName: NSNib.Name? {
|
|
|
|
return NSNib.Name(self.className)
|
|
|
|
}
|
|
|
|
|
|
|
|
var actionViewController: DocumentActionViewController! {
|
|
|
|
return parent as? DocumentActionViewController
|
|
|
|
}
|
2024-02-21 12:58:39 +03:00
|
|
|
|
|
|
|
init(_ itemIdentifiers: [NSFileProviderItemIdentifier]) {
|
|
|
|
self.itemIdentifiers = itemIdentifiers
|
|
|
|
super.init(nibName: nil, bundle: nil)
|
2024-02-21 13:49:58 +03:00
|
|
|
|
|
|
|
guard let firstItem = itemIdentifiers.first else {
|
|
|
|
Logger.shareViewController.error("called without items")
|
|
|
|
closeAction(self)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
Task {
|
|
|
|
await processItemIdentifier(firstItem)
|
|
|
|
}
|
2024-02-21 12:58:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
2024-02-21 13:03:29 +03:00
|
|
|
|
2024-02-28 16:36:19 +03:00
|
|
|
override func viewDidLoad() {
|
|
|
|
hideOptions()
|
|
|
|
}
|
|
|
|
|
2024-02-21 13:03:29 +03:00
|
|
|
@IBAction func closeAction(_ sender: Any) {
|
|
|
|
actionViewController.extensionContext.completeRequest()
|
|
|
|
}
|
2024-02-21 13:49:58 +03:00
|
|
|
|
2024-02-21 13:59:39 +03:00
|
|
|
private func processItemIdentifier(_ itemIdentifier: NSFileProviderItemIdentifier) async {
|
2024-02-21 13:49:58 +03:00
|
|
|
guard let manager = NSFileProviderManager(for: actionViewController.domain) else {
|
|
|
|
fatalError("NSFileProviderManager isn't expected to fail")
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
let itemUrl = try await manager.getUserVisibleURL(for: itemIdentifier)
|
2024-02-21 13:59:39 +03:00
|
|
|
await updateDisplay(itemUrl: itemUrl)
|
2024-02-28 16:36:19 +03:00
|
|
|
shareDataSource.uiDelegate = self
|
2024-02-27 15:29:07 +03:00
|
|
|
shareDataSource.sharesTableView = tableView
|
2024-02-27 18:56:55 +03:00
|
|
|
shareDataSource.loadItem(url: itemUrl)
|
2024-02-21 13:49:58 +03:00
|
|
|
} catch let error {
|
|
|
|
let errorString = "Error processing item: \(error)"
|
|
|
|
Logger.shareViewController.error("\(errorString)")
|
|
|
|
fileNameLabel.stringValue = "Unknown item"
|
|
|
|
descriptionLabel.stringValue = errorString
|
|
|
|
}
|
|
|
|
}
|
2024-02-21 13:59:39 +03:00
|
|
|
|
|
|
|
private func updateDisplay(itemUrl: URL) async {
|
|
|
|
fileNameLabel.stringValue = itemUrl.lastPathComponent
|
|
|
|
|
|
|
|
let request = QLThumbnailGenerator.Request(
|
|
|
|
fileAt: itemUrl,
|
|
|
|
size: CGSize(width: 128, height: 128),
|
|
|
|
scale: 1.0,
|
|
|
|
representationTypes: .icon
|
|
|
|
)
|
|
|
|
|
|
|
|
let generator = QLThumbnailGenerator.shared
|
|
|
|
let fileThumbnail = await withCheckedContinuation { continuation in
|
|
|
|
generator.generateRepresentations(for: request) { thumbnail, type, error in
|
|
|
|
if thumbnail == nil || error != nil {
|
|
|
|
Logger.shareViewController.error("Could not get thumbnail: \(error)")
|
|
|
|
}
|
|
|
|
continuation.resume(returning: thumbnail)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fileNameIcon.image = fileThumbnail?.nsImage
|
|
|
|
}
|
2024-02-28 16:36:19 +03:00
|
|
|
|
|
|
|
func hideOptions() {
|
|
|
|
splitView.removeArrangedSubview(optionsView)
|
|
|
|
optionsView.isHidden = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func showOptions(share: NKShare) {
|
|
|
|
splitView.addArrangedSubview(optionsView)
|
|
|
|
optionsView.isHidden = false
|
|
|
|
}
|
2024-02-21 12:21:50 +03:00
|
|
|
}
|