Skip to content

Commit

Permalink
feat(PreviewViewController): Download in cache public share files for…
Browse files Browse the repository at this point in the history
… preview (#1395)
  • Loading branch information
PhilippeWeidmann authored Jan 23, 2025
2 parents c2c63dd + 65fca3b commit 0d04c47
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 34 deletions.
91 changes: 58 additions & 33 deletions kDrive/UI/Controller/Files/Preview/PreviewViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -542,52 +542,77 @@ final class PreviewViewController: UIViewController, PreviewContentCellDelegate,
return
}

downloadFile(at: indexPath)
if let publicShareProxy = driveFileManager.publicShareProxy {
downloadPublicShareFile(at: indexPath, publicShareProxy: publicShareProxy)
} else {
downloadFile(at: indexPath)
}
}

private func downloadPublicShareFile(at indexPath: IndexPath, publicShareProxy: PublicShareProxy) {
DownloadQueue.instance.addPublicShareToQueue(
file: currentFile,
driveFileManager: driveFileManager,
publicShareProxy: publicShareProxy,
onOperationCreated: { operation in
self.trackOperationCreated(at: indexPath, downloadOperation: operation)
}, completion: { error in
self.downloadCompletion(at: indexPath, error: error)
}
)
}

private func downloadFile(at indexPath: IndexPath) {
DownloadQueue.instance.temporaryDownload(
file: currentFile,
userId: accountManager.currentUserId,
onOperationCreated: { operation in
Task { @MainActor [weak self] in
guard let self else {
return
}

currentDownloadOperation = operation
if let progress = currentDownloadOperation?.progress,
let cell = collectionView.cellForItem(at: indexPath) as? DownloadProgressObserver {
cell.setDownloadProgress(progress)
}
}
self.trackOperationCreated(at: indexPath, downloadOperation: operation)
},
completion: { error in
Task { @MainActor [weak self] in
guard let self else { return }

currentDownloadOperation = nil

guard view.window != nil else { return }

if let error {
if error != .taskCancelled {
previewErrors[currentFile.id] = PreviewError(fileId: currentFile.id, downloadError: error)
collectionView.reloadItems(at: [indexPath])
}
} else {
(collectionView.cellForItem(at: indexPath) as? DownloadingPreviewCollectionViewCell)?
.previewDownloadTask?.cancel()
previewErrors[currentFile.id] = nil
collectionView.endEditing(true)
collectionView.reloadItems(at: [indexPath])
updateNavigationBar()
}
}
self.downloadCompletion(at: indexPath, error: error)
}
)
}

private func trackOperationCreated(at indexPath: IndexPath, downloadOperation: DownloadAuthenticatedOperation?) {
Task { @MainActor [weak self] in
guard let self else {
return
}

currentDownloadOperation = downloadOperation
if let progress = currentDownloadOperation?.progress,
let cell = collectionView.cellForItem(at: indexPath) as? DownloadProgressObserver {
cell.setDownloadProgress(progress)
}
}
}

private func downloadCompletion(at indexPath: IndexPath, error: DriveError?) {
Task { @MainActor [weak self] in
guard let self else { return }

currentDownloadOperation = nil

guard view.window != nil else { return }

if let error {
if error != .taskCancelled {
previewErrors[currentFile.id] = PreviewError(fileId: currentFile.id, downloadError: error)
collectionView.reloadItems(at: [indexPath])
}
} else {
(collectionView.cellForItem(at: indexPath) as? DownloadingPreviewCollectionViewCell)?
.previewDownloadTask?.cancel()
previewErrors[currentFile.id] = nil
collectionView.endEditing(true)
collectionView.reloadItems(at: [indexPath])
updateNavigationBar()
}
}
}

static func instantiate(
files: [File],
index: Int,
Expand Down
6 changes: 5 additions & 1 deletion kDriveCore/Data/DownloadQueue/DownloadQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ public final class DownloadQueue: ParallelismHeuristicDelegate {
public func addPublicShareToQueue(file: File,
driveFileManager: DriveFileManager,
publicShareProxy: PublicShareProxy,
itemIdentifier: NSFileProviderItemIdentifier? = nil) {
itemIdentifier: NSFileProviderItemIdentifier? = nil,
onOperationCreated: ((DownloadPublicShareOperation?) -> Void)? = nil,
completion: ((DriveError?) -> Void)? = nil) {
Log.downloadQueue("addPublicShareToQueue file:\(file.id)")
let file = file.freezeIfNeeded()

Expand All @@ -139,10 +141,12 @@ public final class DownloadQueue: ParallelismHeuristicDelegate {
self.fileOperationsInQueue.removeValue(forKey: file.id)
self.publishFileDownloaded(fileId: file.id, error: operation.error)
OperationQueueHelper.disableIdleTimer(false, hasOperationsInQueue: !self.fileOperationsInQueue.isEmpty)
completion?(operation.error)
}
}
self.operationQueue.addOperation(operation)
self.fileOperationsInQueue[file.id] = operation
onOperationCreated?(operation)
}
}

Expand Down

0 comments on commit 0d04c47

Please sign in to comment.