Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Fix #1467: Display a QuickLook preview when tapping a downloaded file (
Browse files Browse the repository at this point in the history
…#1978)

Falls back to previous logic if the QuickLook service isn't able to preview the document type
  • Loading branch information
kylehickinson authored Nov 18, 2019
1 parent efd68e0 commit a1fadbd
Showing 1 changed file with 34 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import UIKit
import Shared
import Storage
import QuickLook

private struct DownloadsPanelUX {
static let WelcomeScreenPadding: CGFloat = 15
Expand Down Expand Up @@ -295,13 +296,6 @@ class DownloadsPanel: UIViewController, UITableViewDelegate, UITableViewDataSour
return configureDownloadedFile(cell, for: indexPath)
}

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
if let header = view as? UITableViewHeaderFooterView {
header.textLabel?.textColor = UIColor.Photon.Grey90
header.contentView.backgroundColor = UIColor.Photon.Grey10
}
}

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
guard groupedDownloadedFiles.numberOfItemsForSection(section) > 0 else { return nil }

Expand All @@ -324,10 +318,21 @@ class DownloadsPanel: UIViewController, UITableViewDelegate, UITableViewDataSour
return cell
}

private var previewingFileDataSource: PreviewingDataSource?

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)

if let downloadedFile = downloadedFileForIndexPath(indexPath) {
let previewingItem = PreviewingDataSource(file: downloadedFile)
if QLPreviewController.canPreview(previewingItem.file.path as QLPreviewItem) {
previewingFileDataSource = previewingItem
let quickLook = QLPreviewController()
quickLook.dataSource = previewingFileDataSource
quickLook.delegate = self
present(quickLook, animated: true)
return
}

guard downloadedFile.canShowInWebView else {
shareDownloadedFile(downloadedFile, indexPath: indexPath)
Expand Down Expand Up @@ -395,3 +400,25 @@ extension DownloadsPanel: Themeable {
tableView.reloadData()
}
}

extension DownloadsPanel: QLPreviewControllerDelegate {
func previewControllerDidDismiss(_ controller: QLPreviewController) {
previewingFileDataSource = nil
}
}

private class PreviewingDataSource: NSObject, QLPreviewControllerDataSource {
let file: DownloadedFile

init(file: DownloadedFile) {
self.file = file
}

func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
return 1
}

func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
return file.path as QLPreviewItem
}
}

0 comments on commit a1fadbd

Please sign in to comment.